Example usage for com.google.common.base Optional fromNullable

List of usage examples for com.google.common.base Optional fromNullable

Introduction

In this page you can find the example usage for com.google.common.base Optional fromNullable.

Prototype

public static <T> Optional<T> fromNullable(@Nullable T nullableReference) 

Source Link

Document

If nullableReference is non-null, returns an Optional instance containing that reference; otherwise returns Optional#absent .

Usage

From source file:de.haber.xmind2latex.cli.PropertyLoader.java

/**
 * @param fileName file name of the property file
 * @param propNames names of the properties to read out
 *
 * @return reads out properties 'propName' from the given property file 'fileName' and stored
 * them in the result list in the same order. If a property is not available or the property
 * file cannot be loaded, the corresponding {@link Optional} is absent.
 *//*  ww w .  j  a  v  a2s.c o  m*/
public static List<Optional<String>> getProperties(String fileName, String... propNames) {
    checkNotNull(fileName);
    checkNotNull(propNames);

    List<Optional<String>> result = new ArrayList<Optional<String>>(propNames.length);

    Properties prop = new Properties();
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    InputStream stream = loader.getResourceAsStream(fileName);
    try {
        prop.load(stream);
        for (String p : propNames) {
            String versionNumber = prop.getProperty(p);
            result.add(Optional.fromNullable(versionNumber));
        }
    } catch (Exception e) {
        for (int i = 0; i < propNames.length; i++) {
            result.add(Optional.<String>absent());
        }
    } finally {
        try {
            Closeables.close(stream, true);
        } catch (IOException e) {
            Throwables.propagate(e);
        }
    }
    return ImmutableList.copyOf(result);
}

From source file:org.whispersystems.textsecuregcm.storage.Account.java

public Optional<Device> getAuthenticatedDevice() {
    return Optional.fromNullable(authenticatedDevice);
}

From source file:com.github.ykrasik.jerminal.internal.util.StringUtils.java

/**
 * Transforms an all-whitespace string to an absent value.
 *
 * @param str String to check.//from  w  w w . ja  va 2s  .c  om
 * @return A present value if the string had any non-whitespace character, or an absent value otherwise.
 */
public static Optional<String> getOptionalString(String str) {
    return Optional.fromNullable(Strings.emptyToNull(str.trim()));
}

From source file:org.geogig.osm.internal.history.Changeset.java

public Optional<Envelope> getWgs84Bounds() {
    return Optional.fromNullable(wgs84Bounds);
}

From source file:org.whispersystems.textsecuregcm.storage.PendingAccountsManager.java

public Optional<String> getCodeForNumber(String number) {
    Optional<String> code = memcacheGet(number);

    if (!code.isPresent()) {
        code = Optional.fromNullable(pendingAccounts.getCodeForNumber(number));

        if (code.isPresent()) {
            memcacheSet(number, code.get());
        }/*from  www. ja va2s  .  c om*/
    }

    return code;
}

From source file:com.spotify.heroic.elasticsearch.StandaloneClientSetup.java

@JsonCreator
public StandaloneClientSetup(@JsonProperty("clusterName") String clusterName,
        @JsonProperty("root") String root) {
    this.clusterName = Optional.fromNullable(clusterName).or(DEFAULT_CLUSTER_NAME);
    this.root = checkDataDirectory(
            Paths.get(Optional.fromNullable(root).or(temporaryDirectory())).toAbsolutePath());
}

From source file:org.jclouds.openstack.marconi.v1.domain.Queue.java

/**
 * @return The key/value metadata for this queue.
 *//*  w w  w.  j  a va  2s .  c o m*/
public Optional<Map<String, String>> getMetadata() {
    return Optional.fromNullable(metadata);
}

From source file:org.apache.myriad.configuration.MyriadExecutorConfiguration.java

public Optional<Double> getJvmMaxMemoryMB() {
    return Optional.fromNullable(jvmMaxMemoryMB);
}

From source file:io.crate.sql.tree.ShowColumns.java

public ShowColumns(QualifiedName table, @Nullable QualifiedName schema, @Nullable String likePattern) {
    this.table = checkNotNull(table, "table is null");
    this.schema = Optional.fromNullable(schema);
    this.likePattern = Optional.fromNullable(likePattern);
    this.where = Optional.absent();
}

From source file:springfox.documentation.swagger.web.ApiListingTagReader.java

@Override
public void apply(ApiListingContext apiListingContext) {
    Class<?> controllerClass = apiListingContext.getGroup().getControllerClass();
    Set<String> tagSet = Optional.fromNullable(AnnotationUtils.findAnnotation(controllerClass, Api.class))
            .transform(tags()).or(Sets.<String>newTreeSet());
    if (tagSet.isEmpty()) {
        tagSet.add(apiListingContext.getGroup().getGroupName());
    }//from  w  w w  .  j a v  a  2s  . c  o  m
    apiListingContext.apiListingBuilder().tags(tagSet);
}