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

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

Introduction

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

Prototype

public abstract boolean isPresent();

Source Link

Document

Returns true if this holder contains a (non-null) instance.

Usage

From source file:gobblin.http.HttpClientConfiguratorLoader.java

private static Class<? extends HttpClientConfigurator> getConfiguratorClass(Optional<String> configuratorType)
        throws ClassNotFoundException {
    return configuratorType.isPresent() ? TYPE_RESOLVER.resolveClass(configuratorType.get())
            : DEFAULT_CONFIGURATOR_CLASS;
}

From source file:net.pterodactylus.sonitus.io.Mp3Identifier.java

/**
 * Tries to identify the MP3 file contained in the given stream.
 *
 * @param inputStream//from w  ww .ja va  2  s .c om
 *       The input stream
 * @return The identified metadata, or {@link Optional#absent()} if the
 *         metadata can not be identified
 * @throws IOException
 *       if an I/O error occurs
 */
public static Optional<Metadata> identify(InputStream inputStream) throws IOException {
    Parser mp3Parser = new Parser(inputStream);
    Frame frame = mp3Parser.nextFrame();
    FormatMetadata formatMetadata = new FormatMetadata((frame.channelMode() == SINGLE_CHANNEL) ? 1 : 2,
            frame.samplingRate(), "MP3");
    ContentMetadata contentMetadata = new ContentMetadata("");
    /* check for ID3v2 tag. */
    Optional<byte[]> id3v2TagBuffer = mp3Parser.getId3Tag();
    if (id3v2TagBuffer.isPresent()) {
        byte[] buffer = id3v2TagBuffer.get();
        ByteArrayInputStream tagInputStream = new ByteArrayInputStream(
                Arrays.copyOfRange(buffer, 3, buffer.length));
        try {
            /* skip ID3? header tag. */
            ID3V2Tag id3v2Tag = ID3V2Tag.read(tagInputStream);
            if (id3v2Tag != null) {
                contentMetadata = contentMetadata.artist(id3v2Tag.getArtist()).name(id3v2Tag.getTitle());
            }
        } catch (ID3Exception id3e1) {
            id3e1.printStackTrace();
        } finally {
            close(tagInputStream, true);
        }
    }
    return Optional.of(new Metadata(formatMetadata, contentMetadata));
}

From source file:com.google.caliper.runner.PlatformModule.java

/**
 * Chooses the {@link DalvikPlatform} if available, otherwise uses the default
 * {@link JvmPlatform}./* www. j av  a  2  s . c o  m*/
 */
@Provides
static Platform providePlatform(Optional<DalvikPlatform> optionalDalvikPlatform,
        Provider<JvmPlatform> jvmPlatformProvider) {
    if (optionalDalvikPlatform.isPresent()) {
        return optionalDalvikPlatform.get();
    } else {
        return jvmPlatformProvider.get();
    }
}

From source file:org.opendaylight.groupbasedpolicy.neutron.ovsdb.util.NeutronHelper.java

/**
 * This looks up the Endpoint L2 key from an
 * operational data store kept in neutron-mapper
 *
 * @param externalId The neutron port UUID
 * @param dataBroker {@link DataBroker} to use for the transaction
 * @return {@link EndpointKey} of the matching Endpoint, null if not found
 *//*from   w w w . ja v a  2  s .c om*/
public static EndpointKey getEpKeyFromNeutronMapper(UniqueId externalId, DataBroker dataBroker) {

    ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
    InstanceIdentifier<EndpointByPort> iid = InstanceIdentifier.create(Mappings.class)
            .child(GbpByNeutronMappings.class).child(EndpointsByPorts.class)
            .child(EndpointByPort.class, new EndpointByPortKey(externalId));
    Optional<EndpointByPort> optionalEp = readFromDs(LogicalDatastoreType.OPERATIONAL, iid, transaction);
    if (optionalEp.isPresent()) {
        EndpointByPort epByPort = optionalEp.get();
        return new EndpointKey(epByPort.getL2Context(), epByPort.getMacAddress());
    }
    return null;
}

From source file:org.sonar.server.computation.formula.coverage.CoverageUtils.java

static MeasureVariations getMeasureVariations(CounterInitializationContext counterContext, String metricKey) {
    Optional<Measure> measure = counterContext.getMeasure(metricKey);
    if (!measure.isPresent() || !measure.get().hasVariations()) {
        return DEFAULT_VARIATIONS;
    }//ww w  .  j  a  va2s  . c om
    return measure.get().getVariations();
}

From source file:org.obm.push.mail.bean.Flag.java

public static Flag from(String value) {
    Optional<Flag> systemFlag = isSystemFlag(value);
    if (systemFlag.isPresent()) {
        return systemFlag.get();
    }// w w w  .  j  a va  2  s .  co m
    return new Flag(value, false);
}

From source file:org.locationtech.geogig.repository.SpatialOps.java

@Nullable
public static Envelope boundsOf(RevFeature feat) {
    Envelope env = null;//from  w  w w  . jav  a 2 s .c  om
    for (Optional<Object> opt : feat.getValues()) {
        if (opt.isPresent() && opt.get() instanceof Geometry) {
            if (env == null) {
                env = new Envelope();
            }
            env.expandToInclude(((Geometry) opt.get()).getEnvelopeInternal());
        }
    }
    return env;
}

From source file:org.locationtech.geogig.remotes.internal.RemoteResolver.java

/**
 * Constructs an interface to allow access to a remote repository.
 * //from   w  w w  .  j  av a2s.com
 * @param remote the remote to connect to
 * @param remoteHints hints for the remote repo, like read-only, etc.
 * @return an {@link Optional} of the interface to the remote repository, or
 *         {@link Optional#absent()} if a connection to the remote could not be established.
 */
public static Optional<IRemoteRepo> newRemote(Remote remote, @Nullable Hints remoteHints) {

    if (remoteHints == null) {
        remoteHints = new Hints();
    }

    Iterator<RemoteResolver> resolvers = ServiceLoader
            .load(RemoteResolver.class, RemoteResolver.class.getClassLoader()).iterator();

    while (resolvers.hasNext()) {
        RemoteResolver resolver = resolvers.next();
        Optional<IRemoteRepo> resolved = resolver.resolve(remote, remoteHints);
        if (resolved.isPresent()) {
            return resolved;
        }
    }
    return Optional.absent();
}

From source file:org.locationtech.geogig.storage.impl.Blobs.java

public static Optional<String> getBlobAsString(BlobStore blobStore, String blobName) {
    Optional<byte[]> blob = getBlob(blobStore, blobName);
    Optional<String> str = Optional.absent();
    if (blob.isPresent()) {
        str = Optional.of(new String(blob.get(), Charsets.UTF_8));
    }/*from ww  w .  j av a2  s  .  c  om*/
    return str;
}

From source file:com.palantir.giraffe.internal.ProcessStreamHandler.java

private static SharedByteArrayStream newStreamWithWindow(Optional<Integer> window) {
    if (window.isPresent()) {
        return new SharedByteArrayStream(window.get());
    } else {/*from  w w w . jav  a 2  s.  c o  m*/
        return new SharedByteArrayStream();
    }
}