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:fi.helsinki.opintoni.config.locale.AngularCookieLocaleResolver.java

private void parseLocaleCookieIfNecessary(HttpServletRequest request) {
    if (request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) == null) {

        Locale locale = Optional.ofNullable(WebUtils.getCookie(request, getCookieName())).map(Cookie::getValue)
                .map(value -> StringUtils.replace(value, "%22", "")).map(StringUtils::parseLocaleString)
                .orElse(determineDefaultLocale(request));

        request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME, locale);
    }/*from  w ww .j a va 2s.  co m*/
}

From source file:ddf.catalog.data.impl.InjectableAttributeImpl.java

/**
 * Constructs an {@link InjectableAttributeImpl} with the given attribute name and metacard type
 * names./*from w w  w  . ja v a2s .co  m*/
 *
 * <p>If this attribute should be injected into all {@link ddf.catalog.data.MetacardType}s, then
 * {@code metacardTypes} should be null or empty.
 *
 * @param attribute the name of the attribute that this {@code InjectableAttributeImpl}
 *     represents, cannot be null
 * @param metacardTypes the names of the {@link ddf.catalog.data.MetacardType}s into which the
 *     attribute named {@code attribute} should be injected, can be null
 * @throws IllegalArgumentException if {@code attribute} is null
 */
public InjectableAttributeImpl(String attribute, @Nullable Collection<String> metacardTypes) {
    notNull(attribute, "The attribute name cannot be null.");
    this.attribute = attribute;

    Optional.ofNullable(metacardTypes).ifPresent(this.metacardTypes::addAll);
}

From source file:de.is24.aws.instancemetadataserver.RemoteHostRoleNameProducer.java

private Optional<String> unifyHamAndBerToPro(String rawLocTyp) {
    Matcher matcher = Pattern.compile("^((ber)|(ham))(.+)$").matcher(rawLocTyp);

    if (!matcher.find()) {
        return Optional.of(rawLocTyp);
    }//from   w ww  . j  a va2s  .  c  o m

    return Optional.ofNullable(matcher.group(4)).map((typ) -> "pro" + typ);
}

From source file:com.arpnetworking.configuration.jackson.JsonNodeLiteralSource.java

private JsonNodeLiteralSource(final Builder builder) {
    super(builder);
    _source = builder._source;//w w w.ja  v a  2 s  .  c  om

    JsonNode jsonNode = null;
    try {
        jsonNode = _objectMapper.readTree(_source);
    } catch (final IOException e) {
        throw Throwables.propagate(e);
    }
    _jsonNode = Optional.ofNullable(jsonNode);
}

From source file:org.cyclop.service.queryprotocoling.intern.AsyncFileStore.java

public Optional<H> getFromWriteQueue(UserIdentifier identifier) {
    LOG.debug("Reading history from queue for: {}", identifier);
    synchronized (diskQueue) {
        final H hist = diskQueue.get(identifier);
        LOG.trace("Found history: {}", hist);
        return Optional.ofNullable(hist);
    }/*  w w w. j a v a  2 s .  c om*/
}

From source file:fi.helsinki.opintoni.cache.SpringFeedFetcherCache.java

private SyndFeedInfo get(URL feedUrl) {
    Optional<ValueWrapper> valueWrapper = Optional.ofNullable(cache.get(feedUrl));

    return valueWrapper.map(w -> (SyndFeedInfo) w.get()).orElse(null);
}

From source file:com.kazuki43zoo.jpetstore.ui.Cart.java

public void removeItemById(String itemId) {
    Optional.ofNullable(itemMap.remove(itemId)).ifPresent(itemList::remove);
}

From source file:com.opentangerine.clean.Yconfig.java

/**
 * Load config from file.//  w ww .ja  va  2  s.  c  o m
 *
 * @param file File.
 * @return Config Object.
 */
public static Yconfig load(final File file) {
    Yconfig config = new Yconfig();
    if (file.exists()) {
        config = Optional.ofNullable(new Yaml().loadAs(Tool.preprocess(file), Yconfig.class)).orElse(config);
    }
    return config;
}

From source file:com.fantasy.stataggregator.entities.dao.AbstractRepository.java

protected T findLast() {
    javax.persistence.criteria.CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));//  w  ww  .  j  a  v  a 2  s  .c  o  m
    javax.persistence.Query q = em.createQuery(cq);

    List<T> option = Optional.ofNullable(q.setMaxResults(1).getResultList()).orElse(new ArrayList());

    T entity = null;
    if (Objects.nonNull(option.get(0))) {
        entity = option.get(0);
    }
    return entity;
}

From source file:fi.helsinki.opintoni.integration.pagemetadata.SpringPageMetaDataHttpClient.java

@Override
public Optional<String> getPageBody(String pageUrl) {
    Optional<String> pageBody = Optional.empty();
    try {/*from w  w w.  jav a 2 s.  c om*/
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Lists.newArrayList(MediaType.TEXT_HTML));
        headers.add(USER_AGENT_KEY, USER_AGENT);
        HttpEntity<String> entity = new HttpEntity<>(PARAMETERS_KEY, headers);

        ResponseEntity<String> response = metaDataRestTemplate.exchange(pageUrl, HttpMethod.GET, entity,
                String.class);
        if (response.getStatusCode().equals(HttpStatus.OK)) {
            pageBody = Optional.ofNullable(response.getBody());
        }
    } catch (Exception e) {
    }

    return pageBody;

}