Skip to content

Commit 5c118f1

Browse files
committed
Mapped TSKs can put initial values and the original key can fetch them
1 parent 4c5e147 commit 5c118f1

File tree

5 files changed

+60
-87
lines changed

5 files changed

+60
-87
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
1010
### Changed
1111
- `TypeSafeKey.Simple` now has a default `#apply` implementation
1212

13+
### Fixed
14+
- mapped `TypeSafeKey` instances can be used for initial put in an `HMap`, and the base key can be used to retrieve them
15+
1316
## [3.0.1] - 2018-05-13
1417
### Changed
1518
- `ToMap` accepts an `Iterable` covariant in `Map.Entry`

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.jnape.palatable.lambda.lens.Iso;
77
import com.jnape.palatable.lambda.lens.LensLike;
88

9+
import java.util.Objects;
10+
911
/**
1012
* An interface representing a parametrized key for use in {@link HMap}s. Additionally, every {@link TypeSafeKey} is an
1113
* {@link Iso} from the type the value is stored as to the type it's viewed and set as (on the way in / on the way out).
@@ -87,6 +89,10 @@ public boolean equals(Object obj) {
8789
*/
8890
static <A> Simple<A> typeSafeKey() {
8991
return new TypeSafeKey.Simple<A>() {
92+
@Override
93+
public boolean equals(Object obj) {
94+
return obj instanceof Simple ? this == obj : Objects.equals(obj, this);
95+
}
9096
};
9197
}
9298

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

Lines changed: 0 additions & 87 deletions
This file was deleted.

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Test;
44

5+
import java.math.BigInteger;
56
import java.util.HashMap;
67
import java.util.NoSuchElementException;
78

@@ -12,6 +13,8 @@
1213
import static com.jnape.palatable.lambda.adt.hmap.HMap.hMap;
1314
import static com.jnape.palatable.lambda.adt.hmap.HMap.singletonHMap;
1415
import static com.jnape.palatable.lambda.adt.hmap.TypeSafeKey.typeSafeKey;
16+
import static com.jnape.palatable.lambda.lens.Iso.simpleIso;
17+
import static java.math.BigInteger.ONE;
1518
import static org.junit.Assert.assertEquals;
1619
import static org.junit.Assert.assertFalse;
1720
import static org.junit.Assert.assertNotEquals;
@@ -36,6 +39,28 @@ public void getForAbsentKey() {
3639
.get(typeSafeKey()));
3740
}
3841

42+
@Test
43+
public void storesTypeSafeKeyBaseValue() {
44+
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));
47+
48+
HMap hMap = singletonHMap(stringKey, "1");
49+
assertEquals(just("1"), hMap.get(stringKey));
50+
assertEquals(just(1L), hMap.get(longKey));
51+
assertEquals(just(ONE), hMap.get(bigIntegerKey));
52+
53+
assertNotEquals(typeSafeKey(), typeSafeKey());
54+
55+
assertEquals(emptyHMap().put(longKey, 1L).get(longKey), emptyHMap().put(stringKey, "1").get(longKey));
56+
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));
58+
59+
assertEquals(singletonHMap(stringKey, "1"), singletonHMap(longKey, 1L));
60+
assertEquals(singletonHMap(stringKey, "1"), singletonHMap(bigIntegerKey, ONE));
61+
assertEquals(singletonHMap(longKey, 1L), singletonHMap(bigIntegerKey, ONE));
62+
}
63+
3964
@Test
4065
public void getForPresentKeyWithNullValue() {
4166
TypeSafeKey<String, String> stringKey = typeSafeKey();

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static com.jnape.palatable.lambda.lens.Iso.simpleIso;
1010
import static java.util.Arrays.asList;
1111
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertNotEquals;
1213
import static testsupport.assertion.LensAssert.assertLensLawfulness;
1314

1415

@@ -45,4 +46,29 @@ public void discardRPreservesTypeSafeKey() {
4546

4647
assertEquals(just("123"), map.get(discardedKey));
4748
}
49+
50+
@Test
51+
public void defaultEquality() {
52+
TypeSafeKey.Simple<Object> keyA = typeSafeKey();
53+
TypeSafeKey<Object, Object> mappedKeyA = keyA.andThen(simpleIso(id(), id()));
54+
55+
assertEquals(keyA, keyA);
56+
assertEquals(keyA, mappedKeyA);
57+
assertEquals(mappedKeyA, keyA);
58+
assertEquals(keyA.hashCode(), mappedKeyA.hashCode());
59+
60+
TypeSafeKey.Simple<Object> keyB = typeSafeKey();
61+
assertNotEquals(keyA, keyB);
62+
assertNotEquals(keyB, keyA);
63+
assertNotEquals(keyB, mappedKeyA);
64+
assertNotEquals(mappedKeyA, keyB);
65+
66+
TypeSafeKey<Object, Object> differentMappedKeyA = keyA.andThen(simpleIso(id(), id()));
67+
assertEquals(keyA, differentMappedKeyA);
68+
assertEquals(differentMappedKeyA, keyA);
69+
assertEquals(mappedKeyA, differentMappedKeyA);
70+
assertEquals(differentMappedKeyA, mappedKeyA);
71+
assertEquals(keyA.hashCode(), differentMappedKeyA.hashCode());
72+
assertEquals(mappedKeyA.hashCode(), differentMappedKeyA.hashCode());
73+
}
4874
}

0 commit comments

Comments
 (0)