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
  • Loading branch information
garydgregory committed Jul 10, 2024
1 parent 2965ea1 commit d5dd6e4
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
* @param <T> the type of the input to the predicate.
* @since 4.0
*/
public class ComparatorPredicate<T> implements Predicate<T>, Serializable {
public class ComparatorPredicate<T> extends AbstractPredicate<T> implements Serializable {

public enum Criterion {
EQUAL, GREATER, LESS, GREATER_OR_EQUAL, LESS_OR_EQUAL,
Expand Down Expand Up @@ -158,7 +158,7 @@ public ComparatorPredicate(final T object, final Comparator<T> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @param <T> the type of the input to the predicate.
* @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 */
private static final long serialVersionUID = 5633766978029907089L;
Expand Down Expand Up @@ -101,7 +101,7 @@ public EqualPredicate(final T object, final Equator<T> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @param <T> the type of the input to the predicate.
* @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 */
private static final long serialVersionUID = 7179106032121985545L;
Expand Down Expand Up @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @param <T> the type of the input to the predicate.
* @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 */
private static final long serialVersionUID = 7533784454832764388L;
Expand Down Expand Up @@ -59,7 +59,7 @@ private FalsePredicate() {
* @return false always
*/
@Override
public boolean evaluate(final T object) {
public boolean test(final T object) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @param <T> the type of the input to the predicate.
* @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 */
private static final long serialVersionUID = -89901658494523293L;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
* @since 3.0
*/
public final class InstanceofPredicate implements Predicate<Object>, Serializable {
public final class InstanceofPredicate extends AbstractPredicate<Object> implements Serializable {

/** Serial version UID */
private static final long serialVersionUID = -6682656911025165584L;
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @param <T> the type of the input to the predicate.
* @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 */
private static final long serialVersionUID = 7533784454832764388L;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @param <T> the type of the input to the predicate.
* @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 */
private static final long serialVersionUID = 7533784454832764388L;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @param <T> the type of the input to the predicate.
* @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 */
private static final long serialVersionUID = -2407966402920578741L;
Expand Down Expand Up @@ -67,7 +67,7 @@ public TransformerPredicate(final Transformer<? super T, Boolean> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @param <T> the type of the input to the predicate.
* @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 */
private static final long serialVersionUID = 3374767158756189740L;
Expand Down Expand Up @@ -59,7 +59,7 @@ private TruePredicate() {
* @return true always
*/
@Override
public boolean evaluate(final T object) {
public boolean test(final T object) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @param <T> the type of the input to the predicate.
* @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 */
private static final long serialVersionUID = -3319417438027438040L;
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit d5dd6e4

Please sign in to comment.