import { getQualifiedClassName } from "../../framework/impl/getQualifiedClassName" ; import { isInstanceOfType } from "./isInstanceOfType" ; import { IType } from "./IType" ; import { ITypeFilter } from "./ITypeFilter" ; private export class TypeFilter implements ITypeFilter { protected _allOfTypes : IType < any > [ ] ; inheritDoc public get allOfTypes ( ) : IType < any > [ ] { return this . _allOfTypes ; } protected _anyOfTypes : IType < any > [ ] ; inheritDoc public get anyOfTypes ( ) : IType < any > [ ] { return this . _anyOfTypes ; } protected _noneOfTypes : IType < any > [ ] ; inheritDoc public get noneOfTypes ( ) : IType < any > [ ] { return this . _noneOfTypes ; } protected _descriptor : string ; inheritDoc public get descriptor ( ) : string { return ( this . _descriptor = this . _descriptor || this . createDescriptor ( ) ) ; } private constructor ( allOf , anyOf , noneOf ) { if ( ! allOf || ! anyOf || ! noneOf ) { throw Error ( "TypeFilter parameters can not be null" ) ; } this . _allOfTypes = allOf ; this . _anyOfTypes = anyOf ; this . _noneOfTypes = noneOf ; } inheritDoc public matches ( item ) { let i = this . _allOfTypes . length ; while ( i -- ) { if ( ! isInstanceOfType ( item , this . _allOfTypes [ i ] ) ) { return false ; } } i = this . _noneOfTypes . length ; while ( i -- ) { if ( isInstanceOfType ( item , this . _noneOfTypes [ i ] ) ) { return false ; } } if ( this . _anyOfTypes . length === 0 && ( this . _allOfTypes . length > 0 || this . _noneOfTypes . length > 0 ) ) { return true ; } i = this . _anyOfTypes . length ; while ( i -- ) { if ( isInstanceOfType ( item , this . _anyOfTypes [ i ] ) ) { return true ; } } return false ; } protected alphabetiseCaseInsensitiveFCQNs ( classes ) { let fqcn ; let allFCQNs = [ ] ; let iLength = classes . length ; for ( let i = 0 ; i < iLength ; i ++ ) { fqcn = getQualifiedClassName ( classes [ i ] ) ; allFCQNs [ allFCQNs . length ] = fqcn ; } return allFCQNs . sort ( this . stringSort ) ; } protected createDescriptor ( ) { let allOf_FCQNs = this . alphabetiseCaseInsensitiveFCQNs ( this . allOfTypes ) ; let anyOf_FCQNs = this . alphabetiseCaseInsensitiveFCQNs ( this . anyOfTypes ) ; let noneOf_FQCNs = this . alphabetiseCaseInsensitiveFCQNs ( this . noneOfTypes ) ; let description = [ ] ; if ( allOf_FCQNs . length ) { description . push ( "all of: " + allOf_FCQNs . toString ( ) ) ; } if ( anyOf_FCQNs . length ) { description . push ( "any of: " + anyOf_FCQNs . toString ( ) ) ; } if ( noneOf_FQCNs . length ) { description . push ( "none of: " + noneOf_FQCNs . toString ( ) ) ; } return description . join ( "; " ) ; } protected stringSort ( item1 , item2 ) { let result = 0 ; item1 = item1 . toUpperCase ( ) ; item2 = item2 . toUpperCase ( ) ; if ( item1 < item2 ) { result = - 1 ; } else if ( item1 > item2 ) { result = 1 ; } return result ; } }