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

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

Introduction

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

Prototype

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

Source Link

Document

Returns an Optional instance containing the given non-null reference.

Usage

From source file:org.eclipse.buildship.core.util.file.FileUtils.java

/**
 * Derives a {@code File} instance with absolute path from the specified {@code File} instance.
 *
 * @param file the relative or absolute file of the {@code File} instance to derive
 * @return the absolute {@code File} if the file is not {@code null}, otherwise
 * {@link Optional#absent()}// w  w w . j av a  2s. com
 */
public static Optional<File> getAbsoluteFile(File file) {
    if (file == null) {
        return Optional.absent();
    } else {
        return Optional.of(file.isAbsolute() ? file : file.getAbsoluteFile());
    }
}

From source file:gobblin.runtime.std.DefaultJobSpecScheduleImpl.java

/** Creates a schedule denoting that the job is to be executed immediately */
public static DefaultJobSpecScheduleImpl createImmediateSchedule(JobSpec jobSpec, Runnable jobRunnable) {
    return new DefaultJobSpecScheduleImpl(jobSpec, jobRunnable, Optional.of(System.currentTimeMillis()));
}

From source file:org.asciidoctor.asciidoclet.OutputTemplates.java

static Optional<OutputTemplates> create(DocErrorReporter errorReporter) {
    File dir = prepareTemplateDir(errorReporter);
    return dir == null ? Optional.<OutputTemplates>absent() : Optional.of(new OutputTemplates(dir));
}

From source file:eu.thebluemountain.customers.dctm.brownbag.badcontentslister.config.parser.XMLParsers.java

/**
 * The method returns the attribute value if any
 * @param in provides access to the XML data
 * @param name identifies the attribute to get value of
 * @return the matching value if any//  w w  w.  j a v a 2 s. c  o m
 */
public static Optional<String> getAttribute(XMLStreamReader in, String name) {
    int size = in.getAttributeCount();
    for (int index = 0; index < size; index++) {
        if (in.getAttributeLocalName(index).equals(name)) {
            return Optional.of(in.getAttributeValue(index));
        }
    }
    return Optional.absent();
}

From source file:de.keyle.mypet.npc.util.UpdateCheck.java

public static Optional<String> checkForUpdate() {
    try {//from ww  w.j av a2s.c  o m
        String parameter = "";
        parameter += "build=" + MyPetNpcVersion.getBuild();

        // no data will be saved on the server
        String content = Util.readUrlContent("http://update.mypet-plugin.de/MyPet-NPC?" + parameter);
        JSONParser parser = new JSONParser();
        JSONObject result = (JSONObject) parser.parse(content);

        if (result.containsKey("latest")) {
            return Optional.of(result.get("latest").toString());
        }
    } catch (Exception ignored) {
    }
    return Optional.absent();
}

From source file:springfox.documentation.spring.web.readers.operation.ModelRefs.java

public static Optional<ModelRef> modelRef(Optional<ResolvedType> type, ModelContext modelContext,
        TypeNameExtractor nameExtractor) {
    if (!type.isPresent()) {
        return Optional.absent();
    }/*from w w  w.  j a  v  a2 s  .  c o  m*/
    return Optional.of(modelRef(type.get(), modelContext, nameExtractor));
}

From source file:org.eclipse.buildship.core.util.binding.Validators.java

public static Validator<File> requiredDirectoryValidator(final String prefix) {
    return new Validator<File>() {

        @Override/*  w  w w.  j a va2  s  . co  m*/
        public Optional<String> validate(File file) {
            if (file == null) {
                return Optional.of(NLS.bind(CoreMessages.ErrorMessage_0_MustBeSpecified, prefix));
            } else if (!file.exists()) {
                return Optional.of(NLS.bind(CoreMessages.ErrorMessage_0_DoesNotExist, prefix));
            } else if (!file.isDirectory()) {
                return Optional.of(NLS.bind(CoreMessages.ErrorMessage_0_MustBeDirectory, prefix));
            } else {
                return Optional.absent();
            }
        }
    };
}

From source file:com.google.errorprone.ErrorProneVersion.java

/**
 * Loads the Error Prone version./*from w ww  . j  av  a 2s  .  co  m*/
 *
 * <p>This depends on the Maven build, and will always return {@code Optional.absent()} with
 * other build systems.
 */
public static Optional<String> loadVersionFromPom() {
    try (InputStream stream = ErrorProneVersion.class.getResourceAsStream(PROPERTIES_RESOURCE)) {
        if (stream == null) {
            return Optional.absent();
        }
        Properties mavenProperties = new Properties();
        mavenProperties.load(stream);
        return Optional.of(mavenProperties.getProperty("version"));
    } catch (IOException expected) {
        return Optional.absent();
    }
}

From source file:com.infobip.jira.Version.java

public static Version of(String name, ProjectKey projectKey, Boolean released, Date releaseDate) {

    return new Version(name, Optional.of(projectKey), released, formatter.print(new DateTime(releaseDate)),
            Optional.<Integer>absent());
}

From source file:com.github.rinde.rinsim.geom.LengthData.java

/**
 * Create a new {@link LengthData} instance using the specified length.
 * @param length The length of the connection.
 * @return A new instance./*from w  w w . jav a2 s.  c om*/
 */
public static LengthData create(double length) {
    checkArgument(length >= 0d && Doubles.isFinite(length),
            "Only positive values are allowed for length, it is: %s.", length);
    return new AutoValue_LengthData(Optional.of(length));
}