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:nl.knaw.huygens.alexandria.endpoint.AbstractAccountablePrototype.java

public Optional<ProvenancePrototype> getProvenance() {
    return Optional.ofNullable(provenance);
}

From source file:com.nestedbird.modules.sitemap.SitemapEntity.java

@Builder
private SitemapEntity(final String externalUrl, final String relativeUrl, final DateTime lastModified,
        final String changeFrequency, final Double priority) {
    this.externalUrl = Optional.ofNullable(externalUrl).orElse("");
    this.relativeUrl = Optional.ofNullable(relativeUrl).orElse("");
    this.lastModified = Optional.ofNullable(lastModified).orElse(new DateTime());
    this.changeFrequency = Optional.ofNullable(changeFrequency).orElse("daily");
    this.priority = Optional.ofNullable(priority).orElse(0.5);
}

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

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

From source file:org.obiba.mica.micaConfig.service.OpalCredentialService.java

@NotNull
public OpalCredential getOpalCredential(@NotNull String id) throws NoSuchOpalCredential {
    OpalCredential opalCredential = Optional.ofNullable(repository.findOne(id))
            .orElseThrow(NoSuchOpalCredential::new);

    if (opalCredential.getAuthType() == AuthType.USERNAME)
        opalCredential.setPassword(micaConfigService.decrypt(opalCredential.getPassword()));

    return opalCredential;
}

From source file:com.vsct.dt.strowgr.admin.nsq.payload.fragment.Header.java

@JsonCreator
public Header(@JsonProperty("correlationId") String correlationId,
        @JsonProperty("application") String application, @JsonProperty("platform") String platform,
        @JsonProperty("timestamp") Long timestamp, @JsonProperty("source") String source) {
    this.correlationId = Optional.ofNullable(correlationId).orElseGet(() -> UUID.randomUUID().toString());
    this.application = checkNotNull(application);
    this.platform = checkNotNull(platform);
    this.timestamp = Optional.ofNullable(timestamp).orElseGet(() -> System.currentTimeMillis());
    this.source = Optional.ofNullable(source).orElseGet(() -> UNKNOWN_SOURCE);
}

From source file:com.epages.checkout.CartController.java

@GetMapping("/{cartId}")
public ResponseEntity<Resource<Cart>> getCart(@PathVariable Long cartId) {
    return Optional.ofNullable(cartRepository.findOne(cartId))
            .map(cart -> new Resource<>(cart, entityLinks.linkForSingleResource(cart).withSelfRel()))
            .map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
}

From source file:demo.domain.service.MyUserDetailsService.java

@Override
@Transactional(readOnly = true)/*from ww  w  .  j  av  a  2  s.co m*/
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

    Account account = Optional.ofNullable(accountService.selectFindOne(username))
            .orElseThrow(() -> new UsernameNotFoundException("?????"));

    List<UserProjectRoleAuthority> userProjectRoleAuthority = Optional
            .ofNullable(userProjectRoleAuthorityService.selectFindByUserId(account.getUserId()))
            .orElseThrow(() -> new UsernameNotFoundException("??????"));

    List<GrantedAuthority> authorities = new ArrayList<>();
    userProjectRoleAuthority.forEach((UserProjectRoleAuthority upra) -> {
        StringBuilder sb = new StringBuilder();
        sb.append(upra.getProjectId());
        sb.append(".");
        sb.append(upra.getRoleId());
        sb.append(".");
        sb.append(upra.getAuthorityId());
        authorities.add(new SimpleGrantedAuthority(sb.toString()));
    });

    return new MyUserDetails(account, authorities);
}

From source file:cn.edu.zjnu.acm.judge.service.UserDetailService.java

@Nonnull
public static Optional<UserModel> getCurrentUser(@Nonnull HttpServletRequest request) {
    return Optional.ofNullable(request.getUserPrincipal()).filter(x -> x instanceof Authentication)
            .map(Authentication.class::cast).map(Authentication::getPrincipal)
            .filter(x -> x instanceof UserModel).map(UserModel.class::cast);
}

From source file:com.ikanow.aleph2.logging.utils.Log4JUtils.java

public static String getLog4JMessage(final JsonNode logObject, final Level level, final StackTraceElement stack,
        final String date_field, final Map<String, Object> map, final String hostname) {
    StringBuilder sb = new StringBuilder();
    final String c = stack.getClassName().substring(stack.getClassName().lastIndexOf(".") + 1);
    final String l = Integer.toString(stack.getLineNumber());
    final String thread = Thread.currentThread().getName().isEmpty()
            ? Long.toString(Thread.currentThread().getId())
            : Thread.currentThread().getName();
    sb.append(String.format(message_format, date_format.format(new Date(logObject.get(date_field).asLong())),
            thread, level.name(), c, l, logObject.get("message").asText()));
    sb.append(String.format(field_format, "bucket", logObject.get("bucket").asText()));
    sb.append(String.format(field_format, "hostname", hostname));
    sb.append(String.format(field_format, "subsystem", logObject.get("subsystem").asText()));
    sb.append(String.format(field_format, "command", logObject.get("command").asText()));
    Optional.ofNullable(map).orElse(Collections.emptyMap()).entrySet().stream()
            .forEach(e -> sb.append(String.format(field_format, e.getKey(), e.getValue())));
    return sb.toString();
}

From source file:com.teradata.benchto.driver.graphite.GraphiteProperties.java

public Optional<String> getCpuGraphiteExpr() {
    return Optional.ofNullable(cpuGraphiteExpr);
}