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:net.pkhsolutions.pecsapp.boundary.PageServiceBean.java

@Override
@NotNull//ww w  .  ja v a 2  s . c om
public Optional<Page> findById(@NotNull Long id) {
    LOGGER.debug("Looking for a page with ID {}", id);
    return Optional.ofNullable(pageRepository.findOne(id));
}

From source file:gov.ca.cwds.cals.util.PlacementHomeUtil.java

/**
 * Composes facility name according to applicantDTOs list.
 *
 * <p> <b>Rules for Facility / Family name</b>
 *
 * Reads Applicant Name(s) as Last Name, First Name of Applicant #1 and Last Name, First Name of
 * Applicant #2. (ex. Smith, John & Jones, Jane) Last Name is separated from First Name by a
 * comma. Applicant Names are separated from each other by "and" or "&". When the Last Name is
 * the same for Applicant #1 and Applicant #2, name reads "Common Last Name, First Name
 * Applicant #1 & First Name Applicant #2" (ex. Smith, John & Jane). </p>
 *
 * @param applicantsList applicantsDTOs list
 * @return facility name//from  w w w .j  a  v  a2  s.c  o  m
 */
public static String composeFacilityName(List<ApplicantDTO> applicantsList) {
    // Assume that Facility/Family name composed from the first 2 applicants
    return Optional.ofNullable(applicantsList).map(applicants -> {
        Optional<ApplicantDTO> firstApplicant = getApplicantBuIndex(applicants, 0);
        Optional<ApplicantDTO> secondApplicant = getApplicantBuIndex(applicants, 1);

        StringBuilder firstPartSb = firstApplicant.map(PlacementHomeUtil::composeFirstPartOfFacilityName)
                .orElse(new StringBuilder());

        StringBuilder secondPartSb = secondApplicant
                .map(applicantDTO -> composeSecondPartOfFacilityName(firstApplicant, applicantDTO))
                .orElse(new StringBuilder());

        if (firstPartSb.length() > 0 && secondPartSb.length() > 0) {
            firstPartSb.append(" & ");
        }
        firstPartSb.append(secondPartSb);

        return firstPartSb.toString();
    }).orElse(null);
}

From source file:com.github.mrenou.jacksonatic.internal.mapping.field.FieldMappingInternal.java

public String getMappedName() {
    return Optional.ofNullable(annotations.get(JsonProperty.class))
            .map(annotation -> ((JsonProperty) annotation).value())
            .filter(name1 -> name1 != null && !name1.isEmpty()).orElse(name);
}

From source file:Main.java

/**
 * @param <K> the key type/*from   w  w w. ja  v  a2 s  .  c om*/
 * @param <V> the value type
 * @param m the map
 * @return an <b>UNMODIFIABLE</b> Map&lt;K, V&gt;
 */
public static <K, V> Map<K, V> unmodifiableMap(Map<? extends K, ? extends V> m) {
    return Optional.ofNullable(m).map(Collections::unmodifiableMap).orElseGet(Collections::emptyMap);
}

From source file:org.openlmis.fulfillment.ExportSchemaFlywayCallback.java

@Override
public void afterMigrate(Connection connection) {
    XLOGGER.entry(connection);//from  w  w w  . ja v  a 2  s .  co  m

    XLOGGER.info("After migrations, exporting db schema");

    int exitCode = 0;
    try {
        schemaName = Optional.ofNullable(schemaName).orElse("fulfillment");

        Process proc = Runtime.getRuntime().exec("/app/export_schema.sh " + schemaName);

        StreamGobbler streamGobbler = new StreamGobbler(proc.getInputStream(), XLOGGER::info);
        Executors.newSingleThreadExecutor().submit(streamGobbler);

        exitCode = proc.waitFor();
    } catch (Exception ex) {
        XLOGGER.warn("Exporting db schema failed with message: " + ex);
    }

    XLOGGER.exit(exitCode);
}

From source file:org.ow2.proactive.connector.iaas.service.InstanceScriptService.java

public List<ScriptResult> executeScriptOnInstance(String infrastructureId, String instanceId,
        InstanceScript instanceScript) {

    return Optional.ofNullable(infrastructureService.getInfrastructure(infrastructureId))
            .map(infrastructure -> cloudManager.executeScriptOnInstanceId(infrastructure, instanceId,
                    instanceScript))/*from  w w w . j  a v  a  2  s.c  o  m*/
            .orElseThrow(() -> new NotFoundException(
                    "infrastructure id  : " + infrastructureId + " does not exists"));

}

From source file:us.askplatyp.kb.lucene.wikimedia.rest.model.Summary.java

public Optional<Thumbnail> getThumbnail() {
    return Optional.ofNullable(thumbnail);
}

From source file:io.github.carlomicieli.footballdb.starter.mapping.Height.java

private static Optional<Height> extractValue(String value, Pattern p) {
    Matcher m = p.matcher(value);
    if (m.find()) {
        int f = Integer.parseInt(m.group(1));
        int i = Integer.parseInt(m.group(2));
        return Optional.ofNullable(new Height(f, i));
    }//  w ww  .  j  av a  2 s. com

    return Optional.empty();
}

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

public Optional<String> getNetworkGraphiteExpr() {
    return Optional.ofNullable(networkGraphiteExpr);
}

From source file:newcontroller.handler.impl.DefaultRequest.java

@Override
public Optional<String> param(String name) {
    return Optional.ofNullable(this.request.getParameter(name));
}