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:org.opendaylight.yangtools.yang.data.impl.schema.tree.AbstractDataTreeCandidateNode.java

static DataTreeCandidateNode deltaChild(
        final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
        final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData,
        final PathArgument identifier) {

    final Optional<NormalizedNode<?, ?>> maybeNewChild = getChild(newData, identifier);
    final Optional<NormalizedNode<?, ?>> maybeOldChild = getChild(oldData, identifier);
    if (maybeOldChild.isPresent()) {
        final NormalizedNode<?, ?> oldChild = maybeOldChild.get();
        if (maybeNewChild.isPresent()) {
            return AbstractRecursiveCandidateNode.replaceNode(oldChild, maybeNewChild.get());
        }/*from   w  w  w  . jav a2 s.co  m*/
        return AbstractRecursiveCandidateNode.deleteNode(oldChild);
    }

    return maybeNewChild.isPresent() ? AbstractRecursiveCandidateNode.writeNode(maybeNewChild.get()) : null;
}

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.//from   www  .j  a  v a2 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);
    }
}

From source file:springfox.bean.validators.plugins.Validators.java

private static <T extends Annotation> Optional<T> findAnnotation(
        Optional<? extends AnnotatedElement> annotatedElement, Class<T> annotationType) {
    if (annotatedElement.isPresent()) {
        return Optional.fromNullable(AnnotationUtils.findAnnotation(annotatedElement.get(), annotationType));
    } else {//from  w ww  . ja va 2  s . c  o  m
        return Optional.absent();
    }
}

From source file:net.es.nsi.pce.pf.PfUtils.java

public static String getDestinationStpOrFail(P2PServiceBaseType p2ps) {
    Optional<String> destStp = Optional.fromNullable(Strings.emptyToNull(p2ps.getDestSTP()));
    if (destStp.isPresent()) {
        return destStp.get();
    }// w w w . j a va2  s.c o  m

    throw Exceptions.missingParameter(Point2PointTypes.getSourceStp().getNamespace(),
            Point2PointTypes.getDestStp().getType(), "null");
}

From source file:org.jclouds.aliyun.ecs.domain.ResourceType.java

public static ResourceType fromValue(String value) {
    Optional<ResourceType> resourceType = Enums.getIfPresent(ResourceType.class, value.toUpperCase());
    checkArgument(resourceType.isPresent(), "Expected one of %s but was %s",
            Joiner.on(',').join(ResourceType.values()), value);
    return resourceType.get();
}

From source file:ch.epfl.eagle.daemon.util.ConfigUtil.java

/**
 * Parses the list of backends from a {@link Configuration}.
 *
 * Returns a map of address of backends to a {@link TResourceVector} describing the
 * total resource capacity for that backend.
 *//*from  w w  w . j a  v  a  2  s . c o  m*/
public static Set<InetSocketAddress> parseBackends(Configuration conf) {
    if (!conf.containsKey(EagleConf.STATIC_NODE_MONITORS)) {
        throw new RuntimeException("Missing configuration node monitor list");
    }

    Set<InetSocketAddress> backends = new HashSet<InetSocketAddress>();

    for (String node : conf.getStringArray(EagleConf.STATIC_NODE_MONITORS)) {
        Optional<InetSocketAddress> addr = Serialization.strToSocket(node);
        if (!addr.isPresent()) {
            LOG.warn("Bad backend address: " + node);
            continue;
        }
        backends.add(addr.get());
    }

    return backends;
}

From source file:org.apache.isis.viewer.restfulobjects.server.mappers.ExceptionMapperAbstract.java

private static String messageFor(final Throwable ex) {
    final List<Throwable> chain = Throwables.getCausalChain(ex);
    final Optional<RecoverableException> recoverableIfAny = FluentIterable.from(chain)
            .filter(RecoverableException.class).first();
    return (recoverableIfAny.isPresent() ? recoverableIfAny.get() : ex).getMessage();
}

From source file:utils.PasswordManager.java

private static Optional<BasicTextEncryptor> getEncryptor(Optional<String> masterPassword) {
    Optional<BasicTextEncryptor> encryptor;
    if (masterPassword.isPresent()) {
        encryptor = Optional.of(new BasicTextEncryptor());
        try {/* ww w .  j a  va  2  s . com*/
            encryptor.get().setPassword(masterPassword.get());
        } catch (Exception e) {
            LOG.error("Failed to set master password for encryptor", e);
            encryptor = Optional.absent();
        }
    } else {
        encryptor = Optional.absent();
    }

    return encryptor;
}

From source file:org.opendaylight.ovsdb.southbound.SouthboundUtil.java

public static Optional<OvsdbNodeAugmentation> getManagingNode(DataBroker db, OvsdbBridgeAttributes mn) {
    Preconditions.checkNotNull(mn);//from w  w w  .  ja  v  a 2  s.  c  om
    try {
        OvsdbNodeRef ref = mn.getManagedBy();
        if (ref != null && ref.getValue() != null) {
            ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
            @SuppressWarnings("unchecked") // Note: erasure makes this safe in combination with the typecheck below
            InstanceIdentifier<Node> path = (InstanceIdentifier<Node>) ref.getValue();

            CheckedFuture<Optional<Node>, ReadFailedException> nf = transaction
                    .read(LogicalDatastoreType.OPERATIONAL, path);
            transaction.close();
            Optional<Node> optional = nf.get();
            if (optional != null && optional.isPresent()) {
                OvsdbNodeAugmentation ovsdbNode = null;
                if (optional.get() instanceof Node) {
                    ovsdbNode = optional.get().getAugmentation(OvsdbNodeAugmentation.class);
                } else if (optional.get() instanceof OvsdbNodeAugmentation) {
                    ovsdbNode = (OvsdbNodeAugmentation) optional.get();
                }
                if (ovsdbNode != null) {
                    return Optional.of(ovsdbNode);
                } else {
                    LOG.warn("OvsdbManagedNode {} claims to be managed by {} but "
                            + "that OvsdbNode does not exist", mn, ref.getValue());
                    return Optional.absent();
                }
            } else {
                LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}", optional);
                return Optional.absent();
            }
        } else {
            LOG.warn("Cannot find client for OvsdbManagedNode without a specified ManagedBy {}", mn);
            return Optional.absent();
        }
    } catch (Exception e) {
        LOG.warn("Failed to get OvsdbNode that manages OvsdbManagedNode {}", mn, e);
        return Optional.absent();
    }
}

From source file:org.eclipse.buildship.core.workspace.internal.ProjectNatureUpdater.java

private static Set<String> toNatures(Optional<List<OmniEclipseProjectNature>> projectNatures,
        IProjectDescription description) {
    Set<String> natures = Sets.newLinkedHashSet();
    if (projectNatures.isPresent()) {
        natures.addAll(toNatures(projectNatures.get()));
    } else {//from  www.j ava  2  s  . c  om
        natures.addAll(Arrays.asList(description.getNatureIds()));
    }
    natures.add(GradleProjectNature.ID);
    return natures;
}