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

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

Introduction

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

Prototype

public abstract T get();

Source Link

Document

Returns the contained instance, which must be present.

Usage

From source file:google.registry.keyring.api.PgpHelper.java

/**
 * Search for public key on keyring based on a substring (like an email address).
 *
 * @throws VerifyException if the key couldn't be found.
 * @see #lookupKeyPair//from  w w w . ja  v  a2 s.com
 */
public static PGPPublicKey lookupPublicKey(PGPPublicKeyRingCollection keyring, String query,
        KeyRequirement want) {
    try {
        // Safe by specification.
        @SuppressWarnings("unchecked")
        Iterator<PGPPublicKeyRing> results = keyring.getKeyRings(checkNotNull(query, "query"), true, true);
        verify(results.hasNext(), "No public key found matching substring: %s", query);
        while (results.hasNext()) {
            Optional<PGPPublicKey> result = lookupPublicSubkey(results.next(), want);
            if (result.isPresent()) {
                return result.get();
            }
        }
        throw new VerifyException(
                String.format("No public key (%s) found matching substring: %s", want, query));
    } catch (PGPException e) {
        throw new VerifyException(
                String.format("Public key lookup with query %s failed: %s", query, e.getMessage()));
    }
}

From source file:io.mesosphere.mesos.util.ProtoUtils.java

@NotNull
public static Optional<Long> resourceValueLong(@NotNull final Optional<Resource> resource) {
    if (resource.isPresent()) {
        if (resource.get().getType() != Value.Type.SCALAR) {
            throw new IllegalArgumentException("Resource must be of type SCALAR");
        }/*from   ww  w  .  j  ava 2 s  . c o  m*/
        final long value = (long) resource.get().getScalar().getValue();
        return Optional.of(value);
    } else {
        return Optional.absent();
    }
}

From source file:org.brooklyncentral.catalog.scrape.CatalogItemScraper.java

@SuppressWarnings("unchecked")
private static List<String> parseDirectoryYaml(String repoUrl) {
    Optional<String> directoryYamlString = getGithubRawText(repoUrl, "directory.yaml");

    if (!directoryYamlString.isPresent()) {
        throw new IllegalStateException("Failed to read catalog directory.");
    }/*www . j  a v a  2 s .  c o m*/

    return (List<String>) Yamls.parseAll(directoryYamlString.get()).iterator().next();
}

From source file:org.apache.rya.indexing.pcj.fluo.app.query.StatementPatternIdManager.java

/**
 * Remove specified Set of ids from the Fluo table and updates the entry with Column
 * {@link FluoQueryColumns#STATEMENT_PATTERN_IDS}. Also updates the hash of the updated nodeId Set and writes that
 * to the Column {@link FluoQueryColumns#STATEMENT_PATTERN_IDS_HASH}
 *
 * @param tx - Fluo Transaction object for performing atomic operations on Fluo table.
 * @param ids - ids to remove from the StatementPattern nodeId Set
 *//*from ww w .  j  av a  2 s  .c  o m*/
public static void removeStatementPatternIds(TransactionBase tx, Set<String> ids) {
    checkNotNull(tx);
    checkNotNull(ids);
    Optional<Bytes> val = Optional.fromNullable(tx.get(Bytes.of(STATEMENT_PATTERN_ID), STATEMENT_PATTERN_IDS));
    Set<String> storedIds = new HashSet<>();
    if (val.isPresent()) {
        storedIds = Sets.newHashSet(val.get().toString().split(VAR_DELIM));
    }
    storedIds.removeAll(ids);
    String idString = Joiner.on(VAR_DELIM).join(ids);
    tx.set(Bytes.of(STATEMENT_PATTERN_ID), STATEMENT_PATTERN_IDS, Bytes.of(idString));
    tx.set(Bytes.of(STATEMENT_PATTERN_ID), STATEMENT_PATTERN_IDS_HASH,
            Bytes.of(Hashing.sha256().hashString(idString).toString()));
}

From source file:com.dangdang.ddframe.job.lite.lifecycle.internal.reg.RegistryCenterFactory.java

/**
 * .//from   w w w .j a v  a  2 s  . c  o m
 *
 * @param connectString 
 * @param namespace ??
 * @param digest ?
 * @return 
 */
public static CoordinatorRegistryCenter createCoordinatorRegistryCenter(final String connectString,
        final String namespace, final Optional<String> digest) {
    Hasher hasher = Hashing.md5().newHasher().putString(connectString, Charsets.UTF_8).putString(namespace,
            Charsets.UTF_8);
    if (digest.isPresent()) {
        hasher.putString(digest.get(), Charsets.UTF_8);
    }
    HashCode hashCode = hasher.hash();
    CoordinatorRegistryCenter result = REG_CENTER_REGISTRY.get(hashCode);
    if (null != result) {
        return result;
    }
    ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace);
    if (digest.isPresent()) {
        zkConfig.setDigest(digest.get());
    }
    result = new ZookeeperRegistryCenter(zkConfig);
    result.init();
    REG_CENTER_REGISTRY.put(hashCode, result);
    return result;
}

From source file:org.opendaylight.restconf.restful.utils.RestconfInvokeOperationsUtil.java

/**
 * Invoking rpc via mount point/* w w w.j a  v a 2  s.co  m*/
 *
 * @param mountPoint
 *            - mount point
 * @param data
 *            - input data
 * @param schemaPath
 *            - schema path of data
 * @return {@link CheckedFuture}
 */
public static DOMRpcResult invokeRpcViaMountPoint(final DOMMountPoint mountPoint,
        final NormalizedNode<?, ?> data, final SchemaPath schemaPath) {
    final Optional<DOMRpcService> mountPointService = mountPoint.getService(DOMRpcService.class);
    if (mountPointService.isPresent()) {
        final CheckedFuture<DOMRpcResult, DOMRpcException> rpc = mountPointService.get().invokeRpc(schemaPath,
                data);
        return prepareResult(rpc);
    }
    final String errmsg = "RPC service is missing.";
    LOG.debug(errmsg);
    throw new RestconfDocumentedException(errmsg);
}

From source file:com.eucalyptus.cassandra.config.CassandraSysUtil.java

static void createKeyspaces() {
    CassandraKeyspaces.all(CassandraCluster.datacenter(), 1).forEach(t -> {
        final String keyspace = t._1();
        final Optional<Throwable> throwableOptional = t._2();
        if (throwableOptional.isPresent()) {
            logger.error("Error processing keyspace " + keyspace, throwableOptional.get());
        } else {//from w  w  w. jav  a  2  s  . c  o  m
            logger.info("Processed keyspace " + keyspace);
        }
    });
}

From source file:org.apache.gobblin.runtime.JobLauncherFactory.java

/**
 * Creates a new instance for a JobLauncher with a given type
 * @param sysProps          the system/environment properties
 * @param jobProps          the job properties
 * @param launcherTypeValue the type of the launcher; either a {@link JobLauncherType} value or
 *        the name of the class that extends {@link AbstractJobLauncher} and has a constructor
 *        that has a single Properties parameter..
 * @return the JobLauncher instance//  w  w  w  . j  av a  2s  .com
 * @throws RuntimeException if the instantiation fails
 */
public static JobLauncher newJobLauncher(Properties sysProps, Properties jobProps, String launcherTypeValue,
        SharedResourcesBroker<GobblinScopeTypes> instanceBroker) {
    Optional<JobLauncherType> launcherType = Enums.getIfPresent(JobLauncherType.class, launcherTypeValue);

    try {
        if (launcherType.isPresent()) {
            switch (launcherType.get()) {
            case LOCAL:
                return new LocalJobLauncher(
                        JobConfigurationUtils.combineSysAndJobProperties(sysProps, jobProps), instanceBroker);
            case MAPREDUCE:
                return new MRJobLauncher(JobConfigurationUtils.combineSysAndJobProperties(sysProps, jobProps),
                        instanceBroker);
            default:
                throw new RuntimeException("Unsupported job launcher type: " + launcherType.get().name());
            }
        }

        @SuppressWarnings("unchecked")
        Class<? extends AbstractJobLauncher> launcherClass = (Class<? extends AbstractJobLauncher>) Class
                .forName(launcherTypeValue);
        return launcherClass.getDeclaredConstructor(Properties.class)
                .newInstance(JobConfigurationUtils.combineSysAndJobProperties(sysProps, jobProps));
    } catch (Exception e) {
        throw new RuntimeException("Failed to create job launcher: " + e, e);
    }
}

From source file:org.opendaylight.controller.md.sal.dom.store.impl.tree.data.StoreMetadataNode.java

public static Optional<StoreMetadataNode> getChild(final Optional<StoreMetadataNode> parent,
        final PathArgument child) {
    if (parent.isPresent()) {
        return parent.get().getChild(child);
    }/*from www .j  a  v  a 2 s . c o  m*/
    return Optional.absent();
}

From source file:io.crate.planner.Limits.java

public static Optional<Symbol> mergeMin(Optional<Symbol> limit1, Optional<Symbol> limit2) {
    if (limit1.isPresent()) {
        if (limit2.isPresent()) {
            return Optional
                    .of(new Function(LeastFunction.TWO_LONG_INFO, Arrays.asList(limit1.get(), limit2.get())));
        }//from  ww  w.j  ava  2 s  .  c  o  m
        return limit1;
    }
    return limit2;
}