private static < T > T filterBySupportedProperties ( Class < T > factoryClass , Map < String , String > properties , List < TableFactory > foundFactories , List < TableFactory > classFactories ) { final List < String > plainGivenKeys = new LinkedList <> ( ) ; properties . keySet ( ) . forEach ( k -> { // replace arrays with wildcard String key = k . replaceAll ( ".\\d+" , ".#" ) ; // ignore duplicates if ( ! plainGivenKeys . contains ( key ) ) { plainGivenKeys . add ( key ) ; } } ) ; Optional < String > lastKey = Optional . empty ( ) ; List < TableFactory > supportedFactories = new LinkedList <> ( ) ; for ( TableFactory factory : classFactories ) { Set < String > requiredContextKeys = normalizeContext ( factory ) . keySet ( ) ; Tuple2 < List < String > , List < String > > tuple2 = normalizeSupportedProperties ( factory ) ; // ignore context keys List < String > givenContextFreeKeys = plainGivenKeys . stream ( ) . filter ( p -> ! requiredContextKeys . contains ( p ) ) . collect ( Collectors . toList ( ) ) ; List < String > givenFilteredKeys = filterSupportedPropertiesFactorySpecific ( factory , givenContextFreeKeys ) ; Boolean allTrue = true ; for ( String k : givenFilteredKeys ) { lastKey = Optional . of ( k ) ; if ( ! ( tuple2 . f0 . contains ( k ) || tuple2 . f1 . stream ( ) . anyMatch ( p -> k . startsWith ( p ) ) ) ) { allTrue = false ; break ; } } if ( allTrue ) { supportedFactories . add ( factory ) ; } } if ( supportedFactories . isEmpty ( ) && classFactories . size ( ) == 1 && lastKey . isPresent ( ) ) { // special case: when there is only one matching factory but the last property key // was incorrect TableFactory factory = classFactories . get ( 0 ) ; Tuple2 < List < String > , List < String > > tuple2 = normalizeSupportedProperties ( factory ) ; String errorMessage = String . format ( "The matching factory '%s' doesn't support '%s'.\n\nSupported properties of " + "this factory are:\n%s" , factory . getClass ( ) . getName ( ) , lastKey . get ( ) , String . join ( "\n" , tuple2 . f0 ) ) ; throw new NoMatchingTableFactoryException ( errorMessage , factoryClass , foundFactories , properties ) ; } else if ( supportedFactories . isEmpty ( ) ) { throw new NoMatchingTableFactoryException ( "No factory supports all properties." , factoryClass , foundFactories , properties ) ; } else if ( supportedFactories . size ( ) > 1 ) { throw new AmbiguousTableFactoryException ( supportedFactories , factoryClass , foundFactories , properties ) ; } return ( T ) supportedFactories . get ( 0 ) ; }
Filters the matching class factories by supported properties .