<commit_before>// ------------------------------------------------------------------------------
//  Copyright (c) 2009-2013 the original author or authors. All Rights Reserved.
//
//  NOTICE: You are permitted to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
// ------------------------------------------------------------------------------

import { IInjector } from "../../../framework/api/IInjector";
import { applyHooks } from "../../../framework/impl/applyHooks";
import { guardsApprove } from "../../../framework/impl/guardsApprove";

import { IMediatorMapping } from "../api/IMediatorMapping";
import { ITypeFilter } from "../../matching/ITypeFilter";

import { MediatorManager } from "./MediatorManager";

/**
 * @private
 */
export class MediatorFactory {

    /*============================================================================*/
    /* Private Properties                                                         */
    /*============================================================================*/

    private _mediators: Map<any, any> = new Map<any, any>();

    private _injector: IInjector;

    private _manager: MediatorManager;

    /*============================================================================*/
    /* Constructor                                                                */
    /*============================================================================*/

    /**
     * @private
     */
    constructor(injector: IInjector, manager?: MediatorManager) {
        this._injector = injector;
        this._manager = manager || new MediatorManager(this);
    }

    /*============================================================================*/
    /* Public Functions                                                           */
    /*============================================================================*/

    /**
     * @private
     */
    public getMediator(item: any, mapping: IMediatorMapping): any {
        return this._mediators[item] ? this._mediators[item][<any>mapping] : null;
    }

    /**
     * @private
     */
    public createMediators(item: any, type: FunctionConstructor, mappings: any[]): any[] {
        var createdMediators: any[] = [];
        var mediator: any;
        for (let i in mappings) {
            let mapping: IMediatorMapping = mappings[i];
            mediator = this.getMediator(item, mapping);

            if (!mediator) {
                this.mapTypeForFilterBinding(mapping.matcher, type, item);
                mediator = this.createMediator(item, mapping);
                this.unmapTypeForFilterBinding(mapping.matcher, type, item)
            }

            if (mediator)
                createdMediators.push(mediator);
        }
        return createdMediators;
    }

    /**
     * @private
     */
    public removeMediators(item: any): void {
        var mediators: Map<any, IMediatorMapping> = this._mediators[item];
        if (!mediators)
            return;

        for (var mapping in mediators) {
            this._manager.removeMediator(mediators[mapping], item, <any>mapping);
        }

        delete this._mediators[item];
    }

    /**
     * @private
     */
    public removeAllMediators(): void {
        for (var item in this._mediators) {
            this.removeMediators(item);
        }
    }

    /*============================================================================*/
    /* Private Functions                                                          */
    /*============================================================================*/

    private createMediator(item: any, mapping: IMediatorMapping): any {
        var mediator: any = this.getMediator(item, mapping);

        if (mediator)
            return mediator;

        if (mapping.guards.length == 0 || guardsApprove(mapping.guards, this._injector)) {
            var mediatorClass: FunctionConstructor = mapping.mediatorClass;
            mediator = this._injector.instantiateUnmapped(mediatorClass);
            if (mapping.hooks.length > 0) {
                this._injector.bind(mediatorClass).toConstantValue(mediator);
                applyHooks(mapping.hooks, this._injector);
                this._injector.unbind(mediatorClass);
            }
            this.addMediator(mediator, item, mapping);
        }
        return mediator;
    }

    private addMediator(mediator: any, item: any, mapping: IMediatorMapping): void {
        this._mediators[item] = this._mediators[item] || new Map<any, IMediatorMapping>();
        this._mediators[item][<any>mapping] = mediator;
        this._manager.addMediator(mediator, item, mapping);
    }

    private mapTypeForFilterBinding(filter: ITypeFilter, type: FunctionConstructor, item: any): void {
        let requiredTypes = this.requiredTypesFor(filter, type);
        for (let i in requiredTypes) {
            let requiredType: FunctionConstructor = requiredTypes[i];
            this._injector.bind(requiredType).toConstantValue(item);
        }
    }

    private unmapTypeForFilterBinding(filter: ITypeFilter, type: FunctionConstructor, item: any): void {
        let requiredTypes = this.requiredTypesFor(filter, type);
        for (let i in requiredTypes) {
            let requiredType: FunctionConstructor = requiredTypes[i];
            if (this._injector.isBound(requiredType))
                this._injector.unbind(requiredType);
        }
    }

    private requiredTypesFor(filter: ITypeFilter, type: FunctionConstructor): FunctionConstructor[] {
        var requiredTypes: FunctionConstructor[] = filter.allOfTypes.concat(filter.anyOfTypes);<commit_msg>use get/set/delete instead of key accessor on Map instance.<commit_after>// ------------------------------------------------------------------------------
//  Copyright (c) 2009-2013 the original author or authors. All Rights Reserved.
//
//  NOTICE: You are permitted to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
// ------------------------------------------------------------------------------

import { IInjector } from "../../../framework/api/IInjector";
import { applyHooks } from "../../../framework/impl/applyHooks";
import { guardsApprove } from "../../../framework/impl/guardsApprove";

import { IMediatorMapping } from "../api/IMediatorMapping";
import { ITypeFilter } from "../../matching/ITypeFilter";

import { MediatorManager } from "./MediatorManager";

/**
 * @private
 */
export class MediatorFactory {

    /*============================================================================*/
    /* Private Properties                                                         */
    /*============================================================================*/

    private _mediators: Map<any, any> = new Map<any, any>();

    private _injector: IInjector;

    private _manager: MediatorManager;

    /*============================================================================*/
    /* Constructor                                                                */
    /*============================================================================*/

    /**
     * @private
     */
    constructor(injector: IInjector, manager?: MediatorManager) {
        this._injector = injector;
        this._manager = manager || new MediatorManager(this);
    }

    /*============================================================================*/
    /* Public Functions                                                           */
    /*============================================================================*/

    /**
     * @private
     */
    public getMediator(item: any, mapping: IMediatorMapping): any {
        return this._mediators.get(item) ? this._mediators.get(item)[<any>mapping] : null;
    }

    /**
     * @private
     */
    public createMediators(item: any, type: FunctionConstructor, mappings: any[]): any[] {
        var createdMediators: any[] = [];
        var mediator: any;
        for (let i in mappings) {
            let mapping: IMediatorMapping = mappings[i];
            mediator = this.getMediator(item, mapping);

            if (!mediator) {
                this.mapTypeForFilterBinding(mapping.matcher, type, item);
                mediator = this.createMediator(item, mapping);
                this.unmapTypeForFilterBinding(mapping.matcher, type, item)
            }

            if (mediator)
                createdMediators.push(mediator);
        }
        return createdMediators;
    }

    /**
     * @private
     */
    public removeMediators(item: any): void {
        var mediators: Map<any, IMediatorMapping> = this._mediators.get(item);
        if (!mediators)
            return;

        for (var mapping in mediators) {
            this._manager.removeMediator(mediators[mapping], item, <any>mapping);
        }

        this._mediators.delete(item);
    }

    /**
     * @private
     */
    public removeAllMediators(): void {
        this._mediators.forEach((item) => this.removeMediators(item));
    }

    /*============================================================================*/
    /* Private Functions                                                          */
    /*============================================================================*/

    private createMediator(item: any, mapping: IMediatorMapping): any {
        var mediator: any = this.getMediator(item, mapping);

        if (mediator)
            return mediator;

        if (mapping.guards.length == 0 || guardsApprove(mapping.guards, this._injector)) {
            var mediatorClass: FunctionConstructor = mapping.mediatorClass;
            mediator = this._injector.instantiateUnmapped(mediatorClass);
            if (mapping.hooks.length > 0) {
                this._injector.bind(mediatorClass).toConstantValue(mediator);
                applyHooks(mapping.hooks, this._injector);
                this._injector.unbind(mediatorClass);
            }
            this.addMediator(mediator, item, mapping);
        }
        return mediator;
    }

    private addMediator(mediator: any, item: any, mapping: IMediatorMapping): void {
        this._mediators.set(item, this._mediators[item] || new Map<any, IMediatorMapping>());
        this._mediators.get(item)[<any>mapping] = mediator;
        this._manager.addMediator(mediator, item, mapping);
    }

    private mapTypeForFilterBinding(filter: ITypeFilter, type: FunctionConstructor, item: any): void {
        let requiredTypes = this.requiredTypesFor(filter, type);
        for (let i in requiredTypes) {
            let requiredType: FunctionConstructor = requiredTypes[i];
            this._injector.bind(requiredType).toConstantValue(item);
        }
    }

    private unmapTypeForFilterBinding(filter: ITypeFilter, type: FunctionConstructor, item: any): void {
        let requiredTypes = this.requiredTypesFor(filter, type);
        for (let i in requiredTypes) {
            let requiredType: FunctionConstructor = requiredTypes[i];
            if (this._injector.isBound(requiredType))
                this._injector.unbind(requiredType);
        }
    }

    private requiredTypesFor(filter: ITypeFilter, type: FunctionConstructor): FunctionConstructor[] {
        var requiredTypes: FunctionConstructor[] = filter.allOfTypes.concat(filter.anyOfTypes);
{'fraction_non_alphanumeric': 0.2341131284916201, 'fraction_numerical': 0.0017458100558659217, 'mean_word_length': 2.247448979591837, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3872943', 'n_tokens_mistral': 2803, 'n_tokens_neox': 2632, 'n_words': 586}