package org.hamcrest.core;

import org.hamcrest.*;

import java.util.ArrayList;

public class CombinableMatcher<T> extends TypeSafeDiagnosingMatcher<T> {
  private final Matcher<? super T> matcher;

  public CombinableMatcher(Matcher<? super T> matcher) {
    this.matcher = matcher;
  }

  @Override
  protected boolean matchesSafely(T item, Description mismatch) {
    if (!matcher.matches(item)) {
      matcher.describeMismatch(item, mismatch);
      return false;
    }
    return true;
  }

  @Override
  public void describeTo(Description description) {
    description.appendDescriptionOf(matcher);
  }

  public CombinableMatcher<T> and(Matcher<? super T> other) {
    return new CombinableMatcher<T>(new AllOf<T>(templatedListWith(other)));
  }

  public CombinableMatcher<T> or(Matcher<? super T> other) {
    return new CombinableMatcher<T>(new AnyOf<T>(templatedListWith(other)));
  }

  private ArrayList<Matcher<? super T>> templatedListWith(Matcher<? super T> other) {
    ArrayList<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
    matchers.add(matcher);
    matchers.add(other);
    return matchers;
  }

  /**
   * This is useful for fluently combining matchers that must both pass.  For example:
   * <pre>
   *   assertThat(string, both(containsString("a")).and(containsString("b")));
   * </pre>
   */
  @Factory
  public static <LHS> CombinableMatcher<LHS> both(Matcher<? super LHS> matcher) {
    return new CombinableMatcher<LHS>(matcher);
  }

  /**
   * This is useful for fluently combining matchers where either may pass, for example:
   * <pre>
   *   assertThat(string, both(containsString("a")).and(containsString("b")));
   * </pre>
   */
  @Factory
  public static <LHS> CombinableMatcher<LHS> either(Matcher<? super LHS> matcher) {
    return new CombinableMatcher<LHS>(matcher);
  }
}

package org.hamcrest.core;

import org.hamcrest.*;

import java.util.ArrayList;

public class CombinableMatcher<T> extends TypeSafeDiagnosingMatcher<T> {
  private final Matcher<? super T> matcher;

  public CombinableMatcher(Matcher<? super T> matcher) {
    this.matcher = matcher;
  }

  @Override
  protected boolean matchesSafely(T item, Description mismatch) {
    if (!matcher.matches(item)) {
      matcher.describeMismatch(item, mismatch);
      return false;
    }
    return true;
  }

  @Override
  public void describeTo(Description description) {
    description.appendDescriptionOf(matcher);
  }

  public CombinableMatcher<T> and(Matcher<? super T> other) {
    return new CombinableMatcher<T>(new AllOf<T>(templatedListWith(other)));
  }

  public CombinableMatcher<T> or(Matcher<? super T> other) {
    return new CombinableMatcher<T>(new AnyOf<T>(templatedListWith(other)));
  }

  private ArrayList<Matcher<? super T>> templatedListWith(Matcher<? super T> other) {
    ArrayList<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
    matchers.add(matcher);
    matchers.add(other);
    return matchers;
  }

  /**
   * This is useful for fluently combining matchers that must both pass.  For example:
   * <pre>
   *   assertThat(string, both(containsString("a")).and(containsString("b")));
   * </pre>
   */
  @Factory
  public static <LHS> CombinableBothMatcher<LHS> both(Matcher<? super LHS> matcher) {
    return new CombinableBothMatcher<LHS>(matcher);
  }
  
  public static final class CombinableBothMatcher<X> {
    private final Matcher<? super X> first;
    public CombinableBothMatcher(Matcher<? super X> matcher) {
        this.first = matcher;
    }
    public CombinableMatcher<X> and(Matcher<? super X> other) {
      return new CombinableMatcher<X>(first).and(other);
    }
  }

  /**
   * This is useful for fluently combining matchers where either may pass, for example:
   * <pre>
   *   assertThat(string, both(containsString("a")).and(containsString("b")));
   * </pre>
   */
  @Factory
  public static <LHS> CombinableEitherMatcher<LHS> either(Matcher<? super LHS> matcher) {
    return new CombinableEitherMatcher<LHS>(matcher);
  }
  
  public static final class CombinableEitherMatcher<X> {
    private final Matcher<? super X> first;
    public CombinableEitherMatcher(Matcher<? super X> matcher) {
        this.first = matcher;
    }
    public CombinableMatcher<X> or(Matcher<? super X> other) {
      return new CombinableMatcher<X>(first).or(other);
    }
  }
}
address issue 154 (reported on google code project) : Prevent either().and() and both().or(). This should also attend to issue 236 on the junit project https://github.com/KentBeck/junit/issues/236 and pull request 272 on the junit project https://github.com/KentBeck/junit/pull/272