From d5dd6e446864022acf7d65970ae16beb272d8fd8 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Wed, 10 Jul 2024 09:11:34 -0400 Subject: [PATCH] Migrate toward java.util.function.Predicate Maintains binary and source compatibility --- .../functors/AbstractPredicate.java | 33 +++++++++++++++++++ .../functors/ComparatorPredicate.java | 4 +-- .../collections4/functors/EqualPredicate.java | 4 +-- .../functors/ExceptionPredicate.java | 4 +-- .../collections4/functors/FalsePredicate.java | 4 +-- .../functors/IdentityPredicate.java | 4 +-- .../functors/InstanceofPredicate.java | 4 +-- .../functors/NotNullPredicate.java | 4 +-- .../collections4/functors/NullPredicate.java | 4 +-- .../functors/TransformerPredicate.java | 4 +-- .../collections4/functors/TruePredicate.java | 4 +-- .../functors/UniquePredicate.java | 4 +-- 12 files changed, 55 insertions(+), 22 deletions(-) create mode 100644 src/main/java/org/apache/commons/collections4/functors/AbstractPredicate.java diff --git a/src/main/java/org/apache/commons/collections4/functors/AbstractPredicate.java b/src/main/java/org/apache/commons/collections4/functors/AbstractPredicate.java new file mode 100644 index 0000000000..572ef16f52 --- /dev/null +++ b/src/main/java/org/apache/commons/collections4/functors/AbstractPredicate.java @@ -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 the type of the input to the predicate. + * @since 4.5.0 + */ +public abstract class AbstractPredicate implements Predicate { + + @Override + public boolean evaluate(final T object) { + return test(object); + } +} diff --git a/src/main/java/org/apache/commons/collections4/functors/ComparatorPredicate.java b/src/main/java/org/apache/commons/collections4/functors/ComparatorPredicate.java index 265a967fab..0efc3bebfc 100644 --- a/src/main/java/org/apache/commons/collections4/functors/ComparatorPredicate.java +++ b/src/main/java/org/apache/commons/collections4/functors/ComparatorPredicate.java @@ -77,7 +77,7 @@ * @param the type of the input to the predicate. * @since 4.0 */ -public class ComparatorPredicate implements Predicate, Serializable { +public class ComparatorPredicate extends AbstractPredicate implements Serializable { public enum Criterion { EQUAL, GREATER, LESS, GREATER_OR_EQUAL, LESS_OR_EQUAL, @@ -158,7 +158,7 @@ public ComparatorPredicate(final T object, final Comparator comparator, final * @throws IllegalStateException if the criterion is invalid (really not possible) */ @Override - public boolean evaluate(final T target) { + public boolean test(final T target) { boolean result = false; final int comparison = comparator.compare(object, target); diff --git a/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java b/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java index 260398a29a..827fde15bd 100644 --- a/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java +++ b/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java @@ -28,7 +28,7 @@ * @param the type of the input to the predicate. * @since 3.0 */ -public final class EqualPredicate implements Predicate, Serializable { +public final class EqualPredicate extends AbstractPredicate implements Serializable { /** Serial version UID */ private static final long serialVersionUID = 5633766978029907089L; @@ -101,7 +101,7 @@ public EqualPredicate(final T object, final Equator equator) { * @return true if input object equals stored value */ @Override - public boolean evaluate(final T object) { + public boolean test(final T object) { if (equator != null) { return equator.equate(iValue, object); } diff --git a/src/main/java/org/apache/commons/collections4/functors/ExceptionPredicate.java b/src/main/java/org/apache/commons/collections4/functors/ExceptionPredicate.java index bf46f60eae..e58ec9af52 100644 --- a/src/main/java/org/apache/commons/collections4/functors/ExceptionPredicate.java +++ b/src/main/java/org/apache/commons/collections4/functors/ExceptionPredicate.java @@ -27,7 +27,7 @@ * @param the type of the input to the predicate. * @since 3.0 */ -public final class ExceptionPredicate implements Predicate, Serializable { +public final class ExceptionPredicate extends AbstractPredicate implements Serializable { /** Serial version UID */ private static final long serialVersionUID = 7179106032121985545L; @@ -61,7 +61,7 @@ private ExceptionPredicate() { * @throws FunctorException always */ @Override - public boolean evaluate(final T object) { + public boolean test(final T object) { throw new FunctorException("ExceptionPredicate invoked"); } diff --git a/src/main/java/org/apache/commons/collections4/functors/FalsePredicate.java b/src/main/java/org/apache/commons/collections4/functors/FalsePredicate.java index 184234c6bf..e5896c73d1 100644 --- a/src/main/java/org/apache/commons/collections4/functors/FalsePredicate.java +++ b/src/main/java/org/apache/commons/collections4/functors/FalsePredicate.java @@ -26,7 +26,7 @@ * @param the type of the input to the predicate. * @since 3.0 */ -public final class FalsePredicate implements Predicate, Serializable { +public final class FalsePredicate extends AbstractPredicate implements Serializable { /** Serial version UID */ private static final long serialVersionUID = 7533784454832764388L; @@ -59,7 +59,7 @@ private FalsePredicate() { * @return false always */ @Override - public boolean evaluate(final T object) { + public boolean test(final T object) { return false; } diff --git a/src/main/java/org/apache/commons/collections4/functors/IdentityPredicate.java b/src/main/java/org/apache/commons/collections4/functors/IdentityPredicate.java index 74ef13ad79..f58cd65d75 100644 --- a/src/main/java/org/apache/commons/collections4/functors/IdentityPredicate.java +++ b/src/main/java/org/apache/commons/collections4/functors/IdentityPredicate.java @@ -27,7 +27,7 @@ * @param the type of the input to the predicate. * @since 3.0 */ -public final class IdentityPredicate implements Predicate, Serializable { +public final class IdentityPredicate extends AbstractPredicate implements Serializable { /** Serial version UID */ private static final long serialVersionUID = -89901658494523293L; @@ -67,7 +67,7 @@ public IdentityPredicate(final T object) { * @return true if input is the same object as the stored value */ @Override - public boolean evaluate(final T object) { + public boolean test(final T object) { return iValue == object; } diff --git a/src/main/java/org/apache/commons/collections4/functors/InstanceofPredicate.java b/src/main/java/org/apache/commons/collections4/functors/InstanceofPredicate.java index 6cc5915826..149d7d4b9a 100644 --- a/src/main/java/org/apache/commons/collections4/functors/InstanceofPredicate.java +++ b/src/main/java/org/apache/commons/collections4/functors/InstanceofPredicate.java @@ -27,7 +27,7 @@ * * @since 3.0 */ -public final class InstanceofPredicate implements Predicate, Serializable { +public final class InstanceofPredicate extends AbstractPredicate implements Serializable { /** Serial version UID */ private static final long serialVersionUID = -6682656911025165584L; @@ -63,7 +63,7 @@ public InstanceofPredicate(final Class type) { * @return true if input is of stored type */ @Override - public boolean evaluate(final Object object) { + public boolean test(final Object object) { return iType.isInstance(object); } diff --git a/src/main/java/org/apache/commons/collections4/functors/NotNullPredicate.java b/src/main/java/org/apache/commons/collections4/functors/NotNullPredicate.java index 6913cfdb45..d2dc9e2f64 100644 --- a/src/main/java/org/apache/commons/collections4/functors/NotNullPredicate.java +++ b/src/main/java/org/apache/commons/collections4/functors/NotNullPredicate.java @@ -26,7 +26,7 @@ * @param the type of the input to the predicate. * @since 3.0 */ -public final class NotNullPredicate implements Predicate, Serializable { +public final class NotNullPredicate extends AbstractPredicate implements Serializable { /** Serial version UID */ private static final long serialVersionUID = 7533784454832764388L; @@ -59,7 +59,7 @@ private NotNullPredicate() { * @return true if not null */ @Override - public boolean evaluate(final T object) { + public boolean test(final T object) { return object != null; } diff --git a/src/main/java/org/apache/commons/collections4/functors/NullPredicate.java b/src/main/java/org/apache/commons/collections4/functors/NullPredicate.java index 93820fc54f..645fd811b5 100644 --- a/src/main/java/org/apache/commons/collections4/functors/NullPredicate.java +++ b/src/main/java/org/apache/commons/collections4/functors/NullPredicate.java @@ -26,7 +26,7 @@ * @param the type of the input to the predicate. * @since 3.0 */ -public final class NullPredicate implements Predicate, Serializable { +public final class NullPredicate extends AbstractPredicate implements Serializable { /** Serial version UID */ private static final long serialVersionUID = 7533784454832764388L; @@ -59,7 +59,7 @@ private NullPredicate() { * @return true if input is null */ @Override - public boolean evaluate(final T object) { + public boolean test(final T object) { return object == null; } diff --git a/src/main/java/org/apache/commons/collections4/functors/TransformerPredicate.java b/src/main/java/org/apache/commons/collections4/functors/TransformerPredicate.java index 80d61934ba..ff5c796d97 100644 --- a/src/main/java/org/apache/commons/collections4/functors/TransformerPredicate.java +++ b/src/main/java/org/apache/commons/collections4/functors/TransformerPredicate.java @@ -29,7 +29,7 @@ * @param the type of the input to the predicate. * @since 3.0 */ -public final class TransformerPredicate implements Predicate, Serializable { +public final class TransformerPredicate extends AbstractPredicate implements Serializable { /** Serial version UID */ private static final long serialVersionUID = -2407966402920578741L; @@ -67,7 +67,7 @@ public TransformerPredicate(final Transformer transformer) { * @throws FunctorException if the transformer returns an invalid type */ @Override - public boolean evaluate(final T object) { + public boolean test(final T object) { final Boolean result = iTransformer.transform(object); if (result == null) { throw new FunctorException( diff --git a/src/main/java/org/apache/commons/collections4/functors/TruePredicate.java b/src/main/java/org/apache/commons/collections4/functors/TruePredicate.java index ef1dd6a948..d2c3cdfa4a 100644 --- a/src/main/java/org/apache/commons/collections4/functors/TruePredicate.java +++ b/src/main/java/org/apache/commons/collections4/functors/TruePredicate.java @@ -26,7 +26,7 @@ * @param the type of the input to the predicate. * @since 3.0 */ -public final class TruePredicate implements Predicate, Serializable { +public final class TruePredicate extends AbstractPredicate implements Serializable { /** Serial version UID */ private static final long serialVersionUID = 3374767158756189740L; @@ -59,7 +59,7 @@ private TruePredicate() { * @return true always */ @Override - public boolean evaluate(final T object) { + public boolean test(final T object) { return true; } diff --git a/src/main/java/org/apache/commons/collections4/functors/UniquePredicate.java b/src/main/java/org/apache/commons/collections4/functors/UniquePredicate.java index a8b3489141..a91f4537e7 100644 --- a/src/main/java/org/apache/commons/collections4/functors/UniquePredicate.java +++ b/src/main/java/org/apache/commons/collections4/functors/UniquePredicate.java @@ -29,7 +29,7 @@ * @param the type of the input to the predicate. * @since 3.0 */ -public final class UniquePredicate implements Predicate, Serializable { +public final class UniquePredicate extends AbstractPredicate implements Serializable { /** Serial version UID */ private static final long serialVersionUID = -3319417438027438040L; @@ -63,7 +63,7 @@ public UniquePredicate() { * @return true if this is the first time the object is seen */ @Override - public boolean evaluate(final T object) { + public boolean test(final T object) { return iSet.add(object); }