/** Print the state of the container. Useful for debugging. */ private synchronized void printState(String prefix) { StringBuilder builder = new StringBuilder(prefix).append("->"); builder.append("BatchedPermitsRequester state (").append(hashCode()).append("): "); builder.append("TotalPermits: ").append(this.totalAvailablePermits).append(" "); builder.append("Batches(").append(this.batches.size()).append("): "); for (PermitBatch batch : this.batches.values()) { builder.append(batch.getPermits()).append(","); } log.info(builder.toString()); }
public static <T extends Comparable, E extends Comparable> Iterator<T> extractAndSort(Iterator<T> input, Function<T, E> extractor) { TreeMultimap<E, T> reorgnized = TreeMultimap.create(); while (input.hasNext()) { T t = input.next(); E e = extractor.apply(t); reorgnized.put(e, t); } return reorgnized.values().iterator(); } }
private synchronized boolean tryTake(long permits) { purgeExpiredBatches(); if (this.totalAvailablePermits < permits) { return false; } this.totalAvailablePermits -= permits; Iterator<PermitBatch> batchesIterator = this.batches.values().iterator(); while (batchesIterator.hasNext()) { PermitBatch batch = batchesIterator.next(); if (batch.getPermits() < permits) { permits -= batch.getPermits(); batchesIterator.remove(); } else { batch.decrementPermits(permits); return true; } } // This can only happen if totalAvailablePermits is not in sync with the actual batches throw new RuntimeException("Total permits was unsynced! This is an error in code."); }
/** * Returns a collection of entry objects that was created as a result of the search. The search is finding 'count' number of * nodes which are within 'within' distance away from the node specified by the position * * @param position The position of the node to be searched around * @param count The number of nodes to be found during the search */ @Override public Collection<Entry<T>> findNearest(float[] position, int count, float within) { validatePosition(position); if (count < 1) { throw new IllegalArgumentException("Count cannot be smaller than 1"); } if (within < 0) { throw new IllegalArgumentException("Within cannot be smaller than 0"); } if (rootNode == null) { return Collections.emptyList(); } else { TreeSearch<T> treeSearch = new TreeSearch<>(within, count); executeSearchInNode(position, rootNode, treeSearch); return Collections.unmodifiableCollection(treeSearch.results.values()); } }
public void testOrderedValues() { TreeMultimap<String, Integer> multimap = createPopulate(); assertThat(multimap.values()).containsExactly(7, 3, 1, null, 0, 6, 2).inOrder(); }
public void testOrderedValues() { TreeMultimap<String, Integer> multimap = createPopulate(); assertThat(multimap.values()).containsExactly(1, 3, 7, 2, 6, 0, 4).inOrder(); }
/** * @return the number of permits we should request in the next request. */ private long computeNextPermitRequest() { long candidatePermits = 0; long unsatisfiablePermits = this.permitsOutstanding.getTotalWeight() - this.permitBatchContainer.totalAvailablePermits; if (unsatisfiablePermits > 0) { candidatePermits = unsatisfiablePermits; } if (this.permitBatchContainer.batches.size() > 1) { // If there are multiple batches in the queue, don't create a new request return candidatePermits; } PermitBatch firstBatch = Iterables.getFirst(this.permitBatchContainer.batches.values(), null); if (firstBatch != null) { // If the current batch has more than 20% permits left, don't create a new request if ((double) firstBatch.getPermits() / firstBatch.getInitialPermits() > 0.2) { return candidatePermits; } double averageDepletionRate = firstBatch.getAverageDepletionRate(); long candidatePermitsByDepletion = Math.min((long) (averageDepletionRate * this.targetMillisBetweenRequests), MAX_GROWTH_REQUEST * firstBatch.getInitialPermits()); return Math.max(candidatePermits, candidatePermitsByDepletion); } else { return candidatePermits; } }
@GwtIncompatible // SerializableTester public void testExplicitComparatorSerialization() { TreeMultimap<String, Integer> multimap = createPopulate(); TreeMultimap<String, Integer> copy = SerializableTester.reserializeAndAssert(multimap); assertThat(copy.values()).containsExactly(7, 3, 1, null, 0, 6, 2).inOrder(); assertThat(copy.keySet()).containsExactly(null, "tree", "google").inOrder(); assertEquals(multimap.keyComparator(), copy.keyComparator()); assertEquals(multimap.valueComparator(), copy.valueComparator()); } }
@GwtIncompatible // SerializableTester public void testExplicitComparatorSerialization() { TreeMultimap<String, Integer> multimap = createPopulate(); TreeMultimap<String, Integer> copy = SerializableTester.reserializeAndAssert(multimap); assertThat(copy.values()).containsExactly(1, 3, 7, 2, 6, 0, 4).inOrder(); assertThat(copy.keySet()).containsExactly("foo", "google", "tree").inOrder(); assertEquals(multimap.keyComparator(), copy.keyComparator()); assertEquals(multimap.valueComparator(), copy.valueComparator()); }
public void testTailSetClear() { TreeMultimap<String, Integer> multimap = TreeMultimap.create(); multimap.put("a", 1); multimap.put("a", 11); multimap.put("b", 2); multimap.put("c", 3); multimap.put("d", 4); multimap.put("e", 5); multimap.put("e", 55); multimap.keySet().tailSet("d").clear(); assertEquals(ImmutableSet.of("a", "b", "c"), multimap.keySet()); assertEquals(4, multimap.size()); assertEquals(4, multimap.values().size()); assertEquals(4, multimap.keys().size()); }
@GwtIncompatible // SerializableTester public void testTreeMultimapNonGeneric() { TreeMultimap<LegacyComparable, LegacyComparable> multimap = TreeMultimap.create(); assertEquals(ImmutableMultimap.of(), multimap); multimap.put(new LegacyComparable("foo"), new LegacyComparable("f")); multimap.put(new LegacyComparable("foo"), new LegacyComparable("o")); multimap.put(new LegacyComparable("foo"), new LegacyComparable("o")); multimap.put(new LegacyComparable("bar"), new LegacyComparable("b")); multimap.put(new LegacyComparable("bar"), new LegacyComparable("a")); multimap.put(new LegacyComparable("bar"), new LegacyComparable("r")); assertThat(multimap.keySet()) .containsExactly(new LegacyComparable("bar"), new LegacyComparable("foo")) .inOrder(); assertThat(multimap.values()) .containsExactly( new LegacyComparable("a"), new LegacyComparable("b"), new LegacyComparable("r"), new LegacyComparable("f"), new LegacyComparable("o")) .inOrder(); assertEquals(Ordering.natural(), multimap.keyComparator()); assertEquals(Ordering.natural(), multimap.valueComparator()); SerializableTester.reserializeAndAssert(multimap); }
@GwtIncompatible // SerializableTester public void testTreeMultimapDerived() { TreeMultimap<DerivedComparable, DerivedComparable> multimap = TreeMultimap.create(); assertEquals(ImmutableMultimap.of(), multimap); multimap.put(new DerivedComparable("foo"), new DerivedComparable("f")); multimap.put(new DerivedComparable("foo"), new DerivedComparable("o")); multimap.put(new DerivedComparable("foo"), new DerivedComparable("o")); multimap.put(new DerivedComparable("bar"), new DerivedComparable("b")); multimap.put(new DerivedComparable("bar"), new DerivedComparable("a")); multimap.put(new DerivedComparable("bar"), new DerivedComparable("r")); assertThat(multimap.keySet()) .containsExactly(new DerivedComparable("bar"), new DerivedComparable("foo")) .inOrder(); assertThat(multimap.values()) .containsExactly( new DerivedComparable("a"), new DerivedComparable("b"), new DerivedComparable("r"), new DerivedComparable("f"), new DerivedComparable("o")) .inOrder(); assertEquals(Ordering.natural(), multimap.keyComparator()); assertEquals(Ordering.natural(), multimap.valueComparator()); SerializableTester.reserializeAndAssert(multimap); }
@Override synchronized RamDiskReplicaLru getNextCandidateForEviction() { final Iterator<RamDiskReplicaLru> it = replicasPersisted.values().iterator(); while (it.hasNext()) { final RamDiskReplicaLru ramDiskReplicaLru = it.next(); it.remove(); Map<Long, RamDiskReplicaLru> replicaMap = replicaMaps.get(ramDiskReplicaLru.getBlockPoolId()); if (replicaMap != null && replicaMap.get(ramDiskReplicaLru.getBlockId()) != null) { return ramDiskReplicaLru; } // The replica no longer exists, look for the next one. } return null; }
public List<ScheduledMessage> getScheduledMessagesThatShouldFire(long now) { return this.scheduledMessages.values().stream() .filter(scheduledMessage -> scheduledMessage.getFireTime(TimeUnit.MILLISECONDS) < now) .collect(Collectors.toList()); }
public Itinerary build() { List<Event> events = new ArrayList<>(); Queue<Activity> mealQueue = newLinkedList(foods.values()); Activity hotel = !hotels.isEmpty() ? Iterables.get(hotels.values(), 0) : null; for (Activity activity : activities.values()) { boolean wasAdded = addEvent(events, activity, mealQueue, fixedEvents, hotel); if (!wasAdded) break; } return ImmutableItinerary.of(events); }
public static <T extends Comparable, E extends Comparable> Iterator<T> extractAndSort(Iterator<T> input, Function<T, E> extractor) { TreeMultimap<E, T> reorgnized = TreeMultimap.create(); while (input.hasNext()) { T t = input.next(); E e = extractor.apply(t); reorgnized.put(e, t); } return reorgnized.values().iterator(); } }
public void clear() { size=0; lowerbound = Double.NEGATIVE_INFINITY; if (callback!=null) for (T t : backingQueue.values()) callback.execute(t); backingQueue.clear(); }
public void testOrderedValues() { TreeMultimap<String, Integer> multimap = createPopulate(); assertThat(multimap.values()).containsExactly( 1, 3, 7, 2, 6, 0, 4).inOrder(); }
@GwtIncompatible // SerializableTester public void testExplicitComparatorSerialization() { TreeMultimap<String, Integer> multimap = createPopulate(); TreeMultimap<String, Integer> copy = SerializableTester.reserializeAndAssert(multimap); assertThat(copy.values()).containsExactly(1, 3, 7, 2, 6, 0, 4).inOrder(); assertThat(copy.keySet()).containsExactly("foo", "google", "tree").inOrder(); assertEquals(multimap.keyComparator(), copy.keyComparator()); assertEquals(multimap.valueComparator(), copy.valueComparator()); }
@GwtIncompatible // SerializableTester public void testExplicitComparatorSerialization() { TreeMultimap<String, Integer> multimap = createPopulate(); TreeMultimap<String, Integer> copy = SerializableTester.reserializeAndAssert(multimap); assertThat(copy.values()).containsExactly(7, 3, 1, null, 0, 6, 2).inOrder(); assertThat(copy.keySet()).containsExactly(null, "tree", "google").inOrder(); assertEquals(multimap.keyComparator(), copy.keyComparator()); assertEquals(multimap.valueComparator(), copy.valueComparator()); } }