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

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 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 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 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]", requireNonNull(regex)),
        s -> s.matches(regex)
    );
  }

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

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

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

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

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

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

  public static  Predicate> 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> 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> containsExactly(Collection collection) {
    requireNonNull(collection);
    return Printable.predicate(
        String.format("containsExactly%s", collection),
        c -> c.containsAll(collection) && collection.containsAll(c)
    );
  }

  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 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)
    );
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy