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.espertech.esper.core.EPRuntimeIsolatedImpl.java

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

    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];

        EPRuntimeImpl.processStatementScheduleSingle(handle, unisolatedServices, isolatedTimeEvalContext);

        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();

        EPRuntimeImpl.processStatementScheduleMultiple(handle, callbackObject, unisolatedServices,
                isolatedTimeEvalContext);

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

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

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

    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];

        EPRuntimeImpl.processStatementScheduleSingle(handle, unisolatedServices, isolatedTimeEvalContext);

        handles.clear();
        return;
    }

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

    // sort multiple matches for the event into statements
    Map<EPStatementAgentInstanceHandle, 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];
        EPStatementAgentInstanceHandle handle = handleCallback.getAgentInstanceHandle();
        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<EPStatementAgentInstanceHandle, Object> entry : stmtCallbacks.entrySet()) {
        EPStatementAgentInstanceHandle handle = entry.getKey();
        Object callbackObject = entry.getValue();

        EPRuntimeImpl.processStatementScheduleMultiple(handle, callbackObject, unisolatedServices,
                isolatedTimeEvalContext);

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

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

public void testAdd() throws Exception {
    Object o1 = new Object();
    Object o2 = new Object();
    Object o3 = new Object();

    ArrayDeque<Object> deque = new ArrayDeque<>();
    assertTrue(deque.add(o1));
    checkDequeSizeAndContent(deque, o1);
    assertTrue(deque.add(o2));/*  w ww . j  a va 2s .c  o m*/
    checkDequeSizeAndContent(deque, o1, o2);
    assertTrue(deque.add(o3));
    checkDequeSizeAndContent(deque, o1, o2, o3);

    try {
        deque.add(null);
        fail();
    } catch (NullPointerException expected) {
    }
}

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

public void testRolloverInvariants() {
    ArrayDeque<Integer> deque = new ArrayDeque<>();

    assertTrue(deque.add(1));
    assertEquals(1, (int) deque.removeFirst());
    for (int i = 0; i < 100; i++) {
        assertTrue(deque.add(i));//  w  w w  .jav  a  2 s  .  c o  m
    }
    assertNotNull(deque.peek());
    assertFalse(deque.isEmpty());

    Iterator<Integer> it = deque.iterator();
    for (int i = 0; i < 100; i++) {
        assertTrue(it.hasNext());
        assertEquals(i, (int) it.next());
        it.remove();
    }
    assertFalse(it.hasNext());
    assertNull(deque.peek());
    assertTrue(deque.isEmpty());
}

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

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

    ArrayDeque<Object> deque = new ArrayDeque<>();
    assertNull(deque.poll());/*from  ww w  .  ja v  a2  s.c o m*/

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

    deque.add(o1);
    deque.add(o2);
    assertEquals(o1, deque.poll());
    checkDequeSizeAndContent(deque, o2);
    assertEquals(o2, deque.poll());
    assertTrue(deque.isEmpty());
    assertNull(deque.poll());
}

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

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

    ArrayDeque<Object> deque = new ArrayDeque<>();
    assertNull(deque.peek());/*from ww w.  j av  a2  s. com*/

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

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

    deque.addFirst(o3);
    assertEquals(o3, deque.peek());
    checkDequeSizeAndContent(deque, o3, o1, o2);

    deque.clear();
    assertTrue(deque.isEmpty());
    assertNull(deque.peek());
}

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  .ja  va2  s  . c  om

    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 testPeekLast() {
    Object o1 = new Object();
    Object o2 = new Object();
    Object o3 = new Object();

    ArrayDeque<Object> deque = new ArrayDeque<>();
    assertNull(deque.peekLast());/* w w w  .j  av a  2 s .  co m*/

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

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

    deque.addFirst(o3);
    assertEquals(o2, deque.peekLast());
    checkDequeSizeAndContent(deque, o3, o1, o2);

    deque.clear();
    assertTrue(deque.isEmpty());
    assertNull(deque.peekLast());
}

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

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

    ArrayDeque<Object> deque = new ArrayDeque<>();
    assertNull(deque.peekFirst());// www.  ja  va2  s. com

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

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

    deque.addFirst(o3);
    assertEquals(o3, deque.peekFirst());
    checkDequeSizeAndContent(deque, o3, o1, o2);

    deque.clear();
    assertTrue(deque.isEmpty());
    assertNull(deque.peekFirst());
}

From source file:stroom.util.StreamGrepTool.java

private void processFile(final StreamStore streamStore, final long streamId, final String match) {
    try {/*from  w  w  w.  j av  a2s . c o m*/
        final StreamSource streamSource = streamStore.openStreamSource(streamId);
        if (streamSource != null) {
            final InputStream inputStream = streamSource.getInputStream();

            // Build up 2 buffers so we can output the content either side
            // of
            // the matching line
            final ArrayDeque<String> preBuffer = new ArrayDeque<>();
            final ArrayDeque<String> postBuffer = new ArrayDeque<>();

            final LineNumberReader lineNumberReader = new LineNumberReader(
                    new InputStreamReader(inputStream, StreamUtil.DEFAULT_CHARSET));

            String aline = null;
            while ((aline = lineNumberReader.readLine()) != null) {
                String lines[] = new String[] { aline };
                if (addLineBreak != null) {
                    lines = aline.split(addLineBreak);

                }

                for (final String line : lines) {
                    if (match == null) {
                        System.out.println(lineNumberReader.getLineNumber() + ":" + line);
                    } else {
                        postBuffer.add(lineNumberReader.getLineNumber() + ":" + line);

                        if (postBuffer.size() > 5) {
                            final String searchLine = postBuffer.pop();

                            checkMatch(match, preBuffer, postBuffer, searchLine);

                            preBuffer.add(searchLine);

                            if (preBuffer.size() > 5) {
                                preBuffer.pop();
                            }
                        }
                    }
                }

            }

            // Look at the end
            while (postBuffer.size() > 0) {
                final String searchLine = postBuffer.pop();

                checkMatch(match, preBuffer, postBuffer, searchLine);

                preBuffer.add(searchLine);

                if (preBuffer.size() > 5) {
                    preBuffer.pop();
                }
            }

            inputStream.close();
            streamStore.closeStreamSource(streamSource);
        }
    } catch (final Exception ex) {
        ex.printStackTrace();
    }

}