Example usage for java.util Optional flatMap

List of usage examples for java.util Optional flatMap

Introduction

In this page you can find the example usage for java.util Optional flatMap.

Prototype

public <U> Optional<U> flatMap(Function<? super T, ? extends Optional<? extends U>> mapper) 

Source Link

Document

If a value is present, returns the result of applying the given Optional -bearing mapping function to the value, otherwise returns an empty Optional .

Usage

From source file:com.ikanow.aleph2.analytics.services.AnalyticsContext.java

/** Utility given the streaming/batch type to return the corresponding data service
 * @param bucket/*w ww. j  a v  a  2  s .com*/
 * @param type
 * @return
 */
protected Either<Either<IBatchSubservice<JsonNode>, IDataWriteService<JsonNode>>, String> getExternalEndpoint(
        final DataBucketBean bucket, MasterEnrichmentType type) {
    if (type.equals(MasterEnrichmentType.none)) {
        return null;
    } else if (type.equals(MasterEnrichmentType.streaming)
            || type.equals(MasterEnrichmentType.streaming_and_batch)) {
        return Either.right(_distributed_services.generateTopicName(bucket.full_name(),
                ICoreDistributedServices.QUEUE_START_NAME));
    } else { // batch
        final Optional<IDataWriteService<JsonNode>> crud_storage_service = _storage_service.getDataService()
                .flatMap(s -> s.getWritableDataService(JsonNode.class, bucket,
                        Optional.of(IStorageService.StorageStage.transient_input.toString()),
                        Optional.empty()));

        final Optional<IDataWriteService.IBatchSubservice<JsonNode>> batch_storage_service = crud_storage_service
                .flatMap(IDataWriteService::getBatchWriteSubservice);

        return batch_storage_service
                .<Either<Either<IBatchSubservice<JsonNode>, IDataWriteService<JsonNode>>, String>>map(
                        batch -> Either.left(Either.left(batch)))
                .orElseGet(() -> {
                    return crud_storage_service
                            .<Either<Either<IBatchSubservice<JsonNode>, IDataWriteService<JsonNode>>, String>>map(
                                    slow -> Either.left(Either.right(slow)))
                            .orElse(null);
                });
    }
}

From source file:net.sourceforge.pmd.util.fxdesigner.util.beans.SettingsPersistenceUtil.java

/**
 * Restores properties contained in the file into the given object.
 *
 * @param root Root of the hierarchy/*from w ww  .  jav  a  2s .  c o  m*/
 * @param file Properties file
 */
public static void restoreProperties(SettingsOwner root, File file) {
    Optional<Document> odoc = getDocument(file);

    odoc.flatMap(XmlFormatRevision::getSuitableReader).map(rev -> rev.xmlInterface)
            .flatMap(xmlInterface -> odoc.flatMap(xmlInterface::parseXml))
            .ifPresent(n -> restoreSettings(root, n));
}

From source file:org.apache.james.modules.objectstorage.SwiftKeystone3ConfigurationReader.java

private static Optional<Project> readProjectScope(Configuration configuration) {
    Optional<ProjectName> projectName = Optional
            .ofNullable(configuration.getString(OBJECTSTORAGE_SWIFT_KEYSTONE_3_PROJECT_NAME, null))
            .map(ProjectName::of);/*w  ww. j  ava  2s. c  o  m*/

    Optional<DomainName> projectDomainName = Optional
            .ofNullable(configuration.getString(OBJECTSTORAGE_SWIFT_KEYSTONE_3_PROJECT_DOMAIN_NAME, null))
            .map(DomainName::of);

    Optional<DomainId> projectDomainId = Optional
            .ofNullable(configuration.getString(OBJECTSTORAGE_SWIFT_KEYSTONE_3_PROJECT_DOMAIN_ID, null))
            .map(DomainId::of);

    return OptionalUtils.or(
            projectName.flatMap(project -> projectDomainName.map(domain -> Project.of(project, domain))),
            projectName.flatMap(project -> projectDomainId.map(domain -> Project.of(project, domain))),
            projectName.map(Project::of));
}

From source file:org.zanata.sync.plugin.zanata.util.PushPullOptionsUtil.java

public static Optional<File> findProjectConfig(File repoBase) {
    try {//from   www  .java  2 s .c o m
        Stream<Path> pathStream = Files.find(repoBase.toPath(), MAX_DEPTH,
                (path, basicFileAttributes) -> basicFileAttributes.isRegularFile()
                        && path.toFile().getName().equals("zanata.xml"));
        Optional<Path> projectConfig = pathStream.findFirst();
        return projectConfig.flatMap(path -> Optional.ofNullable(path.toFile()));
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:pl.otros.logview.api.model.LocationInfo.java

/**
 * Constructor/*  w  w  w .j  a  v a2s. co m*/
 *
 * @param className  the name of the class. If this is a FQN, then the package name will be parsed from it.
 * @param methodName the name of the method.
 * @param message    log message
 */
public LocationInfo(Optional<String> className, Optional<String> methodName, Optional<String> message) {
    this.message = message;
    this.packageName = className.flatMap(this::parsePackageName);
    this.className = className;
    this.method = methodName;
    fileName = Optional.empty();
    lineNumber = Optional.empty();
}

From source file:pl.otros.logview.api.model.LocationInfo.java

/**
 * Constructor/*w w  w.  ja v a  2 s.c  om*/
 *
 * @param className  the name of the class. If this is a FQN, then the package name will be parsed from it.
 * @param methodName the name of the method (can be null)
 * @param fileName   the file name
 * @param lineNumber the line number
 * @param message    log message
 */
public LocationInfo(Optional<String> className, Optional<String> methodName, Optional<String> fileName,
        Optional<Integer> lineNumber, Optional<String> message) {
    this.message = message;
    this.packageName = className.flatMap(this::parsePackageName);
    this.className = className;
    this.method = methodName;
    this.fileName = fileName;
    this.lineNumber = lineNumber;
}

From source file:tech.beshu.ror.acl.blocks.rules.impl.JwtAuthSyncRule.java

@SuppressWarnings("unchecked")
private Optional<Set<String>> extractRoles(Claims jws) {
    // Get claim roles
    Optional<Object> rolesObj = settings.getRolesClaim().map(claim -> {
        String[] path = claim.split("[.]");
        if (path.length < 2)
            return jws.get(claim, Object.class);
        else {/* w ww  .ja v  a 2s . c  o  m*/
            // Ok we need to parse all sub sequent path
            Object value = jws.get(path[0], Object.class);
            int i = 1;
            while (i < path.length && value != null && value instanceof Map<?, ?>) {
                value = ((Map<String, Object>) value).get(path[i]);
                i++;
            }
            return value;
        }
    });

    // Casting
    return rolesObj.flatMap(value -> {
        Set<String> set = new HashSet<>();

        if (value instanceof Collection<?>) {
            set.addAll((Collection<String>) value);
        } else if (value instanceof String) {
            set.add((String) value);
        }
        if (set.isEmpty())
            return Optional.empty();
        return Optional.of(set);
    });
}