下面列出了怎么用com.google.common.collect.FluentIterable的API类实例代码及写法,或者点击链接到github查看源代码。
@VisibleForTesting
static AttributeAggregate create(
final Supplier<Iterable<IScheduledTask>> taskSupplier,
final AttributeStore attributeStore) {
final Function<String, Iterable<IAttribute>> getHostAttributes =
host -> {
// Note: this assumes we have access to attributes for hosts where all active tasks
// reside.
requireNonNull(host);
return attributeStore.getHostAttributes(host).get().getAttributes();
};
return create(Suppliers.compose(
tasks -> FluentIterable.from(tasks)
.transform(Tasks::scheduledToSlaveHost)
.transformAndConcat(getHostAttributes),
taskSupplier));
}
private ResponseLinking linkResponses(final Symbol docId,
final Iterable<Response> responses) {
final Predicate<Response> HasRelevantRealis =
compose(in(realisesWhichMustBeAligned), ResponseFunctions.realis());
final ImmutableSet<Response> systemResponsesAlignedRealis =
FluentIterable.from(responses).filter(HasRelevantRealis).toSet();
final Multimap<Symbol, Response> responsesByEventType =
Multimaps.index(systemResponsesAlignedRealis, ResponseFunctions.type());
final ImmutableSet.Builder<ResponseSet> ret = ImmutableSet.builder();
for (final Collection<Response> responseSet : responsesByEventType.asMap().values()) {
ret.add(ResponseSet.from(responseSet));
}
return ResponseLinking.builder().docID(docId).addAllResponseSets(ret.build()).build();
}
public static Iterable<String> getArrayFieldOrEnv(JsonObject config, String name, Iterable<String> defaultValue) {
String envVar = formatEnvVariable(name);
//USED_ENV_VARS.add(envVar);
if (config.containsKey(name)) {
JsonArray values = config.getJsonArray(name);
if (values != null) {
Iterable<String> iterable =
FluentIterable.from(values)
.filter(Predicates.notNull())
.filter(input -> input instanceof String)
.transform(input -> input.toString());
log(name, envVar, name, iterable);
return iterable;
}
} else {
String value = System.getenv(envVar);
if (value != null) {
log(name, envVar, envVar, value);
return Splitter.on(',').split(value);
}
}
log(name, envVar, null, defaultValue);
return defaultValue;
}
@Override
public String toString() {
Iterable<String> errorMessages = FluentIterable.from(errors).transform(OBJECT_ERROR_TO_TABBED_MESSAGE);
StringBuilder help = new StringBuilder(500)
.append("Usage: check-filters.sh --config=<config_file>[,<config_file>,...]")
.append(System.lineSeparator())
.append("Errors found in the provided configuration file:")
.append(System.lineSeparator())
.append(Joiner.on(System.lineSeparator()).join(errorMessages))
.append(System.lineSeparator())
.append("Configuration file help:")
.append(System.lineSeparator())
.append(TAB)
.append("For more information and help please refer to ")
.append("https://github.com/HotelsDotCom/circus-train/tree/master/circus-train-tool-parent");
return help.toString();
}
@Override
public String toString() {
Iterable<String> errorMessages = FluentIterable.from(errors).transform(OBJECT_ERROR_TO_TABBED_MESSAGE);
StringBuilder help = new StringBuilder(500)
.append("Usage: compare-tables.sh --config=<config_file>[,<config_file>,...] --"
+ ComparisonToolArgs.OUTPUT_FILE
+ "=<output_file> [--"
+ ComparisonToolArgs.SOURCE_PARTITION_BATCH_SIZE
+ "=1000] [--"
+ ComparisonToolArgs.REPLICA_PARTITION_BUFFER_SIZE
+ "=1000]")
.append(System.lineSeparator())
.append("Errors found in the provided configuration file:")
.append(System.lineSeparator())
.append(Joiner.on(System.lineSeparator()).join(errorMessages))
.append(System.lineSeparator())
.append("Configuration file help:")
.append(System.lineSeparator())
.append(TAB)
.append("For more information and help please refer to ")
.append("https://github.com/HotelsDotCom/circus-train/blob/master/circus-train-tool/"
+ "circus-train-comparison-tool/README.md");
return help.toString();
}
public int getMedianMaxBatchSize() {
if (diskRuns.size() == 0) {
return 0;
}
return FluentIterable.from(diskRuns)
.transform(new Function<DiskRun, Integer>() {
@Override
public Integer apply(DiskRun diskRun) {
return diskRun.largestBatch;
}
})
.toSortedList(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1, o2);
}
})
.get(diskRuns.size() / 2);
}
private void createHashCode(List<FieldSpec> specs, TypeSpec.Builder incoming) {
String fields = FluentIterable.from(specs).transform(new Function<FieldSpec, String>() {
@Nullable
@Override
public String apply(@Nullable FieldSpec fieldSpec) {
return fieldSpec.name;
}
}).join(Joiner.on(","));
MethodSpec.Builder method = MethodSpec.methodBuilder("hashCode")
.addAnnotation(ClassName.get(Override.class)).addModifiers(Modifier.PUBLIC)
.returns(TypeName.INT)
.addCode(CodeBlock.builder().addStatement("return $T.hash($L)", Objects.class, fields).build());
incoming.addMethod(
method.build()
);
}
protected CachedField[] transform(final CachedField[] fields) {
return FluentIterable
.from(Arrays.asList(fields))
.transform(new Function<CachedField, CachedField>() {
@Nullable
@Override
public CachedField apply(@Nullable final CachedField field) {
final CachedField newField = mapping.findInjection(field.getField().getType())
.transform(new Function<Injection, CachedField>() {
@Nullable
@Override
public CachedField apply(@Nullable final Injection injection) {
return new InjectingCachedField(field, injection.getValue());
}
})
.or(field);
return newField;
}
})
.toArray(CachedField.class);
}
@Test(groups = "Integration")
public void testStartTomcatCluster() throws Exception {
Entity app = createAndStartApplication(loadYaml("test-tomcat-cluster.yaml"));
waitForApplicationTasks(app);
assertNotNull(app);
assertEquals(app.getChildren().size(), 1);
final Entity entity = Iterables.getOnlyElement(app.getChildren());
assertTrue(entity instanceof ControlledDynamicWebAppCluster, "entity="+entity);
ControlledDynamicWebAppCluster cluster = (ControlledDynamicWebAppCluster) entity;
assertTrue(cluster.getController() instanceof NginxController, "controller="+cluster.getController());
Iterable<TomcatServer> tomcats = FluentIterable.from(cluster.getCluster().getMembers()).filter(TomcatServer.class);
assertEquals(Iterables.size(tomcats), 2);
for (TomcatServer tomcat : tomcats) {
assertTrue(tomcat.getAttribute(TomcatServer.SERVICE_UP), "serviceup");
}
EntitySpec<?> spec = entity.getConfig(DynamicCluster.MEMBER_SPEC);
assertNotNull(spec);
assertEquals(spec.getType(), TomcatServer.class);
assertEquals(spec.getConfig().get(DynamicCluster.QUARANTINE_FAILED_ENTITIES), Boolean.FALSE);
assertEquals(spec.getConfig().get(DynamicCluster.INITIAL_QUORUM_SIZE), 2);
}
private void createHashCode(List<FieldSpec> specs, TypeSpec.Builder incoming) {
String fields = FluentIterable.from(specs).transform(new Function<FieldSpec, String>() {
@Nullable
@Override
public String apply(@Nullable FieldSpec fieldSpec) {
return fieldSpec.name;
}
}).join(Joiner.on(","));
MethodSpec.Builder method = MethodSpec.methodBuilder("hashCode")
.addAnnotation(ClassName.get(Override.class)).addModifiers(Modifier.PUBLIC)
.returns(TypeName.INT)
.addCode(CodeBlock.builder().addStatement("return $T.hash($L)", Objects.class, fields).build());
incoming.addMethod(
method.build()
);
}
/**
* Warns about CAS offsets for Responses being inconsistent with actual document text for non-TIME
* roles
*/
private void warnOnMissingOffsets(final File systemOutputStoreFile, final Symbol docID,
final ImmutableSet<Response> responses,
final Map<Symbol, File> docIDMap) throws IOException {
final String text = Files.asCharSource(docIDMap.get(docID), Charsets.UTF_8).read();
for (final Response r : FluentIterable.from(responses)
.filter(Predicates.compose(not(equalTo(TIME)), ResponseFunctions.role()))) {
final KBPString cas = r.canonicalArgument();
final String casTextInRaw =
resolveCharOffsets(cas.charOffsetSpan(), docID, text).replaceAll("\\s+", " ");
// allow whitespace
if (!casTextInRaw.contains(cas.string())) {
log.warn("Warning for {} - response {} CAS does not match text span of {} ",
systemOutputStoreFile.getAbsolutePath(), renderResponse(r, text), casTextInRaw);
}
}
}
@Override
public void start() {
FluentIterable<NodeOperation> nodeOperations = FluentIterable.from(nodeOperationTrees)
.transformAndConcat(new Function<NodeOperationTree, Iterable<? extends NodeOperation>>() {
@Nullable
@Override
public Iterable<? extends NodeOperation> apply(NodeOperationTree input) {
return input.nodeOperations();
}
});
Map<String, Collection<NodeOperation>> operationByServer = NodeOperationGrouper.groupByServer(nodeOperations);
InitializationTracker initializationTracker = new InitializationTracker(operationByServer.size());
List<Tuple<ExecutionPhase, RowReceiver>> handlerPhases = createHandlerPhases(initializationTracker);
try {
setupContext(operationByServer, handlerPhases, initializationTracker);
} catch (Throwable throwable) {
for (SettableFuture<TaskResult> result : results) {
result.setException(throwable);
}
}
}
@Override public List<String> getNamespaces() throws MetaStoreException {
return MetaStoreUtil.executeLockedOperation( readLock(), new Callable<List<String>>() {
@Override public List<String> call() throws Exception {
return FluentIterable.from( attributesInterface.getAttributesMap().keySet() )
.filter( new Predicate<String>() {
@Override public boolean apply( String groupName ) {
return groupName.startsWith( METASTORE_PREFIX );
}
} )
.transform( new Function<String, String>() {
@Override public String apply( String input ) {
return input.substring( METASTORE_PREFIX.length() );
}
} )
.toList();
}
} );
}
@Action(semantics = SemanticsOf.SAFE, restrictTo = RestrictTo.PROTOTYPING)
public List<BadTarget> findBadCommandTargets() {
Set<String> badObjectTypes = Sets.newTreeSet();
List<Map<String, Object>> rows = isisJdoSupport
.executeSql("select distinct(substring(target, 1, charindex(':', target)-1)) as objectType from isiscommand.Command order by 1");
for (Map<String, Object> row : rows) {
String targetStr = (String) row.get("objectType");
addIfBad(badObjectTypes, targetStr);
}
return Lists.newArrayList(
FluentIterable.from(badObjectTypes)
.transform(x -> new BadTarget(x))
.toList());
}
/**
* Collect resources from recursive dependencies.
*
* @param targetGraph The {@link TargetGraph} containing the node and its dependencies.
* @param targetNode {@link TargetNode} at the tip of the traversal.
* @return The recursive resource buildables.
*/
public static ImmutableSet<AppleResourceDescriptionArg> collectRecursiveResources(
XCodeDescriptions xcodeDescriptions,
TargetGraph targetGraph,
Optional<AppleDependenciesCache> cache,
TargetNode<?> targetNode,
RecursiveDependenciesMode mode) {
return FluentIterable.from(
AppleBuildRules.getRecursiveTargetNodeDependenciesOfTypes(
xcodeDescriptions,
targetGraph,
cache,
mode,
targetNode,
ImmutableSet.of(AppleResourceDescription.class)))
.transform(input -> (AppleResourceDescriptionArg) input.getConstructorArg())
.toSet();
}
@Value.Derived
@Value.Auxiliary
public Set<EncodingInfo> encodings() {
if (qualifiedName().endsWith("Enabled")
|| CustomImmutableAnnotations.annotations().contains(qualifiedName())
|| style().isPresent()) {
// See if it is encoding enabled itself
Optional<EncodingInfo> encoding = EncMetadataMirror.find(element()).transform(ENCODING_INFLATER);
if (encoding.isPresent()) {
return encoding.asSet();
}
// trying to find it as meta-meta annotation
List<EncodingInfo> result = new ArrayList<>();
for (AnnotationMirror m : element().getAnnotationMirrors()) {
MetaAnnotated metaAnnotated = MetaAnnotated.from(m, environment());
result.addAll(metaAnnotated.encodings());
}
if (!result.isEmpty()) {
return FluentIterable.from(result).toSet();
}
}
return ImmutableSet.of();
}
private String getExpectedLocalizedXLIFFContent(final String targetLocaleBcp47Tag,
Pair<TMTextUnit, TMTextUnitVariant>... tmTextUnitsWithVariants) {
Function<Pair<TMTextUnit, TMTextUnitVariant>, TextUnit> toTextUnitFunction = new Function<Pair<TMTextUnit, TMTextUnitVariant>, TextUnit>() {
@Override
public TextUnit apply(Pair<TMTextUnit, TMTextUnitVariant> input) {
return createTextUnitFromTmTextUnitsWithVariant(targetLocaleBcp47Tag, input.getLeft(), input.getRight());
}
};
List<Pair<TMTextUnit, TMTextUnitVariant>> tmTextUnitWithVariantList = Arrays.asList(tmTextUnitsWithVariants);
List<TextUnit> textUnitList = FluentIterable.from(tmTextUnitWithVariantList).transform(toTextUnitFunction).toList();
return xliffDataFactory.generateTargetXliff(textUnitList, targetLocaleBcp47Tag);
}
private void assertParsingFailure(final String path, int configVersion,
String... expectedErrors) {
SimpleDiagCollector diag = new SimpleDiagCollector();
new HttpTemplateParser(diag, TEST_LOCATION, path, configVersion).parse();
List<Diag> expectedDiagErrors = FluentIterable.from(Arrays.asList(expectedErrors))
.transform(new Function<String, Diag>() {
@Override public Diag apply(String input) {
return Diag.error(TEST_LOCATION, "In path template '" + path + "': " + input);
}}).toList();
List<Diag> actualErrors = diag.getErrors();
Assert.assertEquals(expectedDiagErrors, actualErrors);
}
private static List<String> getFieldNames( Iterable<Field> fields ) {
return FluentIterable.from( ReflectionUtil.getAllFieldNames( fields ) ).transform( new Function<String, String>() {
@Override
public String apply( String input ) {
return input.replace( '_', ' ' );
}
} ).toList();
}
@Override
public Set<Class<? super T>> rawTypes() {
// Java has no way to express ? super T when we parameterize TypeToken vs. Class.
@SuppressWarnings({"unchecked", "rawtypes"})
ImmutableList<Class<? super T>> collectedTypes = (ImmutableList) TypeCollector.FOR_RAW_TYPE.collectTypes(getRawTypes());
return FluentIterable.from(collectedTypes).filter(new Predicate<Class<?>>() {
@Override
public boolean apply(Class<?> type) {
return type.isInterface();
}
}).toSet();
}
private List<ReflectionMeasureField> toMeasureFields(List<NameAndMeasures> fields){
if(fields == null){
return ImmutableList.of();
}
return FluentIterable.from(fields).transform(new Function<NameAndMeasures, ReflectionMeasureField>(){
@Override
public ReflectionMeasureField apply(NameAndMeasures input) {
return new ReflectionMeasureField(input.getName())
.setMeasureTypeList(input.getMeasureTypes().stream()
.map(AccelerationManagerImpl::toMeasureType)
.collect(Collectors.toList()));
}}).toList();
}
@Override
public String getMessageTemplate(StreamingSourceConfig streamingSourceConfig) {
String template = null;
KafkaConsumer<byte[], byte[]> consumer = null;
try {
String topicName = getTopicName(streamingSourceConfig.getProperties());
Map<String, Object> config = getKafkaConf(streamingSourceConfig.getProperties());
consumer = new KafkaConsumer<>(config);
Set<TopicPartition> partitions = Sets.newHashSet(FluentIterable.from(consumer.partitionsFor(topicName))
.transform(new Function<PartitionInfo, TopicPartition>() {
@Override
public TopicPartition apply(PartitionInfo input) {
return new TopicPartition(input.topic(), input.partition());
}
}));
consumer.assign(partitions);
consumer.seekToBeginning(partitions);
ConsumerRecords<byte[], byte[]> records = consumer.poll(500);
if (records == null) {
return null;
}
Iterator<ConsumerRecord<byte[], byte[]>> iterator = records.iterator();
if (iterator == null || !iterator.hasNext()) {
return null;
}
ConsumerRecord<byte[], byte[]> record = iterator.next();
template = new String(record.value(), "UTF8");
} catch (Exception e) {
logger.error("error when fetch one record from kafka, stream:" + streamingSourceConfig.getName(), e);
} finally {
if (consumer != null) {
consumer.close();
}
}
return template;
}
/**
* Fetches first matching document. If none of the documents matches, then
* {@link Optional#absent()} will be returned.
* @return future of optional matching document
*/
public final FluentFuture<Optional<T>> fetchFirst() {
return fetchWithLimit(1).transform(new Function<List<T>, Optional<T>>() {
@Override
public Optional<T> apply(List<T> input) {
return FluentIterable.from(input).first();
}
});
}
@Override
public List<LocationSummary> list() {
if (!Entitlements.isEntitled(mgmt().getEntitlementManager(), Entitlements.SEE_LOCATION, Entitlements.StringAndArgument.of("list locations", "see"))) {
throw WebResourceUtils.forbidden("User '%s' is not authorized to see locations",
Entitlements.getEntitlementContext().user());
}
Function<LocationDefinition, LocationSummary> transformer = new Function<LocationDefinition, LocationSummary>() {
@Override
public LocationSummary apply(LocationDefinition l) {
try {
return LocationTransformer.newInstance(mgmt(), l, LocationDetailLevel.LOCAL_EXCLUDING_SECRET, ui.getBaseUriBuilder());
} catch (Exception e) {
Exceptions.propagateIfFatal(e);
String spec = l.getSpec();
if (spec == null || specsWarnedOnException.add(spec)) {
log.warn("Unable to find details of location {} in REST call to list (ignoring location): {}", l, e);
if (log.isDebugEnabled()) log.debug("Error details for location " + l, e);
} else {
if (log.isTraceEnabled())
log.trace("Unable again to find details of location {} in REST call to list (ignoring location): {}", l, e);
}
return null;
}
}
};
return FluentIterable.from(brooklyn().getLocationRegistry().getDefinedLocations(true).values())
.transform(transformer)
.filter(LocationSummary.class)
.toSortedList(nameOrSpecComparator());
}
@Override
public <T extends Connectable> Iterable<T> getConnectables(Class<T> clazz) {
Iterable<Terminal> terminals = getTerminals();
return FluentIterable.from(terminals)
.transform(Terminal::getConnectable)
.filter(clazz)
.toSet();
}
public static TypeVariableName fromTypeParameterElement(TypeParameterElement element) {
// We filter out bounds of type Object because those would just clutter the generated code.
Iterable<? extends TypeName> bounds =
FluentIterable.from(element.getBounds())
.filter(new Predicate<TypeMirror>() {
@Override public boolean apply(TypeMirror input) {
return !MoreTypes.isType(input) || !MoreTypes.isTypeOf(Object.class, input);
}
})
.transform(TypeNames.FOR_TYPE_MIRROR);
return new TypeVariableName(element.getSimpleName(), bounds);
}
@Override
protected Set<TypeToken<? super T>> delegate() {
ImmutableSet<TypeToken<? super T>> filteredTypes = types;
if (filteredTypes == null) {
// Java has no way to express ? super T when we parameterize TypeToken vs. Class.
@SuppressWarnings({"unchecked", "rawtypes"})
ImmutableList<TypeToken<? super T>> collectedTypes = (ImmutableList) TypeCollector.FOR_GENERIC_TYPE.collectTypes(TypeToken.this);
return (types =
FluentIterable.from(collectedTypes).filter(TypeFilter.IGNORE_TYPE_VARIABLE_OR_WILDCARD).toSet());
} else {
return filteredTypes;
}
}
public <V> Set<V> keysOfType(Class<V> keyType) {
ImmutableSet.Builder<V> keys = ImmutableSet.builder();
if (parent != null) {
keys.addAll(parent.keysOfType(keyType));
}
keys.addAll(FluentIterable.from(entries.keySet()).filter(keyType).toSet());
return keys.build();
}
private WrittenJarFile writeJarWithManifestClasspathAttribute(final File folder, String identifier, ManifestClasspathEntry... additionalClasspathManifestClasspathEntries) {
Set<ManifestClasspathEntry> classpathManifestEntries = union(createManifestClasspathEntries(identifier), ImmutableSet.copyOf(additionalClasspathManifestClasspathEntries));
JarFile jarFile = new TestJarFile()
.withManifestAttribute(CLASS_PATH, Joiner.on(" ").join(FluentIterable.from(classpathManifestEntries).transform(resolveTo(folder)).toSet()))
.create(new File(folder, identifier.replace(File.separator, "-") + ".jar"));
return new WrittenJarFile(Paths.get(jarFile.getName()), classpathManifestEntries);
}
private ImmutableList<Path> sortDependency(
String depOutput, ImmutableSet<Path> mlInput) { // NOPMD doesn't understand method reference
OcamlDependencyGraphGenerator graphGenerator = new OcamlDependencyGraphGenerator();
return FluentIterable.from(graphGenerator.generate(depOutput))
.transform(Paths::get)
// The output of generate needs to be filtered as .cmo dependencies
// are generated as both .ml and .re files.
.filter(mlInput::contains)
.toList();
}