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:de.azapps.tools.OptionalUtils.java

public static <F> void withOptional(@NonNull final Optional<F> optional,
        @NonNull final Procedure<F> procedure) {
    if (optional.isPresent()) {
        procedure.apply(optional.get());
    }/*from  w  w  w.  jav  a 2s  .  c om*/
}

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

public static <T extends StoreTreeNode<T>> Optional<T> getChild(final Optional<T> parent,
        final PathArgument child) {
    if (parent.isPresent()) {
        return parent.get().getChild(child);
    }/*w w  w  . j  a v  a  2 s. co  m*/
    return Optional.absent();
}

From source file:org.eclipse.recommenders.codesearch.rcp.index.indexer.BindingHelper.java

public static Optional<String> getIdentifier(final IMethodBinding b) {
    if (b == null) {
        return absent();
    }/*from  ww  w .jav a2s .c o m*/
    final Optional<IMethodName> opt = BindingUtils.toMethodName(b);
    if (!opt.isPresent()) {
        return absent();
    }
    return of(opt.get().getIdentifier());
}

From source file:gobblin.compliance.utils.DatasetUtils.java

public static String getProperty(HivePartitionDataset dataset, String property, long defaultValue) {
    Optional<String> propertyValueOptional = Optional.fromNullable(dataset.getParams().get(property));
    if (!propertyValueOptional.isPresent()) {
        return Long.toString(defaultValue);
    }//  w w w  . j a  v a2s.com
    try {
        long propertyVal = Long.parseLong(propertyValueOptional.get());
        if (propertyVal < 0) {
            return Long.toString(defaultValue);
        } else {
            return Long.toString(propertyVal);
        }
    } catch (NumberFormatException e) {
        return Long.toString(defaultValue);
    }
}

From source file:com.arpnetworking.clusteraggregator.AggDataUnifier.java

private static Optional<Unit> getSmaller(final Optional<Unit> a, final Optional<Unit> b) {
    if (a.isPresent() && b.isPresent()) {
        return a.get().isSmallerThan(b.get()) ? a : b;
    } else {/*from   w w  w  .  j  a v  a 2 s  . c om*/
        return a.or(b);
    }
}

From source file:com.google.gerrit.server.query.change.IsReviewedPredicate.java

@SuppressWarnings("deprecation")
private static FieldDef<ChangeData, ?> getField(Schema<ChangeData> schema) {
    Optional<FieldDef<ChangeData, ?>> f = schema.getField(REVIEWEDBY, LEGACY_REVIEWED);
    checkState(f.isPresent(), "Schema %s missing field %s", schema.getVersion(), REVIEWEDBY.getName());
    return f.get();
}

From source file:springfox.documentation.swagger.readers.parameter.ParameterAnnotationReader.java

@SuppressWarnings("unchecked")
private static <A extends Annotation> A searchOnInterfaces(Method method, int parameterIndex,
        Class<A> annotationType, Class<?>[] interfaces) {

    A annotation = null;//from  w w w . j  a  v  a  2 s  .c o m
    for (Class<?> interfaze : interfaces) {
        Optional<Method> interfaceMethod = interfaceMethod(interfaze, method);
        if (interfaceMethod.isPresent()) {
            Method superMethod = interfaceMethod.get();
            Optional<Annotation> found = tryFind(
                    newArrayList(superMethod.getParameterAnnotations()[parameterIndex]),
                    annotationOfType(annotationType));
            if (found.isPresent()) {
                annotation = (A) found.get();
                break;
            }
            Class<?>[] superInterfaces = superMethod.getDeclaringClass().getInterfaces();
            annotation = searchOnInterfaces(superMethod, parameterIndex, annotationType, superInterfaces);
        }
    }
    return annotation;
}

From source file:org.opendaylight.alto.basic.simpleird.SimpleIrdUtils.java

public static IrdInstance readInstance(ResourceId instanceId, ReadTransaction rx)
        throws InterruptedException, ExecutionException {
    InstanceIdentifier<IrdInstance> iid = getInstanceIID(instanceId);

    Optional<IrdInstance> instance = rx.read(LogicalDatastoreType.OPERATIONAL, iid).get();
    if (instance.isPresent()) {
        return instance.get();
    }/*from  w w  w  .j a v  a 2s.c om*/
    return null;
}

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

public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final YangInstanceIdentifier path) {
    T current = tree;/*from  w w  w. j  a v  a 2  s  . co m*/

    int i = 1;
    for (PathArgument pathArg : path.getPathArguments()) {
        Optional<T> potential = current.getChild(pathArg);
        if (!potential.isPresent()) {
            throw new IllegalArgumentException(
                    String.format("Child %s is not present in tree.", path.getAncestor(i)));
        }
        current = potential.get();
        ++i;
    }
    return current;
}

From source file:com.github.filosganga.geogson.model.Matchers.java

public static <T> Matcher<Optional<? extends T>> optionalThatContain(final Matcher<? extends T> matcher) {

    return new TypeSafeMatcher<Optional<? extends T>>() {
        @Override//from  w w w  . j  a  v a2  s  .c  o m
        protected boolean matchesSafely(Optional<? extends T> item) {
            return item.isPresent() && matcher.matches(item.get());
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("Optional that contains ").appendDescriptionOf(matcher);
        }
    };

}