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:keywhiz.api.model.SanitizedSecret.java

@JsonCreator
public static SanitizedSecret of(@JsonProperty("id") long id, @JsonProperty("name") String name,
        @JsonProperty("description") @Nullable String description, @JsonProperty("createdAt") ApiDate createdAt,
        @JsonProperty("createdBy") @Nullable String createdBy, @JsonProperty("updatedAt") ApiDate updatedAt,
        @JsonProperty("updatedBy") @Nullable String updatedBy,
        @JsonProperty("metadata") @Nullable Map<String, String> metadata,
        @JsonProperty("type") @Nullable String type,
        @JsonProperty("generationOptions") @Nullable Map<String, String> generationOptions,
        @JsonProperty("expiry") long expiry) {
    ImmutableMap<String, String> meta = (metadata == null) ? ImmutableMap.of() : ImmutableMap.copyOf(metadata);
    ImmutableMap<String, String> genOptions = (generationOptions == null) ? ImmutableMap.of()
            : ImmutableMap.copyOf(generationOptions);
    return new AutoValue_SanitizedSecret(id, name, nullToEmpty(description), createdAt, nullToEmpty(createdBy),
            updatedAt, nullToEmpty(updatedBy), meta, Optional.ofNullable(type), genOptions, expiry);
}

From source file:io.github.carlomicieli.footballdb.starter.documents.WebDocumentDownloader.java

@Override
public Optional<Document> from(String uri) {
    return Optional.ofNullable(downloadFromURL(uri));
}

From source file:com.devicehive.resource.exceptions.AccessDeniedExceptionMapper.java

@Override
public Response toResponse(AccessDeniedException exception) {
    String realm = Optional.ofNullable(request.getHeader(HttpHeaders.AUTHORIZATION)).map(authHeader -> {
        if (authHeader.startsWith(Constants.TOKEN_SCHEME)) {
            return Messages.OAUTH_REALM;
        } else {/*from w ww.  j a va  2 s  .c o  m*/
            return Messages.BASIC_REALM;
        }
    }).orElse(Messages.BASIC_REALM);
    return Response.status(Response.Status.UNAUTHORIZED).type(MediaType.APPLICATION_JSON_TYPE)
            .header(HttpHeaders.WWW_AUTHENTICATE, realm)
            .entity(new ErrorResponse(Response.Status.UNAUTHORIZED.getStatusCode(), "Unauthorized")).build();
}

From source file:com.example.api.DatabaseServiceImpl.java

@NotNull
public Optional<Message> getMessageById(final long messageId) {
    try {/*from  w w w .j  a  va  2 s .  c o  m*/
        final ResponseEntity<Message> entity = restTemplate
                .getForEntity("http://localhost:5000/messageEntity/" + messageId, Message.class);
        if (entity.getStatusCode().is2xxSuccessful()) {
            return Optional.ofNullable(entity.getBody());
        }
        return Optional.empty();
    } catch (RestClientException e) {
        return Optional.empty();
    }
}

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

@Override
protected DateFormat resolveDefaultFormat(Locale locale) {
    return Optional.ofNullable(this.getPattern())
            .<DateFormat>map(pattern -> new SimpleDateFormat(pattern, locale))
            .orElseGet(DateFormat::getInstance);
}

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

@Transactional
public void save(Long parentId, Long problemId, String userId, String title, String content) {
    long depth = 0;
    long orderNum = 0;

    final long nextId = messageMapper.nextId();
    final Message parent = parentId != null ? Optional.ofNullable(messageMapper.findOne(parentId))
            .orElseThrow(() -> new MessageException("No such parent message", HttpStatus.NOT_FOUND)) : null;
    if (parent != null) {
        orderNum = parent.getOrder();//from   w  w  w.  jav a  2  s  . com
        final long depth1 = parent.getDepth();

        List<Message> messages = messageMapper
                .findAllByThreadIdAndOrderNumGreaterThanOrderByOrderNum(parent.getThread(), parent.getOrder());
        for (Message m : messages) {
            depth = m.getDepth();
            if (depth <= depth1) {
                break;
            }
            orderNum = m.getOrder();
        }
        depth = depth1 + 1;
        messageMapper.updateOrderNumByThreadIdAndOrderNumGreaterThan(parent.getThread(), orderNum);
        ++orderNum;
    }
    messageMapper.save(nextId, parentId, orderNum, problemId, depth, userId, title, content);
    if (parent != null) {
        messageMapper.updateThreadIdByThreadId(nextId, parent.getThread());
    }
}

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

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

From source file:fi.helsinki.opintoni.service.portfolio.PortfolioPathGenerator.java

public String create(String name) {
    return Optional.ofNullable(name).map(slugify::slugify).map(this::makeUnique).orElse(null);
}

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

@Override
protected NumberFormat resolveDefaultFormat(Locale locale) {
    return Optional.ofNullable(this.getPattern())
            .<NumberFormat>map(pattern -> new DecimalFormat(pattern, new DecimalFormatSymbols(locale)))
            .orElseGet(NumberFormat::getInstance);
}

From source file:org.openmhealth.shimmer.common.configuration.DefaultEndpointSettings.java

@Override
public Optional<DateTimeQuerySettings> getEffectiveDateTimeQuerySettings() {
    return Optional.ofNullable(effectiveDateTimeQuerySettings);
}