com.google.common.collect.Streams Java Examples
The following examples show how to use
com.google.common.collect.Streams.
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: PubsubMessageToObjectNode.java From gcp-ingestion with Mozilla Public License 2.0 | 6 votes |
/** * This method gives us a chance to perform some additional type coercions in case the BigQuery * field type is different from the source data type. This should rarely happen, since only * validated payloads get through to this BQ sink path, but there are sets of probes with * heterogeneous types that appear as explicit fields in BQ, but are treated as loosely typed * maps at the validation phase; we need to catch these or they can cause the entire pipeline * to stall. * * <p>Returning {@link Optional#empty} here indicates that no coercion is defined and that the * field should be put to {@code additional_properties}. */ private Optional<JsonNode> coerceToBqType(JsonNode o, Field field) { if (o.isNull()) { // null is valid for any type, just not as an element of a list return Optional.of(o); } else if (field.getMode() == Field.Mode.REPEATED) { if (o.isArray()) { // We have not yet observed a case where an array type contains values that cannot be // coerced to appropriate values, but if it does this will throw NoSuchElementException // and prevent the message from being delivered to BigQuery in a form that could lead to // data being missed in additional_properties. return Optional.of(Json.createArrayNode() .addAll(Streams.stream(o).map(v -> coerceSingleValueToBqType(v, field)) .map(Optional::get).collect(Collectors.toList()))); } else { return Optional.empty(); } } else { return coerceSingleValueToBqType(o, field); } }
Example #2
Source File: ElasticSearchReportPreprocessor.java From vind with Apache License 2.0 | 6 votes |
private JsonArray extractFilterFields(JsonElement filters) { final JsonArray fs = new JsonArray(); //Single filter object or a list of filters if (filters.isJsonObject()) { fs.add(filters.getAsJsonObject()); } else { fs.addAll(filters.getAsJsonArray()); } return Streams.stream(fs.iterator()) .map(JsonElement::getAsJsonObject) .flatMap( jo -> { if (jo.has("delegates")){ return Streams.stream(extractFilterFields(jo.get("delegates")).iterator()); } else { return Stream.of(jo); } }) .filter( f -> ! environmentFilters.contains(f)) .filter( f -> ! systemFieldFilters.contains(f.getAsJsonObject().get("field").getAsString())) .collect(JsonArray::new, JsonArray::add, JsonArray::addAll); }
Example #3
Source File: ConfigurationMakeVariableContext.java From bazel with Apache License 2.0 | 6 votes |
private static ImmutableList<TemplateVariableInfo> getRuleTemplateVariableProviders( RuleContext ruleContext, Iterable<String> attributeNames) { ImmutableList.Builder<TemplateVariableInfo> providers = new ImmutableList.Builder<>(); // Get template variable providers from the attributes. List<TemplateVariableInfo> fromAttributes = Streams.stream(attributeNames) // Only process this attribute it if is present in the rule. .filter(attrName -> ruleContext.attributes().has(attrName)) // Get the TemplateVariableInfo providers from this attribute. .flatMap( attrName -> Streams.stream( ruleContext.getPrerequisites( attrName, TransitionMode.DONT_CHECK, TemplateVariableInfo.PROVIDER))) .collect(Collectors.toList()); providers.addAll(fromAttributes); // Also collect template variable providers from any resolved toolchains. if (ruleContext.getToolchainContext() != null) { providers.addAll(ruleContext.getToolchainContext().templateVariableProviders()); } return providers.build(); }
Example #4
Source File: PruneSemiJoinColumns.java From presto with Apache License 2.0 | 6 votes |
@Override protected Optional<PlanNode> pushDownProjectOff(Context context, SemiJoinNode semiJoinNode, Set<Symbol> referencedOutputs) { if (!referencedOutputs.contains(semiJoinNode.getSemiJoinOutput())) { return Optional.of(semiJoinNode.getSource()); } Set<Symbol> requiredSourceInputs = Streams.concat( referencedOutputs.stream() .filter(symbol -> !symbol.equals(semiJoinNode.getSemiJoinOutput())), Stream.of(semiJoinNode.getSourceJoinSymbol()), semiJoinNode.getSourceHashSymbol().stream()) .collect(toImmutableSet()); return restrictOutputs(context.getIdAllocator(), semiJoinNode.getSource(), requiredSourceInputs) .map(newSource -> semiJoinNode.replaceChildren(ImmutableList.of( newSource, semiJoinNode.getFilteringSource()))); }
Example #5
Source File: DatastoreStore.java From tomcat-runtime with Apache License 2.0 | 6 votes |
/** * Remove expired sessions from the datastore. */ @Override public void processExpires() { log.debug("Processing expired sessions"); Query<Key> query = Query.newKeyQueryBuilder().setKind(sessionKind) .setFilter(PropertyFilter.le(SessionMetadata.EXPIRATION_TIME, clock.millis())) .build(); QueryResults<Key> keys = datastore.run(query); Stream<Key> toDelete = Streams.stream(keys) .parallel() .flatMap(key -> Streams.stream(datastore.run(Query.newKeyQueryBuilder() .setKind(sessionKind) .setFilter(PropertyFilter.hasAncestor(newKey(key.getName()))) .build()))); datastore.delete(toDelete.toArray(Key[]::new)); }
Example #6
Source File: BatchedRangeRead.java From geowave with Apache License 2.0 | 6 votes |
@Override public void onSuccess(final ResultSet result) { try { if (result != null) { final Iterator<GeoWaveRow> iterator = (Iterator) Streams.stream(result.iterator()).map(row -> new CassandraRow(row)).filter( filter).iterator(); rowTransform.apply( rowMerging ? new GeoWaveRowMergingIterator(iterator) : iterator).forEachRemaining( row -> { try { resultQueue.put(row); } catch (final InterruptedException e) { LOGGER.warn("interrupted while waiting to enqueue a cassandra result", e); } }); } } finally { checkFinalize(); } }
Example #7
Source File: PackageMapper.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
public static List<Object> map(Package pack) { List<Object> row = new ArrayList<>(PACKAGE_ATTRS.size()); for (Entry<String, String> entry : PACKAGE_ATTRS.entrySet()) { switch (entry.getKey()) { case EMX_PACKAGE_TAGS: row.add(Streams.stream(pack.getTags()).map(Tag::getId).collect(joining(","))); break; case EMX_PACKAGE_PARENT: Package parent = pack.getParent(); row.add(parent != null ? parent.getId() : null); break; default: Object value = pack.get(entry.getValue()); row.add(value != null ? value.toString() : null); } } return row; }
Example #8
Source File: DashboardTest.java From cloud-opensource-java with Apache License 2.0 | 6 votes |
@Test public void testDashboard_recommendedCoordinates() { Nodes recommendedListItem = dashboard.query("//ul[@id='recommended']/li"); Assert.assertTrue(recommendedListItem.size() > 100); // fails if these are not valid Maven coordinates for (Node node : recommendedListItem) { new DefaultArtifact(node.getValue()); } ImmutableList<String> coordinateList = Streams.stream(recommendedListItem).map(Node::getValue).collect(toImmutableList()); ArrayList<String> sorted = new ArrayList<>(coordinateList); Comparator<String> comparator = new SortWithoutVersion(); Collections.sort(sorted, comparator); for (int i = 0; i < sorted.size(); i++) { Assert.assertEquals( "Coordinates are not sorted: ", sorted.get(i), coordinateList.get(i)); } }
Example #9
Source File: ElasticQueryBuilder.java From vind with Apache License 2.0 | 6 votes |
public static List<String> getSpellCheckedQuery(SearchResponse response) { final Map<String, Pair<String,Double>> spellcheck = Streams.stream(response.getSuggest().iterator()) .map(e ->Pair.of(e.getName(), e.getEntries().stream() .map(word -> word.getOptions().stream() .sorted(Comparator.comparingDouble(Option::getScore).reversed()) .map(o -> Pair.of(o.getText().string(),o.getScore())) .findFirst() .orElse(Pair.of(word.getText().string(),0f)) ).collect(Collectors.toMap( Pair::getKey,Pair::getValue))) ) .collect(Collectors.toMap( Pair::getKey, p -> Pair.of( String.join(" ", p.getValue().keySet()), p.getValue().values().stream().mapToDouble(Float::floatValue).sum()))); return spellcheck.values().stream() .filter( v -> v.getValue() > 0.0) .sorted((p1,p2) -> Double.compare(p2.getValue(),p1.getValue())) .map(Pair::getKey) .collect(Collectors.toList()); }
Example #10
Source File: BuckClasspath.java From buck with Apache License 2.0 | 6 votes |
/** Returns Buck's classpath. */ public static ImmutableList<Path> getClasspath() throws IOException { String classpathFromEnv = getBuckClasspathFromEnvVarOrNull(); if (classpathFromEnv != null) { Optional<String> buckTestClasspath = getBuckTestClasspath(); Stream<Path> classpathStream = Arrays.stream(classpathFromEnv.split(File.pathSeparator)).map(Paths::get); if (buckTestClasspath.isPresent()) { classpathStream = Streams.concat( classpathStream, readClasspaths(Paths.get(buckTestClasspath.get())).stream()); } return classpathStream.collect(ImmutableList.toImmutableList()); } return getBuckClasspathForIntellij(); }
Example #11
Source File: PruneRowNumberColumns.java From presto with Apache License 2.0 | 6 votes |
@Override protected Optional<PlanNode> pushDownProjectOff(Context context, RowNumberNode rowNumberNode, Set<Symbol> referencedOutputs) { // Remove unused RowNumberNode if (!referencedOutputs.contains(rowNumberNode.getRowNumberSymbol())) { if (rowNumberNode.getMaxRowCountPerPartition().isEmpty()) { return Optional.of(rowNumberNode.getSource()); } if (rowNumberNode.getPartitionBy().isEmpty()) { return Optional.of(new LimitNode( rowNumberNode.getId(), rowNumberNode.getSource(), rowNumberNode.getMaxRowCountPerPartition().get(), false)); } } Set<Symbol> requiredInputs = Streams.concat( referencedOutputs.stream() .filter(symbol -> !symbol.equals(rowNumberNode.getRowNumberSymbol())), rowNumberNode.getPartitionBy().stream(), rowNumberNode.getHashSymbol().stream()) .collect(toImmutableSet()); return restrictChildOutputs(context.getIdAllocator(), rowNumberNode, requiredInputs); }
Example #12
Source File: BaseDataStore.java From geowave with Apache License 2.0 | 6 votes |
@Override public <R> R aggregateStatistics(final StatisticsQuery<R> query) { if (query.getStatsType() == null) { LOGGER.error("Statistic Type must be provided for a statistical aggregation"); return null; } try (CloseableIterator<InternalDataStatistics<?, R, ?>> it = internalQueryStatistics(query)) { final Optional<InternalDataStatistics<?, R, ?>> result = Streams.stream(it).reduce(InternalDataStatistics::reduce); if (result.isPresent()) { return result.get().getResult(); } LOGGER.warn("No statistics found matching query criteria for statistical aggregation"); return null; } }
Example #13
Source File: RegistrarSettingsAction.java From nomulus with Apache License 2.0 | 6 votes |
private Map<String, Object> expandRegistrarWithContacts( Iterable<RegistrarContact> contacts, Registrar registrar) { ImmutableSet<Map<String, Object>> expandedContacts = Streams.stream(contacts) .map(RegistrarContact::toDiffableFieldMap) // Note: per the javadoc, toDiffableFieldMap includes sensitive data but we don't want // to display it here .peek( map -> { map.remove("registryLockPasswordHash"); map.remove("registryLockPasswordSalt"); }) .collect(toImmutableSet()); // Use LinkedHashMap here to preserve ordering; null values mean we can't use ImmutableMap. LinkedHashMap<String, Object> result = new LinkedHashMap<>(registrar.toDiffableFieldMap()); result.put("contacts", expandedContacts); return result; }
Example #14
Source File: ResourceFlowTestCase.java From nomulus with Apache License 2.0 | 6 votes |
/** * Confirms that an EppResourceIndex entity exists in Datastore for a given resource. */ protected static <T extends EppResource> void assertEppResourceIndexEntityFor(final T resource) { ImmutableList<EppResourceIndex> indices = Streams.stream( ofy() .load() .type(EppResourceIndex.class) .filter("kind", Key.getKind(resource.getClass()))) .filter( index -> Key.create(resource).equals(index.getKey()) && ofy().load().key(index.getKey()).now().equals(resource)) .collect(toImmutableList()); assertThat(indices).hasSize(1); assertThat(indices.get(0).getBucket()) .isEqualTo(EppResourceIndexBucket.getBucketKey(Key.create(resource))); }
Example #15
Source File: ListObjectsAction.java From nomulus with Apache License 2.0 | 6 votes |
/** * Returns the set of fields to return, aliased or not according to --full_field_names, and * with duplicates eliminated but the ordering otherwise preserved. */ private ImmutableSet<String> getFieldsToUse(ImmutableSet<T> objects) { // Get the list of fields from the received parameter. List<String> fieldsToUse; if ((fields == null) || !fields.isPresent()) { fieldsToUse = new ArrayList<>(); } else { fieldsToUse = Splitter.on(',').splitToList(fields.get()); // Check whether any field name is the wildcard; if so, use all fields. if (fieldsToUse.contains("*")) { fieldsToUse = getAllAvailableFields(objects); } } // Handle aliases according to the state of the fullFieldNames parameter. final ImmutableMap<String, String> nameMapping = ((fullFieldNames != null) && fullFieldNames.isPresent() && fullFieldNames.get()) ? getFieldAliases() : getFieldAliases().inverse(); return Streams.concat(getPrimaryKeyFields().stream(), fieldsToUse.stream()) .map(field -> nameMapping.getOrDefault(field, field)) .collect(toImmutableSet()); }
Example #16
Source File: PartialHealthCheckStrategyTest.java From armeria with Apache License 2.0 | 6 votes |
@Test void updateCandidates() { final List<Endpoint> newCandidates = createCandidates(5); maxCountStrategy.updateCandidates(newCandidates); assertCandidates(maxCountStrategy.getSelectedEndpoints(), newCandidates); final List<Endpoint> someOfOldCandidates = candidatesForMaxCount.subList(0, 3); maxCountStrategy.updateCandidates(someOfOldCandidates); assertCandidates(maxCountStrategy.getSelectedEndpoints(), someOfOldCandidates); final List<Endpoint> mixedCandidates = Streams.concat(createCandidates(2).stream(), someOfOldCandidates.stream()) .collect(toImmutableList()); maxCountStrategy.updateCandidates(mixedCandidates); assertCandidates(maxCountStrategy.getSelectedEndpoints(), mixedCandidates); }
Example #17
Source File: BLS12PairingPrecompiledContractTest.java From besu with Apache License 2.0 | 6 votes |
@Parameterized.Parameters public static Iterable<String[]> parameters() throws IOException { return Streams.concat( CharStreams.readLines( new InputStreamReader( BLS12PairingPrecompiledContractTest.class.getResourceAsStream("pairing.csv"), UTF_8)) .stream(), CharStreams.readLines( new InputStreamReader( BLS12PairingPrecompiledContractTest.class.getResourceAsStream( "invalid_subgroup_for_pairing.csv"), UTF_8)) .stream()) .map(line -> line.split(",", 4)) .collect(Collectors.toList()); }
Example #18
Source File: DefaultClientFactory.java From armeria with Apache License 2.0 | 6 votes |
DefaultClientFactory(HttpClientFactory httpClientFactory) { this.httpClientFactory = httpClientFactory; final List<ClientFactory> availableClientFactories = new ArrayList<>(); availableClientFactories.add(httpClientFactory); Streams.stream(ServiceLoader.load(ClientFactoryProvider.class, DefaultClientFactory.class.getClassLoader())) .map(provider -> provider.newFactory(httpClientFactory)) .forEach(availableClientFactories::add); final ImmutableMap.Builder<Scheme, ClientFactory> builder = ImmutableMap.builder(); for (ClientFactory f : availableClientFactories) { f.supportedSchemes().forEach(s -> builder.put(s, f)); } clientFactories = builder.build(); clientFactoriesToClose = ImmutableList.copyOf(availableClientFactories).reverse(); }
Example #19
Source File: ResourceTableMerger.java From bundletool with Apache License 2.0 | 5 votes |
private static <T, R> ImmutableMap<Integer, R> toIndexMap( ImmutableList<T> list, Function<T, R> valueFn) { Map<Integer, T> map = Streams.mapWithIndex( list.stream(), (value, i) -> new AbstractMap.SimpleEntry<>(Ints.checkedCast(i), value)) .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)); return ImmutableMap.copyOf(Maps.transformValues(map, valueFn::apply)); }
Example #20
Source File: GuavaStreamsUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void createStreamsWithIterable() { Iterable<Integer> numbersIterable = numbers; Stream streamFromIterable = Streams.stream(numbersIterable); assertNotNull(streamFromIterable); assertStreamEquals(streamFromIterable, numbers.stream()); }
Example #21
Source File: BaseQueryClauseGenerator.java From molgenis with GNU Lesser General Public License v3.0 | 5 votes |
@SuppressWarnings("UnstableApiUsage") List<Object> getQueryValues(Attribute attr, Object queryRuleValue) { if (queryRuleValue == null) { throw new MolgenisQueryException(QUERY_VALUE_CANNOT_BE_NULL_MSG); } if (!(queryRuleValue instanceof Iterable<?>)) { throw new MolgenisQueryException( "Query value must be a Iterable instead of [" + queryRuleValue.getClass().getSimpleName() + "]"); } return Streams.stream((Iterable<?>) queryRuleValue) .map(aQueryRuleValue -> getQueryValue(attr, aQueryRuleValue)) .collect(toList()); }
Example #22
Source File: BgpProcess.java From batfish with Apache License 2.0 | 5 votes |
/** * Return a stream over all types of {@link BgpPeerConfig peer configurations} defined for this * process */ @JsonIgnore public Stream<BgpPeerConfig> allPeerConfigsStream() { return Streams.concat( _activeNeighbors.values().stream(), _passiveNeighbors.values().stream(), _interfaceNeighbors.values().stream()); }
Example #23
Source File: SnapshotImpl.java From gsc-core with GNU Lesser General Public License v3.0 | 5 votes |
synchronized void collect(Map<WrappedByteArray, WrappedByteArray> all) { Snapshot next = getRoot().getNext(); while (next != null) { Streams.stream(((SnapshotImpl) next).db) .forEach(e -> all.put(WrappedByteArray.of(e.getKey().getBytes()), WrappedByteArray.of(e.getValue().getBytes()))); next = next.getNext(); } }
Example #24
Source File: DatastoreHelper.java From nomulus with Apache License 2.0 | 5 votes |
/** Assert that the actual poll messages match the expected ones, ignoring IDs and order. */ public static void assertPollMessagesEqual( Iterable<PollMessage> actual, Iterable<PollMessage> expected) { assertThat(Streams.stream(actual).map(POLL_MESSAGE_ID_STRIPPER).collect(toImmutableList())) .containsExactlyElementsIn( Streams.stream(expected).map(POLL_MESSAGE_ID_STRIPPER).collect(toImmutableList())); }
Example #25
Source File: CassandraOperations.java From geowave with Apache License 2.0 | 5 votes |
@Override public RowReader<GeoWaveRow> createReader(final DataIndexReaderParams readerParams) { final byte[][] dataIds; Iterator<GeoWaveRow> iterator; if (readerParams.getDataIds() == null) { if ((readerParams.getStartInclusiveDataId() != null) || (readerParams.getEndInclusiveDataId() != null)) { final List<byte[]> intermediaries = new ArrayList<>(); ByteArrayUtils.addAllIntermediaryByteArrays( intermediaries, new ByteArrayRange( readerParams.getStartInclusiveDataId(), readerParams.getEndInclusiveDataId())); dataIds = intermediaries.toArray(new byte[0][]); iterator = getRows(dataIds, readerParams.getAdapterId()); } else { iterator = getRows(readerParams.getAdapterId()); } } else { dataIds = readerParams.getDataIds(); iterator = getRows(dataIds, readerParams.getAdapterId()); } if (options.isVisibilityEnabled()) { Stream<GeoWaveRow> stream = Streams.stream(iterator); final Set<String> authorizations = Sets.newHashSet(readerParams.getAdditionalAuthorizations()); stream = stream.filter(new ClientVisibilityFilter(authorizations)); iterator = stream.iterator(); } return new RowReaderWrapper<>(new CloseableIterator.Wrapper(iterator)); }
Example #26
Source File: FeesAndCredits.java From nomulus with Apache License 2.0 | 5 votes |
/** Returns the total cost of all fees and credits for the event. */ public Money getTotalCost() { return Money.of( currency, Streams.concat(fees.stream(), credits.stream()) .map(BaseFee::getCost) .reduce(zeroInCurrency(currency), BigDecimal::add)); }
Example #27
Source File: ExecCompatibleCommandLineBuilder.java From buck with Apache License 2.0 | 5 votes |
@Override public CommandLine build(CommandLineArgs commandLineArgs) { ImmutableSortedMap<String, String> env = commandLineArgs.getEnvironmentVariables(); ImmutableList.Builder<String> builder = ImmutableList.builderWithExpectedSize(commandLineArgs.getEstimatedArgsCount()); Streams.mapWithIndex( commandLineArgs.getArgsAndFormatStrings(), (o, i) -> CommandLineArgStringifier.asString(filesystem, i == 0, o)) .forEach(builder::add); return ImmutableCommandLine.of(env, builder.build()); }
Example #28
Source File: EntityManagerImpl.java From molgenis with GNU Lesser General Public License v3.0 | 5 votes |
/** * Return all resolvable attributes: non-computed reference attributes defined in fetch * * @param entityType entity meta data * @param fetch entity fetch * @return resolved attributes */ private static List<Attribute> getResolvableAttrs(EntityType entityType, Fetch fetch) { return Streams.stream(entityType.getAtomicAttributes()) .filter(EntityTypeUtils::isReferenceType) .filter(attr -> attr.getExpression() == null) .filter(attr -> fetch.hasField(attr.getName())) .collect(Collectors.toList()); }
Example #29
Source File: HashCodeSerializerTest.java From exonum-java-binding with Apache License 2.0 | 5 votes |
private static Stream<HashCode> testHashes() { // Hash codes of zeros of various length Stream<HashCode> zeroHashCodes = IntStream.of(1, 2, 16, 32) .mapToObj(byte[]::new) .map(HashCode::fromBytes); // Non-zero 32-byte SHA-256 hash codes Stream<HashCode> sha256HashCodes = Stream.of( "", "a", "hello" ).map(s -> Hashing.sha256().hashString(s, StandardCharsets.UTF_8)); return Streams.concat(zeroHashCodes, sha256HashCodes); }
Example #30
Source File: BackupPaths.java From nomulus with Apache License 2.0 | 5 votes |
/** * Returns an {@link ImmutableList} of regex patterns that match all Datastore export files of the * given {@code kinds}. * * @param exportDir path to the top directory of a Datastore export * @param kinds all entity 'kinds' to be matched */ public static ImmutableList<String> getExportFilePatterns( String exportDir, Iterable<String> kinds) { checkNotNull(kinds, "kinds"); return Streams.stream(kinds) .map(kind -> getExportFileNamePattern(exportDir, kind)) .collect(ImmutableList.toImmutableList()); }