Example usage for java.util Deque peekFirst

List of usage examples for java.util Deque peekFirst

Introduction

In this page you can find the example usage for java.util Deque peekFirst.

Prototype

E peekFirst();

Source Link

Document

Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.

Usage

From source file:Main.java

public static void main(String[] args) {

    Deque<Integer> deque = new ArrayDeque<Integer>(8);

    deque.add(1);/*from   ww  w .  j  a  v a  2s.c  om*/
    deque.add(2);
    deque.add(4);
    deque.add(5);

    int retval = deque.peekFirst();
    System.out.println("Retrieved Element is " + retval);

    System.out.println(deque);
}

From source file:Main.java

public static void main(String[] args) {
    Deque<String> deque = new LinkedList<>();
    deque.addLast("Oracle");
    deque.offerLast("Java");
    deque.offerLast("CSS");
    deque.offerLast("XML");

    System.out.println("Deque: " + deque);

    // remove elements from the Deque until it is empty
    while (deque.peekFirst() != null) {
        System.out.println("Head  Element: " + deque.peekFirst());
        deque.removeFirst();/*from  ww  w. jav  a 2s  .  c o  m*/
        System.out.println("Removed one  element from  Deque");
        System.out.println("Deque: " + deque);
    }

    // the Deque is empty. Try to call its peekFirst(),
    // getFirst(), pollFirst() and removeFirst() methods
    System.out.println("deque.isEmpty(): " + deque.isEmpty());

    System.out.println("deque.peekFirst(): " + deque.peekFirst());
    System.out.println("deque.pollFirst(): " + deque.pollFirst());

    String str = deque.getFirst();
    System.out.println("deque.getFirst(): " + str);
    str = deque.removeFirst();
    System.out.println("deque.removeFirst(): " + str);

}

From source file:bb.mcmc.analysis.ESSConvergeStat.java

@Override
protected double calculateEachProgress(Double stat, Deque<Double> record) {

    if (!Double.isNaN(stat)) {
        if (record.size() > 0) {
            record.pop();/*from ww  w .j  a va  2 s  .co  m*/
        }
        record.add(stat);
    }
    stat = record.peekFirst();
    double progress = stat / essThreshold;
    return progress;
}

From source file:org.molasdin.wbase.xml.parser.light.basic.BasicParser.java

@Override
public Element parse(String value) throws ParserException {
    Deque<BasicElement> elements = new LinkedList<BasicElement>();

    int index = 0;
    boolean leftAngleFound = false;
    int leftAngleIndex = 0;

    boolean possibleCloseTag = false;
    int closeSlashIndex = 0;

    BasicElement rootElement = new BasicElement();
    rootElement.setValid(true);// ww  w.ja  va  2s.c om
    elements.push(rootElement);

    while (index < value.length()) {
        if (value.charAt(index) == '<') {
            //if '<' is before '<'
            if (leftAngleFound) {
                //treat it as '<' symbol
                //build string from the first '<' till the current
                String entry = value.substring(leftAngleIndex, index);
                appendText(entry, elements.peekFirst());

                invokeHandlers(value, leftAngleIndex, ErrorType.LESS_FOUND, "");
            }
            leftAngleFound = true;
            leftAngleIndex = index;
            possibleCloseTag = false;
        } else if (value.charAt(index) == '/') {
            //if '<' has been found
            if (leftAngleFound) {
                //slash may be in closing tag
                closeSlashIndex = index;
                possibleCloseTag = true;
            } else {
                appendText("/", elements.peekFirst());
            }
        } else if (value.charAt(index) == '>') {
            //if '>' without '<' before
            if (!leftAngleFound) {
                //treat '>' as symbol
                appendText(">", elements.peekFirst());
                invokeHandlers(value, index, ErrorType.GREATER_FOUND, "");
            } else {
                leftAngleFound = false;
                BasicElement elem = elements.peekFirst();
                //check if it is a closing tag
                if (possibleCloseTag && isEmptyRange(value, leftAngleIndex + 1, closeSlashIndex)) {
                    String tag = StringUtils.trim(value.substring(leftAngleIndex + 2, index));
                    //if tag is most possible closing
                    if (!elem.isValid()) {
                        //check first opening
                        elem = elements.pop();
                        if (!elem.tagName().equals(tag)) {
                            BasicElement tmp = elem;
                            elem = elements.pop();
                            //check outer opening
                            if (!elem.tagName().equals(tag)) {
                                throw new BadClosingTag(elem.tagName(), tag);
                            }
                            invokeHandlers(value, -1, ErrorType.NO_CLOSING, tmp.tagName());
                            elem.consumeContent(tmp);
                            elem.setValid(true);
                        }
                    } else {
                        //closing tag without opening
                        throw new BadClosingTag("", tag);
                    }
                    elem.setClosed(true);
                    elem.setValid(true);
                    elem.setValidClosing(true);

                } else {
                    //tag is most possible opening or self closing
                    int rightOffset = index;
                    if (possibleCloseTag) {
                        //check if tag is closing but with characters between "<" and "/"
                        if (!elem.isValid()) {
                            String possibleTag = value.substring(closeSlashIndex + 1, index);
                            if (elem.tagName().equals(possibleTag)) {
                                throw new CharactersInClosing(leftAngleIndex);
                            }
                        }

                        //check if "/" is in attributes
                        if (value.substring(closeSlashIndex + 1, rightOffset).trim().length() == 0) {
                            rightOffset = closeSlashIndex;
                        } else {
                            //tag is no closing
                            possibleCloseTag = false;
                        }

                    }

                    //possible start tag
                    String tagName = value.substring(leftAngleIndex + 1, rightOffset);

                    //if no tag but '<>'
                    if (tagName.length() == 0) {
                        //add them to characters
                        String entry = value.substring(leftAngleIndex, index + 1);
                        appendText(entry, elem);
                        invokeHandlers(value, leftAngleIndex, ErrorType.EMPTY_TAG_FOUND, entry);
                    } else {
                        Pair<String, List<Pair<String, String>>> tag = extractTag(tagName);
                        if (tag == null || tag.getLeft() == null) {
                            invokeHandlers(value, leftAngleIndex, ErrorType.INVALID_TEXT_IN_TAG_NAME,
                                    String.valueOf(index));
                            String entry = value.substring(leftAngleIndex, index + 1);
                            appendText(entry, elements.peekFirst());
                        } else {
                            tagName = tag.getLeft();
                            //if tag is allowed
                            if (!predicate.evaluate(tagName)) {
                                throw new DisallowedTagFound(tagName);
                            }
                            //add new element with this tag
                            BasicElement newElem = new BasicElement();
                            newElem.setTagName(tagName);
                            newElem.setAttributes(tag.getRight());
                            elements.peekFirst().addChild(newElem);
                            if (possibleCloseTag) {
                                newElem.setClosed(true);
                                newElem.setShortenIfEmpty(true);
                                newElem.setValid(true);
                                newElem.setValidClosing(true);
                            } else {
                                elements.push(newElem);
                            }
                        }
                    }
                }

                possibleCloseTag = false;
            }

        } else if (!leftAngleFound) {
            //characters block
            BasicElement elem = elements.peekFirst();
            //if other elements exist between tag characters parts
            elem.addCharacter(value.charAt(index));
            /* if (elementTextBreak) {
            elementTextBreak = false;
            elem.addCharacter(value.charAt(index));
             } else {
            elem.lastCharacters().append(value.charAt(index));
             }*/
        }
        index++;
    }

    //if last '<' has not been closed
    if (leftAngleFound) {
        //treat it as symbol
        appendText(value.substring(leftAngleIndex, value.length()), elements.peekFirst());
        invokeHandlers(value, leftAngleIndex, ErrorType.LESS_FOUND, "");
    }

    //find unclosed elements
    if (elements.size() > 1) {
        for (BasicElement elem : elements) {
            if (elem == rootElement) {
                continue;
            }
            if (!elem.isClosed() && !elem.isValid()) {
                invokeHandlers(value, -1, ErrorType.NO_CLOSING, elem.tagName());
                ((BasicElement) elem.parent()).consumeContent(elem);
            }
        }
    }
    return rootElement;
}

From source file:org.springframework.integration.support.management.ExponentialMovingAverageRateTests.java

@Test
@SuppressWarnings("unchecked")
public void testGetTimeSinceLastMeasurement() throws Exception {
    long sleepTime = 20L;

    // fill history with the same value.
    long now = System.nanoTime() - 2 * sleepTime * 1000000;
    for (int i = 0; i < TestUtils.getPropertyValue(history, "retention", Integer.class); i++) {
        history.increment(now);//from w ww. j a v  a  2  s  .c  o  m
    }
    final Deque<Long> times = TestUtils.getPropertyValue(history, "times", Deque.class);
    assertEquals(Long.valueOf(now), times.peekFirst());
    assertEquals(Long.valueOf(now), times.peekLast());

    //increment just so we'll have a different value between first and last
    history.increment(System.nanoTime() - sleepTime * 1000000);
    assertNotEquals(times.peekFirst(), times.peekLast());

    /*
     * We've called Thread.sleep twice with the same value in quick
     * succession. If timeSinceLastSend is pulling off the correct end of
     * the queue, then we should be closer to the sleep time than we are to
     * 2 x sleepTime, but we should definitely be greater than the sleep
     * time.
    */
    double timeSinceLastMeasurement = history.getTimeSinceLastMeasurement();
    assertTrue(timeSinceLastMeasurement > sleepTime);
    assertTrue(timeSinceLastMeasurement <= (1.5 * sleepTime));
}

From source file:org.sybila.parasim.computation.verification.stl.cpu.AbstractUnaryTemporalMonitor.java

private List<Robustness> precomputeRobustness(Monitor subMonitor, FormulaInterval interval) {
    Deque<Robustness> lemireDeque = new LemireDeque<>(createComparator());
    List<Robustness> precomputed = new ArrayList<>();
    Iterator<Robustness> window = subMonitor.iterator();
    Iterator<Robustness> current = subMonitor.iterator();
    int currentIndex = 0;
    float currentTime = current.next().getTime();
    while (window.hasNext()) {
        Robustness memory = null;/*from  w ww.ja v  a2 s  .  c o m*/
        boolean windowEndReached = false;
        // push new points
        while (window.hasNext() && !windowEndReached) {
            memory = window.next();
            // check whether the time upper bound is reached
            if (memory.getTime() < currentTime + interval.getUpperBound()) {
                lemireDeque.offer(memory);
                memory = null;
            } else if (memory.getTime() == currentTime + interval.getUpperBound()) {
                lemireDeque.offer(memory);
                memory = null;
                windowEndReached = true;
            } else {
                windowEndReached = true;
            }
        }
        // check whether the window end has been reached
        if (!windowEndReached) {
            return precomputed;
        }
        // remove useless points
        while (!lemireDeque.isEmpty()
                && lemireDeque.peekFirst().getTime() < currentTime + interval.getLowerBound()) {
            lemireDeque.remove();
        }
        // get the first robustness in deque
        Robustness found = lemireDeque.peekFirst();
        precomputed.add(new SimpleRobustness(found.getValue(), currentTime, getProperty()));
        currentIndex++;
        currentTime = current.next().getTime();
        if (memory != null) {
            lemireDeque.offer(memory);
            memory = null;
        }
    }
    return precomputed;
}

From source file:specminers.evaluation.MinedSpecsFilter.java

private boolean checkIfCatchSectionSwallowsException(String catchBlockStartLine) {
    int startIndex = this.lines.indexOf(catchBlockStartLine);

    // contains return, continue or does not throw.
    Deque<String> blocks = new LinkedList<>();
    blocks.add(catchBlockStartLine);//from  ww w  . j av  a  2s .co  m

    Deque<Integer> blockStartPositions = new LinkedList<>();
    blockStartPositions.add(sourceCode.indexOf(catchBlockStartLine) + catchBlockStartLine.indexOf("{"));

    boolean throwFound = false;

    int currentBlockStart = blockStartPositions.peekFirst();
    int currentIndexInFile = currentBlockStart;
    for (int i = startIndex; i < lines.size(); i++) {
        String line = lines.get(i);
        if (!blocks.isEmpty()) {
            if (line.contains("{") && i != startIndex) {
                blocks.add(line);
                blockStartPositions.add(sourceCode.indexOf(line.trim()));
            }

            if (line.contains("}") && line.indexOf("}") + currentIndexInFile > currentBlockStart) {
                blocks.pop();
                currentBlockStart = blockStartPositions.pop();
            }
            if (line.contains("return ") || line.contains("return;") || line.contains("continue;")) {
                return true;
            }

            if (line.contains("throw ")) {
                throwFound = true;
            }
        }
        currentIndexInFile += line.length();
    }

    return !throwFound;
}