<commit_before>//
//  ResourceCollection.swift
//  Spine
//
//  Created by <NAME> on 30-12-14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation
import BrightFutures

/// A ResourceCollection represents a collection of resources.
public class ResourceCollection: NSObject, NSCoding {
	/// Whether the resources for this collection are loaded
	public var isLoaded: Bool = false
	
	/// The URL of the current page in this collection.
	public var resourcesURL: NSURL?
	
	/// The URL of the next page in this collection.
	public var nextURL: NSURL?
	
	/// The URL of the previous page in this collection.
	public var previousURL: NSURL?
	
	/// The loaded resources
	public internal(set) var resources: [Resource] = []
	
	
	// MARK: Initializers
	
	public override init() {}
	
	public init(resources: [Resource], resourcesURL: NSURL? = nil) {
		self.resources = resources
		self.resourcesURL = resourcesURL
		self.isLoaded = !resources.isEmpty
	}
	
	init(document: JSONAPIDocument) {
		self.resources = document.data ?? []
		self.resourcesURL = document.links?["self"]
		self.nextURL = document.links?["next"]
		self.previousURL = document.links?["previous"]
		self.isLoaded = true
	}
	
	
	// MARK: NSCoding
	
	public required init?(coder: NSCoder) {
		isLoaded = coder.decodeBoolForKey("isLoaded")
		resourcesURL = coder.decodeObjectForKey("resourcesURL") as? NSURL
		resources = coder.decodeObjectForKey("resources") as! [Resource]
	}
	
	public func encodeWithCoder(coder: NSCoder) {
		coder.encodeBool(isLoaded, forKey: "isLoaded")
		coder.encodeObject(resourcesURL, forKey: "resourcesURL")
		coder.encodeObject(resources, forKey: "resources")
	}
	
	
	// MARK: Subscript and count
	
	/// Returns the loaded resource at the given index.
	public subscript (index: Int) -> Resource {
		return resources[index]
	}
	
	/// Returns a resource identified by the given type and id,
	/// or nil if no resource was found.
	public subscript (type: String, id: String) -> Resource? {
		return resources.filter { $0.id == id && $0.resourceType == type }.first
	}
	
	/// Returns how many resources are loaded.
	public var count: Int {
		return resources.count
	}
	
	// MARK: Mutators
	<commit_msg>Added encoding/decoding of nextURL and previousURL in ResourceCollection.<commit_after>//
//  ResourceCollection.swift
//  Spine
//
//  Created by <NAME> on 30-12-14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation
import BrightFutures

/// A ResourceCollection represents a collection of resources.
public class ResourceCollection: NSObject, NSCoding {
	/// Whether the resources for this collection are loaded
	public var isLoaded: Bool = false
	
	/// The URL of the current page in this collection.
	public var resourcesURL: NSURL?
	
	/// The URL of the next page in this collection.
	public var nextURL: NSURL?
	
	/// The URL of the previous page in this collection.
	public var previousURL: NSURL?
	
	/// The loaded resources
	public internal(set) var resources: [Resource] = []
	
	
	// MARK: Initializers
	
	public override init() {}
	
	public init(resources: [Resource], resourcesURL: NSURL? = nil) {
		self.resources = resources
		self.resourcesURL = resourcesURL
		self.isLoaded = !resources.isEmpty
	}
	
	init(document: JSONAPIDocument) {
		self.resources = document.data ?? []
		self.resourcesURL = document.links?["self"]
		self.nextURL = document.links?["next"]
		self.previousURL = document.links?["previous"]
		self.isLoaded = true
	}
	
	
	// MARK: NSCoding
	
	public required init?(coder: NSCoder) {
		isLoaded = coder.decodeBoolForKey("isLoaded")
		resourcesURL = coder.decodeObjectForKey("resourcesURL") as? NSURL
		nextURL = coder.decodeObjectForKey("nextURL") as? NSURL
		previousURL = coder.decodeObjectForKey("previousURL") as? NSURL
		resources = coder.decodeObjectForKey("resources") as! [Resource]
	}
	
	public func encodeWithCoder(coder: NSCoder) {
		coder.encodeBool(isLoaded, forKey: "isLoaded")
		coder.encodeObject(resourcesURL, forKey: "resourcesURL")
		coder.encodeObject(nextURL, forKey: "nextURL")
		coder.encodeObject(previousURL, forKey: "previousURL")
		coder.encodeObject(resources, forKey: "resources")
	}
	
	
	// MARK: Subscript and count
	
	/// Returns the loaded resource at the given index.
	public subscript (index: Int) -> Resource {
		return resources[index]
	}
	
	/// Returns a resource identified by the given type and id,
	/// or nil if no resource was found.
	public subscript (type: String, id: String) -> Resource? {
		return resources.filter { $0.id == id && $0.resourceType == type }.first
	}
	
	/// Returns how many resources are loaded.
	public var count: Int {
		return resources.count
	}
	
	// MARK: Mutators
	
{'fraction_non_alphanumeric': 0.11281396338867604, 'fraction_numerical': 0.005108556832694764, 'mean_word_length': 3.756072874493927, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6934534', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1382, 'n_words': 447}