
org.apache.commons.collections4.PredicateUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-collections4 Show documentation
Show all versions of commons-collections4 Show documentation
The Apache Commons Collections package contains types that extend and augment the Java Collections Framework.
The newest version!
/*
* 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;
import java.util.Collection;
import org.apache.commons.collections4.functors.AllPredicate;
import org.apache.commons.collections4.functors.AndPredicate;
import org.apache.commons.collections4.functors.AnyPredicate;
import org.apache.commons.collections4.functors.EqualPredicate;
import org.apache.commons.collections4.functors.ExceptionPredicate;
import org.apache.commons.collections4.functors.FalsePredicate;
import org.apache.commons.collections4.functors.IdentityPredicate;
import org.apache.commons.collections4.functors.InstanceofPredicate;
import org.apache.commons.collections4.functors.InvokerTransformer;
import org.apache.commons.collections4.functors.NonePredicate;
import org.apache.commons.collections4.functors.NotNullPredicate;
import org.apache.commons.collections4.functors.NotPredicate;
import org.apache.commons.collections4.functors.NullIsExceptionPredicate;
import org.apache.commons.collections4.functors.NullIsFalsePredicate;
import org.apache.commons.collections4.functors.NullIsTruePredicate;
import org.apache.commons.collections4.functors.NullPredicate;
import org.apache.commons.collections4.functors.OnePredicate;
import org.apache.commons.collections4.functors.OrPredicate;
import org.apache.commons.collections4.functors.TransformedPredicate;
import org.apache.commons.collections4.functors.TransformerPredicate;
import org.apache.commons.collections4.functors.TruePredicate;
import org.apache.commons.collections4.functors.UniquePredicate;
/**
* {@code PredicateUtils} provides reference implementations and utilities
* for the Predicate functor interface. The supplied predicates are:
*
* - Invoker - returns the result of a method call on the input object
*
- InstanceOf - true if the object is an instanceof a class
*
- Equal - true if the object equals() a specified object
*
- Identity - true if the object == a specified object
*
- Null - true if the object is null
*
- NotNull - true if the object is not null
*
- Unique - true if the object has not already been evaluated
*
- And/All - true if all of the predicates are true
*
- Or/Any - true if any of the predicates is true
*
- Either/One - true if only one of the predicate is true
*
- Neither/None - true if none of the predicates are true
*
- Not - true if the predicate is false, and vice versa
*
- Transformer - wraps a Transformer as a Predicate
*
- True - always return true
*
- False - always return false
*
- Exception - always throws an exception
*
- NullIsException/NullIsFalse/NullIsTrue - check for null input
*
- Transformed - transforms the input before calling the predicate
*
*
* All the supplied predicates are Serializable.
*
*
* @since 3.0
*/
public class PredicateUtils {
/**
* Create a new Predicate that returns true only if all of the specified
* predicates are true. The predicates are checked in iterator order.
* If the collection of predicates is empty, then this predicate returns true.
*
* @param the type that the predicate queries
* @param predicates a collection of predicates to check, may not be null
* @return the {@code all} predicate
* @throws NullPointerException if the predicates collection is null
* @throws NullPointerException if any predicate in the collection is null
* @see AllPredicate
*/
public static Predicate allPredicate(final Collection extends Predicate super T>> predicates) {
return AllPredicate.allPredicate(predicates);
}
/**
* Create a new Predicate that returns true only if all of the specified
* predicates are true.
* If the array of predicates is empty, then this predicate returns true.
*
* @param the type that the predicate queries
* @param predicates an array of predicates to check, may not be null
* @return the {@code all} predicate
* @throws NullPointerException if the predicates array is null
* @throws NullPointerException if any predicate in the array is null
* @see AllPredicate
*/
public static Predicate allPredicate(final Predicate super T>... predicates) {
return AllPredicate.allPredicate(predicates);
}
/**
* Create a new Predicate that returns true only if both of the specified
* predicates are true.
*
* @param the type that the predicate queries
* @param predicate1 the first predicate, may not be null
* @param predicate2 the second predicate, may not be null
* @return the {@code and} predicate
* @throws NullPointerException if either predicate is null
* @see AndPredicate
*/
public static Predicate andPredicate(final Predicate super T> predicate1,
final Predicate super T> predicate2) {
return AndPredicate.andPredicate(predicate1, predicate2);
}
/**
* Create a new Predicate that returns true if any of the specified
* predicates are true. The predicates are checked in iterator order.
* If the collection of predicates is empty, then this predicate returns false.
*
* @param the type that the predicate queries
* @param predicates a collection of predicates to check, may not be null
* @return the {@code any} predicate
* @throws NullPointerException if the predicates collection is null
* @throws NullPointerException if any predicate in the collection is null
* @see AnyPredicate
*/
public static Predicate anyPredicate(final Collection extends Predicate super T>> predicates) {
return AnyPredicate.anyPredicate(predicates);
}
/**
* Create a new Predicate that returns true if any of the specified
* predicates are true.
* If the array of predicates is empty, then this predicate returns false.
*
* @param the type that the predicate queries
* @param predicates an array of predicates to check, may not be null
* @return the {@code any} predicate
* @throws NullPointerException if the predicates array is null
* @throws NullPointerException if any predicate in the array is null
* @see AnyPredicate
*/
public static Predicate anyPredicate(final Predicate super T>... predicates) {
return AnyPredicate.anyPredicate(predicates);
}
/**
* Create a new Predicate that wraps a Transformer. The Transformer must
* return either {@link Boolean#TRUE} or {@link Boolean#FALSE} otherwise a
* PredicateException will be thrown.
*
* @param the type that the predicate queries
* @param transformer the transformer to wrap, may not be null
* @return the transformer wrapping predicate
* @throws NullPointerException if the transformer is null
* @see TransformerPredicate
*/
public static Predicate asPredicate(final Transformer super T, Boolean> transformer) {
return TransformerPredicate.transformerPredicate(transformer);
}
/**
* Create a new Predicate that returns true if one, but not both, of the
* specified predicates are true. XOR
*
* @param the type that the predicate queries
* @param predicate1 the first predicate, may not be null
* @param predicate2 the second predicate, may not be null
* @return the {@code either} predicate
* @throws NullPointerException if either predicate is null
* @see OnePredicate
*/
public static Predicate eitherPredicate(final Predicate super T> predicate1,
final Predicate super T> predicate2) {
@SuppressWarnings("unchecked")
final Predicate onePredicate = onePredicate(predicate1, predicate2);
return onePredicate;
}
/**
* Creates a Predicate that checks if the input object is equal to the
* specified object using equals().
*
* @param the type that the predicate queries
* @param value the value to compare against
* @return the predicate
* @see EqualPredicate
*/
public static Predicate equalPredicate(final T value) {
return EqualPredicate.equalPredicate(value);
}
/**
* Gets a Predicate that always throws an exception.
* This could be useful during testing as a placeholder.
*
* @param the type that the predicate queries
* @return the predicate
* @see ExceptionPredicate
*/
public static Predicate exceptionPredicate() {
return ExceptionPredicate.exceptionPredicate();
}
/**
* Gets a Predicate that always returns false.
*
* @param the type that the predicate queries
* @return the predicate
* @see FalsePredicate
*/
public static Predicate falsePredicate() {
return FalsePredicate.falsePredicate();
}
/**
* Creates a Predicate that checks if the input object is equal to the
* specified object by identity.
*
* @param the type that the predicate queries
* @param value the value to compare against
* @return the predicate
* @see IdentityPredicate
*/
public static Predicate identityPredicate(final T value) {
return IdentityPredicate.identityPredicate(value);
}
/**
* Creates a Predicate that checks if the object passed in is of
* a particular type, using instanceof. A {@code null} input
* object will return {@code false}.
*
* @param type the type to check for, may not be null
* @return the predicate
* @throws NullPointerException if the class is null
* @see InstanceofPredicate
*/
public static Predicate
© 2015 - 2025 Weber Informatics LLC | Privacy Policy