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:ninja.utils.NinjaModeHelper.java

/**
 * returns NinjaMode.dev if no mode is set. Or the valid mode
 * set via a System Property called "ninja.mode".
 * //from  w  w w  . ja  va 2s  .com
 * E.g. under mvn you can use mvn ... -Dninja.mode=prod or so. Valid values
 * for ninja.mode are "prod", "dev", "test".
 * 
 * @return The valid mode set via a System Property called "ninja.mode"
 *          or NinjaMode.dev if it is not set.
 */
public static NinjaMode determineModeFromSystemPropertiesOrProdIfNotSet() {

    Optional<NinjaMode> ninjaModeOptional = determineModeFromSystemProperties();
    NinjaMode ninjaMode;

    if (!ninjaModeOptional.isPresent()) {
        ninjaMode = NinjaMode.prod;
    } else {
        ninjaMode = ninjaModeOptional.get();
    }

    logger.info("Ninja is running in mode {}", ninjaMode.toString());

    return ninjaMode;

}

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 {/*ww  w.j  a va 2s . co  m*/
            logger.info("Processed keyspace " + keyspace);
        }
    });
}

From source file:com.github.jeluard.extend.osgi.OSGILoader.java

private static <T> T setIfAbsentAndReturn(final AtomicReference<Optional<T>> reference,
        final Supplier<T> callable) {
    //TODO conc./*from   w  ww  .  j  av  a  2  s  . co  m*/
    final Optional<T> optionalValue = reference.get();
    if (optionalValue.isPresent()) {
        return optionalValue.get();
    }

    final T value = callable.get();
    reference.set(Optional.fromNullable(value));
    return value;
}

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

/**
 * Invoking rpc via mount point/*from   w  ww. jav  a2  s.  com*/
 *
 * @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:org.apache.gobblin.metrics.event.EventSubmitter.java

/**
 * Calls submit on submitter if present.
 *///from  w  w  w.  j  ava2 s.  c  om
public static void submit(Optional<EventSubmitter> submitter, String name) {
    if (submitter.isPresent()) {
        submitter.get().submit(name);
    }
}

From source file:org.kuali.rice.core.web.util.PropertySources.java

/**
 * Check system properties and servlet context init params for an annotated Spring configuration
 * class located under {@code key}. If present, create a new
 * {@AnnotationConfigWebApplicationContext} and register
 * the annotated configuration class. Refresh the context and then examine it for a
 * {@code PropertySource<?>} bean. There must be exactly one {@code PropertySource<?>} bean
 * present in the application context.//from  w  w w  .j a  v  a2s . c  o m
 */
public static Optional<PropertySource<?>> getPropertySource(ServletContextEvent sce, String key) {
    Optional<String> annotatedClass = getProperty(sce, key);
    if (annotatedClass.isPresent()) {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setServletContext(sce.getServletContext());
        PropertySource<?> propertySource = getPropertySource(annotatedClass.get(), context);
        return Optional.<PropertySource<?>>of(propertySource);
    } else {
        return Optional.absent();
    }
}

From source file:com.google.gitiles.ArchiveFormat.java

static ArchiveFormat getDefault(Config cfg) {
    for (String allowed : cfg.getStringList("archive", null, "format")) {
        Optional<ArchiveFormat> result = Enums.getIfPresent(ArchiveFormat.class, allowed.toUpperCase());
        if (result.isPresent()) {
            return result.get();
        }//w  w  w  .  ja  v a2  s  .  co  m
    }
    return TGZ;
}

From source file:com.arpnetworking.utility.SampleUtils.java

/**
 * Converts all of the samples to a single unit.
 *
 * @param samples samples to convert//from   ww  w .ja  v  a 2s  .  c om
 * @return list of samples with a unified unit
 */
public static List<Quantity> unifyUnits(final List<Quantity> samples) {
    //This is a 2-pass operation:
    //First pass is to grab the smallest unit in the samples
    //Second pass is to convert everything to that unit
    Optional<Unit> smallestUnit = Optional.absent();
    for (final Quantity sample : samples) {
        if (!smallestUnit.isPresent()) {
            smallestUnit = sample.getUnit();
        } else if (sample.getUnit().isPresent()
                && smallestUnit.get().getSmallerUnit(sample.getUnit().get()) != smallestUnit.get()) {
            smallestUnit = sample.getUnit();
        }
    }

    if (smallestUnit.isPresent()) {
        return FluentIterable.from(samples).transform(new SampleConverter(smallestUnit.get())).toList();
    }

    return samples;
}

From source file:org.opendaylight.genius.interfacemanager.globals.InterfaceServiceUtil.java

public static short getVlanId(String interfaceName, DataBroker broker) {
    InstanceIdentifier<Interface> id = InstanceIdentifier.builder(Interfaces.class)
            .child(Interface.class, new InterfaceKey(interfaceName)).build();
    Optional<Interface> ifInstance = MDSALUtil.read(LogicalDatastoreType.CONFIGURATION, id, broker);
    if (ifInstance.isPresent()) {
        IfL2vlan vlanIface = ifInstance.get().getAugmentation(IfL2vlan.class);
        return vlanIface.getVlanId() == null ? 0 : vlanIface.getVlanId().getValue().shortValue();
    }// w w  w  .  j  a va2 s .  c om
    return -1;
}

From source file:utils.PasswordManager.java

/**
 * Decrypt an encrypted password. A master password must have been provided in the constructor.
 * @param encrypted An encrypted password.
 * @return The decrypted password./*  w  w  w . j  a  v a  2  s  . c o m*/
 */
public static String decryptPassword(String encrypted, Optional<String> masterPassword) {
    Optional<BasicTextEncryptor> encryptor = getEncryptor(masterPassword);
    Preconditions.checkArgument(encryptor.isPresent(), ERROR_DECRY_MSG);

    try {
        return encryptor.get().decrypt(encrypted);
    } catch (Exception e) {
        throw new RuntimeException("Failed to decrypt password " + encrypted, e);
    }
}