All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dakusui.crest.utils.printable.Predicates Maven / Gradle / Ivy

package com.github.dakusui.crest.utils.printable;

import com.github.dakusui.crest.utils.InternalUtils;

import java.util.Collection;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static com.github.dakusui.crest.utils.InternalUtils.summarize;
import static java.util.Objects.requireNonNull;

public enum Predicates {
  ;

  public static  Predicate alwaysTrue() {
    return Printable.predicate("alwaysTrue", t -> true);
  }

  public static Predicate isTrue() {
    return Printable.predicate("isTrue", (Boolean v) -> v);
  }

  public static Predicate isFalse() {
    return Printable.predicate("isFalse", (Boolean v) -> !v);
  }

  public static  Predicate isNull() {
    return Printable.predicate("isNull", Objects::isNull);
  }

  public static  Predicate isNotNull() {
    return Printable.predicate("isNotNull", Objects::nonNull);
  }

  public static  Predicate equalTo(T value) {
    return Printable.predicate(
        () -> String.format("equalTo[%s]", value),
        v -> Objects.equals(v, value)
    );
  }

  public static  Predicate isSameAs(T value) {
    return Printable.predicate(
        () -> String.format("==[%s]", value),
        v -> v == value
    );
  }

  @SuppressWarnings("unchecked")
  public static  Predicate 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 gt(T value) {
    return Printable.predicate(
        () -> String.format(">[%s]", value),
        v -> v.compareTo(value) > 0
    );
  }

  public static > Predicate ge(T value) {
    return Printable.predicate(
        () -> String.format(">=[%s]", value),
        v -> v.compareTo(value) >= 0
    );
  }

  public static > Predicate lt(T value) {
    return Printable.predicate(
        () -> String.format("<[%s]", value),
        v -> v.compareTo(value) < 0
    );
  }

  public static > Predicate le(T value) {
    return Printable.predicate(
        () -> String.format("<=[%s]", value),
        v -> v.compareTo(value) <= 0
    );
  }

  public static > Predicate eq(T value) {
    return Printable.predicate(
        () -> String.format("~[%s]", value),
        v -> v.compareTo(value) == 0
    );
  }

  public static Predicate matchesRegex(String regex) {
    requireNonNull(regex);
    return Printable.predicate(
        () -> String.format("matchesRegex[%s]", regex),
        s -> s.matches(regex)
    );
  }

  public static Predicate containsString(String string) {
    requireNonNull(string);
    return Printable.predicate(
        () -> String.format("containsString[%s]", string),
        s -> s.contains(string)
    );
  }

  public static Predicate startsWith(String string) {
    requireNonNull(string);
    return Printable.predicate(
        () -> String.format("startsWith[%s]", string),
        s -> s.startsWith(string)
    );
  }

  public static Predicate endsWith(String string) {
    requireNonNull(string);
    return Printable.predicate(
        () -> String.format("endsWith[%s]", string),
        s -> s.endsWith(string)
    );
  }

  public static Predicate equalsIgnoreCase(String string) {
    requireNonNull(string);
    return Printable.predicate(
        () -> String.format("equalsIgnoreCase[%s]", string),
        s -> s.equalsIgnoreCase(string)
    );
  }

  public static Predicate isEmptyString() {
    return Printable.predicate(
        "isEmpty",
        String::isEmpty
    );
  }

  public static Predicate isEmptyOrNullString() {
    return Printable.predicate(
        "isEmptyOrNullString",
        s -> Objects.isNull(s) || isEmptyString().test(s)
    );
  }

  public static  Predicate> contains(Object entry) {
    requireNonNull(entry);
    //noinspection SuspiciousMethodCalls
    return Printable.predicate(
        () -> String.format("contains[%s]", entry),
        c -> c.contains(entry)
    );
  }

  public static Predicate isEmptyArray() {
    return Printable.predicate("isEmpty", objects -> objects.length == 0);
  }

  public static Predicate isEmpty() {
    return Printable.predicate("isEmpty", Collection::isEmpty);
  }

  public static  Predicate> allMatch(Predicate predicate) {
    return Printable.predicate(
        () -> String.format("allMatch[%s]", requireNonNull(predicate)),
        stream -> stream.allMatch(predicate)
    );
  }

  public static  Predicate> noneMatch(Predicate predicate) {
    return Printable.predicate(
        () -> String.format("noneMatch[%s]", requireNonNull(predicate)),
        stream -> stream.noneMatch(predicate)
    );
  }

  public static  Predicate> anyMatch(Predicate predicate) {
    return Printable.predicate(
        () -> String.format("anyMatch[%s]", requireNonNull(predicate)),
        stream -> stream.anyMatch(predicate)
    );
  }

  public static  Predicate invoke(String methodName, Object... args) {
    return Printable.predicate(
        () -> String.format(".%s%s", methodName, String.join(",", summarize(args))),
        (Object target) -> InternalUtils.invokeMethod(target, methodName, args)
    );
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy