Example usage for org.apache.commons.collections4.iterators PeekingIterator hasNext

List of usage examples for org.apache.commons.collections4.iterators PeekingIterator hasNext

Introduction

In this page you can find the example usage for org.apache.commons.collections4.iterators PeekingIterator hasNext.

Prototype

public boolean hasNext() 

Source Link

Usage

From source file:com.github.rvesse.airline.parser.options.ClassicGetOptParser.java

public ParseState<T> parseOptions(PeekingIterator<String> tokens, ParseState<T> state,
        List<OptionMetadata> allowedOptions) {
    if (!hasShortNamePrefix(tokens.peek())) {
        return null;
    }/* ww w  . j  a v a 2  s .c o m*/

    // remove leading dash from token
    String remainingToken = tokens.peek().substring(1);

    ParseState<T> nextState = state;
    boolean first = true;
    while (!remainingToken.isEmpty()) {
        char tokenCharacter = remainingToken.charAt(0);

        // is the current token character a single letter option?
        OptionMetadata option = findOption(state, allowedOptions, "-" + tokenCharacter);
        if (option == null) {
            return null;
        }

        nextState = nextState.pushContext(Context.OPTION).withOption(option);

        // remove current token character
        remainingToken = remainingToken.substring(1);

        // for no argument options, process the option and remove the
        // character from the token
        if (option.getArity() == 0) {
            nextState = nextState.withOptionValue(option, Boolean.TRUE.toString()).popContext();
            first = false;
            continue;
        }

        if (option.getArity() == 1) {
            // we must, consume the current token so we can see the next
            // token
            tokens.next();

            // if current token has more characters, this is the value;
            // otherwise it is the next token
            if (!remainingToken.isEmpty()) {
                nextState = nextState.withOptionValue(option, remainingToken).popContext();
            } else if (tokens.hasNext()) {
                nextState = nextState.withOptionValue(option, tokens.next()).popContext();
            }

            return nextState;
        }

        // Don't throw an error if this is the first option we have seen as
        // in that case the option may legitimately be processed by another
        // option parser
        if (first)
            return null;
        throw new ParseOptionUnexpectedException("Short options style can not be used with option %s", option);
    }

    // consume the current token
    tokens.next();

    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  va 2s .c o  m*/

    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  av 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);
}

From source file:org.openvpms.archetype.rules.workflow.FreeSlotIterators.java

/**
 * Gets the next iterator.//from w w w.j  av a2s.c  o m
 */
private void getNext() {
    next = null;
    ListIterator<PeekingIterator<Slot>> listIterator = iterators.listIterator();
    Date minDate = null;
    while (listIterator.hasNext()) {
        PeekingIterator<Slot> iterator = listIterator.next();
        if (!iterator.hasNext()) {
            listIterator.remove();
        } else {
            Slot slot = iterator.peek();
            if (minDate == null || DateRules.compareTo(minDate, slot.getStartTime()) > 0) {
                minDate = slot.getStartTime();
                next = iterator;
            }
        }
    }
}