![JAR search and dependency download from the Maven repository](/logo.png)
org.lockss.tdb.Predicates Maven / Gradle / Ivy
/*
Copyright (c) 2000-2018, Board of Trustees of Leland Stanford Jr. University,
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lockss.tdb;
/**
*
* Useful {@link Predicate} utilities.
*
*
* @author Thib Guicherd-Callin
* @since 1.67
*/
public class Predicates {
/**
*
* Prevent instantionation.
*
*
* @since 1.67
*/
private Predicates() {
// Prevent instantiation
}
/**
*
* A predicate that is always true.
*
*
* @author Thib Guicherd-Callin
* @param The argument type over which the predicate applies.
* @since 1.67
*/
public static class TruePredicate implements Predicate {
@Override
public boolean test(A a) {
return true;
}
}
/**
*
* A predicate that is always false.
*
*
* @author Thib Guicherd-Callin
* @param The argument type over which the predicate applies.
* @since 1.67
*/
public static class FalsePredicate implements Predicate {
@Override
public boolean test(A a) {
return false;
}
}
/**
*
* An abstract predicate that has another predicate as a member.
*
*
* @author Thib Guicherd-Callin
* @param The argument type over which the predicate applies.
* @since 1.67
*/
public static abstract class UnaryPredicate implements Predicate {
/**
*
* A predicate.
*
*
* @since 1.67
*/
protected Predicate predicate1;
/**
*
* Makes a new unary predicate with the given predicate.
*
*
* @param predicate1 A predicate.
*/
public UnaryPredicate(Predicate predicate1) {
this.predicate1 = predicate1;
}
}
/**
*
* A predicate that is the opposite of another predicate.
*
*
* @author Thib Guicherd-Callin
* @param The argument type over which the predicate applies.
* @since 1.67
*/
public static class NotPredicate extends UnaryPredicate {
/**
*
* Makes a new predicate that is the opposite of the given predicate.
*
*
* @param predicate1 A predicate to negate.
* @since 1.67
*/
public NotPredicate(Predicate predicate1) {
super(predicate1);
}
@Override
public boolean test(A a) {
return !predicate1.test(a);
}
}
/**
*
* An abstract predicate that has two other predicates as members.
*
*
* @author Thib Guicherd-Callin
* @param The argument type over which the predicate applies.
* @since 1.67
*/
public static abstract class BinaryPredicate extends UnaryPredicate {
/**
*
* Another predicate.
*
*
* @since 1.67
*/
protected Predicate predicate2;
/**
*
* Makes a new binary predicate with the two given predicates.
*
*
* @param predicate1 A predicate.
* @param predicate2 Another predicate.
* @since 1.67
*/
public BinaryPredicate(Predicate predicate1,
Predicate predicate2) {
super(predicate1);
this.predicate2 = predicate2;
}
}
/**
*
* A predicate that is true if and only if both of its constituent predicates
* are true.
*
* This is implemented in usual short-circuit manner: if the first
* predicate evaluates to false, the second predicate is not evaluated.
*
* @author Thib Guicherd-Callin
* @param The argument type over which the predicate applies.
* @since 1.67
*/
public static class AndPredicate extends BinaryPredicate {
/**
*
* Makes a new predicate that is true if and only if both given predicates
* are true.
*
*
* @param predicate1 A predicate.
* @param predicate2 Another predicate.
* @since 1.67
*/
public AndPredicate(Predicate predicate1,
Predicate predicate2) {
super(predicate1, predicate2);
}
@Override
public boolean test(A a) {
return predicate1.test(a) && predicate2.test(a);
}
}
/**
*
* A predicate that is true if and only if either of its constituent
* predicates is true.
*
*
* This is implemented in usual short-circuit manner: if the first
* predicate evaluates to true, the second predicate is not evaluated.
*
*
* @author Thib Guicherd-Callin
* @param
* The argument type over which the predicate applies.
* @since 1.67
*/
public static class OrPredicate extends BinaryPredicate {
/**
*
* Makes a new predicate that is true if and only if either given predicates
* is true.
*
*
* @param predicate1 A predicate.
* @param predicate2 Another predicate.
* @since 1.67
*/
public OrPredicate(Predicate predicate1,
Predicate predicate2) {
super(predicate1, predicate2);
}
@Override
public boolean test(A a) {
return predicate1.test(a) || predicate2.test(a);
}
}
}