1
package org.hamcrest.collection;
3
import org.hamcrest.Description;
4
import org.hamcrest.Factory;
5
import org.hamcrest.Matcher;
6
import org.hamcrest.TypeSafeMatcher;
8
import static org.hamcrest.core.IsEqual.equalTo;
11
import java.util.Map.Entry;
13
public class IsMapContaining<K,V> extends TypeSafeMatcher<Map<? extends K, ? extends V>> {
14
private final Matcher<? super K> keyMatcher;
15
private final Matcher<? super V> valueMatcher;
17
public IsMapContaining(Matcher<? super K> keyMatcher, Matcher<? super V> valueMatcher) {
18
this.keyMatcher = keyMatcher;
19
this.valueMatcher = valueMatcher;
23
public boolean matchesSafely(Map<? extends K, ? extends V> map) {
24
for (Entry<? extends K, ? extends V> entry : map.entrySet()) {
25
if (keyMatcher.matches(entry.getKey()) && valueMatcher.matches(entry.getValue())) {
33
public void describeMismatchSafely(Map<? extends K, ? extends V> map, Description mismatchDescription) {
34
mismatchDescription.appendText("map was ").appendValueList("[", ", ", "]", map.entrySet());
37
public void describeTo(Description description) {
38
description.appendText("map containing [")
39
.appendDescriptionOf(keyMatcher)
41
.appendDescriptionOf(valueMatcher)
46
public static <K,V> Matcher<Map<? extends K,? extends V>> hasEntry(Matcher<? super K> keyMatcher, Matcher<? super V> valueMatcher) {
47
return new IsMapContaining<K,V>(keyMatcher, valueMatcher);
51
public static <K,V> Matcher<Map<? extends K,? extends V>> hasEntry(K key, V value) {
52
return IsMapContaining.<K,V>hasEntry(equalTo(key), equalTo(value));