Example usage for com.google.common.collect MoreCollectors toOptional

List of usage examples for com.google.common.collect MoreCollectors toOptional

Introduction

In this page you can find the example usage for com.google.common.collect MoreCollectors toOptional.

Prototype

@SuppressWarnings("unchecked")
public static <T> Collector<T, ?, Optional<T>> toOptional() 

Source Link

Document

A collector that converts a stream of zero or one elements to an Optional .

Usage

From source file:org.ambraproject.wombat.controller.PowerPointController.java

private Optional<Map<String, ?>> findFigureViewFor(List<Map<String, ?>> figureView, AssetPointer assetPointer) {
    return figureView.stream().filter((Map<String, ?> figure) -> {
        String figureDoi = (String) figure.get("doi");
        return figureDoi.equals(assetPointer.getAssetDoi());
    }).collect(MoreCollectors.toOptional());
}

From source file:com.google.devtools.build.lib.sandbox.DockerSandboxedSpawnRunner.java

private Optional<String> dockerContainerFromSpawn(Spawn spawn) {
    Platform platform = null;//from   w  ww  .j a v  a2s. co m
    // TODO(philwo) Figure out if this is the right mechanism to specify a Docker image per action.
    String platformDescription = spawn.getExecutionPlatform().remoteExecutionProperties();
    if (platformDescription != null) {
        try {
            Platform.Builder platformBuilder = Platform.newBuilder();
            TextFormat.getParser().merge(platformDescription, platformBuilder);
            platform = platformBuilder.build();
        } catch (ParseException e) {
            throw new IllegalArgumentException(
                    String.format("Failed to parse remote_execution_properties from platform %s",
                            spawn.getExecutionPlatform().label()),
                    e);
        }
    }

    if (platform != null) {
        try {
            return platform.getPropertiesList().stream()
                    .filter(p -> p.getName().equals(CONTAINER_IMAGE_ENTRY_NAME)).map(p -> p.getValue())
                    .filter(r -> r.startsWith(DOCKER_IMAGE_PREFIX))
                    .map(r -> r.substring(DOCKER_IMAGE_PREFIX.length())).collect(MoreCollectors.toOptional());
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(String.format(
                    "Platform %s contained multiple container-image entries, but only one is allowed.",
                    spawn.getExecutionPlatform().label()));
        }
    } else {
        return Optional.empty();
    }
}