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:uk.ac.ebi.ep.data.service.UniprotEntryService.java

@Transactional(readOnly = true)
public Optional<UniprotEntry> findByAccession(String accession) {

    return Optional.ofNullable(repository.findByAccession(accession));
}

From source file:co.runrightfast.vertx.core.eventbus.MessageHeader.java

public static Optional<String> getMessageId(@NonNull final Message message) {
    return Optional.ofNullable(message.headers().get(MESSAGE_ID.header));
}

From source file:alfio.controller.support.SessionUtil.java

public static Optional<String> retrieveSpecialPriceCode(HttpServletRequest request) {
    return Optional.ofNullable((String) request.getSession().getAttribute(SPECIAL_PRICE_CODE));
}

From source file:Main.java

/**
 * @param <T> the element type//from   w w w. j  a va  2 s  .com
 * @param l the list
 * @return an <b>UNMODIFIABLE</b> List&lt;T&gt;
 */
public static <T> List<T> unmodifiableList(List<? extends T> l) {
    return Optional.ofNullable(l).map(Collections::unmodifiableList).orElseGet(Collections::emptyList);
}

From source file:Main.java

/**
 * Examine a collection and determines if it is null, empty, or contains
 * only null values//w  ww. j a  v a  2s. c o  m
 *
 * @param collection
 *            Collection to examine
 * @return whether the collection is null, empty, or contains only nulls
 */
public static boolean nullEmptyOrContainsOnlyNulls(final Collection<? extends Object> collection) {
    return Optional.ofNullable(collection).map(Collection::stream).orElseGet(Stream::empty)
            .allMatch(Objects::isNull);
}

From source file:de.perdian.commons.lang.conversion.impl.converters.ObjectToStringConverter.java

@Override
public String convert(Object source) {
    return Optional.ofNullable(source).map(Object::toString).orElse(null);
}

From source file:org.n52.shetland.util.HTTP.java

public static String getAsString(URI uri) throws IOException {
    try (CloseableHttpResponse response = CLIENT.execute(new HttpGet(uri))) {
        HttpEntity entity = response.getEntity();
        String encoding = Optional.ofNullable(entity.getContentEncoding()).map(Header::getValue)
                .orElse(StandardCharsets.UTF_8.name());
        Charset charset = Charset.forName(encoding);
        try (InputStream is = entity.getContent(); Reader reader = new InputStreamReader(is, charset)) {
            return CharStreams.toString(reader);
        }/*from w  w w . j  av  a  2  s .  c om*/
    }
}

From source file:com.palantir.docker.compose.configuration.RemoteHostIpResolver.java

@Override
public String resolveIp(String dockerHost) {
    return Optional.ofNullable(emptyToNull(dockerHost))
            .map(host -> StringUtils.substringAfter(host, TCP_PROTOCOL))
            .map(ipAndMaybePort -> StringUtils.substringBefore(ipAndMaybePort, ":"))
            .orElseThrow(() -> new IllegalArgumentException("DOCKER_HOST cannot be blank/null"));
}

From source file:uk.co.sdev.undertow.rx.rest.JacksonCompletionHandler.java

public JacksonCompletionHandler(ObjectMapper mapper, JavaType type,
        Subscriber<? super Optional<T>> subscriber) {
    super(subscriber, body -> {
        try {/*w  w w . ja v  a 2 s  . co  m*/
            return Optional.ofNullable(mapper.readValue(body, type));
        } catch (Exception e) {
            return Optional.<T>empty();
        }
    });
}

From source file:enmasse.controller.flavor.FlavorParser.java

private static Flavor parseFlavor(JsonNode node) {
    String name = node.get(KEY_NAME).asText();
    Flavor.Builder builder = new Flavor.Builder(name, node.get(KEY_TEMPLATE_NAME).asText());

    if (node.has(KEY_TYPE)) {
        builder.type(node.get(KEY_TYPE).asText());
    }//from w ww. ja  v  a2  s.c  om

    if (node.has(KEY_DESCRIPTION)) {
        builder.description(node.get(KEY_DESCRIPTION).asText());
    }

    builder.uuid(Optional.ofNullable(node.get(KEY_UUID)).map(JsonNode::asText));

    if (node.has(KEY_TEMPLATE_PARAMETERS)) {
        Iterator<Map.Entry<String, JsonNode>> it = node.get(KEY_TEMPLATE_PARAMETERS).fields();
        while (it.hasNext()) {
            Map.Entry<String, JsonNode> entry = it.next();
            builder.templateParameter(entry.getKey(), entry.getValue().asText());
        }
    }
    return builder.build();
}