Example usage for java.util ArrayDeque ArrayDeque

List of usage examples for java.util ArrayDeque ArrayDeque

Introduction

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

Prototype

public ArrayDeque() 

Source Link

Document

Constructs an empty array deque with an initial capacity sufficient to hold 16 elements.

Usage

From source file:com.google.gwt.emultest.java.util.ArrayDequeTest.java

public void testRemoveElement() {
    Object o1 = new Object();
    Object o2 = new Object();

    ArrayDeque<Object> deque = new ArrayDeque<>();
    assertFalse(deque.remove(o1));//from   w  w w.  j av a2s .  c  o  m

    deque.add(o1);
    assertTrue(deque.remove(o1));
    assertTrue(deque.isEmpty());

    deque.add(o1);
    deque.add(o2);
    assertTrue(deque.remove(o1));
    checkDequeSizeAndContent(deque, o2);
    assertTrue(deque.remove(o2));
    assertTrue(deque.isEmpty());

    assertFalse(deque.remove(null));
}

From source file:com.google.gwt.emultest.java.util.ArrayDequeTest.java

public void testRemoveFirst() {
    Object o1 = new Object();
    Object o2 = new Object();

    ArrayDeque<Object> deque = new ArrayDeque<>();
    try {/* www. j  a  va2 s.co m*/
        deque.removeFirst();
        fail();
    } catch (NoSuchElementException expected) {
    }

    deque.add(o1);
    assertEquals(o1, deque.removeFirst());
    assertTrue(deque.isEmpty());

    deque.add(o1);
    deque.add(o2);
    assertEquals(o1, deque.removeFirst());
    checkDequeSizeAndContent(deque, o2);
    assertEquals(o2, deque.removeFirst());
    assertTrue(deque.isEmpty());

    try {
        deque.removeFirst();
        fail();
    } catch (NoSuchElementException expected) {
    }
}

From source file:com.espertech.esper.core.service.EPRuntimeIsolatedImpl.java

private void processMatches(EventBean theEvent) {
    // get matching filters
    ArrayBackedCollection<FilterHandle> matches = matchesArrayThreadLocal.get();
    services.getFilterService().evaluate(theEvent, matches);

    if (ThreadLogUtil.ENABLED_TRACE) {
        ThreadLogUtil.trace("Found matches for underlying ", matches.size(), theEvent.getUnderlying());
    }//from  w w  w . j av  a2s  .c  om

    if (matches.size() == 0) {
        return;
    }

    Map<EPStatementAgentInstanceHandle, ArrayDeque<FilterHandleCallback>> stmtCallbacks = matchesPerStmtThreadLocal
            .get();
    Object[] matchArray = matches.getArray();
    int entryCount = matches.size();

    for (int i = 0; i < entryCount; i++) {
        EPStatementHandleCallback handleCallback = (EPStatementHandleCallback) matchArray[i];
        EPStatementAgentInstanceHandle handle = handleCallback.getAgentInstanceHandle();

        // Self-joins require that the internal dispatch happens after all streams are evaluated.
        // Priority or preemptive settings also require special ordering.
        if (handle.isCanSelfJoin() || isPrioritized) {
            ArrayDeque<FilterHandleCallback> callbacks = stmtCallbacks.get(handle);
            if (callbacks == null) {
                callbacks = new ArrayDeque<FilterHandleCallback>();
                stmtCallbacks.put(handle, callbacks);
            }
            callbacks.add(handleCallback.getFilterCallback());
            continue;
        }

        processStatementFilterSingle(handle, handleCallback, theEvent);
    }
    matches.clear();
    if (stmtCallbacks.isEmpty()) {
        return;
    }

    for (Map.Entry<EPStatementAgentInstanceHandle, ArrayDeque<FilterHandleCallback>> entry : stmtCallbacks
            .entrySet()) {
        EPStatementAgentInstanceHandle handle = entry.getKey();
        ArrayDeque<FilterHandleCallback> callbackList = entry.getValue();

        processStatementFilterMultiple(handle, callbackList, theEvent);

        if ((isPrioritized) && (handle.isPreemptive())) {
            break;
        }
    }
    stmtCallbacks.clear();
}

From source file:com.google.gwt.emultest.java.util.ArrayDequeTest.java

public void testRemoveFirstOccurrence() {
    Object o1 = new Object();
    Object o2 = new Object();
    Object o3 = new Object();

    ArrayDeque<Object> deque = new ArrayDeque<>();
    assertFalse(deque.removeFirstOccurrence(o1));

    deque.add(o1);// w  ww .j a  v  a2s  . c o m
    assertTrue(deque.removeFirstOccurrence(o1));
    assertTrue(deque.isEmpty());

    deque = new ArrayDeque<>();
    deque.add(o1);
    deque.add(o2);
    deque.add(o3);
    assertTrue(deque.removeFirstOccurrence(o2));
    checkDequeSizeAndContent(deque, o1, o3);

    deque = new ArrayDeque<>();
    deque.add(o1);
    deque.add(o2);
    deque.add(o3);
    deque.add(o1);
    deque.add(o2);
    deque.add(o3);
    assertTrue(deque.removeFirstOccurrence(o2));
    checkDequeSizeAndContent(deque, o1, o3, o1, o2, o3);
    assertTrue(deque.removeFirstOccurrence(o2));
    checkDequeSizeAndContent(deque, o1, o3, o1, o3);
    assertTrue(deque.removeFirstOccurrence(o1));
    checkDequeSizeAndContent(deque, o3, o1, o3);
    assertTrue(deque.removeFirstOccurrence(o1));
    checkDequeSizeAndContent(deque, o3, o3);
    assertFalse(deque.removeFirstOccurrence(o1));

    assertFalse(deque.removeFirstOccurrence(null));
}

From source file:org.gdms.source.DefaultSourceManager.java

private void removeFromSchema(String name) {
    if (name.isEmpty()) {
        throw new IllegalArgumentException("Empty table name!");
    }/*from www . j a v a  2 s .c o  m*/

    // split on the dots '.' into
    // schema1.schema2.schema3.table1

    String[] l = DOT.split(name);

    if (l.length <= 1) {
        // just a table, we remove it from the root schema
        schema.removeTable(name);
    } else {
        Deque<Schema> path = new ArrayDeque<Schema>();
        path.add(schema);
        // we get down
        // to the last schema before the table
        for (int i = 0; i < l.length - 1; i++) {
            final Schema n = path.getFirst().getSubSchemaByName(l[i]);
            path.addFirst(n);
        }

        boolean stop = false;
        while (!path.isEmpty() && !stop) {
            // take the last schema in the path (top of the pile)
            final Schema n = path.pollFirst();
            n.removeTable(l[l.length - 1]);
            if (n.getTableCount() != 0 || n.getSubSchemaNames().length != 0) {
                // the schema is still needed, we must not remove it
                stop = true;
            } else {
                Schema p = n.getParentSchema();
                if (p != null) {
                    p.removeSubSchema(n.getName());
                } else {
                    // we have reached root, it stays were it is...
                    stop = true;
                }
            }
        }
    }
}

From source file:uniol.apt.adt.automaton.FiniteAutomatonUtility.java

/**
 * Find a word whose prefixes (including the word) conform to a given predicate and which itself also conforms
 * to a second predicate.//w  w  w.  j a v  a2  s.  c  o  m
 *
 * This method uses a depth-first search. A breath-first search would use more memory.
 *
 * @param a The automaton whose accepted words should get checked.
 * @param prefixPredicate The predicate to check the prefixes.
 * @param wordPredicate The predicate to check the words.
 * @return A word which conforms to the predicates.
 */
static public List<String> findPredicateWord(FiniteAutomaton a, Predicate<List<String>> prefixPredicate,
        Predicate<List<String>> wordPredicate) {
    MinimalDeterministicFiniteAutomaton dfa = minimizeInternal(a);
    Deque<Pair<DFAState, Iterator<Symbol>>> trace = new ArrayDeque<>();
    LinkedList<String> word = new LinkedList<>();
    DFAState initial = dfa.getInitialState();
    DFAState sinkState = findSinkState(dfa);
    trace.add(new Pair<>(initial, initial.getDefinedSymbols().iterator()));

    while (!trace.isEmpty()) {
        Pair<DFAState, Iterator<Symbol>> pair = trace.peekLast();
        if (!pair.getSecond().hasNext()) {
            trace.removeLast();
            word.pollLast();
        } else {
            Symbol symbol = pair.getSecond().next();
            DFAState nextState = pair.getFirst().getFollowingState(symbol);
            if (!nextState.equals(sinkState)) {
                word.add(symbol.getEvent());

                List<String> roWord = ListUtils.unmodifiableList(word);

                if (prefixPredicate.evaluate(roWord)) {
                    trace.addLast(new Pair<>(nextState, nextState.getDefinedSymbols().iterator()));

                    if (nextState.isFinalState() && wordPredicate.evaluate(roWord))
                        return word;
                } else {
                    word.removeLast();
                }
            }
        }
    }

    return null;
}

From source file:org.apache.xml.security.stax.impl.processor.input.AbstractDecryptInputProcessor.java

private EncryptedDataType parseEncryptedDataStructure(boolean isSecurityHeaderEvent, XMLSecEvent xmlSecEvent,
        InputProcessorChain subInputProcessorChain) throws XMLStreamException, XMLSecurityException {

    Deque<XMLSecEvent> xmlSecEvents = new ArrayDeque<XMLSecEvent>();
    xmlSecEvents.push(xmlSecEvent);//from   w  w w  .  j  a va  2s .com
    XMLSecEvent encryptedDataXMLSecEvent;
    int count = 0;
    int keyInfoCount = 0;
    do {
        subInputProcessorChain.reset();
        if (isSecurityHeaderEvent) {
            encryptedDataXMLSecEvent = subInputProcessorChain.processHeaderEvent();
        } else {
            encryptedDataXMLSecEvent = subInputProcessorChain.processEvent();
        }

        xmlSecEvents.push(encryptedDataXMLSecEvent);
        if (++count >= maximumAllowedEncryptedDataEvents) {
            throw new XMLSecurityException("stax.xmlStructureSizeExceeded",
                    new Object[] { maximumAllowedEncryptedDataEvents });
        }

        //the keyInfoCount is necessary to prevent early while-loop abort when the KeyInfo also contains a CipherValue.
        if (encryptedDataXMLSecEvent.getEventType() == XMLStreamConstants.START_ELEMENT
                && encryptedDataXMLSecEvent.asStartElement().getName()
                        .equals(XMLSecurityConstants.TAG_dsig_KeyInfo)) {
            keyInfoCount++;
        } else if (encryptedDataXMLSecEvent.getEventType() == XMLStreamConstants.END_ELEMENT
                && encryptedDataXMLSecEvent.asEndElement().getName()
                        .equals(XMLSecurityConstants.TAG_dsig_KeyInfo)) {
            keyInfoCount--;
        }
    } while (!((encryptedDataXMLSecEvent.getEventType() == XMLStreamConstants.START_ELEMENT
            && encryptedDataXMLSecEvent.asStartElement().getName()
                    .equals(XMLSecurityConstants.TAG_xenc_CipherValue)
            || encryptedDataXMLSecEvent.getEventType() == XMLStreamConstants.END_ELEMENT
                    && encryptedDataXMLSecEvent.asEndElement().getName()
                            .equals(XMLSecurityConstants.TAG_xenc_EncryptedData))
            && keyInfoCount == 0));

    xmlSecEvents.push(XMLSecEventFactory.createXmlSecEndElement(XMLSecurityConstants.TAG_xenc_CipherValue));
    xmlSecEvents.push(XMLSecEventFactory.createXmlSecEndElement(XMLSecurityConstants.TAG_xenc_CipherData));
    xmlSecEvents.push(XMLSecEventFactory.createXmlSecEndElement(XMLSecurityConstants.TAG_xenc_EncryptedData));

    EncryptedDataType encryptedDataType;

    try {
        Unmarshaller unmarshaller = XMLSecurityConstants
                .getJaxbUnmarshaller(getSecurityProperties().isDisableSchemaValidation());
        @SuppressWarnings("unchecked")
        JAXBElement<EncryptedDataType> encryptedDataTypeJAXBElement = (JAXBElement<EncryptedDataType>) unmarshaller
                .unmarshal(new XMLSecurityEventReader(xmlSecEvents, 0));
        encryptedDataType = encryptedDataTypeJAXBElement.getValue();

    } catch (JAXBException e) {
        throw new XMLSecurityException(e);
    }
    return encryptedDataType;
}

From source file:edu.upenn.cis.orchestra.workloadgenerator.Generator.java

public void findSimpleCycles(List<List<Integer>> cycles, List<List<Object>> mappings) {
    // First, index the edges
    List<List<Integer>> edges = new ArrayList<List<Integer>>();

    for (int i = 0; i < _peers.size(); i++) {
        edges.add(new ArrayList<Integer>());
    }//from  w  w w  .  ja  v a 2 s. com

    for (List<Object> thisMapping : mappings) {
        edges.get((Integer) thisMapping.get(0)).add((Integer) thisMapping.get(1));
    }

    for (List<Integer> thisEdge : edges) {
        Collections.sort(thisEdge);
    }

    // Find simple cycles as follows:
    // - Handle the peers in order
    // - Find simple cycles where the smallest node in the cycle
    // is the peer
    cycles.clear();
    for (int i = 0; i < _peers.size(); i++) {
        Deque<List<Integer>> paths = new ArrayDeque<List<Integer>>();
        paths.push(new ArrayList<Integer>());
        paths.peek().add(i);
        while (0 != paths.size()) {
            List<Integer> path = paths.pop();
            for (Integer j : edges.get(path.get(path.size() - 1))) {
                if (j.equals(i)) {
                    List<Integer> cycle = new ArrayList<Integer>();
                    cycle.addAll(path);
                    cycle.add(j);
                    cycles.add(cycle);
                } else if (j > i && !path.contains(j)) {
                    List<Integer> newPath = new ArrayList<Integer>();
                    newPath.addAll(path);
                    newPath.add(j);
                    paths.push(newPath);
                }
            }
        }
    }
}

From source file:edu.oregonstate.eecs.mcplan.domains.planetwars.PwSimulator.java

@Override
public void takeAction(final JointAction<PwEvent> a) {
    final ArrayDeque<PwEvent> frame = new ArrayDeque<PwEvent>();
    event_history.push(frame);//w  w w.  j av  a 2 s  .com

    for (final PwEvent e : a) {
        applyEvent(e);
    }

    advance();
    s.t += 1;
    depth_ += frame.size();
}

From source file:com.google.gwt.emultest.java.util.ArrayDequeTest.java

public void testRemoveLast() {
    Object o1 = new Object();
    Object o2 = new Object();

    ArrayDeque<Object> deque = new ArrayDeque<>();
    try {//from  ww w .  j ava  2  s  .  c  o  m
        deque.removeLast();
        fail();
    } catch (NoSuchElementException expected) {
    }

    deque.add(o1);
    assertEquals(o1, deque.removeLast());
    assertTrue(deque.isEmpty());

    deque.add(o1);
    deque.add(o2);
    assertEquals(o2, deque.removeLast());
    checkDequeSizeAndContent(deque, o1);
    assertEquals(o1, deque.removeLast());
    assertEquals(0, deque.size());

    try {
        deque.removeLast();
        fail();
    } catch (NoSuchElementException expected) {
    }
}