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:org.jddd.jackson.SimpleValueObjectSerializerModifier.java

@Override
public BeanSerializerBuilder updateBuilder(SerializationConfig config, BeanDescription beanDesc,
        BeanSerializerBuilder builder) {

    for (BeanPropertyWriter writer : builder.getProperties()) {

        JavaType propertyType = writer.getMember().getType();
        Class<?> type = propertyType.getRawClass();
        List<BeanPropertyDefinition> properties = getProperties(propertyType, config);

        Optional.ofNullable(AnnotationUtils.findAnnotation(type, ValueObject.class))//
                .filter(it -> properties.size() == 1)//
                .flatMap(it -> properties.stream().findFirst())//
                .ifPresent(it -> writer.assignSerializer(new PropertyAccessingSerializer(it)));
    }//w  w w  . j  av  a  2 s. c om

    return builder;
}

From source file:io.gravitee.repository.redis.management.RedisApplicationRepository.java

@Override
public Optional<Application> findById(String applicationId) throws TechnicalException {
    RedisApplication redisApplication = this.applicationRedisRepository.find(applicationId);
    return Optional.ofNullable(convert(redisApplication));
}

From source file:com.example.app.profile.model.terminology.FallbackProfileTermProvider.java

@Override
public TextSource clients() {
    return isBlank(Optional.ofNullable(getProfileTermProvider()).map(ProfileTermProvider::clients).orElse(null))
            ? _defaultProfileTermProvider.clients()
            : getProfileTermProvider().clients();
}

From source file:cn.edu.zjnu.acm.judge.config.JudgeHandlerInterceptor.java

@ModelAttribute
public void addAttributes(HttpServletRequest request, @RequestParam(value = "url", required = false) String url,
        Authentication authentication) {
    if (Boolean.TRUE.equals(request.getAttribute(APPLIED_ONCE_KEY))) {
        return;/*from  w  ww  .j  av  a2s  .c  om*/
    }
    request.setAttribute(APPLIED_ONCE_KEY, true);

    if (!StringUtils.isEmptyOrWhitespace(url)) {
        request.setAttribute(BACK_URL_ATTRIBUTE_NAME, url);
    } else {
        String uri = getString(RequestDispatcher.FORWARD_SERVLET_PATH, HttpServletRequest::getServletPath,
                request);
        String query = getString(RequestDispatcher.FORWARD_QUERY_STRING, HttpServletRequest::getQueryString,
                request);
        if (query != null) {
            uri = uri + '?' + query;
        }
        request.setAttribute(BACK_URL_ATTRIBUTE_NAME, uri);
    }
    Optional.ofNullable(authentication).map(Authentication::getName).map(mailMapper::getMailInfo)
            .ifPresent(mailInfo -> request.setAttribute("mailInfo", mailInfo));
}

From source file:com.mesosphere.dcos.cassandra.scheduler.resources.TasksResource.java

@GET
@Path("/{name}/status")
@ManagedAsync/*  w w w .  j a va  2 s . c o m*/
public void getStatus(@PathParam("name") final String name, @Suspended final AsyncResponse response) {

    Optional<CassandraDaemonTask> taskOption = Optional.ofNullable(state.getDaemons().get(name));
    if (!taskOption.isPresent()) {
        response.resume(Response.status(Response.Status.NOT_FOUND));
    } else {
        CassandraDaemonTask task = taskOption.get();
        client.status(task.getHostname(), task.getExecutor().getApiPort())
                .whenCompleteAsync((status, error) -> {
                    if (status != null) {
                        response.resume(status);
                    } else {
                        response.resume(Response.serverError());
                    }
                });
    }
}

From source file:io.curly.artifact.service.DefaultArtifactCommand.java

@Override
@Loggable/*from w w  w .  jav a2s. com*/
@HystrixCommand(fallbackMethod = "defaultFindAll", ignoreExceptions = IllegalStateException.class, commandProperties = @HystrixProperty(name = EXECUTION_ISOLATION, value = SEMAPHORE))
public Observable<Optional<Page<Artifact>>> findAllByPage(Pageable pageable) {
    return new ObservableResult<Optional<Page<Artifact>>>() {
        @Override
        public Optional<Page<Artifact>> invoke() {
            return Optional.ofNullable(artifactService.findAll(pageable));
        }
    };
}

From source file:org.ow2.proactive.connector.iaas.rest.InstanceRest.java

@GET
@Path("{infrastructureId}/instances")
@Produces(MediaType.APPLICATION_JSON)//from   w  ww. j av a  2s .c  om
public Response getInstances(@PathParam("infrastructureId") String infrastructureId,
        @QueryParam("instanceId") String instanceId, @QueryParam("instanceTag") String instanceTag,
        @QueryParam("allInstances") Boolean allInstances) {

    if (Optional.ofNullable(instanceId).isPresent()) {
        return Response.ok(instanceService.getInstanceById(infrastructureId, instanceId)).build();
    } else if (Optional.ofNullable(instanceTag).isPresent()) {
        return Response.ok(instanceService.getInstanceByTag(infrastructureId, instanceTag)).build();
    } else if (Optional.ofNullable(allInstances).isPresent() && allInstances) {
        return Response.ok(instanceService.getAllInstances(infrastructureId)).build();
    } else {
        return Response.ok(instanceService.getCreatedInstances(infrastructureId)).build();
    }
}

From source file:io.gravitee.repository.redis.management.RedisApiRepository.java

@Override
public Optional<Api> findById(String apiId) throws TechnicalException {
    RedisApi redisApi = this.apiRedisRepository.find(apiId);
    return Optional.ofNullable(convert(redisApi));
}

From source file:io.mapzone.controller.catalog.http.CatalogRequest.java

/** 
 * The parsed request body if this is a POST request. 
 */
public <T> Optional<T> parsedBody() {
    return Optional.ofNullable((T) parsedBody);
}

From source file:com.arpnetworking.metrics.mad.model.json.Version2e.java

private Version2e(final Builder builder) {
    _time = builder._time;//from   www.  j a v a2  s . c o  m
    _name = builder._name;
    _level = builder._level;
    _data = builder._data;
    _id = Optional.ofNullable(builder._id);
    _context = ImmutableMap
            .copyOf(MoreObjects.firstNonNull(builder._context, Collections.<String, String>emptyMap()));
}