List of usage examples for org.apache.commons.collections4.iterators PeekingIterator next
public E next()
From source file:com.github.rvesse.airline.parser.aliases.AliasResolver.java
public PeekingIterator<String> resolveAliases(PeekingIterator<String> tokens, ParseState<T> state) { Predicate<? super CommandGroupMetadata> findGroupPredicate; Predicate<? super CommandMetadata> findCommandPredicate; // Nothing to do if no further tokens if (!tokens.hasNext()) return tokens; // Nothing to do if no aliases defined if (state.getParserConfiguration().getAliases().size() == 0) return tokens; Set<String> aliasesSeen = new TreeSet<String>(); do {/*w w w . ja v a 2 s .co m*/ // Try to find an alias AliasMetadata alias = CollectionUtils.find(state.getParserConfiguration().getAliases(), new AliasFinder(tokens.peek())); // Nothing further to do if no aliases found if (alias == null) return tokens; // Check for circular references if (!aliasesSeen.add(alias.getName())) { throw new ParseAliasCircularReferenceException(alias.getName(), aliasesSeen); } // Can we override built-ins? if (!state.getParserConfiguration().aliasesOverrideBuiltIns()) { // If not we must check we don't have a default // group/command with the same name as otherwise that // would take precedence if (state.getGlobal() != null) { GlobalMetadata<T> metadata = state.getGlobal(); findGroupPredicate = new GroupFinder(tokens.peek()); findCommandPredicate = new CommandFinder(tokens.peek()); if (CollectionUtils.find(metadata.getCommandGroups(), findGroupPredicate) != null || CollectionUtils.find(metadata.getDefaultGroupCommands(), findCommandPredicate) != null) return tokens; } } // Discard the alias token tokens.next(); // Apply the alias List<String> newParams = new ArrayList<String>(); List<String> remainingParams = new ArrayList<String>(); while (tokens.hasNext()) { remainingParams.add(tokens.next()); } // Process alias arguments Set<Integer> used = new TreeSet<Integer>(); for (String arg : alias.getArguments()) { if (arg.startsWith("$")) { // May be a positional parameter try { int num = Integer.parseInt(arg.substring(1)); num--; if (num >= 0 && num < remainingParams.size()) { // Valid positional parameter newParams.add(remainingParams.get(num)); used.add(num); continue; } } catch (NumberFormatException e) { // Ignore - the number was invalid so we'll // treat it as an ordinary parameter } } // Some other parameter newParams.add(arg); } // Remove used positional parameters int removed = 0; for (int pos : used) { remainingParams.remove(pos - removed); removed++; } // Pass through any remaining parameters for (String arg : remainingParams) { newParams.add(arg); } // Prepare a new tokens iterator tokens = new PeekingIterator<String>(newParams.iterator()); } while (state.getParserConfiguration().aliasesMayChain()); return tokens; }
From source file:com.github.rvesse.airline.examples.userguide.parser.options.JdbcStyleOptionParser.java
@Override public ParseState<T> parseOptions(PeekingIterator<String> tokens, ParseState<T> state, List<OptionMetadata> allowedOptions) { // Peek at the first input String options = tokens.peek(); // Must have at least one name=value pair for this to be a JDBC style // option specification if (!options.contains(NAME_VALUE_SEPARATOR)) return null; // May potentially be multiple name value pairs in the string separated // by a semicolon String[] optionPairs = options.split(OPTION_SEPARATOR); // Try and parse the options ParseState<T> nextState = state; boolean first = true; for (String pair : optionPairs) { // Allow for empty pair, this may occur if the user terminates the // options with a semicolon which is legitimate if (StringUtils.isEmpty(pair)) continue; if (!pair.contains(NAME_VALUE_SEPARATOR)) { // This would be invalid for us but if this is the first option // we've seen might be valid for another option parser if (first) return null; // Otherwise treat as an invalid option state.getParserConfiguration().getErrorHandler() .handleError(new ParseOptionMissingValueException(pair)); }//from ww w .j av a2 s .c om // Find the relevant option String[] nameValue = pair.split(NAME_VALUE_SEPARATOR, 2); OptionMetadata option = findOption(state, allowedOptions, nameValue[0]); if (option == null) { // No such option, let another option parser try to parse the // option string if (first) return null; state.getParserConfiguration().getErrorHandler().handleError(new ParseOptionUnexpectedException( "JDBC style option '%s' refers to option '%s' which does not refer to a known option", pair, nameValue[0])); } // Tell the parser we're parsing an option nextState = nextState.pushContext(Context.OPTION).withOption(option); if (option.getArity() == 1) { if (first) { // If this is the first valid option we've seen we now // consume the input token tokens.next(); first = false; } // Set the option value nextState = nextState.withOptionValue(option, nameValue[1]).popContext(); } else { // We only permit arity 1 options if (first) return null; state.getParserConfiguration().getErrorHandler().handleError(new ParseOptionUnexpectedException( "JDBC style option '%s' refers to option '%s' which has arity %d, only arity 1 options are supported", pair, nameValue[0], option.getArity())); } } // If we didn't parse anything let other parsers try if (first) return null; // Otherwise return the new state return nextState; }
From source file:org.openscore.lang.compiler.modeller.ExecutableBuilder.java
private Workflow compileWorkFlow(List<Map<String, Map<String, Object>>> workFlowRawData, Map<String, String> imports, Workflow onFailureWorkFlow, boolean onFailureSection) { Deque<Task> tasks = new LinkedList<>(); Validate.notEmpty(workFlowRawData, "Flow must have tasks in its workflow"); PeekingIterator<Map<String, Map<String, Object>>> iterator = new PeekingIterator<>( workFlowRawData.iterator()); boolean isOnFailureDefined = onFailureWorkFlow != null; String defaultFailure = isOnFailureDefined ? onFailureWorkFlow.getTasks().getFirst().getName() : FAILURE_RESULT;/*from w w w . j a v a 2s . com*/ Set<String> taskNames = new HashSet<>(); while (iterator.hasNext()) { Map<String, Map<String, Object>> taskRawData = iterator.next(); Map<String, Map<String, Object>> nextTaskData = iterator.peek(); String taskName = taskRawData.keySet().iterator().next(); if (taskNames.contains(taskName)) { throw new RuntimeException("Task name: \'" + taskName + "\' appears more than once in the workflow. Each task name in the workflow must be unique"); } taskNames.add(taskName); Map<String, Object> taskRawDataValue; String message = "Task: " + taskName + " syntax is illegal.\nBelow task name, there should be a map of values in the format:\ndo:\n\top_name:"; try { taskRawDataValue = taskRawData.values().iterator().next(); if (MapUtils.isNotEmpty(taskRawDataValue) && taskRawDataValue.containsKey(LOOP_KEY)) { message = "Task: " + taskName + " syntax is illegal.\nBelow the 'loop' keyword, there should be a map of values in the format:\nfor:\ndo:\n\top_name:"; taskRawDataValue.putAll((Map<String, Object>) taskRawDataValue.remove(LOOP_KEY)); } } catch (ClassCastException ex) { throw new RuntimeException(message); } String defaultSuccess; if (nextTaskData != null) { defaultSuccess = nextTaskData.keySet().iterator().next(); } else { defaultSuccess = onFailureSection ? FAILURE_RESULT : SUCCESS_RESULT; } Task task = compileTask(taskName, taskRawDataValue, defaultSuccess, imports, defaultFailure); tasks.add(task); } if (isOnFailureDefined) { tasks.addAll(onFailureWorkFlow.getTasks()); } return new Workflow(tasks); }
From source file:org.openscore.lang.compiler.utils.ExecutableBuilder.java
private Workflow compileWorkFlow(LinkedHashMap<String, Map<String, Object>> workFlowRawData, Map<String, String> imports, Workflow onFailureWorkFlow, boolean onFailureSection) { Deque<Task> tasks = new LinkedList<>(); Validate.notEmpty(workFlowRawData, "Flow must have tasks in its workflow"); PeekingIterator<Map.Entry<String, Map<String, Object>>> iterator = new PeekingIterator<>( workFlowRawData.entrySet().iterator()); boolean isOnFailureDefined = onFailureWorkFlow != null; String defaultFailure = isOnFailureDefined ? onFailureWorkFlow.getTasks().getFirst().getName() : FAILURE_RESULT;/* w w w . j a v a 2 s .co m*/ while (iterator.hasNext()) { Map.Entry<String, Map<String, Object>> taskRawData = iterator.next(); Map.Entry<String, Map<String, Object>> nextTaskData = iterator.peek(); String taskName = taskRawData.getKey(); Map<String, Object> taskRawDataValue; try { taskRawDataValue = taskRawData.getValue(); } catch (ClassCastException ex) { throw new RuntimeException("Task: " + taskName + " syntax is illegal.\nBelow task name, there should be a map of values in the format:\ndo:\n\top_name:"); } String defaultSuccess; if (nextTaskData != null) { defaultSuccess = nextTaskData.getKey(); } else { defaultSuccess = onFailureSection ? FAILURE_RESULT : SUCCESS_RESULT; } Task task = compileTask(taskName, taskRawDataValue, defaultSuccess, imports, defaultFailure); tasks.add(task); } if (isOnFailureDefined) { tasks.addAll(onFailureWorkFlow.getTasks()); } return new Workflow(tasks); }