import * as ERROR_MSGS from "../constants/error_msgs" ; import { interfaces } from "../interfaces/interfaces" ; import { isClonable } from "../utils/clonable" ; class Lookup < T > implements interfaces . Lookup < T > { private _map : Map < interfaces . ServiceIdentifier , T [ ] > ; public constructor ( ) { this . _map = new Map < interfaces . ServiceIdentifier , T [ ] > ( ) ; } public getMap ( ) { return this . _map ; } public add ( serviceIdentifier , value ) { if ( serviceIdentifier === null || serviceIdentifier === undefined ) { throw new Error ( ERROR_MSGS . NULL_ARGUMENT ) ; } if ( value === null || value === undefined ) { throw new Error ( ERROR_MSGS . NULL_ARGUMENT ) ; } const entry = this . _map . get ( serviceIdentifier ) ; if ( entry !== undefined ) { entry . push ( value ) ; } else { this . _map . set ( serviceIdentifier , [ value ] ) ; } } public get ( serviceIdentifier ) { if ( serviceIdentifier === null || serviceIdentifier === undefined ) { throw new Error ( ERROR_MSGS . NULL_ARGUMENT ) ; } const entry = this . _map . get ( serviceIdentifier ) ; if ( entry !== undefined ) { return entry ; } else { throw new Error ( ERROR_MSGS . KEY_NOT_FOUND ) ; } } public remove ( serviceIdentifier ) { if ( serviceIdentifier === null || serviceIdentifier === undefined ) { throw new Error ( ERROR_MSGS . NULL_ARGUMENT ) ; } if ( ! this . _map . delete ( serviceIdentifier ) ) { throw new Error ( ERROR_MSGS . KEY_NOT_FOUND ) ; } } public removeIntersection ( lookup ) { this . traverse ( ( serviceIdentifier , value ) => { const lookupActivations = lookup . hasKey ( serviceIdentifier ) ? lookup . get ( serviceIdentifier ) : undefined ; if ( lookupActivations !== undefined ) { const filteredValues = value . filter ( ( lookupValue ) => ! lookupActivations . some ( ( moduleActivation ) => lookupValue === moduleActivation ) ) ; this . _setValue ( serviceIdentifier , filteredValues ) ; } } ) ; } public removeByCondition ( condition ) { const removals = [ ] ; this . _map . forEach ( ( entries , key ) => { const updatedEntries = [ ] ; for ( const entry of entries ) { const remove = condition ( entry ) ; if ( remove ) { removals . push ( entry ) ; } else { updatedEntries . push ( entry ) ; } } this . _setValue ( key , updatedEntries ) ; } ) ; return removals ; } public hasKey ( serviceIdentifier ) { if ( serviceIdentifier === null || serviceIdentifier === undefined ) { throw new Error ( ERROR_MSGS . NULL_ARGUMENT ) ; } return this . _map . has ( serviceIdentifier ) ; } public clone ( ) { const copy = new Lookup < T > ( ) ; this . _map . forEach ( ( value , key ) => { value . forEach ( ( b ) => copy . add ( key , isClonable < T > ( b ) ? b . clone ( ) : b ) ) ; } ) ; return copy ; } public traverse ( func ) { this . _map . forEach ( ( value , key ) => { func ( key , value ) ; } ) ; } private _setValue ( serviceIdentifier , value ) { if ( value . length > 0 ) { this . _map . set ( serviceIdentifier , value ) ; } else { this . _map . delete ( serviceIdentifier ) ; } } } export { Lookup } ;