<commit_before>//
//  ArgumentCaptor.swift
//  InstantMock
//
//  Created by Patrick on 12/05/2017.
//  Copyright © 2017 pirishd. All rights reserved.
//


/** This class enables to capture arguments of any types */
class ArgumentCaptor<T> {

    /// Default value for construction
    fileprivate var defaultValue: T?

    /// Argument capture instance
    fileprivate var arg: ArgumentCapture?

    /// Captured value
    var value: T? {
        var ret: T?
        if let captureArg = self.arg {
            ret = captureArg.value as? T
        }
        return ret
    }

    /// All captured values
    var allValues: [T?] {
        var ret = [T?]()
        if let captureArg = self.arg {
            ret = captureArg.allValues.map { $0 as? T }.flatMap { $0 }
        }
        return ret
    }


    /** Initialize captor with default value */
    init(_ defaultValue: T? = nil) {
        self.defaultValue = defaultValue
    }
}


/** Extension for capturing values */
extension ArgumentCaptor {

    /** Capture an argument of expected type */
    func capture() -> T {
        let factory = ArgumentFactoryImpl<T>()
        return self.capture(argFactory: factory, argStorage: ArgumentStorageImpl.instance)
    }


    /** Capture an argument of expected type (for dependency injection) */
    func capture<F>(argFactory: F, argStorage: ArgumentStorage) -> T where F: ArgumentFactory, F.Value == T {

        // store instance
        let typeDescription = "\(T.self)"
        let arg = argFactory.argumentCapture(typeDescription)
        self.arg = arg
        argStorage.store(arg)

        // return default value directly if set
        if let defaultValue = self.defaultValue {
            return defaultValue<commit_msg>Make argument captor public<commit_after>//
//  ArgumentCaptor.swift
//  InstantMock
//
//  Created by Patrick on 12/05/2017.
//  Copyright © 2017 pirishd. All rights reserved.
//


/** This class enables to capture arguments of any types */
public class ArgumentCaptor<T> {

    /// Default value for construction
    fileprivate var defaultValue: T?

    /// Argument capture instance
    fileprivate var arg: ArgumentCapture?

    /// Captured value
    public var value: T? {
        var ret: T?
        if let captureArg = self.arg {
            ret = captureArg.value as? T
        }
        return ret
    }

    /// All captured values
    public var allValues: [T?] {
        var ret = [T?]()
        if let captureArg = self.arg {
            ret = captureArg.allValues.map { $0 as? T }.flatMap { $0 }
        }
        return ret
    }


    /** Initialize captor with default value */
    public init(_ defaultValue: T? = nil) {
        self.defaultValue = defaultValue
    }
}


/** Extension for capturing values */
extension ArgumentCaptor {

    /** Capture an argument of expected type */
    public func capture() -> T {
        let factory = ArgumentFactoryImpl<T>()
        return self.capture(argFactory: factory, argStorage: ArgumentStorageImpl.instance)
    }


    /** Capture an argument of expected type (for dependency injection) */
    func capture<F>(argFactory: F, argStorage: ArgumentStorage) -> T where F: ArgumentFactory, F.Value == T {

        // store instance
        let typeDescription = "\(T.self)"
        let arg = argFactory.argumentCapture(typeDescription)
        self.arg = arg
        argStorage.store(arg)

        // return default value directly if set
        if let defaultValue = self.defaultValue {
            return defaultValue
{'fraction_non_alphanumeric': 0.10059676044330776, 'fraction_numerical': 0.007956805910770106, 'mean_word_length': 2.1797651309846433, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '5924979', 'n_tokens_mistral': 982, 'n_tokens_neox': 931, 'n_words': 323}