Migrate toward java.util.function.Predicate

- Maintains binary and source compatibility
- Javadoc
This commit is contained in:
Gary Gregory 2024-07-10 09:35:54 -04:00
parent d5dd6e4468
commit 3618193221
31 changed files with 71 additions and 67 deletions

View File

@ -833,7 +833,7 @@ public class CollectionUtils {
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;
}
@ -1740,7 +1740,7 @@ public class CollectionUtils {
if (inputCollection != null && predicate != null) {
for (final O item : inputCollection) {
if (predicate.evaluate(item)) {
if (predicate.test(item)) {
outputCollection.add(item);
}
}
@ -1783,7 +1783,7 @@ public class CollectionUtils {
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);
@ -1837,7 +1837,7 @@ public class CollectionUtils {
if (inputCollection != null && predicate != null) {
for (final O item : inputCollection) {
if (!predicate.evaluate(item)) {
if (!predicate.test(item)) {
outputCollection.add(item);
}
}
@ -1998,7 +1998,7 @@ public class CollectionUtils {
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);
}
}

View File

@ -698,7 +698,7 @@ public class IterableUtils {
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;

View File

@ -673,7 +673,7 @@ public class IteratorUtils {
if (iterator != null) {
while (iterator.hasNext()) {
final E element = iterator.next();
if (predicate.evaluate(element)) {
if (predicate.test(element)) {
return element;
}
}
@ -860,7 +860,7 @@ public class IteratorUtils {
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;
}
}
@ -935,7 +935,7 @@ public class IteratorUtils {
if (iterator != null) {
while (iterator.hasNext()) {
final E element = iterator.next();
if (!predicate.evaluate(element)) {
if (!predicate.test(element)) {
return false;
}
}

View File

@ -242,7 +242,7 @@ public class ListUtils {
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;
}
}

View File

@ -31,7 +31,6 @@ package org.apache.commons.collections4;
* </p>
*
* @param <T> the type of the input to the predicate.
*
* @since 1.0
* @deprecated Use {@link java.util.function.Predicate}.
*/

View File

@ -118,7 +118,7 @@ public class PredicatedCollection<E> extends AbstractCollectionDecorator<E> {
* @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);
@ -419,7 +419,7 @@ public class PredicatedCollection<E> extends AbstractCollectionDecorator<E> {
* @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");
}

View File

@ -23,9 +23,10 @@ import org.apache.commons.collections4.Predicate;
/**
* 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;
@ -53,8 +54,4 @@ public abstract class AbstractQuantifierPredicate<T> implements PredicateDecorat
return FunctorUtils.<T>copy(iPredicates);
}
@Override
public boolean evaluate(final T object) {
return test(object);
}
}

View File

@ -33,6 +33,7 @@ import org.apache.commons.collections4.Predicate;
* 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> {
@ -106,7 +107,7 @@ public final class AllPredicate<T> extends AbstractQuantifierPredicate<T> {
@Override
public boolean test(final T object) {
for (final Predicate<? super T> iPredicate : iPredicates) {
if (!iPredicate.evaluate(object)) {
if (!iPredicate.test(object)) {
return false;
}
}

View File

@ -24,9 +24,10 @@ import org.apache.commons.collections4.Predicate;
/**
* 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;
@ -70,8 +71,8 @@ public final class AndPredicate<T> implements PredicateDecorator<T>, Serializabl
* @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);
}
/**

View File

@ -29,6 +29,7 @@ import org.apache.commons.collections4.Predicate;
* 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> {
@ -103,7 +104,7 @@ public final class AnyPredicate<T> extends AbstractQuantifierPredicate<T> {
@Override
public boolean test(final T object) {
for (final Predicate<? super T> iPredicate : iPredicates) {
if (iPredicate.evaluate(object)) {
if (iPredicate.test(object)) {
return true;
}
}

View File

@ -45,7 +45,7 @@ import org.apache.commons.collections4.Predicate;
* 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
@ -67,7 +67,7 @@ import org.apache.commons.collections4.Predicate;
* 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}
@ -150,7 +150,7 @@ public class ComparatorPredicate<T> extends AbstractPredicate<T> implements Seri
* <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

View File

@ -49,7 +49,7 @@ final class FunctorUtils {
/**
* 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
@ -96,7 +96,7 @@ final class FunctorUtils {
/**
* 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

View File

@ -112,7 +112,7 @@ public class IfClosure<E> implements Closure<E>, Serializable {
*/
@Override
public void execute(final E input) {
if (iPredicate.evaluate(input)) {
if (iPredicate.test(input)) {
iTrueClosure.accept(input);
} else {
iFalseClosure.accept(input);

View File

@ -133,7 +133,7 @@ public class IfTransformer<I, O> implements Transformer<I, O>, Serializable {
*/
@Override
public O transform(final I input) {
if (iPredicate.evaluate(input)) {
if (iPredicate.test(input)) {
return iTrueTransformer.transform(input);
}
return iFalseTransformer.transform(input);

View File

@ -29,6 +29,7 @@ import org.apache.commons.collections4.Predicate;
* 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> {
@ -93,7 +94,7 @@ public final class NonePredicate<T> extends AbstractQuantifierPredicate<T> {
@Override
public boolean test(final T object) {
for (final Predicate<? super T> iPredicate : iPredicates) {
if (iPredicate.evaluate(object)) {
if (iPredicate.test(object)) {
return false;
}
}

View File

@ -24,9 +24,10 @@ import org.apache.commons.collections4.Predicate;
/**
* 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;
@ -63,8 +64,8 @@ public final class NotPredicate<T> implements PredicateDecorator<T>, Serializabl
* @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);
}
/**

View File

@ -25,9 +25,10 @@ import org.apache.commons.collections4.Predicate;
/**
* 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;
@ -66,11 +67,11 @@ public final class NullIsExceptionPredicate<T> implements PredicateDecorator<T>,
* @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);
}
/**

View File

@ -24,9 +24,10 @@ import org.apache.commons.collections4.Predicate;
/**
* 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;
@ -64,11 +65,11 @@ public final class NullIsFalsePredicate<T> implements PredicateDecorator<T>, Ser
* @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);
}
/**

View File

@ -24,9 +24,10 @@ import org.apache.commons.collections4.Predicate;
/**
* 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;
@ -64,11 +65,11 @@ public final class NullIsTruePredicate<T> implements PredicateDecorator<T>, Seri
* @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);
}
/**

View File

@ -29,6 +29,7 @@ import org.apache.commons.collections4.Predicate;
* 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> {
@ -95,7 +96,7 @@ public final class OnePredicate<T> extends AbstractQuantifierPredicate<T> {
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;
}

View File

@ -24,9 +24,10 @@ import org.apache.commons.collections4.Predicate;
/**
* Predicate implementation that returns true if either of the predicates return true.
*
* @param <T> the type of the input to the predicate.
* @since 3.0
*/
public final class OrPredicate<T> implements PredicateDecorator<T>, Serializable {
public final class OrPredicate<T> extends AbstractPredicate<T> implements PredicateDecorator<T>, Serializable {
/** Serial version UID */
private static final long serialVersionUID = -8791518325735182855L;
@ -70,8 +71,8 @@ public final class OrPredicate<T> implements PredicateDecorator<T>, Serializable
* @return true if either decorated predicate returns 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);
}
/**

View File

@ -78,7 +78,7 @@ public class PredicateTransformer<T> implements Transformer<T, Boolean>, Seriali
*/
@Override
public Boolean transform(final T input) {
return Boolean.valueOf(iPredicate.evaluate(input));
return Boolean.valueOf(iPredicate.test(input));
}
}

View File

@ -142,7 +142,7 @@ public class SwitchClosure<E> implements Closure<E>, Serializable {
@Override
public void execute(final E input) {
for (int i = 0; i < iPredicates.length; i++) {
if (iPredicates[i].evaluate(input)) {
if (iPredicates[i].test(input)) {
iClosures[i].accept(input);
return;
}

View File

@ -187,7 +187,7 @@ public class SwitchTransformer<I, O> implements Transformer<I, O>, Serializable
@Override
public O transform(final I input) {
for (int i = 0; i < iPredicates.length; i++) {
if (iPredicates[i].evaluate(input)) {
if (iPredicates[i].test(input)) {
return iTransformers[i].transform(input);
}
}

View File

@ -26,9 +26,10 @@ import org.apache.commons.collections4.Transformer;
* Predicate implementation that transforms the given object before invoking
* another {@code Predicate}.
*
* @param <T> the type of the input to the predicate.
* @since 3.1
*/
public final class TransformedPredicate<T> implements PredicateDecorator<T>, Serializable {
public final class TransformedPredicate<T> extends AbstractPredicate<T> implements PredicateDecorator<T>, Serializable {
/** Serial version UID */
private static final long serialVersionUID = -5596090919668315834L;
@ -75,9 +76,9 @@ public final class TransformedPredicate<T> implements PredicateDecorator<T>, Ser
* @return true if decorated predicate returns true
*/
@Override
public boolean evaluate(final T object) {
public boolean test(final T object) {
final T result = iTransformer.transform(object);
return iPredicate.evaluate(result);
return iPredicate.test(result);
}
/**

View File

@ -82,7 +82,7 @@ public class WhileClosure<E> implements Closure<E> {
if (iDoLoop) {
iClosure.accept(input);
}
while (iPredicate.evaluate(input)) {
while (iPredicate.test(input)) {
iClosure.accept(input);
}
}

View File

@ -155,7 +155,7 @@ public class FilterIterator<E> implements Iterator<E> {
private boolean setNextObject() {
while (iterator.hasNext()) {
final E object = iterator.next();
if (predicate.evaluate(object)) {
if (predicate.test(object)) {
nextObject = object;
nextObjectSet = true;
return true;

View File

@ -229,7 +229,7 @@ public class FilterListIterator<E> implements ListIterator<E> {
}
while (iterator.hasNext()) {
final E object = iterator.next();
if (predicate.evaluate(object)) {
if (predicate.test(object)) {
nextObject = object;
nextObjectSet = true;
return true;
@ -265,7 +265,7 @@ public class FilterListIterator<E> implements ListIterator<E> {
}
while (iterator.hasPrevious()) {
final E object = iterator.previous();
if (predicate.evaluate(object)) {
if (predicate.test(object)) {
previousObject = object;
previousObjectSet = true;
return true;

View File

@ -111,7 +111,7 @@ public class PredicatedMap<K, V>
*/
@Override
protected V checkSetValue(final V value) {
if (!valuePredicate.evaluate(value)) {
if (!valuePredicate.test(value)) {
throw new IllegalArgumentException("Cannot set value - Predicate rejected it");
}
return value;
@ -164,10 +164,10 @@ public class PredicatedMap<K, V>
* @throws IllegalArgumentException if invalid
*/
protected void validate(final K key, final V value) {
if (keyPredicate != null && !keyPredicate.evaluate(key)) {
if (keyPredicate != null && !keyPredicate.test(key)) {
throw new IllegalArgumentException("Cannot add key - Predicate rejected it");
}
if (valuePredicate != null && !valuePredicate.evaluate(value)) {
if (valuePredicate != null && !valuePredicate.test(value)) {
throw new IllegalArgumentException("Cannot add value - Predicate rejected it");
}
}

View File

@ -62,7 +62,7 @@ public abstract class AbstractMockPredicateTest<T> {
protected final Predicate<T> createMockPredicate(final Boolean returnValue) {
final Predicate<T> mockPredicate = EasyMock.createMock(Predicate.class);
if (returnValue != null) {
EasyMock.expect(mockPredicate.evaluate(testValue)).andReturn(returnValue);
EasyMock.expect(mockPredicate.test(testValue)).andReturn(returnValue);
}
replay(mockPredicate);
mockPredicatesToVerify.add(mockPredicate);

View File

@ -80,8 +80,7 @@ public class AllPredicateTest extends AbstractAnyAllOnePredicateTest<Integer> {
*/
@Test
public void testEmptyCollectionToGetInstance() {
final Predicate<Integer> allPredicate = getPredicateInstance(
Collections.<Predicate<Integer>>emptyList());
final Predicate<Integer> allPredicate = getPredicateInstance(Collections.<Predicate<Integer>>emptyList());
assertTrue(allPredicate.evaluate(getTestValue()), "empty collection not true");
}
@ -94,8 +93,7 @@ public class AllPredicateTest extends AbstractAnyAllOnePredicateTest<Integer> {
// use the constructor directly, as getInstance() returns the original predicate when passed
// an array of size one.
final Predicate<Integer> predicate = createMockPredicate(false);
assertFalse(allPredicate(predicate).evaluate(getTestValue()),
"single false predicate evaluated to true");
assertFalse(allPredicate(predicate).test(getTestValue()), "single false predicate evaluated to true");
}
/**
@ -107,8 +105,7 @@ public class AllPredicateTest extends AbstractAnyAllOnePredicateTest<Integer> {
// use the constructor directly, as getInstance() returns the original predicate when passed
// an array of size one.
final Predicate<Integer> predicate = createMockPredicate(true);
assertTrue(allPredicate(predicate).evaluate(getTestValue()), "single true predicate evaluated to false");
assertTrue(allPredicate(predicate).test(getTestValue()), "single true predicate evaluated to false");
}
/**