Skip to content

Commit 6991040

Browse files
committed
adding HMap#toMap
1 parent 42d8c97 commit 6991040

File tree

2 files changed

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

2 files changed

+23
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ public Iterable<Object> values() {
125125
return map(Tuple2::_2, this);
126126
}
127127

128+
/**
129+
* Return a standard {@link Map} view of the current snapshot of this {@link HMap}. Note that updates to either the
130+
* {@link Map} view or to the original {@link HMap} do not propagate to the other.
131+
*
132+
* @return the map view
133+
*/
134+
public Map<TypeSafeKey, Object> toMap() {
135+
return new HashMap<>(table);
136+
}
137+
128138
@Override
129139
public Iterator<Tuple2<TypeSafeKey, Object>> iterator() {
130140
return map(Tuple2::fromEntry, table.entrySet()).iterator();

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

Lines changed: 13 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.util.HashMap;
56
import java.util.NoSuchElementException;
67
import java.util.Optional;
78

@@ -128,6 +129,18 @@ public void demandForAbsentKey() {
128129
emptyHMap().demand(typeSafeKey());
129130
}
130131

132+
@Test
133+
public void toMap() {
134+
TypeSafeKey<String> stringKey = typeSafeKey();
135+
TypeSafeKey<Integer> intKey = typeSafeKey();
136+
137+
assertEquals(new HashMap<TypeSafeKey, Object>() {{
138+
put(stringKey, "string");
139+
put(intKey, 1);
140+
}}, hMap(stringKey, "string",
141+
intKey, 1).toMap());
142+
}
143+
131144
@Test
132145
public void iteratesKVPairsAsTuples() {
133146
TypeSafeKey<String> stringKey = typeSafeKey();

0 commit comments

Comments
 (0)