private void triggerActions(long targetTimeInNanoseconds) { for (;;) { TimedRunnable current = queue.peek(); if (current == null || current.time > targetTimeInNanoseconds) { break; } // if scheduled time is 0 (immediate) use current virtual time time = current.time == 0 ? time : current.time; queue.remove(current); // Only execute if not unsubscribed if (!current.scheduler.disposed) { current.run.run(); } } time = targetTimeInNanoseconds; }
class BlockingQueue { Queue<String> buffer = new LinkedList<String>(); public void give(String data) { buffer.add(data); notify(); // Since someone may be waiting in take! } public String take() throws InterruptedException { while (buffer.isEmpty()) // don't use "if" due to spurious wakeups. wait(); return buffer.remove(); } }
@Override public void dropDatabase(DropDatabaseRequest request, StreamObserver<Empty> responseObserver) { Object response = responses.remove(); if (response instanceof Empty) { requests.add(request); responseObserver.onNext((Empty) response); responseObserver.onCompleted(); } else if (response instanceof Exception) { responseObserver.onError((Exception) response); } else { responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); } }
/** * Given a set of features, add to it all the features directly or indirectly implied by any of * them, and return it. * * @param features the set of features to expand * @return the same set of features, expanded with all implied features */ public static Set<Feature<?>> addImpliedFeatures(Set<Feature<?>> features) { Queue<Feature<?>> queue = new ArrayDeque<>(features); while (!queue.isEmpty()) { Feature<?> feature = queue.remove(); for (Feature<?> implied : feature.getImpliedFeatures()) { if (features.add(implied)) { queue.add(implied); } } } return features; }
static <T> Stream<Node<T>> traverseLevelOrder(Node<T> node) { Stream<Node<T>> result = Stream.empty(); final java.util.Queue<Node<T>> queue = new java.util.LinkedList<>(); queue.add(node); while (!queue.isEmpty()) { final Node<T> next = queue.remove(); result = result.prepend(next); queue.addAll(next.getChildren().toJavaList()); } return result.reverse(); }
public void testHoldsLockOnAllOperations() { create().element(); create().offer("foo"); create().peek(); create().poll(); create().remove(); create().add("foo"); create().addAll(ImmutableList.of("foo")); create().clear(); create().contains("foo"); create().containsAll(ImmutableList.of("foo")); create().equals(new ArrayDeque<>(ImmutableList.of("foo"))); create().hashCode(); create().isEmpty(); create().iterator(); create().remove("foo"); create().removeAll(ImmutableList.of("foo")); create().retainAll(ImmutableList.of("foo")); create().size(); create().toArray(); create().toArray(new String[] {"foo"}); } }
/** Obtain an instance of a Component nested inside the given inspectable Component. */ @Nullable public InspectableComponent getNestedInstance(Component component) { final Queue<DebugComponent> queue = new LinkedList<>(mComponent.getChildComponents()); while (!queue.isEmpty()) { final DebugComponent childComponent = queue.remove(); if (childComponent.getComponent() == component) { return new InspectableComponent(childComponent); } queue.addAll(childComponent.getChildComponents()); } return null; }
@Override public void getDataset(GetDatasetRequest request, StreamObserver<Dataset> responseObserver) { Object response = responses.remove(); if (response instanceof Dataset) { requests.add(request); responseObserver.onNext((Dataset) response); responseObserver.onCompleted(); } else if (response instanceof Exception) { responseObserver.onError((Exception) response); } else { responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); } }
import java.util.LinkedList; import java.util.Queue; class Test { public static void main(String args[]) { char arr[] = {3,1,4,1,5,9,2,6,5,3,5,8,9}; Queue<Integer> fifo = new LinkedList<Integer>(); for (int i = 0; i < arr.length; i++) fifo.add (new Integer (arr[i])); System.out.print (fifo.remove() + "."); while (! fifo.isEmpty()) System.out.print (fifo.remove()); System.out.println(); } }
/** * Given a set of features, return a new set of all features directly or indirectly implied by any * of them. * * @param features the set of features whose implications to find * @return the implied set of features */ public static Set<Feature<?>> impliedFeatures(Set<Feature<?>> features) { Set<Feature<?>> impliedSet = new LinkedHashSet<>(); Queue<Feature<?>> queue = new ArrayDeque<>(features); while (!queue.isEmpty()) { Feature<?> feature = queue.remove(); for (Feature<?> implied : feature.getImpliedFeatures()) { if (!features.contains(implied) && impliedSet.add(implied)) { queue.add(implied); } } } return impliedSet; }
private void triggerActions(long targetTimeInNanoseconds) { for (;;) { TimedRunnable current = queue.peek(); if (current == null || current.time > targetTimeInNanoseconds) { break; } // if scheduled time is 0 (immediate) use current virtual time time = current.time == 0 ? time : current.time; queue.remove(current); // Only execute if not unsubscribed if (!current.scheduler.disposed) { current.run.run(); } } time = targetTimeInNanoseconds; }
@Override public void exportData(ExportDataRequest request, StreamObserver<Operation> responseObserver) { Object response = responses.remove(); if (response instanceof Operation) { requests.add(request); responseObserver.onNext((Operation) response); responseObserver.onCompleted(); } else if (response instanceof Exception) { responseObserver.onError((Exception) response); } else { responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); } }
/** * Returns the set of nodes that are reachable from {@code node}. Node B is defined as reachable * from node A if there exists a path (a sequence of adjacent outgoing edges) starting at node A * and ending at node B. Note that a node is always reachable from itself via a zero-length path. * * <p>This is a "snapshot" based on the current topology of {@code graph}, rather than a live view * of the set of nodes reachable from {@code node}. In other words, the returned {@link Set} will * not be updated after modifications to {@code graph}. * * @throws IllegalArgumentException if {@code node} is not present in {@code graph} */ public static <N> Set<N> reachableNodes(Graph<N> graph, N node) { checkArgument(graph.nodes().contains(node), NODE_NOT_IN_GRAPH, node); Set<N> visitedNodes = new LinkedHashSet<N>(); Queue<N> queuedNodes = new ArrayDeque<N>(); visitedNodes.add(node); queuedNodes.add(node); // Perform a breadth-first traversal rooted at the input node. while (!queuedNodes.isEmpty()) { N currentNode = queuedNodes.remove(); for (N successor : graph.successors(currentNode)) { if (visitedNodes.add(successor)) { queuedNodes.add(successor); } } } return Collections.unmodifiableSet(visitedNodes); }
@Override public void deleteModel(DeleteModelRequest request, StreamObserver<Operation> responseObserver) { Object response = responses.remove(); if (response instanceof Operation) { requests.add(request); responseObserver.onNext((Operation) response); responseObserver.onCompleted(); } else if (response instanceof Exception) { responseObserver.onError((Exception) response); } else { responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); } }