<commit_before>//
//  BRReusableQueue.swift
//  BRReusableQueue
//
//  Created by <NAME> on 30.01.16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import UIKit

@objc protocol Reusable: class {
    optional static func reuseIdentifier() -> String // if not implemented, classname is used
    optional static func newForReuse() -> AnyObject? // if implemented, dequeueOrCreateReusableWithClass can be used
    optional func prepareForReuse() -> Void
}

@objc class ReusableQueue : NSObject {
    var reusables: NSCache = NSCache();
    
    static let sharedQueue = ReusableQueue()
    
    override init () {
        super.init()
        NSNotificationCenter.defaultCenter().addObserver(
            self,
            selector: "emptyQueue:",
            name: UIApplicationDidReceiveMemoryWarningNotification,
            object: nil)
    }
    
    func enqueueReusable(reusable: Reusable) -> Void {
        let identifier = identifierFromReusableClass(reusable.dynamicType)
        var objects: Array<Reusable> = reusables.objectForKey(identifier) as? Array<Reusable> ??  Array<Reusable>()
        reusable.prepareForReuse?()
        objects.append(reusable)
        reusables.setObject(objects, forKey: identifier)
    }
    
    func dequeueReusableWithIdentifier(identifier: String) -> Reusable? {
        if var objects: Array<Reusable> = reusables.objectForKey(identifier) as? Array<Reusable> where
            objects.count > 0{
            let reusable = objects.removeFirst()
            reusables.setObject(objects, forKey: identifier)
            return reusable
        }
        return nil
    }
    
    func dequeueReusableWithClass(reusableClass: Reusable.Type) -> Reusable? {
        return dequeueReusableWithIdentifier(identifierFromReusableClass(reusableClass))
    }
    
    func dequeueOrCreateReusableWithClass(reusableClass: Reusable.Type) -> Reusable? {
        if let reusable = dequeueReusableWithClass(reusableClass) {
            return reusable
        }
        let createdReusable = reusableClass.newForReuse?()
        if let reusable = createdReusable as? Reusable {
            return reusable
        }
        return nil
    }
    
    func emptyQueue() -> Void {
        reusables.removeAllObjects()
    }
    
    private func identifierFromReusableClass(reusable: Reusable.Type) -> String {
        if let identifier = reusable.reuseIdentifier?() {
            return identifier
        }
        return  "\(reusable)"
    }
    
}<commit_msg>changed needed protocol, classes and methods to be public<commit_after>//
//  BRReusableQueue.swift
//  BRReusableQueue
//
//  Created by <NAME> on 30.01.16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import UIKit

@objc public protocol Reusable: class {
    optional static func reuseIdentifier() -> String // if not implemented, classname is used
    optional static func newForReuse() -> AnyObject? // if implemented, dequeueOrCreateReusableWithClass can be used
    optional func prepareForReuse() -> Void
}

@objc public class ReusableQueue : NSObject {
    var reusables: NSCache = NSCache();
    
    public static let sharedQueue = ReusableQueue()
    
    override init () {
        super.init()
        NSNotificationCenter.defaultCenter().addObserver(
            self,
            selector: "emptyQueue:",
            name: UIApplicationDidReceiveMemoryWarningNotification,
            object: nil)
    }
    
    public func enqueueReusable(reusable: Reusable) -> Void {
        let identifier = identifierFromReusableClass(reusable.dynamicType)
        var objects: Array<Reusable> = reusables.objectForKey(identifier) as? Array<Reusable> ??  Array<Reusable>()
        reusable.prepareForReuse?()
        objects.append(reusable)
        reusables.setObject(objects, forKey: identifier)
    }
    
    public func dequeueReusableWithIdentifier(identifier: String) -> Reusable? {
        if var objects: Array<Reusable> = reusables.objectForKey(identifier) as? Array<Reusable> where
            objects.count > 0{
            let reusable = objects.removeFirst()
            reusables.setObject(objects, forKey: identifier)
            return reusable
        }
        return nil
    }
    
    public func dequeueReusableWithClass(reusableClass: Reusable.Type) -> Reusable? {
        return dequeueReusableWithIdentifier(identifierFromReusableClass(reusableClass))
    }
    
    public func dequeueOrCreateReusableWithClass(reusableClass: Reusable.Type) -> Reusable? {
        if let reusable = dequeueReusableWithClass(reusableClass) {
            return reusable
        }
        let createdReusable = reusableClass.newForReuse?()
        if let reusable = createdReusable as? Reusable {
            return reusable
        }
        return nil
    }
    
    public func emptyQueue() -> Void {
        reusables.removeAllObjects()
    }
    
    private func identifierFromReusableClass(reusable: Reusable.Type) -> String {
        if let identifier = reusable.reuseIdentifier?() {
            return identifier
        }
        return  "\(reusable)"
    }
    
}
{'fraction_non_alphanumeric': 0.08603199375731564, 'fraction_numerical': 0.004291845493562232, 'mean_word_length': 2.6284501061571124, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 37, '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': '113047', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1384, 'n_words': 338}