/** * Factory to create the predicate. * * @param <T> the type that the predicate queries * @param object the object to compare to * @return the predicate */ public static <T> Predicate<T> equalPredicate(final T object) { if (object == null) { return NullPredicate.nullPredicate(); } return new EqualPredicate<>(object); }
/** * Creates a Predicate that checks if the input object is equal to the * specified object using equals(). * * @param <T> the type that the predicate queries * @param value the value to compare against * @return the predicate * @see EqualPredicate */ public static <T> Predicate<T> equalPredicate(final T value) { return EqualPredicate.equalPredicate(value); }
/** * Checks if the object is contained in the given iterator. * <p> * A <code>null</code> or empty iterator returns false. * * @param <E> the type of object the {@link Iterator} contains * @param iterator the iterator to check, may be null * @param object the object to check * @return true if the object is contained in the iterator, false otherwise * @since 4.1 */ public static <E> boolean contains(final Iterator<E> iterator, final Object object) { return matchesAny(iterator, EqualPredicate.equalPredicate(object)); }
/** * Factory to create the identity predicate. * * @param <T> the type that the predicate queries * @param object the object to compare to * @param equator the equator to use for comparison * @return the predicate * @since 4.0 */ public static <T> Predicate<T> equalPredicate(final T object, final Equator<T> equator) { if (object == null) { return NullPredicate.nullPredicate(); } return new EqualPredicate<>(object, equator); }
/** * 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)); }
int i = 0; for (final Map.Entry<I, Transformer<I, O>> entry : objectsAndTransformers.entrySet()) { preds[i] = EqualPredicate.<I>equalPredicate(entry.getKey()); trs[i++] = entry.getValue();
int i = 0; for (final Map.Entry<? extends E, Closure<E>> entry : objectsAndClosures.entrySet()) { preds[i] = EqualPredicate.<E>equalPredicate(entry.getKey()); trs[i] = entry.getValue(); i++;
/** * Returns the number of occurrences of the provided object in the iterable. * * @param <E> the element type that the {@link Iterable} may contain * @param <T> the element type of the object to find * @param iterable the {@link Iterable} to search * @param obj the object to find the cardinality of * @return the number of occurrences of obj in iterable */ public static <E, T extends E> int frequency(final Iterable<E> iterable, final T obj) { if (iterable instanceof Set<?>) { return ((Set<E>) iterable).contains(obj) ? 1 : 0; } if (iterable instanceof Bag<?>) { return ((Bag<E>) iterable).getCount(obj); } return size(filteredIterable(emptyIfNull(iterable), EqualPredicate.<E>equalPredicate(obj))); }