@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()); } }
private static Optional<? extends VariableTree> findParameterWithSymbol( List<? extends VariableTree> parameters, Symbol symbol) { return parameters.stream() .filter(parameter -> symbol.equals(ASTHelpers.getSymbol(parameter))) .collect(toOptional()); }
/** * 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 testToOptionalNull() { Stream<Object> stream = Stream.of((Object) null); try { stream.collect(MoreCollectors.toOptional()); fail("Expected NullPointerException"); } catch (NullPointerException expected) { } }
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 testToOptionalMultiple() { try { Stream.of(1, 2).collect(MoreCollectors.toOptional()); 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); } } }
public void testToOptionalSingleton() { assertThat(Stream.of(1).collect(MoreCollectors.toOptional())).hasValue(1); }
public void testToOptionalEmpty() { assertThat(Stream.empty().collect(MoreCollectors.toOptional())).isEmpty(); }
/** * 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()); }
public final Optional<NodeWrapperT> getChildText() { return getChildrenText().collect(toOptional()); }
/** Returns the {@link ModuleEntry} associated with the given path, or empty if not found. */ public Optional<ModuleEntry> findEntry(ZipPath path) { return getEntries() .stream() .filter(entry -> entry.getPath().equals(path)) .collect(toOptional()); }
public static Optional<Entry> lookupEntryByResourceId( ResourceTable resourceTable, int resourceId) { return entries(resourceTable) .filter(entry -> entry.getResourceId().getFullResourceId() == resourceId) .map(ResourceTableEntry::getEntry) .collect(toOptional()); }
private Optional<Package> findPackage(Predicate<Package> predicate) { return actual().getPackageList().stream().filter(predicate).collect(toOptional()); }
private Optional<Entry> findEntry(String entryName) { return actual() .getEntryList() .stream() .filter(entry -> entry.getName().equals(entryName)) .collect(toOptional()); } }
private Optional<Type> findType(String typeName) { return actual() .getTypeList() .stream() .filter(type -> type.getName().equals(typeName)) .collect(toOptional()); } }
private ManifestEditor setMetadataValue(String name, XmlProtoAttributeBuilder valueAttr) { XmlProtoElementBuilder applicationEl = manifestElement.getOrCreateChildElement(APPLICATION_ELEMENT_NAME); Optional<XmlProtoElementBuilder> existingMetadataEl = applicationEl .getChildrenElements(META_DATA_ELEMENT_NAME) .filter( metadataEl -> metadataEl .getAndroidAttribute(NAME_RESOURCE_ID) .map(nameAttr -> name.equals(nameAttr.getValueAsString())) .orElse(false)) .collect(toOptional()); if (existingMetadataEl.isPresent()) { existingMetadataEl.get().removeAndroidAttribute(VALUE_RESOURCE_ID).addAttribute(valueAttr); } else { applicationEl.addChildElement( XmlProtoElementBuilder.create(META_DATA_ELEMENT_NAME) .addAttribute(createAndroidAttribute("name", NAME_RESOURCE_ID).setValueAsString(name)) .addAttribute(valueAttr)); } return this; }
.stream() .filter(type -> type.getName().equals(typeName)) .collect(toOptional()); if (!matchingType.isPresent()) { return Optional.empty(); .stream() .filter(entry -> entry.getName().equals(resourceName)) .collect(toOptional()); if (!matchingEntry.isPresent()) { return Optional.empty();
@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()); } }
/** * Find an enumerated constant using it's {@code toString()} value. An {@code IllegalArgumentException} is * thrown if multiple values are matched. */ public Optional<E> byToString(String value) { return withToString(value).collect(toOptional()); }