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.service.EPRuntimeImpl.java

private void processScheduleHandles(ArrayBackedCollection<ScheduleHandle> handles) {
    if (ThreadLogUtil.ENABLED_TRACE) {
        ThreadLogUtil.trace("Found schedules for", handles.size());
    }/*from   w w w .  jav a2  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];

        if ((MetricReportingPath.isMetricsEnabled)
                && (handle.getAgentInstanceHandle().getStatementHandle().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.getAgentInstanceHandle().getStatementHandle().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<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();

        if ((MetricReportingPath.isMetricsEnabled)
                && (handle.getStatementHandle().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.getStatementHandle().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.service.EPRuntimeImpl.java

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

    if (ThreadLogUtil.ENABLED_TRACE) {
        ThreadLogUtil.trace("Found matches for underlying ", matches.size(), theEvent.getUnderlying());
    }/*from ww  w.  ja v  a 2  s  .c  o m*/

    if (matches.size() == 0) {
        if (unmatchedListener != null) {
            services.getEventProcessingRWLock().releaseReadLock(); // Allow listener to create new statements
            try {
                unmatchedListener.update(theEvent);
            } catch (Throwable t) {
                log.error("Exception thrown by unmatched listener: " + t.getMessage(), t);
            } finally {
                // acquire read lock for release by caller
                services.getEventProcessingRWLock().acquireReadLock();
            }
        }
        return;
    }

    Map<EPStatementAgentInstanceHandle, Object> 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) {
            Object callbacks = stmtCallbacks.get(handle);
            if (callbacks == null) {
                stmtCallbacks.put(handle, handleCallback.getFilterCallback());
            } else if (callbacks instanceof ArrayDeque) {
                ArrayDeque<FilterHandleCallback> q = (ArrayDeque<FilterHandleCallback>) callbacks;
                q.add(handleCallback.getFilterCallback());
            } else {
                ArrayDeque<FilterHandleCallback> q = new ArrayDeque<FilterHandleCallback>(4);
                q.add((FilterHandleCallback) callbacks);
                q.add(handleCallback.getFilterCallback());
                stmtCallbacks.put(handle, q);
            }
            continue;
        }

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

            processStatementFilterSingle(handle, handleCallback, theEvent, version);

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

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

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

            processStatementFilterMultiple(handle, callbackList, theEvent, version);

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

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

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

/**
 * Processing multiple filter matches for a statement.
 * @param handle statement handle// w  w  w  . j  a  va 2s .  c o  m
 * @param callbackList object containing callbacks
 * @param theEvent to process
 * @param version filter version
 */
public void processStatementFilterMultiple(EPStatementAgentInstanceHandle handle, Object callbackList,
        EventBean theEvent, long version) {
    handle.getStatementAgentInstanceLock().acquireWriteLock(services.getStatementLockFactory());
    try {
        if (handle.isHasVariables()) {
            services.getVariableService().setLocalVersion();
        }
        if (!handle.isCurrentFilter(version)) {
            if (handle.getFilterFaultHandler() != null) {
                handle.getFilterFaultHandler().handleFilterFault(theEvent, version);
            }

            ArrayDeque<FilterHandle> callbackListNew = getCallbackList(theEvent, handle.getStatementId());
            if (callbackListNew.isEmpty()) {
                callbackList = Collections.emptyList();
            } else if (callbackListNew.size() == 1) {
                callbackList = ((EPStatementHandleCallback) callbackListNew.getFirst()).getFilterCallback();
            } else {
                ArrayDeque<FilterHandleCallback> q = new ArrayDeque<FilterHandleCallback>(
                        callbackListNew.size());
                callbackList = q;
                for (FilterHandle callback : callbackListNew) {
                    EPStatementHandleCallback handleCallbackFilter = (EPStatementHandleCallback) callback;
                    q.add(handleCallbackFilter.getFilterCallback());
                }
            }
        }

        if (callbackList instanceof Collection) {
            Collection<FilterHandleCallback> callbackColl = (Collection<FilterHandleCallback>) callbackList;
            if (isSubselectPreeval) {
                // sub-selects always go first
                for (FilterHandleCallback callback : callbackColl) {
                    if (callback.isSubSelect()) {
                        callback.matchFound(theEvent, callbackColl);
                    }
                }

                for (FilterHandleCallback callback : callbackColl) {
                    if (!callback.isSubSelect()) {
                        callback.matchFound(theEvent, callbackColl);
                    }
                }
            } else {
                // sub-selects always go last
                for (FilterHandleCallback callback : callbackColl) {
                    if (!callback.isSubSelect()) {
                        callback.matchFound(theEvent, callbackColl);
                    }
                }

                for (FilterHandleCallback callback : callbackColl) {
                    if (callback.isSubSelect()) {
                        callback.matchFound(theEvent, callbackColl);
                    }
                }
            }
        } else {
            FilterHandleCallback single = (FilterHandleCallback) callbackList;
            single.matchFound(theEvent, null);
        }

        // internal join processing, if applicable
        handle.internalDispatch(this.engineFilterAndDispatchTimeContext);
    } catch (RuntimeException ex) {
        services.getExceptionHandlingService().handleException(ex, handle);
    } finally {
        handle.getStatementAgentInstanceLock().releaseWriteLock(services.getStatementLockFactory());
    }
}

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

/**
 * Processing multiple filter matches for a statement.
 * @param handle statement handle//from  ww  w  . j av  a 2  s  .c  o m
 * @param callbackList object containing callbacks
 * @param event to process
 * @param version filter version
 */
public void processStatementFilterMultiple(EPStatementHandle handle,
        ArrayDeque<FilterHandleCallback> callbackList, EventBean event, long version) {
    handle.getStatementLock().acquireWriteLock(services.getStatementLockFactory());
    try {
        if (handle.isHasVariables()) {
            services.getVariableService().setLocalVersion();
        }
        if (!handle.isCurrentFilter(version)) {
            callbackList.clear();
            ArrayDeque<FilterHandle> callbackListNew = getCallbackList(event, handle.getStatementId());
            for (FilterHandle callback : callbackListNew) {
                EPStatementHandleCallback handleCallbackFilter = (EPStatementHandleCallback) callback;
                callbackList.add(handleCallbackFilter.getFilterCallback());
            }
        }

        if (isSubselectPreeval) {
            // sub-selects always go first
            for (FilterHandleCallback callback : callbackList) {
                if (callback.isSubSelect()) {
                    callback.matchFound(event, callbackList);
                }
            }

            for (FilterHandleCallback callback : callbackList) {
                if (!callback.isSubSelect()) {
                    callback.matchFound(event, callbackList);
                }
            }
        } else {
            // sub-selects always go last
            for (FilterHandleCallback callback : callbackList) {
                if (!callback.isSubSelect()) {
                    callback.matchFound(event, callbackList);
                }
            }

            for (FilterHandleCallback callback : callbackList) {
                if (callback.isSubSelect()) {
                    callback.matchFound(event, callbackList);
                }
            }
        }

        // internal join processing, if applicable
        handle.internalDispatch(this.engineFilterAndDispatchTimeContext);
    } catch (RuntimeException ex) {
        services.getExceptionHandlingService().handleException(ex, handle);
    } finally {
        handle.getStatementLock().releaseWriteLock(services.getStatementLockFactory());
    }
}

From source file:com.sonymobile.android.media.internal.VUParser.java

private boolean parseMtdt(BoxHeader header) {
    boolean parseOK = true;
    // if mCurrentBoxSequence contains trak, then add metadata to current
    // track/*from  w w w  .j av a  2  s . c o  m*/
    // else metadata is for file
    // we're currently not interested in anything on track level
    try {
        int numberOfUnits = mDataSource.readShort();
        mHmmpTitles = new ArrayList<String>(1);
        ArrayDeque<String> titleLanguages = new ArrayDeque<String>(1);
        ArrayDeque<String> iconLanguages = new ArrayDeque<String>(1);
        for (int i = 0; i < numberOfUnits; i++) {
            short dataUnitSize = mDataSource.readShort();
            int dataTypeID = mDataSource.readInt();
            short language = mDataSource.readShort();
            char l1 = Character.toChars(((language >> 10) & 0x1F) + 96)[0];
            char l2 = Character.toChars(((language >> 5) & 0x1F) + 96)[0];
            char l3 = Character.toChars(((language) & 0x1F) + 96)[0];
            String languageString = "" + l1 + l2 + l3;
            short encodingType = mDataSource.readShort();
            if (encodingType == 0x01) {
                byte[] metadata = new byte[dataUnitSize - 10];
                mDataSource.read(metadata);
                String metadataString = new String(metadata, "UTF-16BE").trim();
                if ((dataTypeID & 0xFFFF) == 0x01) {
                    mHmmpTitles.add(metadataString);
                    titleLanguages.add(languageString);
                }
            } else if (encodingType == 0x101) {
                if (dataTypeID == 0xA04) {
                    if (mIconList == null) {
                        mIconList = new ArrayList<IconInfo>();
                    }
                    mDataSource.skipBytes(4); // selectionFlags
                    mDataSource.skipBytes(4); // reserved
                    int artworkCount = mDataSource.readInt();
                    for (int j = 0; j < artworkCount; j++) {
                        IconInfo iconInfo = new IconInfo();
                        iconInfo.mtsmId = mDataSource.readInt();
                        iconInfo.mdstIndex = mDataSource.readInt();
                        iconInfo.languageIndex = iconLanguages.size();
                        mDataSource.skipBytes(4);
                        mIconList.add(iconInfo);
                    }
                    iconLanguages.add(languageString);
                }
            }
        }
        addMetaDataValue(KEY_HMMP_TITLE_LANGUAGES, titleLanguages.toArray());
        addMetaDataValue(KEY_HMMP_ICON_LANGUAGES, iconLanguages.toArray());
    } catch (EOFException e) {
        if (LOGS_ENABLED)
            Log.e(TAG, "Exception while reading from 'MTDT' box", e);
        return false;
    } catch (IOException e) {
        if (LOGS_ENABLED)
            Log.e(TAG, "Exception while reading from 'MTDT' box", e);
        return false;
    }
    return parseOK;
}