Skip to content

Commit

Permalink
Migrate toward java.util.function.Predicate
Browse files Browse the repository at this point in the history
- Maintains binary and source compatibility
- Javadoc
  • Loading branch information
garydgregory committed Jul 10, 2024
1 parent d5dd6e4 commit 3618193
Show file tree
Hide file tree
Showing 31 changed files with 71 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ public static <T> boolean filter(final Iterable<T> collection, final Predicate<?
boolean result = false;
if (collection != null && predicate != null) {
for (final Iterator<T> it = collection.iterator(); it.hasNext();) {
if (!predicate.evaluate(it.next())) {
if (!predicate.test(it.next())) {
it.remove();
result = true;
}
Expand Down Expand Up @@ -1740,7 +1740,7 @@ public static <O, R extends Collection<? super O>> R select(final Iterable<? ext

if (inputCollection != null && predicate != null) {
for (final O item : inputCollection) {
if (predicate.evaluate(item)) {
if (predicate.test(item)) {
outputCollection.add(item);
}
}
Expand Down Expand Up @@ -1783,7 +1783,7 @@ public static <O, R extends Collection<? super O>> R select(final Iterable<? ext

if (inputCollection != null && predicate != null) {
for (final O element : inputCollection) {
if (predicate.evaluate(element)) {
if (predicate.test(element)) {
outputCollection.add(element);
} else {
rejectedCollection.add(element);
Expand Down Expand Up @@ -1837,7 +1837,7 @@ public static <O, R extends Collection<? super O>> R selectRejected(final Iterab

if (inputCollection != null && predicate != null) {
for (final O item : inputCollection) {
if (!predicate.evaluate(item)) {
if (!predicate.test(item)) {
outputCollection.add(item);
}
}
Expand Down Expand Up @@ -1998,7 +1998,7 @@ public static <O> Collection<O> subtract(final Iterable<? extends O> a,
final ArrayList<O> list = new ArrayList<>();
final HashBag<O> bag = new HashBag<>();
for (final O element : b) {
if (p.evaluate(element)) {
if (p.test(element)) {
bag.add(element);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ public static <O, R extends Collection<O>> List<R> partition(final Iterable<? ex
for (final O element : iterable) {
boolean elementAssigned = false;
for (int i = 0; i < numberOfPredicates; ++i) {
if (predicates[i].evaluate(element)) {
if (predicates[i].test(element)) {
partitions.get(i).add(element);
elementAssigned = true;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ public static <E> E find(final Iterator<E> iterator, final Predicate<? super E>
if (iterator != null) {
while (iterator.hasNext()) {
final E element = iterator.next();
if (predicate.evaluate(element)) {
if (predicate.test(element)) {
return element;
}
}
Expand Down Expand Up @@ -860,7 +860,7 @@ public static <E> int indexOf(final Iterator<E> iterator, final Predicate<? supe
if (iterator != null) {
for (int index = 0; iterator.hasNext(); index++) {
final E element = iterator.next();
if (predicate.evaluate(element)) {
if (predicate.test(element)) {
return index;
}
}
Expand Down Expand Up @@ -935,7 +935,7 @@ public static <E> boolean matchesAll(final Iterator<E> iterator, final Predicate
if (iterator != null) {
while (iterator.hasNext()) {
final E element = iterator.next();
if (!predicate.evaluate(element)) {
if (!predicate.test(element)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public static <E> int indexOf(final List<E> list, final Predicate<E> predicate)
if (list != null && predicate != null) {
for (int i = 0; i < list.size(); i++) {
final E item = list.get(i);
if (predicate.evaluate(item)) {
if (predicate.test(item)) {
return i;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
* </p>
*
* @param <T> the type of the input to the predicate.
*
* @since 1.0
* @deprecated Use {@link java.util.function.Predicate}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public Builder(final Predicate<? super E> predicate) {
* @return the PredicatedCollectionBuilder.
*/
public Builder<E> add(final E item) {
if (predicate.evaluate(item)) {
if (predicate.test(item)) {
accepted.add(item);
} else {
rejected.add(item);
Expand Down Expand Up @@ -419,7 +419,7 @@ public boolean addAll(final Collection<? extends E> coll) {
* @throws IllegalArgumentException if the add is invalid
*/
protected void validate(final E object) {
if (!predicate.evaluate(object)) {
if (!predicate.test(object)) {
throw new IllegalArgumentException("Cannot add Object '" + object + "' - Predicate '" +
predicate + "' rejected it");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
/**
* Abstract base class for quantification predicates, e.g. All, Any, None.
*
* @param <T> the type of the input to the predicate.
* @since 4.0
*/
public abstract class AbstractQuantifierPredicate<T> implements PredicateDecorator<T>, Serializable {
public abstract class AbstractQuantifierPredicate<T> extends AbstractPredicate<T> implements PredicateDecorator<T>, Serializable {

/** Serial version UID */
private static final long serialVersionUID = -3094696765038308799L;
Expand Down Expand Up @@ -53,8 +54,4 @@ public Predicate<? super T>[] getPredicates() {
return FunctorUtils.<T>copy(iPredicates);
}

@Override
public boolean evaluate(final T object) {
return test(object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* threw an exception.
* </p>
*
* @param <T> the type of the input to the predicate.
* @since 3.0
*/
public final class AllPredicate<T> extends AbstractQuantifierPredicate<T> {
Expand Down Expand Up @@ -106,7 +107,7 @@ public AllPredicate(final Predicate<? super T>... predicates) {
@Override
public boolean test(final T object) {
for (final Predicate<? super T> iPredicate : iPredicates) {
if (!iPredicate.evaluate(object)) {
if (!iPredicate.test(object)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
/**
* Predicate implementation that returns true if both the predicates return true.
*
* @param <T> the type of the input to the predicate.
* @since 3.0
*/
public final class AndPredicate<T> implements PredicateDecorator<T>, Serializable {
public final class AndPredicate<T> extends AbstractPredicate<T> implements PredicateDecorator<T>, Serializable {

/** Serial version UID */
private static final long serialVersionUID = 4189014213763186912L;
Expand Down Expand Up @@ -70,8 +71,8 @@ public AndPredicate(final Predicate<? super T> predicate1, final Predicate<? sup
* @return true if both decorated predicates return true
*/
@Override
public boolean evaluate(final T object) {
return iPredicate1.evaluate(object) && iPredicate2.evaluate(object);
public boolean test(final T object) {
return iPredicate1.test(object) && iPredicate2.test(object);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* threw an exception.
* </p>
*
* @param <T> the type of the input to the predicate.
* @since 3.0
*/
public final class AnyPredicate<T> extends AbstractQuantifierPredicate<T> {
Expand Down Expand Up @@ -103,7 +104,7 @@ public AnyPredicate(final Predicate<? super T>... predicates) {
@Override
public boolean test(final T object) {
for (final Predicate<? super T> iPredicate : iPredicates) {
if (iPredicate.evaluate(object)) {
if (iPredicate.test(object)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* following way:</p>
*
* <pre>
* ComparatorPredicate.comparatorPredicate(ONE, comparator).evaluate(TWO);
* ComparatorPredicate.comparatorPredicate(ONE, comparator).test(TWO);
* </pre>
*
* <p>The input variable {@code TWO} in compared to the stored variable {@code ONE} using
Expand All @@ -67,7 +67,7 @@
* evaluation of a comparator result.</p>
*
* <pre>
* ComparatorPredicate.comparatorPredicate(ONE, comparator,<b>ComparatorPredicate.Criterion.GREATER</b>).evaluate(TWO);
* ComparatorPredicate.comparatorPredicate(ONE, comparator,<b>ComparatorPredicate.Criterion.GREATER</b>).test(TWO);
* </pre>
*
* <p>The input variable TWO is compared to the stored variable ONE using the supplied {@code comparator}
Expand Down Expand Up @@ -150,7 +150,7 @@ public ComparatorPredicate(final T object, final Comparator<T> comparator, final
* <li>{@code comparator.compare(object, input) &lt;= 0 &amp;&amp; criterion == LESS_OR_EQUAL}</li>
* </ul>
*
* @see org.apache.commons.collections4.Predicate#evaluate(Object)
* @see org.apache.commons.collections4.Predicate#test(Object)
* @see java.util.Comparator#compare(Object first, Object second)
*
* @param target the target object to compare to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static <T> Closure<T> coerce(final Closure<? super T> closure) {

/**
* A very simple method that coerces Predicate<? super T> to Predicate<T>.
* Due to the {@link Predicate#evaluate(T)} method, Predicate<? super T> is
* Due to the {@link Predicate#test(T)} method, Predicate<? super T> is
* able to be coerced to Predicate<T> without casting issues.
* <p>This method exists
* simply as centralised documentation and atomic unchecked warning
Expand Down Expand Up @@ -96,7 +96,7 @@ static <E> Closure<E>[] copy(final Closure<? super E>... closures) {

/**
* Clone the predicates to ensure that the internal reference can't be messed with.
* Due to the {@link Predicate#evaluate(T)} method, Predicate<? super T> is
* Due to the {@link Predicate#test(T)} method, Predicate<? super T> is
* able to be coerced to Predicate<T> without casting issues.
*
* @param predicates the predicates to copy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public IfClosure(final Predicate<? super E> predicate, final Closure<? super E>
*/
@Override
public void execute(final E input) {
if (iPredicate.evaluate(input)) {
if (iPredicate.test(input)) {
iTrueClosure.accept(input);
} else {
iFalseClosure.accept(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public Predicate<? super I> getPredicate() {
*/
@Override
public O transform(final I input) {
if (iPredicate.evaluate(input)) {
if (iPredicate.test(input)) {
return iTrueTransformer.transform(input);
}
return iFalseTransformer.transform(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* threw an exception.
* </p>
*
* @param <T> the type of the input to the predicate.
* @since 3.0
*/
public final class NonePredicate<T> extends AbstractQuantifierPredicate<T> {
Expand Down Expand Up @@ -93,7 +94,7 @@ public NonePredicate(final Predicate<? super T>... predicates) {
@Override
public boolean test(final T object) {
for (final Predicate<? super T> iPredicate : iPredicates) {
if (iPredicate.evaluate(object)) {
if (iPredicate.test(object)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
/**
* Predicate implementation that returns the opposite of the decorated predicate.
*
* @param <T> the type of the input to the predicate.
* @since 3.0
*/
public final class NotPredicate<T> implements PredicateDecorator<T>, Serializable {
public final class NotPredicate<T> extends AbstractPredicate<T> implements PredicateDecorator<T>, Serializable {

/** Serial version UID */
private static final long serialVersionUID = -2654603322338049674L;
Expand Down Expand Up @@ -63,8 +64,8 @@ public NotPredicate(final Predicate<? super T> predicate) {
* @return true if predicate returns false
*/
@Override
public boolean evaluate(final T object) {
return !iPredicate.evaluate(object);
public boolean test(final T object) {
return !iPredicate.test(object);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
/**
* Predicate implementation that throws an exception if the input is null.
*
* @param <T> the type of the input to the predicate.
* @since 3.0
*/
public final class NullIsExceptionPredicate<T> implements PredicateDecorator<T>, Serializable {
public final class NullIsExceptionPredicate<T> extends AbstractPredicate<T> implements PredicateDecorator<T>, Serializable {

/** Serial version UID */
private static final long serialVersionUID = 3243449850504576071L;
Expand Down Expand Up @@ -66,11 +67,11 @@ public NullIsExceptionPredicate(final Predicate<? super T> predicate) {
* @throws FunctorException if input is null
*/
@Override
public boolean evaluate(final T object) {
public boolean test(final T object) {
if (object == null) {
throw new FunctorException("Input Object must not be null");
}
return iPredicate.evaluate(object);
return iPredicate.test(object);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
/**
* Predicate implementation that returns false if the input is null.
*
* @param <T> the type of the input to the predicate.
* @since 3.0
*/
public final class NullIsFalsePredicate<T> implements PredicateDecorator<T>, Serializable {
public final class NullIsFalsePredicate<T> extends AbstractPredicate<T> implements PredicateDecorator<T>, Serializable {

/** Serial version UID */
private static final long serialVersionUID = -2997501534564735525L;
Expand Down Expand Up @@ -64,11 +65,11 @@ public NullIsFalsePredicate(final Predicate<? super T> predicate) {
* @return true if decorated predicate returns true, false if input is null
*/
@Override
public boolean evaluate(final T object) {
public boolean test(final T object) {
if (object == null) {
return false;
}
return iPredicate.evaluate(object);
return iPredicate.test(object);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
/**
* Predicate implementation that returns true if the input is null.
*
* @param <T> the type of the input to the predicate.
* @since 3.0
*/
public final class NullIsTruePredicate<T> implements PredicateDecorator<T>, Serializable {
public final class NullIsTruePredicate<T> extends AbstractPredicate<T> implements PredicateDecorator<T>, Serializable {

/** Serial version UID */
private static final long serialVersionUID = -7625133768987126273L;
Expand Down Expand Up @@ -64,11 +65,11 @@ public NullIsTruePredicate(final Predicate<? super T> predicate) {
* @return true if decorated predicate returns true or input is null
*/
@Override
public boolean evaluate(final T object) {
public boolean test(final T object) {
if (object == null) {
return true;
}
return iPredicate.evaluate(object);
return iPredicate.test(object);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* threw an exception.
* </p>
*
* @param <T> the type of the input to the predicate.
* @since 3.0
*/
public final class OnePredicate<T> extends AbstractQuantifierPredicate<T> {
Expand Down Expand Up @@ -95,7 +96,7 @@ public OnePredicate(final Predicate<? super T>... predicates) {
public boolean test(final T object) {
boolean match = false;
for (final Predicate<? super T> iPredicate : iPredicates) {
if (iPredicate.evaluate(object)) {
if (iPredicate.test(object)) {
if (match) {
return false;
}
Expand Down
Loading

0 comments on commit 3618193

Please sign in to comment.