private <T> T pickInstance(Collection<T> instances, T defaultValue) { if (instances.isEmpty()) { return defaultValue; } // generateInt() is 1-based. return Iterables.get(instances, (generateInt() - 1) % instances.size()); }
/** * Returns the element at the specified position in this fluent iterable. * * <p><b>{@code Stream} equivalent:</b> {@code stream.skip(position).findFirst().get()} (but note * that this throws different exception types, and throws an exception if {@code null} would be * returned). * * @param position position of the element to return * @return the element at the specified position in this fluent iterable * @throws IndexOutOfBoundsException if {@code position} is negative or greater than or equal to * the size of this fluent iterable */ // TODO(kevinb): add @Nullable? public final E get(int position) { return Iterables.get(getDelegate(), position); }
/** * Returns the output to input symbol mapping for the given source channel */ public Map<Symbol, SymbolReference> sourceSymbolMap(int sourceIndex) { ImmutableMap.Builder<Symbol, SymbolReference> builder = ImmutableMap.builder(); for (Map.Entry<Symbol, Collection<Symbol>> entry : outputToInputs.asMap().entrySet()) { builder.put(entry.getKey(), Iterables.get(entry.getValue(), sourceIndex).toSymbolReference()); } return builder.build(); }
private ImmutableList<PlanNode> rewriteSetOperationSubPlans(SetOperationNode node, RewriteContext<Set<Symbol>> context, ListMultimap<Symbol, Symbol> rewrittenSymbolMapping) { // Find the corresponding input symbol to the remaining output symbols and prune the subplans ImmutableList.Builder<PlanNode> rewrittenSubPlans = ImmutableList.builder(); for (int i = 0; i < node.getSources().size(); i++) { ImmutableSet.Builder<Symbol> expectedInputSymbols = ImmutableSet.builder(); for (Collection<Symbol> symbols : rewrittenSymbolMapping.asMap().values()) { expectedInputSymbols.add(Iterables.get(symbols, i)); } rewrittenSubPlans.add(context.rewrite(node.getSources().get(i), expectedInputSymbols.build())); } return rewrittenSubPlans.build(); }
public void testGet_withDefault_negativePosition() { try { Iterables.get(newArrayList("a", "b", "c"), -1, "d"); fail(); } catch (IndexOutOfBoundsException expected) { // pass } }
@Override public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws ExecutionException, InterruptedException { assertTaskWrapped(tasks); lastMethodCalled = "invokeAny"; return inline.submit(Iterables.get(tasks, 0)).get(); }
@Override public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws ExecutionException, InterruptedException, TimeoutException { assertTaskWrapped(tasks); lastMethodCalled = "invokeAnyTimeout"; lastTimeoutInMillis = unit.toMillis(timeout); return inline.submit(Iterables.get(tasks, 0)).get(timeout, unit); }
private void testGetOnEmpty(Iterable<String> iterable) { try { Iterables.get(iterable, 0); fail(); } catch (IndexOutOfBoundsException expected) { } }
public void testGet_withDefault_lastPlusOne() { ArrayList<String> list = newArrayList("a", "b", "c"); assertEquals("d", Iterables.get(list, 3, "d")); }
public void testGet_withDefault_iterable() { Set<String> set = ImmutableSet.of("a", "b", "c"); assertEquals("b", Iterables.get(set, 1, "d")); }
public void testGet_withDefault_simple() { ArrayList<String> list = newArrayList("a", "b", "c"); assertEquals("b", Iterables.get(list, 1, "d")); }
public void testGet_withDefault_last() { ArrayList<String> list = newArrayList("a", "b", "c"); assertEquals("c", Iterables.get(list, 2, "d")); }
@Test(dataProvider = "populated") public void contains_whenFound(Queue<Integer> queue) { assertThat(queue.contains(Iterables.get(queue, POPULATED_SIZE / 2)), is(true)); }
private void testGetOnAbc(Iterable<String> iterable) { try { Iterables.get(iterable, -1); fail(); } catch (IndexOutOfBoundsException expected) { } assertEquals("a", Iterables.get(iterable, 0)); assertEquals("b", Iterables.get(iterable, 1)); assertEquals("c", Iterables.get(iterable, 2)); try { Iterables.get(iterable, 3); fail(); } catch (IndexOutOfBoundsException nsee) { } try { Iterables.get(iterable, 4); fail(); } catch (IndexOutOfBoundsException nsee) { } }
public void testGet_withDefault_doesntIterate() { List<String> list = new DiesOnIteratorArrayList(); list.add("a"); assertEquals("a", Iterables.get(list, 0, "b")); }
private static void runTestFuzzyCompare(int toleranceIndex) { double tolerance = get(TOLERANCE_CANDIDATES, toleranceIndex); for (double a : ALL_DOUBLE_CANDIDATES) { for (double b : ALL_DOUBLE_CANDIDATES) { int expected = DoubleMath.fuzzyEquals(a, b, tolerance) ? 0 : Double.compare(a, b); int actual = DoubleMath.fuzzyCompare(a, b, tolerance); assertEquals(Integer.signum(expected), Integer.signum(actual)); } } }
@Test(dataProvider = "full") public void contains_whenFound(LinkedDeque<LinkedValue> deque) { assertThat(deque.contains(Iterables.get(deque, SIZE / 2)), is(true)); }
@Test(dataProvider = "full") public void moveToBack_middle(LinkedDeque<LinkedValue> deque) { checkMoveToBack(deque, Iterables.get(deque, SIZE / 2)); }
@Override Type extractSourceType(MethodInvocationTree tree, VisitorState state) { return extractTypeArgAsMemberOfSupertype( ASTHelpers.getType(Iterables.get(tree.getArguments(), methodArgIndex)), state.getSymbolFromString(methodArgTypeName), methodArgTypeArgIndex, state.getTypes()); }
private static void waitForSync(BatchServerInventoryView batchServerInventoryView, Set<DataSegment> testSegments) throws Exception { final Timing forWaitingTiming = timing.forWaiting(); Stopwatch stopwatch = Stopwatch.createStarted(); while (Iterables.isEmpty(batchServerInventoryView.getInventory()) || Iterables.size(Iterables.get(batchServerInventoryView.getInventory(), 0).getSegments()) != testSegments.size()) { Thread.sleep(100); if (stopwatch.elapsed(TimeUnit.MILLISECONDS) > forWaitingTiming.milliseconds()) { throw new ISE("BatchServerInventoryView is not updating"); } } }