diff options
Diffstat (limited to 'hamcrest-library/src/main/java/org/hamcrest/collection/IsMapContaining.java')
-rw-r--r-- | hamcrest-library/src/main/java/org/hamcrest/collection/IsMapContaining.java | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/hamcrest-library/src/main/java/org/hamcrest/collection/IsMapContaining.java b/hamcrest-library/src/main/java/org/hamcrest/collection/IsMapContaining.java new file mode 100644 index 0000000..74572dd --- /dev/null +++ b/hamcrest-library/src/main/java/org/hamcrest/collection/IsMapContaining.java @@ -0,0 +1,70 @@ +package org.hamcrest.collection; + +import org.hamcrest.Description; +import org.hamcrest.Factory; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; +import org.hamcrest.core.IsAnything; + +import static org.hamcrest.core.IsEqual.equalTo; + +import java.util.Map; +import java.util.Map.Entry; + +public class IsMapContaining<K,V> extends TypeSafeMatcher<Map<K, V>> { + + private final Matcher<K> keyMatcher; + private final Matcher<V> valueMatcher; + + public IsMapContaining(Matcher<K> keyMatcher, Matcher<V> valueMatcher) { + this.keyMatcher = keyMatcher; + this.valueMatcher = valueMatcher; + } + + public boolean matchesSafely(Map<K, V> map) { + for (Entry<K, V> entry : map.entrySet()) { + if (keyMatcher.matches(entry.getKey()) && valueMatcher.matches(entry.getValue())) { + return true; + } + } + return false; + } + + public void describeTo(Description description) { + description.appendText("map containing [") + .appendDescriptionOf(keyMatcher) + .appendText("->") + .appendDescriptionOf(valueMatcher) + .appendText("]"); + } + + @Factory + public static <K,V> Matcher<Map<K,V>> hasEntry(Matcher<K> keyMatcher, Matcher<V> valueMatcher) { + return new IsMapContaining<K,V>(keyMatcher, valueMatcher); + } + + @Factory + public static <K,V> Matcher<Map<K,V>> hasEntry(K key, V value) { + return hasEntry(equalTo(key), equalTo(value)); + } + + @Factory + public static <K,V> Matcher<Map<K,V>> hasKey(Matcher<K> keyMatcher) { + return hasEntry(keyMatcher, IsAnything.<V>anything()); + } + + @Factory + public static <K,V> Matcher<Map<K,V>> hasKey(K key) { + return hasKey(equalTo(key)); + } + + @Factory + public static <K,V> Matcher<Map<K,V>> hasValue(Matcher<V> valueMatcher) { + return hasEntry(IsAnything.<K>anything(), valueMatcher); + } + + @Factory + public static <K,V> Matcher<Map<K,V>> hasValue(V value) { + return hasValue(equalTo(value)); + } +} |