Migrate toward java.util.function.Predicate

Maintains binary and source compatibility
This commit is contained in:
Gary Gregory 2024-07-10 09:11:34 -04:00
parent 2965ea1484
commit d5dd6e4468
12 changed files with 55 additions and 22 deletions

View File

@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections4.functors;
import org.apache.commons.collections4.Predicate;
/**
* Abstract base class for predicates.
*
* @param <T> the type of the input to the predicate.
* @since 4.5.0
*/
public abstract class AbstractPredicate<T> implements Predicate<T> {
@Override
public boolean evaluate(final T object) {
return test(object);
}
}

View File

@ -77,7 +77,7 @@ import org.apache.commons.collections4.Predicate;
* @param <T> the type of the input to the predicate. * @param <T> the type of the input to the predicate.
* @since 4.0 * @since 4.0
*/ */
public class ComparatorPredicate<T> implements Predicate<T>, Serializable { public class ComparatorPredicate<T> extends AbstractPredicate<T> implements Serializable {
public enum Criterion { public enum Criterion {
EQUAL, GREATER, LESS, GREATER_OR_EQUAL, LESS_OR_EQUAL, EQUAL, GREATER, LESS, GREATER_OR_EQUAL, LESS_OR_EQUAL,
@ -158,7 +158,7 @@ public class ComparatorPredicate<T> implements Predicate<T>, Serializable {
* @throws IllegalStateException if the criterion is invalid (really not possible) * @throws IllegalStateException if the criterion is invalid (really not possible)
*/ */
@Override @Override
public boolean evaluate(final T target) { public boolean test(final T target) {
boolean result = false; boolean result = false;
final int comparison = comparator.compare(object, target); final int comparison = comparator.compare(object, target);

View File

@ -28,7 +28,7 @@ import org.apache.commons.collections4.Predicate;
* @param <T> the type of the input to the predicate. * @param <T> the type of the input to the predicate.
* @since 3.0 * @since 3.0
*/ */
public final class EqualPredicate<T> implements Predicate<T>, Serializable { public final class EqualPredicate<T> extends AbstractPredicate<T> implements Serializable {
/** Serial version UID */ /** Serial version UID */
private static final long serialVersionUID = 5633766978029907089L; private static final long serialVersionUID = 5633766978029907089L;
@ -101,7 +101,7 @@ public final class EqualPredicate<T> implements Predicate<T>, Serializable {
* @return true if input object equals stored value * @return true if input object equals stored value
*/ */
@Override @Override
public boolean evaluate(final T object) { public boolean test(final T object) {
if (equator != null) { if (equator != null) {
return equator.equate(iValue, object); return equator.equate(iValue, object);
} }

View File

@ -27,7 +27,7 @@ import org.apache.commons.collections4.Predicate;
* @param <T> the type of the input to the predicate. * @param <T> the type of the input to the predicate.
* @since 3.0 * @since 3.0
*/ */
public final class ExceptionPredicate<T> implements Predicate<T>, Serializable { public final class ExceptionPredicate<T> extends AbstractPredicate<T> implements Serializable {
/** Serial version UID */ /** Serial version UID */
private static final long serialVersionUID = 7179106032121985545L; private static final long serialVersionUID = 7179106032121985545L;
@ -61,7 +61,7 @@ public final class ExceptionPredicate<T> implements Predicate<T>, Serializable {
* @throws FunctorException always * @throws FunctorException always
*/ */
@Override @Override
public boolean evaluate(final T object) { public boolean test(final T object) {
throw new FunctorException("ExceptionPredicate invoked"); throw new FunctorException("ExceptionPredicate invoked");
} }

View File

@ -26,7 +26,7 @@ import org.apache.commons.collections4.Predicate;
* @param <T> the type of the input to the predicate. * @param <T> the type of the input to the predicate.
* @since 3.0 * @since 3.0
*/ */
public final class FalsePredicate<T> implements Predicate<T>, Serializable { public final class FalsePredicate<T> extends AbstractPredicate<T> implements Serializable {
/** Serial version UID */ /** Serial version UID */
private static final long serialVersionUID = 7533784454832764388L; private static final long serialVersionUID = 7533784454832764388L;
@ -59,7 +59,7 @@ public final class FalsePredicate<T> implements Predicate<T>, Serializable {
* @return false always * @return false always
*/ */
@Override @Override
public boolean evaluate(final T object) { public boolean test(final T object) {
return false; return false;
} }

View File

@ -27,7 +27,7 @@ import org.apache.commons.collections4.Predicate;
* @param <T> the type of the input to the predicate. * @param <T> the type of the input to the predicate.
* @since 3.0 * @since 3.0
*/ */
public final class IdentityPredicate<T> implements Predicate<T>, Serializable { public final class IdentityPredicate<T> extends AbstractPredicate<T> implements Serializable {
/** Serial version UID */ /** Serial version UID */
private static final long serialVersionUID = -89901658494523293L; private static final long serialVersionUID = -89901658494523293L;
@ -67,7 +67,7 @@ public final class IdentityPredicate<T> implements Predicate<T>, Serializable {
* @return true if input is the same object as the stored value * @return true if input is the same object as the stored value
*/ */
@Override @Override
public boolean evaluate(final T object) { public boolean test(final T object) {
return iValue == object; return iValue == object;
} }

View File

@ -27,7 +27,7 @@ import org.apache.commons.collections4.Predicate;
* *
* @since 3.0 * @since 3.0
*/ */
public final class InstanceofPredicate implements Predicate<Object>, Serializable { public final class InstanceofPredicate extends AbstractPredicate<Object> implements Serializable {
/** Serial version UID */ /** Serial version UID */
private static final long serialVersionUID = -6682656911025165584L; private static final long serialVersionUID = -6682656911025165584L;
@ -63,7 +63,7 @@ public final class InstanceofPredicate implements Predicate<Object>, Serializabl
* @return true if input is of stored type * @return true if input is of stored type
*/ */
@Override @Override
public boolean evaluate(final Object object) { public boolean test(final Object object) {
return iType.isInstance(object); return iType.isInstance(object);
} }

View File

@ -26,7 +26,7 @@ import org.apache.commons.collections4.Predicate;
* @param <T> the type of the input to the predicate. * @param <T> the type of the input to the predicate.
* @since 3.0 * @since 3.0
*/ */
public final class NotNullPredicate<T> implements Predicate<T>, Serializable { public final class NotNullPredicate<T> extends AbstractPredicate<T> implements Serializable {
/** Serial version UID */ /** Serial version UID */
private static final long serialVersionUID = 7533784454832764388L; private static final long serialVersionUID = 7533784454832764388L;
@ -59,7 +59,7 @@ public final class NotNullPredicate<T> implements Predicate<T>, Serializable {
* @return true if not null * @return true if not null
*/ */
@Override @Override
public boolean evaluate(final T object) { public boolean test(final T object) {
return object != null; return object != null;
} }

View File

@ -26,7 +26,7 @@ import org.apache.commons.collections4.Predicate;
* @param <T> the type of the input to the predicate. * @param <T> the type of the input to the predicate.
* @since 3.0 * @since 3.0
*/ */
public final class NullPredicate<T> implements Predicate<T>, Serializable { public final class NullPredicate<T> extends AbstractPredicate<T> implements Serializable {
/** Serial version UID */ /** Serial version UID */
private static final long serialVersionUID = 7533784454832764388L; private static final long serialVersionUID = 7533784454832764388L;
@ -59,7 +59,7 @@ public final class NullPredicate<T> implements Predicate<T>, Serializable {
* @return true if input is null * @return true if input is null
*/ */
@Override @Override
public boolean evaluate(final T object) { public boolean test(final T object) {
return object == null; return object == null;
} }

View File

@ -29,7 +29,7 @@ import org.apache.commons.collections4.Transformer;
* @param <T> the type of the input to the predicate. * @param <T> the type of the input to the predicate.
* @since 3.0 * @since 3.0
*/ */
public final class TransformerPredicate<T> implements Predicate<T>, Serializable { public final class TransformerPredicate<T> extends AbstractPredicate<T> implements Serializable {
/** Serial version UID */ /** Serial version UID */
private static final long serialVersionUID = -2407966402920578741L; private static final long serialVersionUID = -2407966402920578741L;
@ -67,7 +67,7 @@ public final class TransformerPredicate<T> implements Predicate<T>, Serializable
* @throws FunctorException if the transformer returns an invalid type * @throws FunctorException if the transformer returns an invalid type
*/ */
@Override @Override
public boolean evaluate(final T object) { public boolean test(final T object) {
final Boolean result = iTransformer.transform(object); final Boolean result = iTransformer.transform(object);
if (result == null) { if (result == null) {
throw new FunctorException( throw new FunctorException(

View File

@ -26,7 +26,7 @@ import org.apache.commons.collections4.Predicate;
* @param <T> the type of the input to the predicate. * @param <T> the type of the input to the predicate.
* @since 3.0 * @since 3.0
*/ */
public final class TruePredicate<T> implements Predicate<T>, Serializable { public final class TruePredicate<T> extends AbstractPredicate<T> implements Serializable {
/** Serial version UID */ /** Serial version UID */
private static final long serialVersionUID = 3374767158756189740L; private static final long serialVersionUID = 3374767158756189740L;
@ -59,7 +59,7 @@ public final class TruePredicate<T> implements Predicate<T>, Serializable {
* @return true always * @return true always
*/ */
@Override @Override
public boolean evaluate(final T object) { public boolean test(final T object) {
return true; return true;
} }

View File

@ -29,7 +29,7 @@ import org.apache.commons.collections4.Predicate;
* @param <T> the type of the input to the predicate. * @param <T> the type of the input to the predicate.
* @since 3.0 * @since 3.0
*/ */
public final class UniquePredicate<T> implements Predicate<T>, Serializable { public final class UniquePredicate<T> extends AbstractPredicate<T> implements Serializable {
/** Serial version UID */ /** Serial version UID */
private static final long serialVersionUID = -3319417438027438040L; private static final long serialVersionUID = -3319417438027438040L;
@ -63,7 +63,7 @@ public final class UniquePredicate<T> implements Predicate<T>, Serializable {
* @return true if this is the first time the object is seen * @return true if this is the first time the object is seen
*/ */
@Override @Override
public boolean evaluate(final T object) { public boolean test(final T object) {
return iSet.add(object); return iSet.add(object);
} }