本文整理汇总了Java中java.util.Optional.flatMap方法的典型用法代码示例。如果您正苦于以下问题:Java Optional.flatMap方法的具体用法?Java Optional.flatMap怎么用?Java Optional.flatMap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Optional
的用法示例。
在下文中一共展示了Optional.flatMap方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNodeIdFromIdOrName
import java.util.Optional; //导入方法依赖的package包/类
/**
* Convenience method for retrieving the id of a Node given the id or the name of the node, and
* the id of the cluster and the datacenter it belongs to
*
* @param clusterId id of the cluster the DataCenter belongs to
* @param datacenterId id of the datacenter the DataCenter belongs to
* @param idOrName string which could be the id or the name of the Node
* @return id of the cluster
*/
static Optional<Long> getNodeIdFromIdOrName(
Server server, Long clusterId, Long datacenterId, String idOrName) {
Optional<BoundDataCenter> dc =
server
.getCluster(clusterId)
.getDataCenters()
.stream()
.filter(d -> d.getId().equals(datacenterId))
.findAny();
return dc.flatMap(
d ->
d.getNodes()
.stream()
.filter(n -> n.getName().equals(idOrName) || n.getId().toString().equals(idOrName))
.findAny()
.map(AbstractNodeProperties::getId));
}
示例2: getDisplayName
import java.util.Optional; //导入方法依赖的package包/类
private DisplayName getDisplayName(TestIdentifier testIdentifier) {
LinkedList<String> names = new LinkedList<>();
Optional<TestIdentifier> id = Optional.of(testIdentifier);
do {
TestIdentifier identifier = id.get();
Optional<ClassSource> classSource = identifier.getSource()
.filter(source -> source instanceof ClassSource)
.map(source -> (ClassSource) source)
.filter(source -> !source.getPosition().isPresent())
.filter(source -> classesToSkip.contains(source.getJavaClass()));
if (classSource.isPresent()) {
break;
}
names.addFirst(identifier.getDisplayName());
id = id.flatMap(testPlan::getParent);
} while (id.isPresent());
return DisplayName.create(names);
}
示例3: getContextModuleTypes
import java.util.Optional; //导入方法依赖的package包/类
/**
* Returns module types that are present on the given context or any of its enclosing contexts.
*/
private static Set<Class<? extends Module>> getContextModuleTypes(
Optional<ExtensionContext> context) {
// TODO: Cache?
Set<Class<? extends Module>> contextModuleTypes = new LinkedHashSet<>();
while (context.isPresent() && (hasAnnotatedElement(context) || hasParent(context))) {
context
.flatMap(ExtensionContext::getElement)
.map(GuiceExtension::getModuleTypes)
.ifPresent(contextModuleTypes::addAll);
context = context.flatMap(ExtensionContext::getParent);
}
return contextModuleTypes;
}
示例4: getFieldsWriter
import java.util.Optional; //导入方法依赖的package包/类
/**
* Returns the {@link FieldsWriter} for a given model.
*
* @param singleModel the single model
* @param embeddedPathElements the embedded path element list
* @param requestInfo the current request's information
* @param pathFunction the function to get the {@link Path}
* @param representorFunction the function to get the {@link Representor}
* @return the {@code FieldsWriter} for the model
*/
public static <T> Optional<FieldsWriter<T, ?>> getFieldsWriter(
SingleModel<T> singleModel, FunctionalList<String> embeddedPathElements,
RequestInfo requestInfo, PathFunction pathFunction,
RepresentorFunction representorFunction) {
Optional<Representor<T, ?>> representorOptional =
getRepresentorOptional(
singleModel.getModelClass(), representorFunction);
Optional<Path> pathOptional = getPathOptional(
singleModel, pathFunction, representorFunction);
return representorOptional.flatMap(
representor -> pathOptional.map(
path -> new FieldsWriter<>(
singleModel, requestInfo, representor, path,
embeddedPathElements)));
}
示例5: getOptionalInitParamValue
import java.util.Optional; //导入方法依赖的package包/类
static <T> Optional<T> getOptionalInitParamValue(String name, Map<String, ?> initParams,
Function<String, T> converter) throws UnavailableException
{
Optional<String> value = getSingleValue(name, initParams);
return value.flatMap(s -> Optional.ofNullable(converter.apply(s)));
}
示例6: getDiffs
import java.util.Optional; //导入方法依赖的package包/类
public static TableAction getDiffs(Table table, Schema schema, Database existingDatabase, Log log) {
Optional<Schema> existingSchemaOpt = existingDatabase.findSchema(schema.getName());
Optional<Table> existingTableOpt = existingSchemaOpt.flatMap(existingSchema -> existingSchema.findTable(table.getName()));
if (!existingTableOpt.isPresent()) {
return TableAction.create(table, existingSchemaOpt);
} else if (!existingTableOpt.get().equals(table)) {
return TableAction.update(table, existingSchemaOpt);
}
return TableAction.noAction(table);
}
示例7: toString
import java.util.Optional; //导入方法依赖的package包/类
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Beachline(");
if (getRoot() != null) {
Optional<LeafBeachNode> current = Optional.of(getRoot().getLeftmostLeaf());
while (current.isPresent()) {
sb.append(current.get().getSite()).append(',');
current = current.flatMap(LeafBeachNode::getRightNeighbor);
}
}
sb.append(")");
return sb.toString();
}
示例8: getRegionFromConfigFile
import java.util.Optional; //导入方法依赖的package包/类
private static Optional<String> getRegionFromConfigFile(File file, String profile) {
AWSCLIConfigFile configFile = new AWSCLIConfigFile(file);
AWSCLIConfigFile.Config config = configFile.getConfig();
Optional<AWSCLIConfigFile.Section> profileSection = config.getSection(profile);
// Legacy fallback
if (!profileSection.isPresent()) {
profileSection = config.getSection("profile " + profile);
}
return profileSection.flatMap(s -> s.getProperty("region"));
}
示例9: getPathOptional
import java.util.Optional; //导入方法依赖的package包/类
/**
* Returns a model's {@link Path}, if the model's {@code Representor} and
* {@code Path} exist. Otherwise, this method returns {@code
* Optional#empty()}.
*
* @param singleModel the single model
* @param pathFunction the function that gets the {@code Path}
* @param representorFunction the function that gets the {@code
* Representor}
* @return the model's {@code Path}, if the model's {@code Representor} and
* {@code Path} exist; returns {@code Optional#empty()} otherwise
*/
public static <T> Optional<Path> getPathOptional(
SingleModel<T> singleModel, PathFunction pathFunction,
RepresentorFunction representorFunction) {
Optional<Representor<T, ?>> optional = getRepresentorOptional(
singleModel.getModelClass(), representorFunction);
return optional.flatMap(
representor -> pathFunction.apply(
representor.getIdentifier(singleModel.getModel()),
representor.getIdentifierClass(), singleModel.getModelClass()));
}
示例10: find
import java.util.Optional; //导入方法依赖的package包/类
public Optional<Relationship> find(RastDiff diff) {
RastRoot before = diff.getBefore();
RastRoot after = diff.getAfter();
Optional<RastNode> oNodeBefore = RastRootHelper.findByNamePath(before, nodeQBefore.namePath);
Optional<RastNode> oNodeAfter = RastRootHelper.findByNamePath(after, nodeQAfter.namePath);
return oNodeBefore.flatMap(nodeBefore -> oNodeAfter.flatMap(nodeAfter -> {
Relationship r = new Relationship(type, nodeBefore, nodeAfter);
if (diff.getRelationships().contains(r)) {
return Optional.of(r);
}
return Optional.empty();
}));
}
示例11: indicate
import java.util.Optional; //导入方法依赖的package包/类
@Override
public Optional<Ratio> indicate(final C marketPrice) {
final Optional<Price> optDm = directionalMovement.indicate(marketPrice);
final Optional<Price> optTr = trueRange.indicate(marketPrice);
return optDm.flatMap(dm -> optTr.map(tr -> tr.asPipette() == 0 ? ZERO : new Ratio(dm.divide(tr).asBasic())));
}
示例12: indicate
import java.util.Optional; //导入方法依赖的package包/类
@Override
public Optional<Ratio> indicate(final C marketPrice) {
final Optional<Ratio> optPlusDi = averagePlusDi.indicate(marketPrice);
final Optional<Ratio> optMinusDi = averageMinusDi.indicate(marketPrice);
return optPlusDi.flatMap(plusDi -> optMinusDi.map(minusDi -> new Ratio(
abs((plusDi.asBasic() - minusDi.asBasic()) / (plusDi.asBasic() + minusDi.asBasic())))));
}
示例13: lift
import java.util.Optional; //导入方法依赖的package包/类
static <R, T, Z> Optional<Z> lift(Optional<T> left, Optional<R> right,
BiFunction<? super T, ? super R, ? extends Z> function) {
return left.flatMap(leftVal -> right.map(rightVal -> function.apply(leftVal, rightVal)));
}
示例14: create
import java.util.Optional; //导入方法依赖的package包/类
public static @NotNull Optional<CollectorServer> create(@NotNull Optional<ServerParameters> parameters) {
return parameters
.flatMap(params -> CollectorServer.create(params.config())
.map(server -> server.init(params.address(), params.format())));
}
示例15: castTo
import java.util.Optional; //导入方法依赖的package包/类
private static <T> Optional<Converted<T>> castTo(Class<T> type, Object instance) {
Optional<List<Class<?>>> matchingCastPath = Optional.ofNullable(TYPECAST_PATHS_MAP.get(Pair.of(instance.getClass(), type)));
return matchingCastPath.flatMap(path -> castTo(type, instance, path));
}