下面列出了怎么用com.google.common.collect.Streams的API类实例代码及写法,或者点击链接到github查看源代码。
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();
}
@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());
}
@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);
}
/**
* 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());
}
@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;
}
}
@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);
}
/**
* 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));
}
@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())));
}
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;
}
/** 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();
}
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();
}
@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));
}
}
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());
}
/**
* 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)));
}
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;
}
/**
* 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);
}
}
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);
}
@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();
}
}
private static Set<Symbol> getJoinRequiredSymbols(JoinNode node)
{
// extract symbols required by the join itself
return Streams.concat(
node.getCriteria().stream().map(JoinNode.EquiJoinClause::getLeft),
node.getCriteria().stream().map(JoinNode.EquiJoinClause::getRight),
node.getFilter().map(SymbolsExtractor::extractUnique).orElse(ImmutableSet.of()).stream(),
node.getLeftHashSymbol().map(ImmutableSet::of).orElse(ImmutableSet.of()).stream(),
node.getRightHashSymbol().map(ImmutableSet::of).orElse(ImmutableSet.of()).stream())
.collect(toImmutableSet());
}
@TaskAction
public void exec() {
var appPluginConvention =
getProject().getConvention().getPlugin(ApplicationPluginConvention.class);
String classpathArg =
Streams.concat(classpath.getFiles().stream(), Stream.of(jarFile.getAsFile().get()))
.map(File::getAbsolutePath)
.collect(Collectors.joining(":"));
getProject()
.exec(
exec -> {
exec.executable(
DownloadedToolManager.get(getProject())
.getBinDir("graalvm")
.resolve("native-image"));
var args = new ImmutableList.Builder<String>();
args.add(
"-cp",
classpathArg,
"-H:Path=" + outputDir.getAsFile().get().getAbsolutePath(),
"-H:name=" + outputName.get())
.addAll(options.get())
.add(appPluginConvention.getMainClassName());
exec.args(args);
});
}
@Test
public void createStreamsWithOptionalLong() {
LongStream streamFromOptionalLong = Streams.stream(OptionalLong.of(1));
assertNotNull(streamFromOptionalLong);
assertEquals(streamFromOptionalLong.count(), 1);
}
@Test
public void rowNumberIteratorWithNoSelection() {
TableSlice tableSlice = new TableSlice(source);
Integer[] expected = new Integer[] {53, 53, 58, 52, 52};
Integer[] actual =
Streams.stream(tableSlice.sourceRowNumberIterator())
.map(i -> source.column("approval").get(i))
.limit(5)
.toArray(Integer[]::new);
assertArrayEquals(expected, actual);
}
private Stream<JsonResponse<QueryResults>> postQuery(Function<Request.Builder, Request.Builder> requestConfigurer)
{
Request.Builder request = preparePost()
.setUri(uriFor("/v1/statement"))
.setHeader(PRESTO_USER, "user")
.setHeader(PRESTO_SOURCE, "source");
request = requestConfigurer.apply(request);
JsonResponse<QueryResults> queryResults = client.execute(request.build(), createFullJsonResponseHandler(QUERY_RESULTS_CODEC));
return Streams.stream(new QueryResultsIterator(client, queryResults));
}
/**
* Creates a stream of map entries whose elements are those of the first stream followed by those of the second
* stream.
*
* @param a the first stream of entries
* @param b the second stream of entries
* @param <K> the key type
* @param <V> the value type
* @return the concatenation of the two input streams
*/
public static <K, V> MapStream<K, V> concat(
MapStream<? extends K, ? extends V> a,
MapStream<? extends K, ? extends V> b) {
@SuppressWarnings("unchecked")
MapStream<K, V> kvMapStream = new MapStream<>(Streams.concat(
(Stream<? extends Map.Entry<K, V>>) a,
(Stream<? extends Map.Entry<K, V>>) b));
return kvMapStream;
}
@Test
public void buildHelperCreateJavaInfoWithJdeps_javaRuleOutputJarsProvider() throws Exception {
ruleBuilder().build();
scratch.file(
"foo/BUILD",
"load(':extension.bzl', 'my_rule')",
"java_library(name = 'my_java_lib_direct', srcs = ['java/A.java'])",
"my_rule(",
" name = 'my_starlark_rule',",
" output_jar = 'my_starlark_rule_lib.jar',",
" source_jars = ['my_starlark_rule_src.jar'],",
" dep = [':my_java_lib_direct'],",
" jdeps = 'my_jdeps.pb',",
")");
assertNoEvents();
JavaRuleOutputJarsProvider ruleOutputs =
fetchJavaInfo().getProvider(JavaRuleOutputJarsProvider.class);
assertThat(
prettyArtifactNames(
ruleOutputs.getOutputJars().stream()
.map(o -> o.getClassJar())
.collect(ImmutableList.toImmutableList())))
.containsExactly("foo/my_starlark_rule_lib.jar");
assertThat(
prettyArtifactNames(
ruleOutputs.getOutputJars().stream()
.flatMap(o -> Streams.stream(o.getSrcJars()))
.collect(ImmutableList.toImmutableList())))
.containsExactly("foo/my_starlark_rule_src.jar");
assertThat(ruleOutputs.getJdeps().prettyPrint()).isEqualTo("foo/my_jdeps.pb");
}
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));
}
@Test
public void streamsZipTest() {
Stream<String> stringSream = Stream.of("a", "b", "c");
Stream<Integer> intStream = Stream.of(1, 2, 3);
Stream<String> mappedStream = Streams.zip(stringSream, intStream, (str, index) -> str + ":" + index);
//Assert.assertNotNull(mappedStream);
assertEquals(mappedStream
.findFirst()
.get(), "a:1");
}
@Test
public void rowNumberIteratorWithSort() {
Selection selection = Selection.withRange(0, 5);
TableSlice tableSlice = new TableSlice(source, selection);
tableSlice.sortOn(Sort.on("approval", Order.ASCEND));
Integer[] expected = new Integer[] {52, 52, 53, 53, 58};
Integer[] actual =
Streams.stream(tableSlice.sourceRowNumberIterator())
.map(i -> source.column("approval").get(i))
.toArray(Integer[]::new);
assertArrayEquals(expected, actual);
}
/**
* 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());
}
@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());
}