private void processChildren ( List < RemovrSpec > children , Object subInput ) { if ( subInput != null ) { if ( subInput instanceof List ) { List < Object > subList = ( List < Object > ) subInput ; Set < Integer > indiciesToRemove = new HashSet <> ( ) ; // build a list of all indicies to remove for ( RemovrSpec childSpec : children ) { indiciesToRemove . addAll ( childSpec . applyToList ( subList ) ) ; } List < Integer > uniqueIndiciesToRemove = new ArrayList <> ( indiciesToRemove ) ; // Sort the list from Biggest to Smallest, so that when we remove items from the input // list we don't muck up the order. // Aka removing 0 _then_ 3 would be bad, because we would have actually removed // 0 and 4 from the "original" list. Collections . sort ( uniqueIndiciesToRemove , new Comparator < Integer > ( ) { @ Override public int compare ( Integer o1 , Integer o2 ) { return o2 . compareTo ( o1 ) ; } } ) ; for ( int index : uniqueIndiciesToRemove ) { subList . remove ( index ) ; } } else if ( subInput instanceof Map ) { Map < String , Object > subInputMap = ( Map < String , Object > ) subInput ; List < String > keysToRemove = new LinkedList <> ( ) ; for ( RemovrSpec childSpec : children ) { keysToRemove . addAll ( childSpec . applyToMap ( subInputMap ) ) ; } subInputMap . keySet ( ) . removeAll ( keysToRemove ) ; } } }
Call our child nodes build up the set of keys or indices to actually remove and then remove them .