Skip to content

Commit 755e9a4

Browse files
committed
Factory methods. Map hasKey/Value
1 parent 28aeeb9 commit 755e9a4

File tree

4 files changed

+7
-62
lines changed

4 files changed

+7
-62
lines changed

hamcrest-library/src/main/java/org/hamcrest/collection/IsMapContaining.java

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
import java.util.Map;
88
import java.util.Map.Entry;
99

10-
import static org.hamcrest.core.IsAnything.anything;
11-
import static org.hamcrest.core.IsEqual.equalTo;
12-
1310
public class IsMapContaining<K,V> extends TypeSafeMatcher<Map<? extends K, ? extends V>> {
1411
private final Matcher<? super K> keyMatcher;
1512
private final Matcher<? super V> valueMatcher;
@@ -43,55 +40,4 @@ public void describeTo(Description description) {
4340
.appendText("]");
4441
}
4542

46-
/**
47-
* Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains
48-
* at least one key that satisfies the specified matcher.
49-
* For example:
50-
* <pre>assertThat(myMap, hasKey(equalTo("bar")))</pre>
51-
*
52-
* @param keyMatcher
53-
* the matcher that must be satisfied by at least one key
54-
*/
55-
public static <K> Matcher<Map<? extends K, ?>> hasKey(Matcher<? super K> keyMatcher) {
56-
return new IsMapContaining<>(keyMatcher, anything());
57-
}
58-
59-
/**
60-
* Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains
61-
* at least one key that is equal to the specified key.
62-
* For example:
63-
* <pre>assertThat(myMap, hasKey("bar"))</pre>
64-
*
65-
* @param key
66-
* the key that satisfying maps must contain
67-
*/
68-
public static <K> Matcher<Map<? extends K, ?>> hasKey(K key) {
69-
return new IsMapContaining<>(equalTo(key), anything());
70-
}
71-
72-
/**
73-
* Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains
74-
* at least one value that satisfies the specified valueMatcher.
75-
* For example:
76-
* <pre>assertThat(myMap, hasValue(equalTo("foo")))</pre>
77-
*
78-
* @param valueMatcher
79-
* the matcher that must be satisfied by at least one value
80-
*/
81-
public static <V> Matcher<Map<?, ? extends V>> hasValue(Matcher<? super V> valueMatcher) {
82-
return new IsMapContaining<>(anything(), valueMatcher);
83-
}
84-
85-
/**
86-
* Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains
87-
* at least one value that is equal to the specified value.
88-
* For example:
89-
* <pre>assertThat(myMap, hasValue("foo"))</pre>
90-
*
91-
* @param value
92-
* the value that satisfying maps must contain
93-
*/
94-
public static <V> Matcher<Map<?, ? extends V>> hasValue(V value) {
95-
return new IsMapContaining<>(anything(), equalTo(value));
96-
}
9743
}

hamcrest-library/src/main/java/org/hamcrest/collection/MatchingMaps.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.Map;
66

7+
import static org.hamcrest.core.IsAnything.anything;
78
import static org.hamcrest.core.IsEqual.equalTo;
89

910
public class MatchingMaps {
@@ -78,7 +79,7 @@ public class MatchingMaps {
7879
* @param keyMatcher the matcher that must be satisfied by at least one key
7980
*/
8081
public static <K> org.hamcrest.Matcher<Map<? extends K, ?>> hasKey(Matcher<? super K> keyMatcher) {
81-
return IsMapContaining.hasKey(keyMatcher);
82+
return new IsMapContaining<>(keyMatcher, anything());
8283
}
8384

8485
/**
@@ -89,9 +90,7 @@ public class MatchingMaps {
8990
*
9091
* @param key the key that satisfying maps must contain
9192
*/
92-
public static <K> org.hamcrest.Matcher<Map<? extends K, ?>> hasKey(K key) {
93-
return IsMapContaining.hasKey(key);
94-
}
93+
public static <K> org.hamcrest.Matcher<Map<? extends K, ?>> hasKey(K key) { return hasKey(equalTo(key)); }
9594

9695
/**
9796
* Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains
@@ -102,7 +101,7 @@ public class MatchingMaps {
102101
* @param valueMatcher the matcher that must be satisfied by at least one value
103102
*/
104103
public static <V> org.hamcrest.Matcher<Map<?, ? extends V>> hasValue(Matcher<? super V> valueMatcher) {
105-
return IsMapContaining.hasValue(valueMatcher);
104+
return new IsMapContaining<>(anything(), valueMatcher);
106105
}
107106

108107
/**
@@ -114,7 +113,7 @@ public class MatchingMaps {
114113
* @param value the value that satisfying maps must contain
115114
*/
116115
public static <V> org.hamcrest.Matcher<Map<?, ? extends V>> hasValue(V value) {
117-
return IsMapContaining.hasValue(value);
116+
return hasValue(equalTo(value));
118117
}
119118

120119
}

hamcrest-library/src/test/java/org/hamcrest/collection/IsMapContainingKeyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.TreeMap;
99

1010
import static org.hamcrest.MatcherAssert.assertThat;
11-
import static org.hamcrest.collection.IsMapContaining.hasKey;
11+
import static org.hamcrest.collection.MatchingMaps.hasKey;
1212

1313
public class IsMapContainingKeyTest extends AbstractMatcherTest {
1414

hamcrest-library/src/test/java/org/hamcrest/collection/IsMapContainingValueTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.Map;
99
import java.util.TreeMap;
1010

11-
import static org.hamcrest.collection.IsMapContaining.hasValue;
11+
import static org.hamcrest.collection.MatchingMaps.hasValue;
1212

1313
public class IsMapContainingValueTest extends AbstractMatcherTest {
1414

0 commit comments

Comments
 (0)