Example usage for java.util Optional of

List of usage examples for java.util Optional of

Introduction

In this page you can find the example usage for java.util Optional of.

Prototype

public static <T> Optional<T> of(T value) 

Source Link

Document

Returns an Optional describing the given non- null value.

Usage

From source file:ch.ralscha.extdirectspring.provider.RemoteProviderOptional.java

@ExtDirectMethod(group = "optional")
public Optional<String> method2(Optional<String> str) {
    if (str.isPresent()) {
        return Optional.of(str.get());
    }/*from  ww w .  ja  va  2s. c om*/
    return Optional.of("default");
}

From source file:com.spotify.styx.api.MiddlewaresTest.java

private RequestContext mockRequestContext(boolean hasHeader) {
    RequestContext requestContext = mock(RequestContext.class);
    Request request = mock(Request.class);
    if (hasHeader) {
        when(request.header("User-Agent")).thenReturn(Optional.of("Styx CLI 0.1.1"));
    } else {//from w w w  .  ja  v  a  2  s.co  m
        when(request.header("User-Agent")).thenReturn(Optional.empty());
    }
    when(requestContext.request()).thenReturn(request);
    return requestContext;
}

From source file:spring.travel.site.request.RequestInfoInterceptor.java

private Optional<String> getCookieValue(String cookie) {
    String value = cookie.substring(cookie.indexOf('=') + 1);
    if (value.startsWith("\"")) {
        value = value.substring(1);// ww w.j  a va2s .co m
    }
    if (value.endsWith("\"")) {
        value = value.substring(0, value.length() - 1);
    }
    return Optional.of(value);
}

From source file:org.trustedanalytics.metricsprovider.cloudadapter.api.CfServiceInstance.java

@JsonIgnore
public UUID getServiceGuid() {
    Optional<CfServiceInstance> serviceInstance = Optional.of(this);
    return serviceInstance.map(CfServiceInstance::getServicePlan).map(CfServicePlan::getService)
            .map(CfService::getGuid).orElse(null);
}

From source file:com.teradata.benchto.service.TagService.java

public Optional<Tag> latest(String environmentName, ZonedDateTime until) {
    List<Tag> latest = repo.latest(environmentService.findEnvironment(environmentName), until,
            new PageRequest(0, 1));
    if (latest.isEmpty()) {
        return Optional.empty();
    }/*w  ww .ja v a 2  s  .c  o  m*/
    return Optional.of(latest.get(0));
}

From source file:ai.grakn.migration.base.MigrationCLI.java

public static <T extends MigrationOptions> Optional<T> validate(T options) {
    try {//www  . jav  a 2 s. c  o  m
        if (options.isHelp()) {
            printHelpMessage(options);
        }

        if (options.getNumberOptions() == 0) {
            printHelpMessage(options);
            throw new IllegalArgumentException("Helping");
        } else if (options.getNumberOptions() == 1 && options.isHelp()) {
            throw new IllegalArgumentException("Helping");
        }

        if (!Client.serverIsRunning(options.getUri())) {
            System.out.println(COULD_NOT_CONNECT);
        }

        //noinspection unchecked
        return Optional.of(options);
    } catch (Throwable e) {
        return Optional.empty();
    }
}

From source file:org.ow2.proactive.workflow_catalog.rest.controller.WorkflowRevisionControllerTest.java

@Test
public void testCreate() throws Exception {
    MultipartFile mockedFile = mock(MultipartFile.class);
    when(mockedFile.getBytes()).thenReturn(null);
    workflowRevisionController.create(BUCKET_ID, WF_ID, Optional.empty(), mockedFile);
    verify(workflowRevisionService, times(1)).createWorkflowRevision(BUCKET_ID, Optional.of(WF_ID), null,
            Optional.empty());//from ww w  .j a v a2s . com
}

From source file:com.orange.ngsi2.model.RegistrationTest.java

@Test
public void serializationRegistrationTest() throws JsonProcessingException, MalformedURLException {
    ObjectWriter writer = Utils.objectMapper.writer(new DefaultPrettyPrinter());

    Registration registration = new Registration("abcde", new URL("http://localhost:1234"));
    registration.setDuration("PT1M");
    SubjectEntity subjectEntity = new SubjectEntity();
    subjectEntity.setType(Optional.of("Room"));
    SubjectRegistration subjectRegistration = new SubjectRegistration(Collections.singletonList(subjectEntity),
            Collections.singletonList("humidity"));
    registration.setSubject(subjectRegistration);
    String json = writer.writeValueAsString(registration);
    String jsonString = "{\n" + "  \"id\" : \"abcde\",\n" + "  \"subject\" : {\n" + "    \"entities\" : [ {\n"
            + "      \"type\" : \"Room\"\n" + "    } ],\n" + "    \"attributes\" : [ \"humidity\" ]\n"
            + "  },\n" + "  \"callback\" : \"http://localhost:1234\",\n" + "  \"duration\" : \"PT1M\"\n" + "}";
    assertEquals(jsonString, json);//from w w  w  .  j  a  v  a 2  s. co  m
}

From source file:org.openwms.common.HttpCommonGateway.java

@Override
public Optional<TransportUnit> getTransportUnit(String transportUnitBK) {
    try {//from  ww  w  .j  a  v a  2s.  c  o m
        return Optional.of(commonFeignClient.getTransportUnit(transportUnitBK));
    } catch (Exception ex) {
        if (translate(ex) == 404) {
            return Optional.empty();
        } else {
            LOGGER.error(ex.getMessage(), ex);
            throw new ServiceLayerException(ex.getMessage());
        }
    }
}

From source file:com.blackducksoftware.integration.hub.detect.workflow.project.ProjectNameVersionDecider.java

public NameVersion decideProjectNameVersion(String preferredDetectTools,
        final List<DetectToolProjectInfo> detectToolProjectInfo) throws DetectUserFriendlyException {

    Optional<String> decidedProjectName = Optional.empty();
    Optional<String> decidedProjectVersion = Optional.empty();

    if (StringUtils.isNotBlank(projectVersionOptions.overrideProjectName)) {
        decidedProjectName = Optional.of(projectVersionOptions.overrideProjectName);
    }/*from w w w . ja va 2s .c o  m*/

    if (StringUtils.isNotBlank(projectVersionOptions.overrideProjectVersionName)) {
        decidedProjectVersion = Optional.of(projectVersionOptions.overrideProjectVersionName);
    }

    Optional<DetectToolProjectInfo> chosenTool = decideToolProjectInfo(preferredDetectTools,
            detectToolProjectInfo);
    if (chosenTool.isPresent()) {
        if (!decidedProjectName.isPresent()) {
            String suggestedName = chosenTool.get().getSuggestedNameVersion().getName();
            if (StringUtils.isNotBlank(suggestedName)) {
                decidedProjectName = Optional.of(suggestedName);
            }
        }
        if (!decidedProjectVersion.isPresent()) {
            String suggestedVersion = chosenTool.get().getSuggestedNameVersion().getVersion();
            if (StringUtils.isNotBlank(suggestedVersion)) {
                decidedProjectVersion = Optional.of(suggestedVersion);
            }
        }
    }

    if (!decidedProjectName.isPresent()) {
        logger.info("A project name could not be decided. Using the name of the source path.");
        decidedProjectName = Optional.of(projectVersionOptions.sourcePathName);
    }

    if (!decidedProjectVersion.isPresent()) {
        if ("timestamp".equals(projectVersionOptions.defaultProjectVersionScheme)) {
            logger.info("A project version name could not be decided. Using the current timestamp.");
            final String timeformat = projectVersionOptions.defaultProjectVersionFormat;
            final String timeString = DateTimeFormatter.ofPattern(timeformat).withZone(ZoneOffset.UTC)
                    .format(Instant.now().atZone(ZoneOffset.UTC));
            decidedProjectVersion = Optional.of(timeString);
        } else {
            logger.info("A project version name could not be decided. Using the default version text.");
            decidedProjectVersion = Optional.of(projectVersionOptions.defaultProjectVersionText);
        }
    }

    return new NameVersion(decidedProjectName.get(), decidedProjectVersion.get());
}