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.ldp4j.tutorial.frontend.person.PersonMapper.java

static Typed<Person> toPerson(Individual<?, ?> individual) {
    MutablePerson person = new MutablePerson();
    Typed<Person> result = Typed.<Person>create(person);
    for (URI uri : Mapper.create(individual).types()) {
        result.withType(uri.toString());
    }//ww  w.ja  v  a  2 s  .  c o m
    Optional<URI> email = Optional.fromNullable(firstIndividualValue(individual, EMAIL));
    person.setEmail(email.isPresent() ? email.get().toString() : null);
    person.setName(firstLiteralValue(individual, NAME, String.class));
    Optional<URI> location = Optional.fromNullable(firstIndividualValue(individual, LOCATION));
    person.setLocation(location.isPresent() ? location.get().toString() : null);
    Optional<URI> workplaceHomepage = Optional
            .fromNullable(firstIndividualValue(individual, WORKPLACE_HOMEPAGE));
    person.setWorkplaceHomepage(workplaceHomepage.isPresent() ? workplaceHomepage.get().toString() : null);
    return result;
}

From source file:com.eucalyptus.util.async.AsyncExceptions.java

/**
 * Extract a web service error message from the given throwable.
 *
 * @param throwable The possibly web service caused throwable
 * @param defaultMessage The message to use if a service message is not found
 * @return The message or the default message
 *//* w  w  w . ja  v a 2s . co m*/
public static String asWebServiceErrorMessage(final Throwable throwable, final String defaultMessage) {
    String message = defaultMessage;
    final Optional<AsyncWebServiceError> serviceErrorOption = AsyncExceptions.asWebServiceError(throwable);
    if (serviceErrorOption.isPresent()) {
        message = serviceErrorOption.get().getMessage();
    }
    return message;
}

From source file:at.ac.univie.isc.asio.d2rq.D2rqTools.java

/**
 * Find the embedded base resource IRI if present.
 *
 * @param model given configuration// w w w.  j av  a 2 s.  c o m
 * @return base resource IRI embedded in model.
 */
static Optional<String> findEmbeddedBaseUri(final Model model) {
    final Optional<Resource> server = findSingleOfType(model, D2RConfig.Server);
    if (server.isPresent()) {
        final Resource baseUriProperty = server.get().getPropertyResourceValue(D2RConfig.baseURI);
        return baseUriProperty != null ? Optional.fromNullable(baseUriProperty.getURI())
                : Optional.<String>absent();
    } else {
        return Optional.absent();
    }
}

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

public static String getSourceStpOrFail(P2PServiceBaseType p2ps) {
    Optional<String> sourceStp = Optional.fromNullable(Strings.emptyToNull(p2ps.getSourceSTP()));
    if (sourceStp.isPresent()) {
        return sourceStp.get();
    }/*from  w w  w.  j av a 2s. co  m*/

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

From source file:org.jclouds.openstack.keystone.v2_0.suppliers.LocationIdToURIFromAccessForTypeAndVersion.java

private static <K, V> void putIfPresent(K key, Optional<V> value, Builder<K, V> builder) {
    if (value.isPresent())
        builder.put(key, value.get());
}

From source file:org.opendaylight.unimgr.utils.EvcUtils.java

/**
 * Delete EVC data from configuration datastore.
 * @param dataBroker The dataBroker instance to create transactions
 * @param optionalUni Optional Uni Node//from  w w w.  j a  v a  2  s.  c  o m
 */
public static void deleteEvcData(final DataBroker dataBroker, final Optional<Node> optionalUni) {
    if (optionalUni.isPresent()) {
        final UniAugmentation uniAugmentation = optionalUni.get().getAugmentation(UniAugmentation.class);
        final InstanceIdentifier<Node> ovsdbNodeIid = uniAugmentation.getOvsdbNodeRef().getValue()
                .firstIdentifierOf(Node.class);
        final Optional<Node> optionalOvsdNode = MdsalUtils.readNode(dataBroker,
                LogicalDatastoreType.OPERATIONAL, ovsdbNodeIid);
        if (optionalOvsdNode.isPresent()) {
            final Node ovsdbNode = optionalOvsdNode.get();
            final OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode
                    .getAugmentation(OvsdbNodeAugmentation.class);
            for (final ManagedNodeEntry managedNodeEntry : ovsdbNodeAugmentation.getManagedNodeEntry()) {
                final InstanceIdentifier<Node> bridgeIid = managedNodeEntry.getBridgeRef().getValue()
                        .firstIdentifierOf(Node.class);
                final Optional<Node> optBridgeNode = MdsalUtils.readNode(dataBroker, bridgeIid);
                if (optBridgeNode.isPresent()) {
                    final Node bridgeNode = optBridgeNode.get();
                    final InstanceIdentifier<TerminationPoint> iidGreTermPoint = UnimgrMapper
                            .getTerminationPointIid(bridgeNode, UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
                    final InstanceIdentifier<TerminationPoint> iidEthTermPoint = UnimgrMapper
                            .getTerminationPointIid(bridgeNode, UnimgrConstants.DEFAULT_TUNNEL_IFACE);
                    MdsalUtils.deleteNode(dataBroker, iidGreTermPoint, LogicalDatastoreType.CONFIGURATION);
                    MdsalUtils.deleteNode(dataBroker, iidEthTermPoint, LogicalDatastoreType.CONFIGURATION);
                }
            }
        }
    } else {
        LOG.info("Unable to retrieve UNI from the EVC.");
    }
}

From source file:io.crate.testing.SQLPrinter.java

public static String print(QuerySpec spec) {
    StringBuilder sb = new StringBuilder();

    sb.append("SELECT ");
    TESTING_SYMBOL_PRINTER.process(spec.outputs(), sb);

    if (spec.where().hasQuery()) {
        sb.append(" WHERE ");
        TESTING_SYMBOL_PRINTER.process(spec.where().query(), sb);
    }//from  w  w w.jav a2 s  . c  om
    if (spec.groupBy().isPresent()) {
        sb.append(" GROUP BY ");
        TESTING_SYMBOL_PRINTER.process(spec.groupBy().get(), sb);
    }
    if (spec.having().isPresent()) {
        sb.append(" HAVING ");
        TESTING_SYMBOL_PRINTER.process(spec.having().get().query(), sb);
    }
    if (spec.orderBy().isPresent()) {
        sb.append(" ORDER BY ");
        TESTING_SYMBOL_PRINTER.process(spec.orderBy().get(), sb);
    }
    Optional<Symbol> limit = spec.limit();
    if (limit.isPresent()) {
        sb.append(" LIMIT ");
        sb.append(print(limit.get()));
    }
    Optional<Symbol> offset = spec.offset();
    if (offset.isPresent()) {
        sb.append(" OFFSET ");
        sb.append(print(offset.get()));
    }
    return sb.toString();
}

From source file:com.bitranger.parknshop.common.recommend.collections.CollectionUtils.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <E> Iterable<E> fast(final Iterable<E> iter) {
    if (iter instanceof FastIterable) {
        return new Iterable<E>() {
            @Override//w w  w.  jav a 2 s.  co m
            public Iterator<E> iterator() {
                return ((FastIterable) iter).fastIterator();
            }
        };
    } else if (iter instanceof Cursor) {
        throw new IllegalArgumentException();
    } else {
        Optional<Method> fastMethod = fastIteratorMethods.getUnchecked(iter.getClass());
        if (fastMethod.isPresent()) {
            final Method method = fastMethod.get();
            return new Iterable<E>() {
                @Override
                public Iterator<E> iterator() {
                    try {
                        return (Iterator<E>) method.invoke(iter);
                    } catch (IllegalAccessException e) {
                        return iter.iterator();
                    } catch (InvocationTargetException e) {
                        throw Throwables.propagate(e.getCause());
                    }
                }
            };
        } else {
            return iter;
        }
    }
}

From source file:org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNodes.java

/**
 * Finds a node in tree//w w  w.  j  a va2 s . c o m
 * @param <T>
 *          Store tree node type.
 * @param tree Data Tree
 * @param path Path to the node
 * @return Optional with node if the node is present in tree, {@link Optional#absent()} otherwise.
 */
public static <T extends StoreTreeNode<T>> Optional<T> findNode(final T tree,
        final YangInstanceIdentifier path) {
    Optional<T> current = Optional.of(tree);
    Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
    while (current.isPresent() && pathIter.hasNext()) {
        current = current.get().getChild(pathIter.next());
    }
    return current;
}

From source file:org.inferred.internal.source.ModelUtils.java

/** Returns the {@link TypeElement} corresponding to {@code type}, if there is one. */
public static Optional<TypeElement> maybeAsTypeElement(TypeMirror type) {
    Optional<DeclaredType> declaredType = maybeDeclared(type);
    if (declaredType.isPresent()) {
        return maybeType(declaredType.get().asElement());
    } else {/*from   w  w w  . jav  a 2s . c o  m*/
        return Optional.absent();
    }
}