Package org.hamcrest

Class Condition<T>

java.lang.Object
org.hamcrest.Condition<T>
Type Parameters:
T - the matched value type

public abstract class Condition<T> extends Object
A Condition implements part of a multi-step match. We sometimes need to write matchers that have a sequence of steps, where each step depends on the result of the previous step, and we can stop processing as soon as a step fails. These classes provide infrastructure for writing such a sequence.

Based on Nat Pryce's maybe-java.

  • Method Details

    • matching

      public abstract boolean matching(Matcher<T> match, String message)
      Applies the matcher as the final step in the sequence
      Parameters:
      match - the value matcher
      message - a description of the value
      Returns:
      true if the matcher matches the value, otherwise false
    • matching

      public final boolean matching(Matcher<T> match)
      Applies the matcher as the final step in the sequence
      Parameters:
      match - the value matcher
      Returns:
      true if the matcher matches the value, otherwise false
    • and

      public abstract <U> Condition<U> and(Condition.Step<? super T,U> mapping)
      Applies the mapping to the current value in the sequence
      Type Parameters:
      U - the type of the next value
      Parameters:
      mapping - the current step in the sequence
      Returns:
      the condition for the next step in the sequence
    • then

      public final <U> Condition<U> then(Condition.Step<? super T,U> mapping)
      An alias for and(Step), which applies the mapping to the current value in the sequence.
      Type Parameters:
      U - the type of the next value
      Parameters:
      mapping - the current step in the sequence
      Returns:
      the condition for the next step in the sequence
    • notMatched

      public static <T> Condition<T> notMatched()
      Called by steps when a mismatch occurs.
      Type Parameters:
      T - the type of the unmatched value
      Returns:
      a condition in the not matched state
    • matched

      public static <T> Condition<T> matched(T theValue, Description mismatch)
      Called by steps when a match occurs
      Type Parameters:
      T - the type of the matched value
      Parameters:
      theValue - the value that was matched
      mismatch - a description for potential future mismatches
      Returns:
      the condition in a matched state