almost.functional.Predicates Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of almost-functional Show documentation
Show all versions of almost-functional Show documentation
Almost Functional is very low impact functional classes inspired by guava, jdk 1.8 and various others.
/*
* Copyright (c) 2013-2014, [email protected]
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with or
* without fee is hereby granted, provided that the above copyright notice and this permission
* notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR
* ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
* OR PERFORMANCE OF THIS SOFTWARE.
*/
package almost.functional;
import almost.functional.utils.Iterables;
/**
* Utility operations on predicates.
*/
public final class Predicates {
private Predicates(){}
/**
* Returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object).
* @param targetRef the object reference with which to compare for equality, which may be null
* @param the type of arguments to the predicate
* @return a predicate that tests if two arguments are equal according to Objects.equals(Object, Object)
*/
public static Predicate isEqual(final T targetRef) {
return new Predicate() {
@Override
public boolean test(T t) {
return t.equals(targetRef);
}
};
}
/**
* Returns a predicate that that tests if an iterable contains an argument.
* @param iterable the iterable, may not be null
* @param the type of the argument to the predicate
* @return a predicate that that tests if an iterable contains an argument
*/
public static Predicate contains(final Iterable iterable) {
return new Predicate() {
@Override
public boolean test(T t) {
return Iterables.contains(iterable, t);
}
};
}
/**
* Negate an existing predicate's test result.
* @param predicate An existing predicate.
* @param type of the predicate.
* @return new predicate.
* @since 1.5
*/
public static Predicate negate(final Predicate super T> predicate) {
return new Predicate() {
@Override
public boolean test(T t) {
return !predicate.test(t);
}
};
}
/**
* Create a predicate which is a logical and of two existing predicate.
* @param first first predicate.
* @param second second predicate.
* @param type of the predicates
* @return resultant predicate
* @since 1.5
*/
public static Predicate and(final Predicate super T> first, final Predicate super T> second) {
return new Predicate() {
@Override
public boolean test(T t) {
return first.test(t) && second.test(t);
}
};
}
/**
* Create a predicate which is a logical or of two existing predicate.
* @param first first predicate.
* @param second second predicate.
* @param type of the predicates
* @return resultant predicate
* @since 1.5
*/
public static Predicate or(final Predicate super T> first, final Predicate super T> second) {
return new Predicate() {
@Override
public boolean test(T t) {
return first.test(t) || second.test(t);
}
};
}
}