1   package com.lexicalscope.fluentreflection.bean.endtoend;
2   
3   import static org.hamcrest.Matchers.*;
4   
5   import java.util.Map.Entry;
6   
7   import org.hamcrest.Description;
8   import org.hamcrest.Matcher;
9   import org.hamcrest.TypeSafeMatcher;
10  
11  /*
12   * Copyright 2011 Tim Wood
13   *
14   * Licensed under the Apache License, Version 2.0 (the "License");
15   * you may not use this file except in compliance with the License.
16   * You may obtain a copy of the License at
17   *
18   * http://www.apache.org/licenses/LICENSE-2.0
19   *
20   * Unless required by applicable law or agreed to in writing, software
21   * distributed under the License is distributed on an "AS IS" BASIS,
22   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23   * See the License for the specific language governing permissions and
24   * limitations under the License. 
25   */
26  
27  class MapEntryMatcher<K, V> extends TypeSafeMatcher<Entry<K, V>> {
28      private final Matcher<? super K> key;
29      private final Matcher<? super V> value;
30  
31      private MapEntryMatcher(final Matcher<? super K> key, final Matcher<? super V> value) {
32          this.key = key;
33          this.value = value;
34      }
35  
36      @Override protected boolean matchesSafely(final Entry<K, V> item) {
37          return key.matches(item.getKey()) && value.matches(item.getValue());
38      }
39  
40      @Override public void describeTo(final Description description) {
41          description.
42                  appendText("Map.Entry ").
43                  appendDescriptionOf(key).
44                  appendText(" -> ").
45                  appendDescriptionOf(value);
46      }
47  
48      public static <K, V> MapEntryMatcher<K, V> mapEntry(final K key, final V value) {
49          return new MapEntryMatcher<K, V>(equalTo(key), equalTo(value));
50      }
51  
52      public static <K, V> MapEntryMatcher<K, V> mapEntry(final K key) {
53          return new MapEntryMatcher<K, V>(equalTo(key), anything());
54      }
55  }