Example usage for java.util ArrayDeque isEmpty

List of usage examples for java.util ArrayDeque isEmpty

Introduction

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

Prototype

public boolean isEmpty() 

Source Link

Document

Returns true if this deque contains no elements.

Usage

From source file:com.redhat.lightblue.util.Error.java

/**
 * Pops the context information from current thread stack
 *//*from  w  w w .  ja v a2  s  .  co  m*/
public static void pop() {
    ArrayDeque<String> c = THREAD_CONTEXT.get();
    if (!c.isEmpty()) {
        String context = c.removeLast();
        LOGGER.debug("pop: {}", context);
    }
    if (c.isEmpty()) {
        reset();
    }
}

From source file:name.abhijitsarkar.algorithms.core.Sorter.java

private static int[] gather(ArrayDeque<int[]> stack) {
    int[] mergedBucket = null;

    while (!stack.isEmpty()) {
        mergedBucket = stack.remove();//from w  w w  .  j  a v a 2  s  . c  om

        if (!stack.isEmpty()) {
            int[] bucket = stack.remove();

            mergedBucket = merge(mergedBucket, bucket);

            stack.add(mergedBucket);
        }
    }

    return mergedBucket;
}

From source file:io.selendroid.server.model.internal.AbstractNativeElementContext.java

/** visible for testing */
protected static List<AndroidElement> searchViews(AbstractNativeElementContext context, List<View> roots,
        Predicate predicate, boolean findJustOne) {
    List<AndroidElement> elements = new ArrayList<AndroidElement>();
    if (roots == null || roots.isEmpty()) {
        return elements;
    }/* w  w w. ja  v  a  2 s  . c  om*/
    ArrayDeque<View> queue = new ArrayDeque<View>();

    for (View root : roots) {
        queue.add(root);
        while (!queue.isEmpty()) {
            View view = queue.pop();
            if (predicate.apply(view)) {
                elements.add(context.newAndroidElement(view));
                if (findJustOne) {
                    break;
                }
            }
            if (view instanceof ViewGroup) {
                ViewGroup group = (ViewGroup) view;
                int childrenCount = group.getChildCount();
                for (int index = 0; index < childrenCount; index++) {
                    queue.add(group.getChildAt(index));
                }
            }
        }
    }
    return elements;
}

From source file:com.espertech.esper.core.context.util.StatementAgentInstanceUtil.java

private static void evaluateEventForStatementInternal(EPServicesContext servicesContext, EventBean theEvent,
        List<AgentInstance> agentInstances) {
    // context was created - reevaluate for the given event
    ArrayDeque<FilterHandle> callbacks = new ArrayDeque<FilterHandle>(2);
    servicesContext.getFilterService().evaluate(theEvent, callbacks); // evaluates for ALL statements
    if (callbacks.isEmpty()) {
        return;/*from  w w  w.j av  a  2s  .  c om*/
    }

    // there is a single callback and a single context, if they match we are done
    if (agentInstances.size() == 1 && callbacks.size() == 1) {
        AgentInstance agentInstance = agentInstances.get(0);
        if (agentInstance.getAgentInstanceContext().getStatementId()
                .equals(callbacks.getFirst().getStatementId())) {
            process(agentInstance, servicesContext, callbacks, theEvent);
        }
        return;
    }

    // use the right sorted/unsorted Map keyed by AgentInstance to sort
    boolean isPrioritized = servicesContext.getConfigSnapshot().getEngineDefaults().getExecution()
            .isPrioritized();
    Map<AgentInstance, Object> stmtCallbacks;
    if (!isPrioritized) {
        stmtCallbacks = new HashMap<AgentInstance, Object>();
    } else {
        stmtCallbacks = new TreeMap<AgentInstance, Object>(AgentInstanceComparator.INSTANCE);
    }

    // process all callbacks
    for (FilterHandle filterHandle : callbacks) {
        // determine if this filter entry applies to any of the affected agent instances
        String statementId = filterHandle.getStatementId();
        AgentInstance agentInstanceFound = null;
        for (AgentInstance agentInstance : agentInstances) {
            if (agentInstance.getAgentInstanceContext().getStatementId().equals(statementId)) {
                agentInstanceFound = agentInstance;
                break;
            }
        }
        if (agentInstanceFound == null) { // when the callback is for some other stmt
            continue;
        }

        EPStatementHandleCallback handleCallback = (EPStatementHandleCallback) filterHandle;
        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 stmtCallback = stmtCallbacks.get(agentInstanceFound);
            if (stmtCallback == null) {
                stmtCallbacks.put(agentInstanceFound, handleCallback);
            } else if (stmtCallback instanceof ArrayDeque) {
                ArrayDeque<EPStatementHandleCallback> q = (ArrayDeque<EPStatementHandleCallback>) stmtCallback;
                q.add(handleCallback);
            } else {
                ArrayDeque<EPStatementHandleCallback> q = new ArrayDeque<EPStatementHandleCallback>(4);
                q.add((EPStatementHandleCallback) stmtCallback);
                q.add(handleCallback);
                stmtCallbacks.put(agentInstanceFound, q);
            }
            continue;
        }

        // no need to be sorted, process
        process(agentInstanceFound, servicesContext, Collections.<FilterHandle>singletonList(handleCallback),
                theEvent);
    }

    if (stmtCallbacks.isEmpty()) {
        return;
    }

    // Process self-join or sorted prioritized callbacks
    for (Map.Entry<AgentInstance, Object> entry : stmtCallbacks.entrySet()) {
        AgentInstance agentInstance = entry.getKey();
        Object callbackList = entry.getValue();
        if (callbackList instanceof ArrayDeque) {
            process(agentInstance, servicesContext, (Collection<FilterHandle>) callbackList, theEvent);
        } else {
            process(agentInstance, servicesContext,
                    Collections.<FilterHandle>singletonList((FilterHandle) callbackList), theEvent);
        }
        if (agentInstance.getAgentInstanceContext().getEpStatementAgentInstanceHandle().isPreemptive()) {
            return;
        }
    }
}

From source file:org.carrot2.source.microsoft.v5.Bing5NewsDocumentSource.java

@Override
protected void handleResponse(BingResponse response, SearchEngineResponse ser) {
    NewsResponse newsResponse = (NewsResponse) response;
    ser.metadata.put(SearchEngineResponse.RESULTS_TOTAL_KEY, newsResponse.totalEstimatedMatches);

    if (newsResponse.value != null) {
        ArrayDeque<NewsResponse.NewsArticle> articles = new ArrayDeque<>(newsResponse.value);
        while (!articles.isEmpty()) {
            NewsResponse.NewsArticle r = articles.removeFirst();
            if (r.clusteredArticles != null) {
                articles.addAll(r.clusteredArticles);
            }/*from www .  ja v  a  2 s.c o  m*/

            Document doc = new Document(r.name, r.description, r.url);
            if (r.image != null && r.image.thumbnail != null) {
                doc.setField(Document.THUMBNAIL_URL, r.image.thumbnail.contentUrl);
            }
            if (r.provider != null) {
                ArrayList<String> sources = new ArrayList<>();
                for (NewsResponse.NewsArticle.Organization o : r.provider) {
                    sources.add(o.name);
                }
                doc.setField(Document.SOURCES, sources);
            }

            ser.results.add(doc);
        }
    }
}

From source file:com.espertech.esper.epl.property.PropertyEvaluatorNested.java

public EventBean[] getProperty(EventBean theEvent, ExprEvaluatorContext exprEvaluatorContext) {
    ArrayDeque<EventBean> resultEvents = new ArrayDeque<EventBean>();
    eventsPerStream[0] = theEvent;//from   www  .j av a  2 s  .  c o m
    populateEvents(theEvent, 0, resultEvents, exprEvaluatorContext);
    if (resultEvents.isEmpty()) {
        return null;
    }
    return resultEvents.toArray(new EventBean[resultEvents.size()]);
}

From source file:com.espertech.esper.epl.property.PropertyEvaluatorAccumulative.java

/**
 * Returns the accumulative events for the input event.
 * @param theEvent is the input event//  w w  w .j a v a 2  s.  c o m
 * @param exprEvaluatorContext expression evaluation context
 * @return events per stream for each row
 */
public ArrayDeque<EventBean[]> getAccumulative(EventBean theEvent, ExprEvaluatorContext exprEvaluatorContext) {
    ArrayDeque<EventBean[]> resultEvents = new ArrayDeque<EventBean[]>();
    eventsPerStream[0] = theEvent;
    populateEvents(theEvent, 0, resultEvents, exprEvaluatorContext);
    if (resultEvents.isEmpty()) {
        return null;
    }
    return resultEvents;
}

From source file:com.facebook.litho.dataflow.DataFlowGraph.java

private void regenerateSortedNodes() {
    mSortedNodes.clear();//from   w w w .  jav  a 2s  .c  om

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

    final ArraySet<ValueNode> leafNodes = ComponentsPools.acquireArraySet();
    final SimpleArrayMap<ValueNode, Integer> nodesToOutputsLeft = new SimpleArrayMap<>();

    for (int i = 0, bindingsSize = mBindingToNodes.size(); i < bindingsSize; i++) {
        final ArraySet<ValueNode> nodes = mBindingToNodes.valueAt(i);
        for (int j = 0, nodesSize = nodes.size(); j < nodesSize; j++) {
            final ValueNode node = nodes.valueAt(j);
            final int outputCount = node.getOutputCount();
            if (outputCount == 0) {
                leafNodes.add(node);
            } else {
                nodesToOutputsLeft.put(node, outputCount);
            }
        }
    }

    if (!nodesToOutputsLeft.isEmpty() && leafNodes.isEmpty()) {
        throw new DetectedCycleException("Graph has nodes, but they represent a cycle with no leaf nodes!");
    }

    final ArrayDeque<ValueNode> nodesToProcess = ComponentsPools.acquireArrayDeque();
    nodesToProcess.addAll(leafNodes);

    while (!nodesToProcess.isEmpty()) {
        final ValueNode next = nodesToProcess.pollFirst();
        mSortedNodes.add(next);
        for (int i = 0, count = next.getInputCount(); i < count; i++) {
            final ValueNode input = next.getInputAt(i);
            final int outputsLeft = nodesToOutputsLeft.get(input) - 1;
            nodesToOutputsLeft.put(input, outputsLeft);
            if (outputsLeft == 0) {
                nodesToProcess.addLast(input);
            } else if (outputsLeft < 0) {
                throw new DetectedCycleException("Detected cycle.");
            }
        }
    }

    int expectedTotalNodes = nodesToOutputsLeft.size() + leafNodes.size();
    if (mSortedNodes.size() != expectedTotalNodes) {
        throw new DetectedCycleException(
                "Had unreachable nodes in graph -- this likely means there was a cycle");
    }

    Collections.reverse(mSortedNodes);
    mIsDirty = false;

    ComponentsPools.release(nodesToProcess);
    ComponentsPools.release(leafNodes);
}

From source file:androidx.navigation.NavDeepLinkBuilder.java

private void fillInIntent() {
    NavDestination node = null;/*from ww w.  jav  a2  s  .  com*/
    ArrayDeque<NavDestination> possibleDestinations = new ArrayDeque<>();
    possibleDestinations.add(mGraph);
    while (!possibleDestinations.isEmpty() && node == null) {
        NavDestination destination = possibleDestinations.poll();
        if (destination.getId() == mDestId) {
            node = destination;
        } else if (destination instanceof NavGraph) {
            for (NavDestination child : (NavGraph) destination) {
                possibleDestinations.add(child);
            }
        }
    }
    if (node == null) {
        final String dest = NavDestination.getDisplayName(mContext, mDestId);
        throw new IllegalArgumentException(
                "navigation destination " + dest + " is unknown to this NavController");
    }
    mIntent.putExtra(NavController.KEY_DEEP_LINK_IDS, node.buildDeepLinkIds());
}

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

/**
 * Dispatches when the statement is stopped any remaining results.
 *///from   w w  w. ja  va  2  s. c  om
public void dispatchOnStop() {
    lastIterableEvent = null;
    ArrayDeque<UniformPair<EventBean[]>> dispatches = lastResults.get();
    if (dispatches.isEmpty()) {
        return;
    }
    execute();
}