/** * Finds the first element in the given collection which matches the given predicate. * <p> * If the input collection or predicate is null, or no element of the collection * matches the predicate, null is returned. * * @param <T> the type of object the {@link Iterable} contains * @param collection the collection to search, may be null * @param predicate the predicate to use, may be null * @return the first element of the collection which matches the predicate or null if none could be found * @deprecated since 4.1, use {@link IterableUtils#find(Iterable, Predicate)} instead */ @Deprecated public static <T> T find(final Iterable<T> collection, final Predicate<? super T> predicate) { return predicate != null ? IterableUtils.find(collection, predicate) : null; }
/** * Returns the element at the provided position in this iterable. * In order to return the element, an iterator needs to be traversed * up to the requested position. * * @param position the position of the element to return * @return the element * @throws IndexOutOfBoundsException if the provided position is outside the * valid range of this iterable: [0, size) */ public E get(final int position) { return IterableUtils.get(iterable, position); }
/** * Applies the closure to all elements contained in this iterable. * * @param closure the closure to apply to each element, may not be null * @throws NullPointerException if closure is null */ public void forEach(final Closure<? super E> closure) { IterableUtils.forEach(iterable, closure); }
@Test public void testRemovingAllWillNotRemoveLast() { // setup Set<Security> values = EnumUtils.values(Security.class); // execute IterableUtils.forEach(values, new ToggleClosure()); // validate IterableUtils.forEachButLast(values, new RemovedClosure()); assertTrue(fixture.contains(IterableUtils.get(values, values.size() - 1))); }
/** * Checks if this iterable is empty. * * @return true if this iterable does not contain any elements, false otherwise */ public boolean isEmpty() { return IterableUtils.isEmpty(iterable); }
/** * Checks if this iterable contains any element matching the provided predicate. * <p> * A <code>null</code> or empty iterable returns false. * * @param predicate the predicate to use, may not be null * @return true if at least one element contained in this iterable matches the predicate, * false otherwise * @throws NullPointerException if predicate is null */ public boolean anyMatch(final Predicate<? super E> predicate) { return IterableUtils.matchesAny(iterable, predicate); }
/** * Counts the number of elements in the input collection that match the * predicate. * <p> * A <code>null</code> collection or predicate matches no elements. * * @param <C> the type of object the {@link Iterable} contains * @param input the {@link Iterable} to get the input from, may be null * @param predicate the predicate to use, may be null * @return the number of matches for the predicate in the collection * @deprecated since 4.1, use {@link IterableUtils#countMatches(Iterable, Predicate)} instead */ @Deprecated public static <C> int countMatches(final Iterable<C> input, final Predicate<? super C> predicate) { return predicate == null ? 0 : (int) IterableUtils.countMatches(input, predicate); }
@Test public void testRemovingAllWillNotRemoveLast() { // setup Set<Strength> values = EnumUtils.values(Strength.class); // execute IterableUtils.forEach(values, new ToggleClosure()); // validate IterableUtils.forEachButLast(values, new RemovedClosure()); assertTrue(fixture.contains(IterableUtils.get(values, values.size() - 1))); }
private static Collection<String> valueAssignableIntersection(final Iterable<ValueAssignable> a, final Iterable<String> b) { List<String> temp = new ArrayList<>(); if (!IterableUtils.isEmpty(a)) { for (ValueAssignable alist : a) { temp.add((String) alist.getValue()); } } return CollectionUtils.intersection(temp, b); }
/** * Answers true if a predicate is true for at least one element of a * collection. * <p> * A <code>null</code> collection or predicate returns false. * * @param <C> the type of object the {@link Iterable} contains * @param input the {@link Iterable} to get the input from, may be null * @param predicate the predicate to use, may be null * @return true if at least one element of the collection matches the predicate * @deprecated since 4.1, use {@link IterableUtils#matchesAny(Iterable, Predicate)} instead */ @Deprecated public static <C> boolean exists(final Iterable<C> input, final Predicate<? super C> predicate) { return predicate != null && IterableUtils.matchesAny(input, predicate); }
/** * Returns a measure of the "unused-ness" of the given binding. * Returns a value between 0 and 1 (exclusive) -- a higher value means a binding is more unused. */ public double getUnusedScore( final VarBindingDef binding) { // Returns the percentage of unused tuples that include this binding. return unused_.isEmpty() ? 0.0 : (double) IterableUtils.countMatches( unused_, tuple -> tuple.contains( binding)) / unused_.size(); }
@Override public void removePluginJarChangeListener(final PluginJarChangeListener listener) { WeakReference<PluginJarChangeListener> referenceOfListenerToBeRemoved = IterableUtils.find(pluginJarChangeListener, listenerWeakReference -> { PluginJarChangeListener registeredListener = listenerWeakReference.get(); return registeredListener != null && registeredListener == listener; }); pluginJarChangeListener.remove(referenceOfListenerToBeRemoved); removeClearedWeakReferences(); }
/** * Shortcut for {@code get(iterator, 0)}. * <p> * Returns the <code>first</code> value in the <code>iterable</code>'s {@link Iterator}, throwing * <code>IndexOutOfBoundsException</code> if there is no such element. * </p> * <p> * If the {@link Iterable} is a {@link List}, then it will use {@link List#get(int)}. * </p> * * @param <T> the type of object in the {@link Iterable}. * @param iterable the {@link Iterable} to get a value from, may be null * @return the first object * @throws IndexOutOfBoundsException if the request is invalid * @since 4.2 */ public static <T> T first(final Iterable<T> iterable) { return get(iterable, 0); }
/** * Executes the given closure on each element in the collection. * <p> * If the input collection or closure is null, there is no change made. * * @param <T> the type of object the {@link Iterable} contains * @param <C> the closure type * @param collection the collection to get the input from, may be null * @param closure the closure to perform, may be null * @return closure * @deprecated since 4.1, use {@link IterableUtils#forEach(Iterable, Closure)} instead */ @Deprecated public static <T, C extends Closure<? super T>> C forAllDo(final Iterable<T> collection, final C closure) { if (closure != null) { IterableUtils.forEach(collection, closure); } return closure; }
@Test public void testRemovingAllWillNotRemoveLast() { // setup Set<WiFiBand> values = EnumUtils.values(WiFiBand.class); // execute IterableUtils.forEach(values, new ToggleClosure()); // validate IterableUtils.forEachButLast(values, new RemovedClosure()); assertTrue(fixture.contains(IterableUtils.get(values, values.size() - 1))); }
private static Collection intersectIterable(final Iterable a, final Iterable b) { Collection response; if (!IterableUtils.isEmpty(a) && (a instanceof ArrayList) && !IterableUtils.isEmpty(b) && (b instanceof ArrayList)) { //TODO this is a bit of a hack to allow the intersection of two collections of different types. This is primarily //used to facilitate some MVEL execution. We really should be fixing the MVEL to call a method that retrieves //a list of Strings, rather than a list of ValueAssignables. Object aVal = ((ArrayList) a).get(0); Object bVal = ((ArrayList) b).get(0); if (aVal instanceof ValueAssignable && bVal instanceof String) { response = valueAssignableIntersection(a, b); } else { response = CollectionUtils.intersection(a, b); } } else { response = CollectionUtils.intersection(a, b); } return response; }
/** * Checks if the object is contained in the given iterable. Object equality * is tested with an {@code equator} unlike {@link #contains(Iterable, Object)} * which uses {@link Object#equals(Object)}. * <p> * A <code>null</code> or empty iterable returns false. * A <code>null</code> object will not be passed to the equator, instead a * {@link org.apache.commons.collections4.functors.NullPredicate NullPredicate} * will be used. * * @param <E> the type of object the {@link Iterable} contains * @param iterable the iterable to check, may be null * @param object the object to check * @param equator the equator to use to check, may not be null * @return true if the object is contained in the iterable, false otherwise * @throws NullPointerException if equator is null */ public static <E> boolean contains(final Iterable<? extends E> iterable, final E object, final Equator<? super E> equator) { if (equator == null) { throw new NullPointerException("Equator must not be null."); } return matchesAny(iterable, EqualPredicate.equalPredicate(object, equator)); }
public GoPluginDescriptor getPluginByIdOrFileName(String pluginID, final String fileName) { if (pluginID != null) { GoPluginDescriptor descriptor = idToDescriptorMap.get(pluginID); if (descriptor != null) { return descriptor; } } return IterableUtils.find(idToDescriptorMap.values(), object -> object.fileName().equals(fileName)); }
/** * Returns the <code>index</code>-th value in the <code>iterable</code>'s {@link Iterator}, throwing * <code>IndexOutOfBoundsException</code> if there is no such element. * <p> * If the {@link Iterable} is a {@link List}, then it will use {@link List#get(int)}. * * @param iterable the {@link Iterable} to get a value from * @param index the index to get * @param <T> the type of object in the {@link Iterable}. * @return the object at the specified index * @throws IndexOutOfBoundsException if the index is invalid * @deprecated since 4.1, use {@code IterableUtils.get(Iterable, int)} instead */ @Deprecated public static <T> T get(final Iterable<T> iterable, final int index) { return IterableUtils.get(iterable, index); }
@Override public void onEntityConfigChange(PipelineConfig pipelineConfig) { LOGGER.info("[Configuration Changed] Removing deleted jobs for pipeline {}.", pipelineConfig.name()); synchronized (BuildAssignmentService.this) { List<JobPlan> jobsToRemove; if (goConfigService.hasPipelineNamed(pipelineConfig.name())) { jobsToRemove = getMismatchingJobPlansFromUpdatedPipeline(pipelineConfig, jobPlans); } else { jobsToRemove = getAllJobPlansFromDeletedPipeline(pipelineConfig, jobPlans); } IterableUtils.forEach(jobsToRemove, o -> removeJob(o)); } } };