Java Code Examples for com.google.common.collect.Queues#newArrayDeque()

The following examples show how to use com.google.common.collect.Queues#newArrayDeque() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaskManagerImpl.java    From ganttproject with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void breadthFirstSearch(Task root, Predicate<Pair<Task, Task>> predicate) {
  Preconditions.checkNotNull(root);
  Queue<Task> queue = Queues.newArrayDeque();
  if (predicate.apply(Pair.create((Task) null, root))) {
    queue.add(root);
  }
  while (!queue.isEmpty()) {
    Task head = queue.poll();
    for (Task child : head.getNestedTasks()) {
      if (predicate.apply(Pair.create(head, child))) {
        queue.add(child);
      }
    }
  }
}
 
Example 2
Source File: JsonSerializationTest.java    From SpinalTap with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerializeStateHistory() throws Exception {
  SourceState firstState = new SourceState(15l, 20l, -1l, BINLOG_FILE_POS);
  SourceState secondState = new SourceState(16l, 21l, -1l, BINLOG_FILE_POS);
  SourceState thirdState = new SourceState(17l, 22l, -1l, BINLOG_FILE_POS);

  Deque<SourceState> stateHistory = Queues.newArrayDeque();
  stateHistory.addLast(firstState);
  stateHistory.addLast(secondState);
  stateHistory.addLast(thirdState);

  Collection<SourceState> states =
      JsonUtil.OBJECT_MAPPER.readValue(
          JsonUtil.OBJECT_MAPPER.writeValueAsString(stateHistory),
          new TypeReference<Collection<SourceState>>() {});

  stateHistory = Queues.newArrayDeque(states);

  assertEquals(3, states.size());
  assertEquals(thirdState, stateHistory.removeLast());
  assertEquals(secondState, stateHistory.removeLast());
  assertEquals(firstState, stateHistory.removeLast());
}
 
Example 3
Source File: BatchedPermitsRequesterTest.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetriableFail() throws Exception {
  Queue<RequestAndCallback> queue = Queues.newArrayDeque();

  BatchedPermitsRequester container = BatchedPermitsRequester.builder().resourceId("resource")
      .requestorIdentifier("requestor").requestSender(new TestRequestSender(queue, false))
      .maxTimeoutMillis(1000).build();
  try (ParallelRequester requester = new ParallelRequester(container)) {

    Future<Boolean> future = requester.request(10);

    for (int i = 0; i < BatchedPermitsRequester.MAX_RETRIES; i++) {
      // container will fail 5 times
      await(new QueueSize(queue, 1), 1000);
      Assert.assertFalse(future.isDone());
      failRequestBuilder().requestAndCallback(queue.poll()).fail();
    }

    // should return a failure
    Assert.assertFalse(future.get());
    // should not make any more request
    Assert.assertEquals(queue.size(), 0);
  }
}
 
Example 4
Source File: ProtoAutoSyncProvider.java    From intellij with Apache License 2.0 6 votes vote down vote up
/**
 * Finds all 'plain' targets in a target+aspect's rdeps, stopping at the first plain target it
 * finds along each path.
 */
private static List<Label> getPlainTargets(Project project, TargetKey target) {
  if (target.isPlainTarget()) {
    return ImmutableList.of(target.getLabel());
  }
  List<Label> output = new ArrayList<>();
  Queue<TargetKey> todo = Queues.newArrayDeque();
  ImmutableMultimap<TargetKey, TargetKey> reverseDependencyMap =
      ReverseDependencyMap.get(project);
  todo.addAll(reverseDependencyMap.get(target));
  Set<TargetKey> seen = Sets.newHashSet();
  while (!todo.isEmpty()) {
    TargetKey targetKey = todo.remove();
    if (!seen.add(targetKey)) {
      continue;
    }
    if (targetKey.isPlainTarget()) {
      output.add(targetKey.getLabel());
    } else {
      todo.addAll(reverseDependencyMap.get(targetKey));
    }
  }
  return output;
}
 
Example 5
Source File: DefaultDocumentTreeService.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public void clear() {
  Queue<DocumentPath> toClearQueue = Queues.newArrayDeque();
  Map<String, Versioned<byte[]>> topLevelChildren = docTree.getChildren(DocumentPath.ROOT);
  toClearQueue.addAll(topLevelChildren.keySet()
      .stream()
      .map(name -> new DocumentPath(name, DocumentPath.ROOT))
      .collect(Collectors.toList()));
  while (!toClearQueue.isEmpty()) {
    DocumentPath path = toClearQueue.remove();
    Map<String, Versioned<byte[]>> children = docTree.getChildren(path);
    if (children.size() == 0) {
      docTree.remove(path);
    } else {
      children.keySet().forEach(name -> toClearQueue.add(new DocumentPath(name, path)));
      toClearQueue.add(path);
    }
  }
}
 
Example 6
Source File: TreeUtil.java    From ganttproject with GNU General Public License v3.0 5 votes vote down vote up
public static void breadthFirstSearch(MutableTreeTableNode root, Predicate<Pair<MutableTreeTableNode, MutableTreeTableNode>> predicate) {
  final Queue<MutableTreeTableNode> queue = Queues.newArrayDeque();
  if (predicate.apply(Pair.create((MutableTreeTableNode) null, root))) {
    queue.add(root);
  }
  while (!queue.isEmpty()) {
    MutableTreeTableNode head = queue.poll();
    for (int i = 0; i < head.getChildCount(); i++) {
      MutableTreeTableNode child = (MutableTreeTableNode) head.getChildAt(i);
      if (predicate.apply(Pair.create(head, child))) {
        queue.add(child);
      }
    }
  }
}
 
Example 7
Source File: ConfigurationAnnotations.java    From dagger2-sample with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the full set of modules transitively {@linkplain Module#includes included} from the
 * given seed modules.  If a module is malformed and a type listed in {@link Module#includes}
 * is not annotated with {@link Module}, it is ignored.
 */
static ImmutableSet<TypeElement> getTransitiveModules(
    Types types, Elements elements, Iterable<TypeElement> seedModules) {
  TypeMirror objectType = elements.getTypeElement(Object.class.getCanonicalName()).asType();
  Queue<TypeElement> moduleQueue = Queues.newArrayDeque(seedModules);
  Set<TypeElement> moduleElements = Sets.newLinkedHashSet();
  for (TypeElement moduleElement = moduleQueue.poll();
      moduleElement != null;
      moduleElement = moduleQueue.poll()) {
    Optional<AnnotationMirror> moduleMirror = getAnnotationMirror(moduleElement, Module.class)
        .or(getAnnotationMirror(moduleElement, ProducerModule.class));
    if (moduleMirror.isPresent()) {
      ImmutableSet.Builder<TypeElement> moduleDependenciesBuilder = ImmutableSet.builder();
      moduleDependenciesBuilder.addAll(
          MoreTypes.asTypeElements(getModuleIncludes(moduleMirror.get())));
      // (note: we don't recurse on the parent class because we don't want the parent class as a
      // root that the component depends on, and also because we want the dependencies rooted
      // against this element, not the parent.)
      addIncludesFromSuperclasses(types, moduleElement, moduleDependenciesBuilder, objectType);
      ImmutableSet<TypeElement> moduleDependencies = moduleDependenciesBuilder.build();
      moduleElements.add(moduleElement);
      for (TypeElement dependencyType : moduleDependencies) {
        if (!moduleElements.contains(dependencyType)) {
          moduleQueue.add(dependencyType);
        }
      }
    }
  }
  return ImmutableSet.copyOf(moduleElements);
}
 
Example 8
Source File: RangeStreamScanner.java    From datawave with Apache License 2.0 5 votes vote down vote up
/**
 * @param tableName
 * @param auths
 * @param delegator
 * @param maxResults
 */
public RangeStreamScanner(String tableName, Set<Authorizations> auths, ResourceQueue delegator, int maxResults, Query settings, SessionOptions options,
                Collection<Range> ranges) {
    super(tableName, auths, delegator, maxResults, settings, options, ranges);
    delegatedResourceInitializer = BatchResource.class;
    currentQueue = Queues.newArrayDeque();
    readLock = queueLock.readLock();
    writeLock = queueLock.writeLock();
    myExecutor = MoreExecutors.sameThreadExecutor();
    if (null != stats)
        initializeTimers();
}
 
Example 9
Source File: BatchedPermitsRequesterTest.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@Test
public void testWaitToUsePermits() throws Exception {
  Queue<RequestAndCallback> queue = Queues.newArrayDeque();

  BatchedPermitsRequester container = BatchedPermitsRequester.builder().resourceId("resource")
      .requestorIdentifier("requestor").requestSender(new TestRequestSender(queue, false)).build();

  Sleeper.MockSleeper mockWaiter = new Sleeper.MockSleeper();
  BatchedPermitsRequester.AllocationCallback callback = container.createAllocationCallback(mockWaiter);

  PermitAllocation allocation = new PermitAllocation();
  allocation.setPermits(10);
  allocation.setWaitForPermitUseMillis(20);
  allocation.setExpiration(Long.MAX_VALUE);

  Response<PermitAllocation> response = Mockito.mock(Response.class);
  Mockito.when(response.getEntity()).thenReturn(allocation);

  // Normally the semaphore is reserved during a request. Since we're mocking a response without ever starting a request,
  // manually reserve the semaphore
  Assert.assertTrue(container.reserveSemaphore());

  callback.onSuccess(response);
  Assert.assertEquals((long) mockWaiter.getRequestedSleeps().peek(), 20);
  Assert.assertEquals(container.getPermitBatchContainer().getTotalAvailablePermits(), 10);

  // A zero wait will not trigger a wait in the requester
  allocation.setWaitForPermitUseMillis(0);
  mockWaiter.reset();
  callback.onSuccess(response);
  Assert.assertTrue(mockWaiter.getRequestedSleeps().isEmpty());
  Assert.assertEquals(container.getPermitBatchContainer().getTotalAvailablePermits(), 20);
}
 
Example 10
Source File: BuildGraph.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
private Set<BuildGraphNode> expandAliasTargets(Set<BuildGraphNode> initialTargets) {
  Set<BuildGraphNode> results = Sets.newHashSet();
  ArrayDeque<BuildGraphNode> q = Queues.newArrayDeque(initialTargets);
  while (!q.isEmpty()) {
    BuildGraphNode curr = q.pop();
    if (curr.isAliasTarget()) {
      q.addAll(curr.getDependencies());
    }
    else {
      results.add(curr);
    }
  }
  return results;
}
 
Example 11
Source File: ChunkLogger.java    From ForgeHax with MIT License 5 votes vote down vote up
@Override
protected void onEnabled() {
  chunkLock.lock();
  try {
    if (max_chunks.get() <= 0) {
      chunks = Queues.newArrayDeque();
    } else {
      chunks = EvictingQueue.create(max_chunks.get());
    }
  } finally {
    chunkLock.unlock();
  }
}
 
Example 12
Source File: CassandraResourceTreeWalker.java    From newts with Apache License 2.0 5 votes vote down vote up
/**
 * Visits all nodes in the resource tree bellow the given resource using
 * depth-first search.
 */
public void depthFirstSearch(Context context, SearchResultVisitor visitor, Resource root) {
    ArrayDeque<SearchResults.Result> stack = Queues.newArrayDeque();

    // Build an instance of a SearchResult for the root resource
    // but don't invoke the visitor with it
    boolean skipFirstVisit = true;
    SearchResults initialResults = new SearchResults();
    initialResults.addResult(root, new ArrayList<String>(0));
    stack.add(initialResults.iterator().next());

    while (!stack.isEmpty()) {
        SearchResults.Result r = stack.pop();
        if (skipFirstVisit) {
            skipFirstVisit = false;
        } else {
            if (!visitor.visit(r)) {
                return;
            }
        }

        // Reverse the order of the results so we walk the left-most
        // branches first
        ImmutableList<SearchResults.Result> results = ImmutableList.copyOf(m_searcher.search(
                context, matchKeyAndValue(Constants.PARENT_TERM_FIELD, r.getResource().getId())));
        for (SearchResults.Result result : results.reverse()) {
            stack.push(result);
        }
    }
}
 
Example 13
Source File: Dispatcher.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
protected Queue<Event> initialValue() {
  return Queues.newArrayDeque();
}
 
Example 14
Source File: SerializeTest.java    From ig-json-parser with MIT License 4 votes vote down vote up
@Test
public void serializeOrderTest() throws IOException {
  final int intValue = 25;
  final int integerValue = 37;
  final float floatValue = 1f;
  final float floatObjectValue = 5f;
  final String stringValue = "hello world\r\n\'\"";
  final List<Integer> integerList = Lists.newArrayList(1, 2, 3, 4);
  final ArrayList<Integer> integerArrayList = Lists.newArrayList(1, 2, 3, 4);
  final Queue<Integer> integerQueue = Queues.newArrayDeque(Arrays.asList(1, 2, 3, 4));
  final Set<Integer> integerSet = Sets.newHashSet(1, 2, 3, 4);
  final int subIntValue = 30;
  final SimpleParseUUT.SubenumUUT subEnum = SimpleParseUUT.SubenumUUT.A;
  final List<SimpleParseUUT.SubenumUUT> subEnumList =
      Lists.newArrayList(SimpleParseUUT.SubenumUUT.A, SimpleParseUUT.SubenumUUT.B);

  SimpleParseUUT source = new SimpleParseUUT();
  source.intField = intValue;
  source.integerField = integerValue;
  source.floatField = floatValue;
  source.FloatField = floatObjectValue;
  source.stringField = stringValue;
  source.integerListField = integerList;
  source.integerArrayListField = integerArrayList;
  source.integerQueueField = integerQueue;
  source.integerSetField = integerSet;
  source.subobjectField = new SimpleParseUUT.SubobjectParseUUT();
  source.subobjectField.intField = subIntValue;
  source.subenumField = subEnum;
  source.subenumFieldList = subEnumList;

  StringWriter stringWriter = new StringWriter();
  JsonGenerator jsonGenerator = new JsonFactory().createGenerator(stringWriter);

  SimpleParseUUT__JsonHelper.serializeToJson(jsonGenerator, source, true);
  jsonGenerator.close();

  String inputString = stringWriter.toString();

  // Test that fields appear in the order specified in the class
  for (int i = 0; i < FIELD_DECLARATION_ORDER.length - 1; i++) {
    assertTrue(
        inputString.indexOf("\"" + FIELD_DECLARATION_ORDER[i] + "\"")
            < inputString.indexOf("\"" + FIELD_DECLARATION_ORDER[i + 1] + "\""));
  }
}
 
Example 15
Source File: Dispatcher.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
protected Queue<Event> initialValue() {
  return Queues.newArrayDeque();
}
 
Example 16
Source File: Dispatcher.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
protected Queue<Event> initialValue() {
  return Queues.newArrayDeque();
}
 
Example 17
Source File: ShardLimitingIterator.java    From datawave with Apache License 2.0 4 votes vote down vote up
public ShardLimitingIterator(Iterator<Entry<Key,Value>> kvIter, int maxShardsPerDay) {
    this.kvIter = new PeekingIterator<>(kvIter);
    this.maxShardsPerDay = maxShardsPerDay;
    currentQueue = Queues.newArrayDeque();
}
 
Example 18
Source File: RangeStreamScanner.java    From datawave with Apache License 2.0 4 votes vote down vote up
private int dequeue(boolean forceAll) {
    int count = 0;
    
    Queue<Entry<Key,Value>> kvIter = Queues.newArrayDeque(currentQueue);
    
    currentQueue.clear();
    boolean result = true;
    for (Entry<Key,Value> top : kvIter) {
        
        if (result) {
            do {
                result = resultQueue.offer(top);
                
                if (!result) {
                    if (log.isTraceEnabled())
                        log.trace("Failed adding " + resultQueue.size() + " " + forceAll);
                    if (forceAll)
                        continue;
                }
                
                break;
            } while (!finished && forceAll);
        }
        
        if (!result && !(!finished && forceAll)) {
            if (log.isTraceEnabled())
                log.trace("Adding " + top.getKey() + " back ");
            currentQueue.add(top);
        } else {
            if (log.isTraceEnabled())
                log.trace("missing " + top.getKey() + " true? " + result);
        }
        
        if (log.isTraceEnabled())
            log.trace("Last key is " + lastSeenKey);
        
        count++;
    }
    
    if (log.isTraceEnabled()) {
        log.trace("we have " + currentQueue.size() + " " + kvIter.size());
    }
    
    return count;
}
 
Example 19
Source File: BatchedPermitsRequesterTest.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
@Test
public void testNoMoreThanOneRequestAtATime() throws Exception {
  Queue<RequestAndCallback> queue = Queues.newArrayDeque();

  BatchedPermitsRequester container = BatchedPermitsRequester.builder().resourceId("resource")
      .requestorIdentifier("requestor").requestSender(new TestRequestSender(queue, false)).build();
  try (ParallelRequester requester = new ParallelRequester(container)) {

    Future<Boolean> future = requester.request(1);

    await(new QueueSize(queue, 1), 1000);
    Assert.assertEquals(queue.size(), 1);

    Future<Boolean> future2 = requester.request(2);
    Future<Boolean> future3 = requester.request(3);
    Future<Boolean> future4 = requester.request(4);
    Future<Boolean> future5 = requester.request(5);

    Thread.sleep(100);

    Assert.assertEquals(queue.size(), 1);
    satisfyRequestBuilder().requestAndCallback(queue.poll()).satisfy();

    future.get(1, TimeUnit.SECONDS);
    Assert.assertTrue(future.isDone());
    Assert.assertTrue(future.get());

    await(new QueueSize(queue, 1), 1000);
    Assert.assertEquals(queue.size(), 1);
    satisfyRequestBuilder().requestAndCallback(queue.poll()).satisfy();

    future2.get(1, TimeUnit.SECONDS);
    future3.get(1, TimeUnit.SECONDS);
    future4.get(1, TimeUnit.SECONDS);
    future5.get(1, TimeUnit.SECONDS);

    Assert.assertTrue(future2.get());
    Assert.assertTrue(future3.get());
    Assert.assertTrue(future4.get());
    Assert.assertTrue(future5.get());
  }

}
 
Example 20
Source File: NestedQueryIterator.java    From datawave with Apache License 2.0 4 votes vote down vote up
public NestedQueryIterator(NestedQuery<T> start) {
    nests = Queues.newArrayDeque();
    addNestedIterator(start);
}