Skip to content

Commit 62495da

Browse files
committed
Adding HMap#isEmpty
1 parent d5e2291 commit 62495da

File tree

2 files changed

+37
-19
lines changed
  • src
    • main/java/com/jnape/palatable/lambda/adt/hmap
    • test/java/com/jnape/palatable/lambda/adt/hmap

2 files changed

+37
-19
lines changed

src/main/java/com/jnape/palatable/lambda/adt/hmap/HMap.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ public HMap removeAll(HMap hMap) {
115115
return alter(t -> t.keySet().removeAll(hMap.table.keySet()));
116116
}
117117

118+
/**
119+
* Test whether this {@link HMap} is empty.
120+
*
121+
* @return true if the {@link HMap} is empty; false otherwise.
122+
*/
123+
public boolean isEmpty() {
124+
return table.isEmpty();
125+
}
126+
118127
/**
119128
* Retrieve all the mapped keys.
120129
* <p>

src/test/java/com/jnape/palatable/lambda/adt/hmap/HMapTest.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,19 @@ public void getForAbsentKey() {
3939
.get(typeSafeKey()));
4040
}
4141

42+
@Test
43+
public void isEmpty() {
44+
assertTrue(emptyHMap().isEmpty());
45+
assertFalse(singletonHMap(typeSafeKey(), "foo").isEmpty());
46+
}
47+
4248
@Test
4349
public void storesTypeSafeKeyBaseValue() {
4450
TypeSafeKey.Simple<String> stringKey = typeSafeKey();
45-
TypeSafeKey<String, Long> longKey = stringKey.andThen(simpleIso(Long::parseLong, String::valueOf));
46-
TypeSafeKey<String, BigInteger> bigIntegerKey = longKey.andThen(simpleIso(BigInteger::valueOf, BigInteger::longValue));
51+
TypeSafeKey<String, Long> longKey = stringKey.andThen(simpleIso(Long::parseLong,
52+
String::valueOf));
53+
TypeSafeKey<String, BigInteger> bigIntegerKey = longKey.andThen(simpleIso(BigInteger::valueOf,
54+
BigInteger::longValue));
4755

4856
HMap hMap = singletonHMap(stringKey, "1");
4957
assertEquals(just("1"), hMap.get(stringKey));
@@ -54,7 +62,8 @@ public void storesTypeSafeKeyBaseValue() {
5462

5563
assertEquals(emptyHMap().put(longKey, 1L).get(longKey), emptyHMap().put(stringKey, "1").get(longKey));
5664
assertEquals(emptyHMap().put(stringKey, "1").get(stringKey), emptyHMap().put(longKey, 1L).get(stringKey));
57-
assertEquals(emptyHMap().put(stringKey, "1").get(stringKey), emptyHMap().put(bigIntegerKey, ONE).get(stringKey));
65+
assertEquals(emptyHMap().put(stringKey, "1").get(stringKey),
66+
emptyHMap().put(bigIntegerKey, ONE).get(stringKey));
5867

5968
assertEquals(singletonHMap(stringKey, "1"), singletonHMap(longKey, 1L));
6069
assertEquals(singletonHMap(stringKey, "1"), singletonHMap(bigIntegerKey, ONE));
@@ -82,9 +91,9 @@ public void put() {
8291

8392
@Test
8493
public void putAll() {
85-
TypeSafeKey<String, String> stringKey1 = typeSafeKey();
86-
TypeSafeKey<String, String> stringKey2 = typeSafeKey();
87-
TypeSafeKey<Integer, Integer> intKey = typeSafeKey();
94+
TypeSafeKey<String, String> stringKey1 = typeSafeKey();
95+
TypeSafeKey<String, String> stringKey2 = typeSafeKey();
96+
TypeSafeKey<Integer, Integer> intKey = typeSafeKey();
8897

8998
HMap left = hMap(stringKey1, "string value",
9099
intKey, 1);
@@ -132,9 +141,9 @@ public void removeAll() {
132141

133142
@Test
134143
public void containsKey() {
135-
TypeSafeKey<String, String> stringKey1 = typeSafeKey();
136-
TypeSafeKey<String, String> stringKey2 = typeSafeKey();
137-
TypeSafeKey<Integer, Integer> intKey = typeSafeKey();
144+
TypeSafeKey<String, String> stringKey1 = typeSafeKey();
145+
TypeSafeKey<String, String> stringKey2 = typeSafeKey();
146+
TypeSafeKey<Integer, Integer> intKey = typeSafeKey();
138147

139148
HMap hMap = singletonHMap(stringKey1, "string");
140149

@@ -158,8 +167,8 @@ public void demandForAbsentKey() {
158167
@Test
159168
@SuppressWarnings("serial")
160169
public void toMap() {
161-
TypeSafeKey<String, String> stringKey = typeSafeKey();
162-
TypeSafeKey<Integer, Integer> intKey = typeSafeKey();
170+
TypeSafeKey<String, String> stringKey = typeSafeKey();
171+
TypeSafeKey<Integer, Integer> intKey = typeSafeKey();
163172

164173
assertEquals(new HashMap<TypeSafeKey<?, ?>, Object>() {{
165174
put(stringKey, "string");
@@ -192,14 +201,14 @@ public void values() {
192201

193202
@Test
194203
public void convenienceStaticFactoryMethods() {
195-
TypeSafeKey.Simple<String> stringKey = typeSafeKey();
196-
TypeSafeKey.Simple<Integer> intKey = typeSafeKey();
197-
TypeSafeKey.Simple<Float> floatKey = typeSafeKey();
198-
TypeSafeKey.Simple<Byte> byteKey = typeSafeKey();
199-
TypeSafeKey.Simple<Short> shortKey = typeSafeKey();
200-
TypeSafeKey.Simple<Long> longKey = typeSafeKey();
201-
TypeSafeKey.Simple<Double> doubleKey = typeSafeKey();
202-
TypeSafeKey.Simple<Character> charKey = typeSafeKey();
204+
TypeSafeKey.Simple<String> stringKey = typeSafeKey();
205+
TypeSafeKey.Simple<Integer> intKey = typeSafeKey();
206+
TypeSafeKey.Simple<Float> floatKey = typeSafeKey();
207+
TypeSafeKey.Simple<Byte> byteKey = typeSafeKey();
208+
TypeSafeKey.Simple<Short> shortKey = typeSafeKey();
209+
TypeSafeKey.Simple<Long> longKey = typeSafeKey();
210+
TypeSafeKey.Simple<Double> doubleKey = typeSafeKey();
211+
TypeSafeKey.Simple<Character> charKey = typeSafeKey();
203212

204213
HMap m1 = emptyHMap().put(stringKey, "string value");
205214
HMap m2 = m1.put(intKey, 1);

0 commit comments

Comments
 (0)