Example usage for com.google.common.collect Iterables tryFind

List of usage examples for com.google.common.collect Iterables tryFind

Introduction

In this page you can find the example usage for com.google.common.collect Iterables tryFind.

Prototype

public static <T> Optional<T> tryFind(Iterable<T> iterable, Predicate<? super T> predicate) 

Source Link

Document

Returns an Optional containing the first element in iterable that satisfies the given predicate, if such an element exists.

Usage

From source file:org.jmingo.util.ReflectionUtils.java

/**
 * Gets a method by name ignoring case. Be careful, this method returns first matched method.
 *
 * @param type the class to be searched//  www  .j  a  va  2s. c o m
 * @param name the method name
 * @return the first first method from {@code type} that satisfies the given name or null if no method
 * in {@code type} matches the given name
 */
public static Method getMethodByName(Class<?> type, String name) {
    return Iterables.tryFind(Arrays.asList(type.getDeclaredMethods()),
            (Method method) -> org.apache.commons.lang3.StringUtils.equalsIgnoreCase(name, method.getName()))
            .orNull();
}

From source file:org.asoem.greyfish.utils.collect.Functionals.java

@SuppressWarnings("unchecked") // safe cast
public static <E> Optional<E> tryFind(final Iterable<E> iterable, final Predicate<? super E> predicate) {
    if (iterable instanceof Searchable) {
        return ((Searchable<E>) iterable).findFirst(predicate);
    } else {//  w ww .  j a va2s  .co  m
        return Iterables.tryFind(iterable, predicate);
    }
}

From source file:pt.ist.maidSyncher.utils.MiscUtils.java

public static SynchableObject findACObjectsById(long id, Class<? extends SynchableObject> clazz) {
    checkNotNull(clazz);//  ww w.  ja v  a  2  s  . c  o  m
    if (id <= 0)
        return null;
    MaidRoot maidRoot = MaidRoot.getInstance();
    PredicateFindGHObjectByClassAndId predicateFindGHObjectByClassAndId = new SynchableObject.ObjectFindStrategy.PredicateFindGHObjectByClassAndId(
            clazz, id);
    return Iterables.tryFind(maidRoot.getAcObjectsSet(), predicateFindGHObjectByClassAndId).get();

}

From source file:org.ow2.petals.cloud.manager.api.deployment.utils.NodeHelper.java

/**
 * Get a property from a set of properties or null if not found
 *
 * @param properties// w  ww  . j av a  2 s.  c om
 * @param name
 * @return
 */
public static final Property getProperty(List<Property> properties, final String name) {
    return Iterables.tryFind(properties, new Predicate<Property>() {
        public boolean apply(org.ow2.petals.cloud.manager.api.deployment.Property property) {
            return property != null && property.getName() != null && property.getName().equals(name);
        }
    }).orNull();
}

From source file:org.jmingo.util.ReflectionUtils.java

/**
 * Finds a method that satisfies the given predicate.
 *
 * @param type      the class to be searched
 * @param predicate the predicate/*w ww  .  j a v  a 2 s .  c o  m*/
 * @return the first first method from {@code type} that satisfies the given name or null if no method
 * in {@code type} matches the given name
 */
public static Method findMethod(Class<?> type, Predicate<Method> predicate) {
    return Iterables.tryFind(Arrays.asList(type.getDeclaredMethods()), predicate).orNull();
}

From source file:pt.ist.maidSyncher.domain.dsi.DSIMilestone.java

public ACMilestone getAcMilestone(final ACProject acProject) {
    Optional<ACMilestone> optionalMilestone = Iterables.tryFind(getAcMilestonesSet(),
            new Predicate<ACMilestone>() {
                @Override/* w w  w.  j a  v  a 2  s .c om*/
                public boolean apply(ACMilestone input) {
                    if (input == null)
                        return false;
                    return ObjectUtils.equals(input.getProject(), acProject);
                }
            });
    return optionalMilestone.orNull();
}

From source file:org.opendaylight.protocol.bgp.openconfig.impl.moduleconfig.DataBrokerFunction.java

public static <T extends ServiceRef & ChildOf<Module>> T getRibInstance(
        final BGPConfigModuleProvider configModuleOp, final Function<String, T> function,
        final String instanceName, final ReadOnlyTransaction rTx) {
    Preconditions.checkNotNull(rTx);/*from  w ww  . ja v  a 2s  .  c om*/
    try {
        final Optional<Service> maybeService = configModuleOp
                .readConfigService(new ServiceKey(DomAsyncDataBroker.class), rTx);
        if (maybeService.isPresent()) {
            final Optional<Instance> maybeInstance = Iterables.tryFind(maybeService.get().getInstance(),
                    new Predicate<Instance>() {
                        @Override
                        public boolean apply(final Instance instance) {
                            final String moduleName = OpenConfigUtil.getModuleName(instance.getProvider());
                            if (moduleName.equals(instanceName)) {
                                return true;
                            }
                            return false;
                        }
                    });
            if (maybeInstance.isPresent()) {
                return function.apply(maybeInstance.get().getName());
            }
        }
        return null;
    } catch (ReadFailedException e) {
        throw new IllegalStateException("Failed to read service.", e);
    }
}

From source file:com.greensopinion.swagger.jaxrsgen.mock.noswagger.model.ServerError.java

private String computeMessage(Throwable t) {
    Optional<Throwable> found = Iterables.tryFind(Throwables.getCausalChain(t), new Predicate<Throwable>() {

        @Override//from   ww w  .  ja  va  2s .  c  o m
        public boolean apply(Throwable input) {
            return input != null && !Strings.isNullOrEmpty(input.getMessage());
        }
    });
    if (found.isPresent()) {
        return found.get().getMessage();
    }
    return null;
}

From source file:org.asoem.greyfish.utils.collect.AbstractFunctionalList.java

@Override
public Optional<E> findFirst(final Predicate<? super E> predicate) {
    return Iterables.tryFind(this, predicate);
}

From source file:org.jmingo.util.DocumentUtils.java

private static Optional<Field> getIdField(List<Field> fields) {
    return Iterables.tryFind(fields, field -> field.isAnnotationPresent(Id.class));
}