package org.hamcrest;

/**
 * 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 https://github.com/npryce/maybe-java
 * @author Steve Freeman 2012 http://www.hamcrest.com
 */

public abstract class Condition<T> {
    public static final NotMatched<Object> NOT_MATCHED = new NotMatched<Object>();

    public interface Step<I, O> {
        Condition<O> apply(I value, Description mismatch);
    }

    public boolean matching(Matcher<T> match) { return matching(match, ""); }
    public abstract boolean matching(Matcher<T> match, String message);
    public abstract <U> Condition<U> and(Step<? super T, U> mapping);
    public final <U> Condition<U> then(Step<? super T, U> mapping) { return and(mapping); }

    @SuppressWarnings("unchecked")
    public static <T> Condition<T> notMatched() {
        return (Condition<T>) NOT_MATCHED;
    }

    public static <T> Condition<T> matched(final T theValue, final Description mismatch) {
        return new Condition<T>() {
            @Override
            public boolean matching(Matcher<T> matcher, String message) {
                if (matcher.matches(theValue)) {
                    return true;
                }
                mismatch.appendText(message);
                matcher.describeMismatch(theValue, mismatch);
                return false;
            }

            @Override
            public <U> Condition<U> and(Step<? super T, U> next) {
                return next.apply(theValue, mismatch);
            }
        };
    }

    private static class NotMatched<T> extends Condition<T> {
        @Override public boolean matching(Matcher<T> match, String message) { return false; }

        @Override public <U> Condition<U> and(Step<? super T, U> mapping) {
            return notMatched();
        }
    }
}

@@ -668,80 +668,31 @@
    p
-ublic boolean matching(Matcher%3CT%3E match) %7B return matching(match, %22%22); %7D
+rivate Condition() %7B %7D%0A
 %0A   
@@ -829,16 +829,101 @@
 apping);
+%0A%0A    public final boolean matching(Matcher%3CT%3E match) %7B return matching(match, %22%22); %7D
 %0A    pub
@@ -1253,16 +1253,105 @@
 urn new 
+Matched%3CT%3E(theValue, mismatch);%0A    %7D%0A%0A    private static final class Matched%3CT%3E extends 
 Conditio
@@ -1354,18 +1354,16 @@
 ition%3CT%3E
-()
  %7B%0A     
@@ -1357,32 +1357,254 @@
 on%3CT%3E %7B%0A        
+private final T theValue;%0A        private final Description mismatch;%0A%0A        private Matched(T theValue, Description mismatch) %7B%0A            this.theValue = theValue;%0A            this.mismatch = mismatch;%0A        %7D%0A%0A    
     @Override%0A  
@@ -1593,36 +1593,32 @@
       @Override%0A
-    
         public b
@@ -1667,28 +1667,24 @@
  message) %7B%0A
-    
             
@@ -1728,28 +1728,24 @@
             
-    
 return true;
@@ -1753,33 +1753,26 @@
             
-    %7D%0A   
+%7D%0A
             
@@ -1771,17 +1771,16 @@
         
- 
 mismatch
@@ -1801,20 +1801,16 @@
 ssage);%0A
-    
         
@@ -1867,28 +1867,24 @@
             
-    
 return false
@@ -1889,35 +1889,27 @@
 se;%0A        
-   
- 
 %7D%0A%0A
-    
         @Ove
@@ -1910,28 +1910,24 @@
   @Override%0A
-    
         publ
@@ -1981,36 +1981,32 @@
 ) %7B%0A            
-  
-  
 return next.appl
@@ -2040,24 +2040,9 @@
     
-    %7D%0A        %7D;
+%7D
 %0A   
@@ -2064,16 +2064,22 @@
  static 
+final 
 class No

tidy up Condition a bit, and prevent sub-classing.