
org.omnifaces.utils.function.Predicates Maven / Gradle / Ivy
/*
* Copyright 2021 OmniFaces
*
* Licensed 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
*
* https://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.omnifaces.utils.function;
import static java.util.Comparator.naturalOrder;
import java.util.Comparator;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import org.omnifaces.utils.Lang;
public final class Predicates {
private Predicates() {
}
public static Predicate always() {
return t -> true;
}
public static Predicate never() {
return t -> false;
}
public static Predicate isEmpty() {
return Lang::isEmpty;
}
public static Predicate isNotEmpty() {
return Predicates.isEmpty().negate();
}
public static Predicate isNull() {
return t -> t == null;
}
public static Predicate isNotNull() {
return t -> t != null;
}
public static > Predicate isLessThan(T value) {
return isLessThan(value, naturalOrder());
}
public static Predicate isLessThan(T value, Comparator super T> comparator) {
Objects.requireNonNull(value);
Objects.requireNonNull(comparator);
return t -> comparator.compare(t, value) < 0;
}
public static > Predicate isLessThanOrEqual(T value) {
return isLessThanOrEqual(value, naturalOrder());
}
public static Predicate isLessThanOrEqual(T value, Comparator super T> comparator) {
Objects.requireNonNull(value);
Objects.requireNonNull(comparator);
return t -> comparator.compare(t, value) <= 0;
}
public static > Predicate isComparativelyEqualTo(T value) {
return isComparativelyEqualTo(value, naturalOrder());
}
public static > Predicate isComparativelyEqualTo(T value, Comparator super T> comparator) {
Objects.requireNonNull(value);
Objects.requireNonNull(comparator);
return t -> comparator.compare(t, value) == 0;
}
public static > Predicate isGreaterThan(T value) {
return isGreaterThan(value, naturalOrder());
}
public static Predicate isGreaterThan(T value, Comparator super T> comparator) {
Objects.requireNonNull(value);
Objects.requireNonNull(comparator);
return t -> comparator.compare(t, value) > 0;
}
public static > Predicate isGreaterThanOrEqual(T value) {
return isGreaterThanOrEqual(value, naturalOrder());
}
public static Predicate isGreaterThanOrEqual(T value, Comparator super T> comparator) {
Objects.requireNonNull(value);
Objects.requireNonNull(comparator);
return t -> comparator.compare(t, value) >= 0;
}
public static Predicate mapped(Function super T, R> function, Predicate super R> predicate) {
return t -> predicate.test(function.apply(t));
}
public static Predicate not(Predicate predicate) {
return t -> !predicate.test(t);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy