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

org.pitest.sequence.Match Maven / Gradle / Ivy

The newest version!
package org.pitest.sequence;

import java.util.function.Predicate;

import static org.pitest.sequence.Result.result;

/**
 * Predicate with additional context.
 *
 * Implemented as abstract class as we're still on Java
 * 6 and don't have default methods
 *
 * @param  Type to match
 */
@FunctionalInterface
public interface Match {

  Result test(Context c, T t);

  static  Match always() {
    return (c, t) -> result(true, c);
  }

  static  Match never() {
    return (c, t) -> result(false, c);
  }

  static  Match isEqual(final Object targetRef) {
    return (c, t) -> result(targetRef.equals(t), c);
  }

  default Match and(final Match other) {
      return (c, t) -> {
        Result r = this.test(c,t);
        if (!r.result()) {
          return r;
        }
        return other.test(r.context(), t);
      };
  }

  default Match negate() {
    return (c, t) -> {
      Result r = this.test(c,t);
      if (!r.result()) {
        return result(true, r.context());
      }
      return result(false, c);
    };
  }

  default Match or(final Match other) {
    return (c, t) -> {
      Result r = this.test(c,t);
      if (r.result()) {
        return r;
      }
      return other.test(c,t);
    };
  }

  /**
   * Convert to plain predicate with no context
   */
  default Predicate asPredicate() {
    return t -> this.test(Context.start(), t).result();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy