Example usage for java.util ArrayDeque add

List of usage examples for java.util ArrayDeque add

Introduction

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

Prototype

public boolean add(E e) 

Source Link

Document

Inserts the specified element at the end of this deque.

Usage

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

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

    ArrayDeque<Object> deque = new ArrayDeque<>();
    try {/*from   w w  w  .j a  va  2s  .c  om*/
        deque.getLast();
        fail();
    } catch (NoSuchElementException expected) {
    }

    deque.add(o1);
    assertEquals(o1, deque.getLast());
    checkDequeSizeAndContent(deque, o1);

    deque.add(o2);
    assertEquals(o2, deque.getLast());
    checkDequeSizeAndContent(deque, o1, o2);

    deque.clear();
    assertTrue(deque.isEmpty());
    try {
        deque.getLast();
        fail();
    } catch (NoSuchElementException expected) {
    }
}

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

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

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

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

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

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

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

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

    ArrayDeque<Object> deque = new ArrayDeque<>();
    try {/*from  w  w  w .  ja va2 s  . c  o  m*/
        deque.getFirst();
        fail();
    } catch (NoSuchElementException expected) {
    }

    deque.add(o1);
    assertEquals(o1, deque.getFirst());
    checkDequeSizeAndContent(deque, o1);

    deque.add(o2);
    assertEquals(o1, deque.getFirst());
    checkDequeSizeAndContent(deque, o1, o2);

    deque.clear();
    assertTrue(deque.isEmpty());
    try {
        deque.getFirst();
        fail();
    } catch (NoSuchElementException expected) {
    }
}

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  w  w w  .  java  2 s.  c  om*/
        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) {
    }
}

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 {//from  w  ww  . j  ava 2  s .c  om
        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.google.gwt.emultest.java.util.ArrayDequeTest.java

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

    ArrayDeque<Object> deque = new ArrayDeque<>();
    Iterator<Object> it = deque.descendingIterator();
    assertFalse(it.hasNext());/*from   w w  w  .j  a va  2s.c  om*/
    try {
        it.next();
        fail();
    } catch (NoSuchElementException expected) {
    }

    deque.add(o1);
    deque.add(o2);
    deque.add(o3);
    it = deque.descendingIterator();
    assertTrue(it.hasNext());
    assertEquals(o3, it.next());
    assertTrue(it.hasNext());
    assertEquals(o2, it.next());
    assertTrue(it.hasNext());
    assertEquals(o1, it.next());
    assertFalse(it.hasNext());
    try {
        it.next();
        fail();
    } catch (NoSuchElementException expected) {
    }
    checkDequeSizeAndContent(deque, o1, o2, o3);

    deque = new ArrayDeque<>();
    deque.add(o1);
    deque.add(o2);
    deque.add(o3);
    it = deque.descendingIterator();
    assertTrue(it.hasNext());
    assertEquals(o3, it.next());
    it.remove();
    assertEquals(2, deque.size());
    assertTrue(it.hasNext());
    assertEquals(o2, it.next());
    assertTrue(it.hasNext());
    assertEquals(o1, it.next());
    it.remove();
    checkDequeSizeAndContent(deque, o2);
}

From source file:com.espertech.esper.core.start.EPPreparedExecuteMethodQuery.java

/**
 * Executes the prepared query.//from  ww  w  .  ja v  a2 s . c o m
 * @return query results
 */
public EPPreparedQueryResult execute(ContextPartitionSelector[] contextPartitionSelectors) {
    int numStreams = processors.length;

    if (contextPartitionSelectors != null && contextPartitionSelectors.length != numStreams) {
        throw new IllegalArgumentException(
                "Number of context partition selectors does not match the number of named windows in the from-clause");
    }

    // handle non-context case
    if (statementSpec.getOptionalContextName() == null) {

        Collection<EventBean>[] snapshots = new Collection[numStreams];
        for (int i = 0; i < numStreams; i++) {

            ContextPartitionSelector selector = contextPartitionSelectors == null ? null
                    : contextPartitionSelectors[i];
            snapshots[i] = getStreamFilterSnapshot(i, selector);
        }

        resultSetProcessor.clear();
        return process(snapshots);
    }

    List<ContextPartitionResult> contextPartitionResults = new ArrayList<ContextPartitionResult>();
    ContextPartitionSelector singleSelector = contextPartitionSelectors != null
            && contextPartitionSelectors.length > 0 ? contextPartitionSelectors[0] : null;

    // context partition runtime query
    Collection<Integer> agentInstanceIds = EPPreparedExecuteMethodHelper.getAgentInstanceIds(processors[0],
            singleSelector, services.getContextManagementService(), statementSpec.getOptionalContextName());

    // collect events and agent instances
    for (int agentInstanceId : agentInstanceIds) {
        NamedWindowProcessorInstance processorInstance = processors[0].getProcessorInstance(agentInstanceId);
        if (processorInstance != null) {
            Collection<EventBean> coll = processorInstance.getTailViewInstance().snapshot(filters[0],
                    statementSpec.getAnnotations());
            contextPartitionResults.add(new ContextPartitionResult(coll,
                    processorInstance.getTailViewInstance().getAgentInstanceContext()));
        }
    }

    // process context partitions
    ArrayDeque<EventBean[]> events = new ArrayDeque<EventBean[]>();
    for (ContextPartitionResult contextPartitionResult : contextPartitionResults) {
        Collection<EventBean> snapshot = contextPartitionResult.getEvents();
        if (statementSpec.getFilterRootNode() != null) {
            snapshot = getFiltered(snapshot, Collections.singletonList(statementSpec.getFilterRootNode()));
        }
        EventBean[] rows = snapshot.toArray(new EventBean[snapshot.size()]);
        resultSetProcessor.setAgentInstanceContext(contextPartitionResult.getContext());
        UniformPair<EventBean[]> results = resultSetProcessor.processViewResult(rows, null, true);
        if (results != null && results.getFirst() != null && results.getFirst().length > 0) {
            events.add(results.getFirst());
        }
    }
    return new EPPreparedQueryResult(resultSetProcessor.getResultEventType(), EventBeanUtility.flatten(events));
}

From source file:com.espertech.esper.core.start.EPPreparedExecuteMethod.java

/**
 * Executes the prepared query.// w w w  .  j  a  v a2 s. co  m
 * @return query results
 */
public EPPreparedQueryResult execute(ContextPartitionSelector[] contextPartitionSelectors) {
    int numStreams = processors.length;

    if (contextPartitionSelectors != null && contextPartitionSelectors.length != numStreams) {
        throw new IllegalArgumentException(
                "Number of context partition selectors does not match the number of named windows in the from-clause");
    }

    // handle non-context case
    if (statementSpec.getOptionalContextName() == null) {

        Collection<EventBean>[] snapshots = new Collection[numStreams];
        for (int i = 0; i < numStreams; i++) {

            ContextPartitionSelector selector = contextPartitionSelectors == null ? null
                    : contextPartitionSelectors[i];
            snapshots[i] = getStreamFilterSnapshot(i, selector);
        }

        resultSetProcessor.clear();
        return process(snapshots);
    }

    List<ContextPartitionResult> contextPartitionResults = new ArrayList<ContextPartitionResult>();

    // context partition runtime query
    Collection<Integer> agentInstanceIds;
    if (contextPartitionSelectors == null
            || contextPartitionSelectors[0] instanceof ContextPartitionSelectorAll) {
        agentInstanceIds = processors[0].getProcessorInstancesAll();
    } else {
        ContextManager contextManager = services.getContextManagementService()
                .getContextManager(statementSpec.getOptionalContextName());
        agentInstanceIds = contextManager.getAgentInstanceIds(contextPartitionSelectors[0]);
    }

    // collect events and agent instances
    for (int agentInstanceId : agentInstanceIds) {
        NamedWindowProcessorInstance processorInstance = processors[0].getProcessorInstance(agentInstanceId);
        if (processorInstance != null) {
            Collection<EventBean> coll = processorInstance.getTailViewInstance().snapshot(filters[0],
                    statementSpec.getAnnotations());
            contextPartitionResults.add(new ContextPartitionResult(coll,
                    processorInstance.getTailViewInstance().getAgentInstanceContext()));
        }
    }

    // process context partitions
    ArrayDeque<EventBean[]> events = new ArrayDeque<EventBean[]>();
    for (ContextPartitionResult contextPartitionResult : contextPartitionResults) {
        Collection<EventBean> snapshot = contextPartitionResult.getEvents();
        if (statementSpec.getFilterRootNode() != null) {
            snapshot = getFiltered(snapshot, Collections.singletonList(statementSpec.getFilterRootNode()));
        }
        EventBean[] rows = snapshot.toArray(new EventBean[snapshot.size()]);
        resultSetProcessor.setAgentInstanceContext(contextPartitionResult.getContext());
        UniformPair<EventBean[]> results = resultSetProcessor.processViewResult(rows, null, true);
        if (results != null && results.getFirst() != null && results.getFirst().length > 0) {
            events.add(results.getFirst());
        }
    }
    return new EPPreparedQueryResult(resultSetProcessor.getResultEventType(), EventBeanUtility.flatten(events));
}

From source file:com.espertech.esper.core.EPRuntimeImpl.java

private void processScheduleHandles(ArrayBackedCollection<ScheduleHandle> handles) {
    if (ThreadLogUtil.ENABLED_TRACE) {
        ThreadLogUtil.trace("Found schedules for", handles.size());
    }/*from   w  w  w . j av a  2s .  c  om*/

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

    // handle 1 result separatly for performance reasons
    if (handles.size() == 1) {
        Object[] handleArray = handles.getArray();
        EPStatementHandleCallback handle = (EPStatementHandleCallback) handleArray[0];

        if ((MetricReportingPath.isMetricsEnabled)
                && (handle.getEpStatementHandle().getMetricsHandle().isEnabled())) {
            long cpuTimeBefore = MetricUtil.getCPUCurrentThread();
            long wallTimeBefore = MetricUtil.getWall();

            processStatementScheduleSingle(handle, services, engineFilterAndDispatchTimeContext);

            long wallTimeAfter = MetricUtil.getWall();
            long cpuTimeAfter = MetricUtil.getCPUCurrentThread();
            long deltaCPU = cpuTimeAfter - cpuTimeBefore;
            long deltaWall = wallTimeAfter - wallTimeBefore;
            services.getMetricsReportingService().accountTime(handle.getEpStatementHandle().getMetricsHandle(),
                    deltaCPU, deltaWall, 1);
        } else {
            if ((ThreadingOption.isThreadingEnabled) && (services.getThreadingService().isTimerThreading())) {
                services.getThreadingService().submitTimerWork(
                        new TimerUnitSingle(services, this, handle, this.engineFilterAndDispatchTimeContext));
            } else {
                processStatementScheduleSingle(handle, services, engineFilterAndDispatchTimeContext);
            }
        }

        handles.clear();
        return;
    }

    Object[] matchArray = handles.getArray();
    int entryCount = handles.size();

    // sort multiple matches for the event into statements
    Map<EPStatementHandle, Object> stmtCallbacks = schedulePerStmtThreadLocal.get();
    stmtCallbacks.clear();
    for (int i = 0; i < entryCount; i++) // need to use the size of the collection
    {
        EPStatementHandleCallback handleCallback = (EPStatementHandleCallback) matchArray[i];
        EPStatementHandle handle = handleCallback.getEpStatementHandle();
        ScheduleHandleCallback callback = handleCallback.getScheduleCallback();

        Object entry = stmtCallbacks.get(handle);

        // This statement has not been encountered before
        if (entry == null) {
            stmtCallbacks.put(handle, callback);
            continue;
        }

        // This statement has been encountered once before
        if (entry instanceof ScheduleHandleCallback) {
            ScheduleHandleCallback existingCallback = (ScheduleHandleCallback) entry;
            ArrayDeque<ScheduleHandleCallback> entries = new ArrayDeque<ScheduleHandleCallback>();
            entries.add(existingCallback);
            entries.add(callback);
            stmtCallbacks.put(handle, entries);
            continue;
        }

        // This statement has been encountered more then once before
        ArrayDeque<ScheduleHandleCallback> entries = (ArrayDeque<ScheduleHandleCallback>) entry;
        entries.add(callback);
    }
    handles.clear();

    for (Map.Entry<EPStatementHandle, Object> entry : stmtCallbacks.entrySet()) {
        EPStatementHandle handle = entry.getKey();
        Object callbackObject = entry.getValue();

        if ((MetricReportingPath.isMetricsEnabled) && (handle.getMetricsHandle().isEnabled())) {
            long cpuTimeBefore = MetricUtil.getCPUCurrentThread();
            long wallTimeBefore = MetricUtil.getWall();

            processStatementScheduleMultiple(handle, callbackObject, services,
                    this.engineFilterAndDispatchTimeContext);

            long wallTimeAfter = MetricUtil.getWall();
            long cpuTimeAfter = MetricUtil.getCPUCurrentThread();
            long deltaCPU = cpuTimeAfter - cpuTimeBefore;
            long deltaWall = wallTimeAfter - wallTimeBefore;
            int numInput = (callbackObject instanceof Collection) ? ((Collection) callbackObject).size() : 1;
            services.getMetricsReportingService().accountTime(handle.getMetricsHandle(), deltaCPU, deltaWall,
                    numInput);
        } else {
            if ((ThreadingOption.isThreadingEnabled) && (services.getThreadingService().isTimerThreading())) {
                services.getThreadingService().submitTimerWork(new TimerUnitMultiple(services, this, handle,
                        callbackObject, this.engineFilterAndDispatchTimeContext));
            } else {
                processStatementScheduleMultiple(handle, callbackObject, services,
                        this.engineFilterAndDispatchTimeContext);
            }
        }

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

From source file:com.espertech.esper.core.EPRuntimeImpl.java

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

    if (ThreadLogUtil.ENABLED_TRACE) {
        ThreadLogUtil.trace("Found matches for underlying ", matches.size(), event.getUnderlying());
    }//  ww w .ja v  a2s  .c  o  m

    if (matches.size() == 0) {
        if (unmatchedListener != null) {
            unmatchedListener.update(event);
        }
        return;
    }

    Map<EPStatementHandle, 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];
        EPStatementHandle handle = handleCallback.getEpStatementHandle();

        // 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;
        }

        if ((MetricReportingPath.isMetricsEnabled) && (handle.getMetricsHandle().isEnabled())) {
            long cpuTimeBefore = MetricUtil.getCPUCurrentThread();
            long wallTimeBefore = MetricUtil.getWall();

            processStatementFilterSingle(handle, handleCallback, event, version);

            long wallTimeAfter = MetricUtil.getWall();
            long cpuTimeAfter = MetricUtil.getCPUCurrentThread();
            long deltaCPU = cpuTimeAfter - cpuTimeBefore;
            long deltaWall = wallTimeAfter - wallTimeBefore;
            services.getMetricsReportingService().accountTime(handle.getMetricsHandle(), deltaCPU, deltaWall,
                    1);
        } else {
            if ((ThreadingOption.isThreadingEnabled) && (services.getThreadingService().isRouteThreading())) {
                services.getThreadingService()
                        .submitRoute(new RouteUnitSingle(this, handleCallback, event, version));
            } else {
                processStatementFilterSingle(handle, handleCallback, event, version);
            }
        }
    }
    matches.clear();
    if (stmtCallbacks.isEmpty()) {
        return;
    }

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

        if ((MetricReportingPath.isMetricsEnabled) && (handle.getMetricsHandle().isEnabled())) {
            long cpuTimeBefore = MetricUtil.getCPUCurrentThread();
            long wallTimeBefore = MetricUtil.getWall();

            processStatementFilterMultiple(handle, callbackList, event, version);

            long wallTimeAfter = MetricUtil.getWall();
            long cpuTimeAfter = MetricUtil.getCPUCurrentThread();
            long deltaCPU = cpuTimeAfter - cpuTimeBefore;
            long deltaWall = wallTimeAfter - wallTimeBefore;
            services.getMetricsReportingService().accountTime(handle.getMetricsHandle(), deltaCPU, deltaWall,
                    callbackList.size());
        } else {
            if ((ThreadingOption.isThreadingEnabled) && (services.getThreadingService().isRouteThreading())) {
                services.getThreadingService()
                        .submitRoute(new RouteUnitMultiple(this, callbackList, event, handle, version));
            } else {
                processStatementFilterMultiple(handle, callbackList, event, version);
            }

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