public Map < String , String > detectVariableValues ( TokenList tokenList ) { Map < String , String > variables = new HashMap < String , String > ( ) ; boolean thisContainsVariables = containsVariables ( ) ; boolean thatContainsVariables = tokenList . containsVariables ( ) ; // If both lists contain variables, we get into trouble. if ( thisContainsVariables && thatContainsVariables ) { throw new VariableValueDetectionException ( "Cannot detect variable values if both token lists contain variables." ) ; } // If both lists don't contain variables, no variables can be detected and we can return the empty map. if ( ! thisContainsVariables && ! thatContainsVariables ) { return variables ; } ListIterator < Token > iteratorWithVariables = ( thisContainsVariables ? this : tokenList ) . listIterator ( ) ; ListIterator < Token > iteratorWithoutVariables = ( thisContainsVariables ? tokenList : this ) . listIterator ( ) ; // We now know that {@code iteratorWithVariables} contains at least one variable. We iterate over both lists // simultaneously and if {@code possibleVariableToken} is a {@link VariableToken}, we know that // {@code literalToken} contains the value for this variable. while ( iteratorWithVariables . hasNext ( ) && iteratorWithoutVariables . hasNext ( ) ) { Token possibleVariableToken = iteratorWithVariables . next ( ) ; Token literalToken = iteratorWithoutVariables . next ( ) ; if ( possibleVariableToken instanceof VariableToken ) { variables . put ( possibleVariableToken . getText ( ) , literalToken . getText ( ) ) ; } } return variables ; }
Detects variable values using another token list .