Example usage for java.util Optional ofNullable

List of usage examples for java.util Optional ofNullable

Introduction

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

Prototype

@SuppressWarnings("unchecked")
public static <T> Optional<T> ofNullable(T value) 

Source Link

Document

Returns an Optional describing the given value, if non- null , otherwise returns an empty Optional .

Usage

From source file:com.devicehive.handler.RequestDispatcher.java

@Override
@SuppressWarnings("unchecked")
public Response handle(Request request) {
    final Action action = Action.valueOf(request.getBody().getAction());
    try {/* ww  w  .jav a 2 s  . com*/
        return Optional.ofNullable(handlerMap.get(action)).map(handler -> handler.handle(request))
                .orElseThrow(() -> new RuntimeException("Action '" + action + "' is not supported."));
    } catch (Exception e) {
        logger.error("Unable to handle request.", e);
        return Response.newBuilder().withBody(new ErrorResponse(e.getMessage())).withLast(true)
                .buildFailed(HttpStatus.INTERNAL_SERVER_ERROR.value());
    }
}

From source file:org.dswarm.graph.resources.GraphResource.java

protected Optional<String> getStringValue(final String key, final JsonNode json) {

    final JsonNode node = json.get(key);
    final Optional<String> optionalValue;

    if (node != null) {

        optionalValue = Optional.ofNullable(node.asText());
    } else {//from   w  ww.j  av a2s.  c  om

        optionalValue = Optional.empty();
    }

    return optionalValue;
}

From source file:com.spotify.ffwd.template.TemplateOutputPlugin.java

@JsonCreator
public TemplateOutputPlugin(@JsonProperty("protocol") final ProtocolFactory protocol,
        @JsonProperty("retry") final RetryPolicy retry) {
    this.protocol = Optional.ofNullable(protocol).orElseGet(ProtocolFactory.defaultFor())
            .protocol(DEFAULT_PROTOCOL, DEFAULT_PORT);
    this.retry = Optional.ofNullable(retry).orElseGet(RetryPolicy.Exponential::new);
}

From source file:com.kazuki43zoo.jpetstore.service.CatalogService.java

public Product getProduct(String productId) {
    return Optional.ofNullable(productMapper.getProduct(productId))
            .orElseThrow(() -> new ResourceNotFoundException("Product", productId));
}

From source file:org.dhatim.dropwizard.jwt.cookie.authentication.DefaultJwtCookiePrincipal.java

/**
 * Builds a new instance of DefaultJwtCookiePrincipal
 *
 * @param name the principal name//from ww  w. j  av a2s . c  om
 * @param persistent if the cookie must be persistent
 * @param roles the roles the principal is in
 * @param claims custom data associated with the principal
 */
public DefaultJwtCookiePrincipal(@JsonProperty("name") String name,
        @JsonProperty("persistent") boolean persistent, @JsonProperty("roles") Collection<String> roles,
        @JsonProperty("claims") Claims claims) {
    this.claims = Optional.ofNullable(claims).orElseGet(Jwts::claims);
    this.claims.setSubject(name);
    this.claims.put(PERSISTENT, persistent);
    this.claims.put(ROLES, roles);
}

From source file:org.zalando.riptide.Selector.java

/**
 * Attempts to find a matching binding for the given attribute. Defaults to a direct map lookup.
 *
 * @param attribute the previously selected attribute
 * @param bindings  all bindings//from w  w w.j  av a 2  s .  c om
 * @return an optional binding match, if found
 */
default Optional<Binding<A>> select(final Optional<A> attribute, final Map<Optional<A>, Binding<A>> bindings) {
    return Optional.ofNullable(bindings.get(attribute));
}

From source file:com.epam.ta.reportportal.info.AnalyticsInfoContributor.java

@Override
public Map<String, ?> contribute() {
    Optional<Map<String, AnalyticsDetails>> analytics = Optional
            .ofNullable(settingsRepository.findOne("default"))
            .flatMap(settings -> Optional.ofNullable(settings.getAnalyticsDetails()));
    return analytics.isPresent()
            ? ImmutableMap.<String, Object>builder().put(ANALYTICS_KEY, analytics.get()).build()
            : Collections.emptyMap();
}

From source file:com.mattjtodd.coherence.CoherenceCache.java

@Override
public ValueWrapper putIfAbsent(Object key, Object value) {
    return Optional.ofNullable(namedCache.putIfAbsent(key, value)).map(SimpleValueWrapper::new).orElse(null);
}

From source file:io.github.carlomicieli.footballdb.starter.pages.TableBuilder.java

public static Optional<Table> fromElement(Element tableElement) {
    return Optional.ofNullable(tableElement).filter(e -> e.tagName().equals("table")).map(e -> {
        TableBuilder tb = new TableBuilder();
        childrenWithTag(e, "tr").forEach(r -> addToTable(tb, r));
        return tb.toTable();
    });/*from w  w w  .  j  av a2  s. com*/
}

From source file:com.adobe.acs.commons.models.injectors.annotation.impl.HierarchicalPagePropertyAnnotationProcessorFactory.java

@Override
public InjectAnnotationProcessor2 createAnnotationProcessor(AnnotatedElement element) {
    return Optional.ofNullable(element.getAnnotation(HierarchicalPageProperty.class))
            .map(PagePropertyAnnotationProcessor::new).orElse(null);
}