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

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

Introduction

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

Prototype

@Nullable
public static <T> T find(Iterable<? extends T> iterable, Predicate<? super T> predicate,
        @Nullable T defaultValue) 

Source Link

Document

Returns the first element in iterable that satisfies the given predicate, or defaultValue if none found.

Usage

From source file:org.artifactory.repo.interceptor.YumCalculationInterceptor.java

private boolean isYumGroupFile(final String path, LocalRepoDescriptor hostingLocalRepo) {
    List<String> yumGroupFileNames = PathUtils
            .delimitedListToStringList(hostingLocalRepo.getYumGroupFileNames(), ",");

    return StringUtils.isNotBlank(Iterables.find(yumGroupFileNames, new Predicate<String>() {
        @Override/*from   ww w. j a  va  2s  .  c om*/
        public boolean apply(@Nullable String input) {
            return StringUtils.endsWith(path, YumAddon.REPO_DATA_DIR + input);
        }
    }, null));
}

From source file:controllers.factories.ViewModelFactory.java

@SuppressWarnings("unchecked")
private ViewModel createSpecific(JsonNode json) {
    if (!json.has("type")) {
        return null;
    }//from   w w  w . ja  va  2 s  .c  o  m

    String type = json.get("type").asText();
    if (StringUtils.isEmpty(type)) {
        return null;
    }

    if (type.endsWith(MODEL_SUFFIX)) {
        StringUtils.stripEnd(type, MODEL_SUFFIX);
    }

    final String typeName = type;
    Reflections reflections = new Reflections("edu.sabanciuniv.sentilab.sare.models");
    Class<? extends PersistentObject> model = Iterables.find(reflections.getSubTypesOf(PersistentObject.class),
            new Predicate<Class<? extends PersistentObject>>() {
                @Override
                public boolean apply(@Nullable Class<? extends PersistentObject> input) {
                    return typeName.equals(ClassUtils.getShortClassName(input));
                }
            }, null);

    Class<? extends ViewModel> availableViewModelClass = null;
    while (model != null) {
        final String modelName = ClassUtils.getShortClassName(model);
        reflections = new Reflections("models", Play.application().classloader());
        availableViewModelClass = Iterables.find(reflections.getSubTypesOf(ViewModel.class),
                new Predicate<Class<? extends ViewModel>>() {
                    @Override
                    public boolean apply(Class<? extends ViewModel> viewModelClass) {
                        return ClassUtils.getShortClassName(viewModelClass).equals(modelName + MODEL_SUFFIX);
                    }
                }, null);

        if (availableViewModelClass != null) {
            break;
        }

        model = (Class<? extends PersistentObject>) model.getSuperclass();
        if (!PersistentObject.class.isAssignableFrom(model)) {
            break;
        }
    }

    if (availableViewModelClass == null) {
        return null;
    }

    return Json.fromJson(json, availableViewModelClass);
}

From source file:org.eclipse.emf.compare.uml2.internal.postprocessor.extension.sequence.UMLMessageChangeFactory.java

/**
 * {@inheritDoc}/*from   w w  w.  j  a v a 2 s  .  c  o m*/
 * 
 * @see org.eclipse.emf.compare.uml2.internal.postprocessor.AbstractUMLChangeFactory#getDiscriminant(org.eclipse.emf.compare.Diff)
 */
@Override
protected EObject getDiscriminant(Diff input) {
    return Iterables.find(getDiscriminants(input), instanceOf(Message.class), null);
}

From source file:com.slimgears.slimrepo.apt.MetaFields.java

private static <P extends PropertyInfo> P findFieldByName(Iterable<? extends P> fields, String... names) {
    final Set<String> nameSet = new HashSet<>(Arrays.asList(names));
    return Iterables.find(fields, field -> nameSet.contains(field.getName()), null);
}

From source file:controllers.modules.AspectLexBuilder.java

@Override
public String getRoute() {
    DocumentCorpusModel corpus = (DocumentCorpusModel) Iterables.find(this.viewModels,
            Predicates.instanceOf(DocumentCorpusModel.class), null);
    AspectLexiconModel lexicon = (AspectLexiconModel) Iterables.find(this.viewModels,
            Predicates.instanceOf(AspectLexiconModel.class), null);
    return controllers.modules.routes.AspectLexBuilder
            .modulePage(corpus != null ? corpus.getIdentifier() : null,
                    lexicon != null ? lexicon.getIdentifier() : null, false)
            .url();//  w w  w .  j a  va  2s  .com
}

From source file:com.censoredsoftware.infractions.bukkit.legacy.util.MiscUtil.java

public static String getInfractionsPlayer(final String guess) {
    Dossier dossier = Iterables.find(Infractions.allDossiers(), new Predicate<Dossier>() {
        @Override// w w  w . ja  v  a 2s  . co m
        public boolean apply(Dossier dossier) {
            return dossier instanceof CompleteDossier && ((CompleteDossier) dossier).getLastKnownName()
                    .toLowerCase().startsWith(guess.toLowerCase());
        }
    }, null);
    if (dossier != null && dossier.getId() != null)
        return ((CompleteDossier) dossier).getLastKnownName();
    return null;
}

From source file:org.polymap.core.data.operations.feature.CopyGeoResourceDropAction.java

@Override
public void perform(IProgressMonitor monitor) {
    final IResolve data = (IResolve) getData();
    final IResolve destination = (IResolve) getDestination();
    try {//ww w  .j  av a 2  s  . c  o  m
        // check ACL permission
        if (destination instanceof IAdaptable) {
            ACL acl = (ACL) ((IAdaptable) destination).getAdapter(ACL.class);
            if (acl != null) {
                if (!ACLUtils.checkPermission(acl, AclPermission.WRITE, false)) {
                    throw new RuntimeException(i18n.get("accessErrorMsg"));
                }
            }
        }

        final AtomicBoolean confirmed = new AtomicBoolean();
        final Display display = Polymap.getSessionDisplay();
        display.syncExec(new Runnable() {
            public void run() {
                confirmed.set(MessageDialog.openConfirm(display.getActiveShell(), i18n.get("confirmTitle"),
                        i18n.get("confirmMsg", data.getTitle(), destination.getTitle())));
            }
        });

        if (confirmed.get()) {
            final FeatureSource srcFs = data.resolve(FeatureSource.class, monitor);
            final DataAccess destDs = destination.resolve(DataAccess.class, monitor);

            if (destDs == null) {
                throw new RuntimeException("Wrong destination type: " + getDestination());
            }

            // check schema
            Name schemaExists = Iterables.find(destDs.getNames(), new Predicate<Name>() {
                String srcName = srcFs.getSchema().getName().getLocalPart();

                public boolean apply(Name input) {
                    return input.getLocalPart().equals(srcName);
                }
            }, null);
            // create schema
            if (schemaExists == null) {
                destDs.createSchema(srcFs.getSchema());
            }

            // copy features
            FeatureStore destFs = (FeatureStore) destDs.getFeatureSource(srcFs.getSchema().getName());
            List createdFids = destFs
                    .addFeatures(srcFs.getFeatures()/*, new ProgressListenerAdaptor( monitor )*/ );
        }
    } catch (Exception e) {
        PolymapWorkbench.handleError(DataPlugin.PLUGIN_ID, this, i18n.get("errorMsg"), e);
    } finally {
        // reload also on error as the schema may has been created
        IService reset = null;
        if (destination instanceof IService) {
            reset = (IService) destination;
        } else if (destination instanceof IResolveFolder) {
            reset = ((IResolveFolder) destination).getService(monitor);
        }
        if (reset != null) {
            ResetServiceAction.reset(Collections.singletonList(reset), monitor);
        }
    }
}

From source file:org.sonar.core.test.DefaultTestCase.java

public CoverageBlock coverageBlock(final Testable testable) {
    return Iterables.find(getEdges(DefaultCoverageBlock.class, Direction.OUT, "covers"),
            new Predicate<CoverageBlock>() {
                public boolean apply(CoverageBlock input) {
                    return input.testable().component().key().equals(testable.component().key());
                }/*from  w  w  w. j a  v  a 2s. c o m*/
            }, null);
}

From source file:org.bonitasoft.studio.businessobject.core.expression.QueryExpressionProvider.java

protected List<Expression> getExpressions(final BusinessObject bo) {
    final List<Expression> result = new ArrayList<Expression>();
    for (final Query q : BDMQueryUtil.createProvidedQueriesForBusinessObject(bo)) {
        result.add(createExpression(bo, q));
    }//from w  ww. j  av  a 2s. c  o m
    for (final Query q : bo.getQueries()) {
        final Expression queryExpression = createExpression(bo, q);
        if (Iterables.find(result, withName(queryExpression.getName()), null) == null) {
            result.add(queryExpression);
        }
    }
    return result;
}

From source file:org.onebusaway.nyc.vehicle_tracking.impl.inference.ObservationCache.java

public void putValueForObservation(Observation observation, EObservationCacheKey key, Object value) {
    final NycRawLocationRecord record = observation.getRecord();
    final MinMaxPriorityQueue<ObservationContents> contentsCache = _contentsByVehicleId
            .getUnchecked(record.getVehicleId());

    /*//from  w  w  w .  ja v  a2 s .c o m
     * Synchronize for integration tests, which can do crazy things.
     */
    ObservationContents contents;
    synchronized (contentsCache) {
        contents = Iterables.find(contentsCache, new ObsSearch(observation), null);

        if (contents == null) {
            contents = new ObservationContents(observation);
            /*
             * If we attempt to add observations that are behind our current, 
             * then just return
             */
            if (!contentsCache.offer(contents)) {
                return;
            }
        }
    }

    contents.putValueForValueType(key, value);
}