static TypeParameterTree typeParameterInList( List<? extends TypeParameterTree> typeParameters, Symbol v) { return typeParameters.stream() .filter(t -> t.getName().contentEquals(v.name)) .collect(MoreCollectors.onlyElement()); }
@Override public Optional<SystemTable> getSystemTable(ConnectorSession session, SchemaTableName tableName) { return delegates.stream() .map(delegate -> delegate.getSystemTable(session, tableName)) .filter(Optional::isPresent) // this ensures there is 0 or 1 element in stream .map(Optional::get) .collect(toOptional()); } }
@VisibleForTesting static TransferExtension findTransferExtension( ImmutableList<TransferExtension> transferExtensions, String service) { try { return transferExtensions .stream() .filter(ext -> ext.getServiceId().toLowerCase().equals(service.toLowerCase())) .collect(onlyElement()); } catch (IllegalArgumentException e) { throw new IllegalStateException( "Found multiple transfer extensions for service " + service, e); } catch (NoSuchElementException e) { throw new IllegalStateException( "Did not find a valid transfer extension for service " + service, e); } }
private static Optional<? extends VariableTree> findParameterWithSymbol( List<? extends VariableTree> parameters, Symbol symbol) { return parameters.stream() .filter(parameter -> symbol.equals(ASTHelpers.getSymbol(parameter))) .collect(toOptional()); }
public void testOnlyElement() { try { Stream.empty().collect(MoreCollectors.onlyElement()); fail("Expected NoSuchElementException"); } catch (NoSuchElementException expected) { } }
/** * Resolves a node by materializing GroupReference nodes * representing symbolic references to other nodes. This method * is deprecated since is assumes group contains only one node. * <p> * If the node is not a GroupReference, it returns the * argument as is. */ @Deprecated default PlanNode resolve(PlanNode node) { if (node instanceof GroupReference) { return resolveGroup(node).collect(toOptional()).get(); } return node; }
public void testOnlyElementNull() { assertThat(Stream.of((Object) null).collect(MoreCollectors.onlyElement())).isNull(); }
public void testToOptionalNull() { Stream<Object> stream = Stream.of((Object) null); try { stream.collect(MoreCollectors.toOptional()); fail("Expected NullPointerException"); } catch (NullPointerException expected) { } }
public void testOnlyElementSingleton() { assertThat(Stream.of(1).collect(MoreCollectors.onlyElement())).isEqualTo(1); }
public void testToOptionalMany() { try { Stream.of(1, 2, 3, 4, 5, 6).collect(MoreCollectors.toOptional()); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage()).contains("1, 2, 3, 4, 5, ..."); } }
public void testOnlyElementMany() { try { Stream.of(1, 2, 3, 4, 5, 6).collect(MoreCollectors.onlyElement()); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage()).contains("1, 2, 3, 4, 5, ..."); } } }
public void testToOptionalMultiple() { try { Stream.of(1, 2).collect(MoreCollectors.toOptional()); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage()).contains("1, 2"); } }
public void testOnlyElementMultiple() { try { Stream.of(1, 2).collect(MoreCollectors.onlyElement()); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage()).contains("1, 2"); } }
private synchronized void callOomKiller(Iterable<QueryExecution> runningQueries) { List<QueryMemoryInfo> queryMemoryInfoList = Streams.stream(runningQueries) .map(this::createQueryMemoryInfo) .collect(toImmutableList()); List<MemoryInfo> nodeMemoryInfos = nodes.values().stream() .map(RemoteNodeMemory::getInfo) .filter(Optional::isPresent) .map(Optional::get) .collect(toImmutableList()); Optional<QueryId> chosenQueryId = lowMemoryKiller.chooseQueryToKill(queryMemoryInfoList, nodeMemoryInfos); if (chosenQueryId.isPresent()) { log.debug("Low memory killer chose %s", chosenQueryId.get()); Optional<QueryExecution> chosenQuery = Streams.stream(runningQueries).filter(query -> chosenQueryId.get().equals(query.getQueryId())).collect(toOptional()); if (chosenQuery.isPresent()) { // See comments in isLastKilledQueryGone for why chosenQuery might be absent. chosenQuery.get().fail(new PrestoException(CLUSTER_OUT_OF_MEMORY, "Query killed because the cluster is out of memory. Please try again in a few minutes.")); queriesKilledDueToOutOfMemory.incrementAndGet(); lastKilledQuery = chosenQueryId.get(); logQueryKill(chosenQueryId.get(), nodeMemoryInfos); } } }
(int) idCount.values().stream() .distinct() .collect(onlyElement()), 3); assertEquals(idCount.keySet(), expectedIds);
public void testToOptionalSingleton() { assertThat(Stream.of(1).collect(MoreCollectors.toOptional())).hasValue(1); }
private static ImmutableSet<AbiAlias> getMultiAbi(VariantTargeting variantTargeting) { if (variantTargeting.getMultiAbiTargeting().getValueList().isEmpty()) { return ImmutableSet.of(); } return variantTargeting.getMultiAbiTargeting().getValueList().stream() // For now we only support one value in MultiAbiTargeting. .collect(MoreCollectors.onlyElement()) .getAbiList() .stream() .map(Abi::getAlias) .collect(toImmutableSet()); }
public void testToOptionalEmpty() { assertThat(Stream.empty().collect(MoreCollectors.toOptional())).isEmpty(); }
private static Optional<AbiAlias> getAbi(VariantTargeting variantTargeting) { if (variantTargeting.getAbiTargeting().getValueList().isEmpty()) { return Optional.empty(); } return Optional.of( variantTargeting .getAbiTargeting() .getValueList() .stream() // For now we only support one value in AbiTargeting. .collect(MoreCollectors.onlyElement()) .getAlias()); }
/** * Returns a single variant matching a full device-spec. * * @throws IllegalArgumentException if multiple variants are matched. */ public Optional<Variant> getMatchingVariant(BuildApksResult buildApksResult) { return getAllMatchingVariants(buildApksResult).stream().collect(MoreCollectors.toOptional()); }