
com.github.dakusui.crest.functions.CrestPredicates Maven / Gradle / Ivy
package com.github.dakusui.crest.functions;
import com.github.dakusui.crest.core.Printable;
import com.github.dakusui.crest.core.InternalUtils;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Stream;
import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;
public enum CrestPredicates {
;
public static Predicate super T> alwaysTrue() {
return Printable.predicate(
"alwaysTrue",
t -> true
);
}
public static Predicate super Boolean> isTrue() {
return Printable.predicate(
"isTrue",
(Boolean v) -> v
);
}
public static Predicate super Boolean> isFalse() {
return Printable.predicate(
"isFalse",
(Boolean v) -> !v
);
}
public static Predicate super T> isNull() {
return Printable.predicate(
"isNull",
Objects::isNull
);
}
public static Predicate super T> isNotNull() {
return Printable.predicate(
"isNotNull",
Objects::nonNull
);
}
public static Predicate super T> equalTo(T value) {
return Printable.predicate(
String.format("equalTo[%s]", value),
v -> Objects.equals(v, value)
);
}
public static Predicate super T> isSameAs(T value) {
return Printable.predicate(
String.format("==[%s]", value),
v -> v == value
);
}
@SuppressWarnings("unchecked")
public static Predicate super T> isInstanceOf(Class> value) {
requireNonNull(value);
//noinspection SimplifiableConditionalExpression
return Printable.predicate(
String.format("isInstanceOf[%s]", value.getCanonicalName()),
v -> v == null ?
false :
value.isAssignableFrom(v.getClass())
);
}
public static Predicate super T> invoke(String methodName, Object[] args) {
return Printable.predicate(
String.format("@%s%s", methodName, asList(args)),
(Object target) -> (boolean) InternalUtils.invokeMethod(target, methodName, args)
);
}
public static > Predicate super T> gt(T value) {
return Printable.predicate(
String.format(">[%s]", value),
v -> v.compareTo(value) > 0
);
}
public static > Predicate super T> ge(T value) {
return Printable.predicate(
String.format(">=[%s]", value),
v -> v.compareTo(value) >= 0
);
}
public static > Predicate super T> lt(T value) {
return Printable.predicate(
String.format("<[%s]", value),
v -> v.compareTo(value) < 0
);
}
public static > Predicate super T> le(T value) {
return Printable.predicate(
String.format("<=[%s]", value),
v -> v.compareTo(value) <= 0
);
}
public static > Predicate super T> eq(T value) {
return Printable.predicate(
String.format("=[%s]", value),
v -> v.compareTo(value) == 0
);
}
public static Predicate super String> matchesRegex(String regex) {
requireNonNull(regex);
return Printable.predicate(
String.format("matchesRegex[%s]", requireNonNull(regex)),
s -> s.matches(regex)
);
}
public static Predicate super String> containsString(String string) {
return Printable.predicate(
String.format("containsString[%s]", requireNonNull(string)),
s -> s.contains(string)
);
}
public static Predicate super String> startsWith(String string) {
return Printable.predicate(
String.format("containsString[%s]", requireNonNull(string)),
s -> s.startsWith(string)
);
}
public static Predicate super String> endsWith(String string) {
return Printable.predicate(
String.format("endsWith[%s]", requireNonNull(string)),
s -> s.endsWith(string)
);
}
public static Predicate super String> equalsIgnoreCase(String string) {
requireNonNull(string);
return Printable.predicate(
String.format("equalsIgnoreCase[%s]", requireNonNull(string)),
s -> s.equalsIgnoreCase(string)
);
}
public static Predicate super String> isEmptyString() {
return Printable.predicate(
"isEmpty",
String::isEmpty
);
}
public static Predicate super String> isEmptyOrNullString() {
return Printable.predicate(
"isEmptyOrNull",
s -> Objects.isNull(s) || isEmptyString().test(s)
);
}
public static Predicate super Collection super E>> containsAll(Collection> collection) {
requireNonNull(collection);
return Printable.predicate(
String.format("containsAll%s", collection),
c -> c.containsAll(collection)
);
}
/*
* in any order
* unlike AssertJ, this method returns true even if target collection does not over all the items in given
* collection as long as all the items in the target collection are found in given one.
*/
public static Predicate super Collection super E>> containsOnly(Collection> collection) {
requireNonNull(collection);
return Printable.predicate(
String.format("containsOnly%s", collection),
collection::containsAll
);
}
/*
* This is more similar to AssertJ's containsOnly method than our containsOnly.
* This method returns true if and only if all the items in the target collection
* and the given collection are equal.
*/
public static Predicate super Collection>> containsExactly(Collection> collection) {
requireNonNull(collection);
return Printable.predicate(
String.format("containsExactly%s", collection),
c -> c.containsAll(collection) && collection.containsAll(c)
);
}
public static Predicate super Collection> contains(Object entry) {
requireNonNull(entry);
//noinspection SuspiciousMethodCalls
return Printable.predicate(
String.format("contains[%s]", entry),
c -> c.contains(entry)
);
}
public static Predicate super Collection> isEmpty() {
return Printable.predicate(
"isEmpty",
Collection::isEmpty
);
}
public static Predicate super Stream extends E>> allMatch(Predicate predicate) {
return Printable.predicate(
String.format("allMatch[%s]", requireNonNull(predicate)),
stream -> stream.allMatch(predicate)
);
}
public static Predicate super Stream extends E>> noneMatch(Predicate predicate) {
return Printable.predicate(
String.format("noneMatch[%s]", requireNonNull(predicate)),
stream -> stream.noneMatch(predicate)
);
}
public static Predicate super Stream extends E>> anyMatch(Predicate predicate) {
return Printable.predicate(
String.format("anyMatch[%s]", requireNonNull(predicate)),
stream -> stream.anyMatch(predicate)
);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy