View Javadoc

1   package com.lexicalscope.fluentreflection;
2   
3   /*
4    * Copyright 2011 Tim Wood
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import static com.lexicalscope.fluentreflection.MatcherAnd.andOf;
20  import static com.lexicalscope.fluentreflection.MatcherOr.orOf;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.hamcrest.Matcher;
26  import org.hamcrest.TypeSafeMatcher;
27  
28  /**
29   * Base class for all the matchers included in the library.
30   *
31   * @author tim
32   *
33   * @param <T>
34   */
35  public abstract class ReflectionMatcher<T> extends TypeSafeMatcher<T> {
36      /**
37       * Creates an "and" matcher combining this matcher and the given one
38       *
39       * @param matcher
40       *            The matcher to be put in and with this one
41       * @return A matcher that return true if this matcher and the passed one
42       *         return true
43       */
44      public ReflectionMatcher<T> and(final Matcher<? super T> matcher) {
45          final List<Matcher<? super T>> list = new ArrayList<Matcher<? super T>>();
46          list.add(this);
47          list.add(matcher);
48          return andOf(list);
49      }
50  
51      /**
52       * Creates an "or" matcher combining this matcher and the given one
53       *
54       * @param matcher
55       *            The matcher to be put in or with this one
56       * @return A matcher that return true if this matcher or the passed one
57       *         return true
58       */
59      public ReflectionMatcher<T> or(final Matcher<? super T> matcher) {
60          final List<Matcher<? super T>> list = new ArrayList<Matcher<? super T>>();
61          list.add(this);
62          list.add(matcher);
63          return orOf(list);
64      }
65  
66      static <T> Matcher<T> allOf(
67              final Matcher<? super T> T0,
68              final Matcher<? super T> T1) {
69          final List<Matcher<? super T>> list = new ArrayList<Matcher<? super T>>();
70          list.add(T0);
71          list.add(T1);
72          return andOf(list);
73      }
74  }