View Javadoc

1   package com.lexicalscope.fluentreflection;
2   
3   import static org.hamcrest.Matchers.contains;
4   
5   import java.util.ArrayList;
6   import java.util.List;
7   
8   import org.apache.commons.lang3.builder.EqualsBuilder;
9   import org.apache.commons.lang3.builder.HashCodeBuilder;
10  import org.hamcrest.Description;
11  import org.hamcrest.Matcher;
12  
13  final class MatcherArgumentTypes extends ReflectionMatcher<FluentMember> {
14      private final List<Matcher<? super FluentClass<?>>> argumentTypeMatchers;
15  
16      public MatcherArgumentTypes(final List<? extends Matcher<? super FluentClass<?>>> argumentTypeMatchers) {
17          this.argumentTypeMatchers = new ArrayList<Matcher<? super FluentClass<?>>>(argumentTypeMatchers);
18      }
19  
20      @Override
21      protected boolean matchesSafely(final FluentMember item) {
22          final int actualArgumentCount = item.argCount();
23          final int expectedArgumentCount = argumentTypeMatchers.size();
24  
25          if (actualArgumentCount != expectedArgumentCount) {
26              return false;
27          } else if (actualArgumentCount == 0 && expectedArgumentCount == 0) {
28              return true;
29          }
30  
31          return contains(argumentTypeMatchers).matches(item.args());
32      }
33  
34      @Override
35      public void describeTo(final Description description) {
36          description.appendText("callable with arguments ").appendList("(", ", ", ")", argumentTypeMatchers);
37      }
38  
39      @Override
40      public final boolean equals(final Object that) {
41          if (that != null && that.getClass().equals(this.getClass())) {
42              return new EqualsBuilder()
43                      .append(argumentTypeMatchers, ((MatcherArgumentTypes) that).argumentTypeMatchers)
44                      .isEquals();
45          }
46          return false;
47      }
48  
49      @Override
50      public final int hashCode() {
51          return new HashCodeBuilder().append(argumentTypeMatchers).toHashCode();
52      }
53  }