Class HMap
- java.lang.Object
-
- com.jnape.palatable.lambda.adt.hmap.HMap
-
- All Implemented Interfaces:
java.lang.Iterable<Tuple2<TypeSafeKey<?,?>,java.lang.Object>>
public final class HMap extends java.lang.Object implements java.lang.Iterable<Tuple2<TypeSafeKey<?,?>,java.lang.Object>>
An immutable heterogeneous mapping from a parametrized type-safe key to any value, supporting a minimal mapping interface.- See Also:
TypeSafeKey
,HList
-
-
Field Summary
Fields Modifier and Type Field Description private static HMap
EMPTY
private java.util.Map<TypeSafeKey<?,?>,java.lang.Object>
table
-
Constructor Summary
Constructors Modifier Constructor Description private
HMap(java.util.Map<TypeSafeKey<?,?>,java.lang.Object> table)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private HMap
alter(java.util.function.Consumer<java.util.Map<TypeSafeKey<?,?>,java.lang.Object>> alterFn)
boolean
containsKey(TypeSafeKey<?,?> key)
Determine if a key is mapped.<V> V
demand(TypeSafeKey<?,V> key)
Retrieve the value at this key, throwing aNoSuchElementException
if this key is unmapped.static HMap
emptyHMap()
Static factory method for creating an empty HMap.boolean
equals(java.lang.Object other)
<A,B>
Maybe<B>get(TypeSafeKey<A,B> key)
Retrieve the value at this key.int
hashCode()
static <V1,V2>
HMaphMap(TypeSafeKey<?,V1> key1, V1 value1, TypeSafeKey<?,V2> key2, V2 value2)
Static factory method for creating an HMap from two given associations.static <V1,V2,V3>
HMaphMap(TypeSafeKey<?,V1> key1, V1 value1, TypeSafeKey<?,V2> key2, V2 value2, TypeSafeKey<?,V3> key3, V3 value3)
Static factory method for creating an HMap from three given associations.static <V1,V2,V3,V4>
HMaphMap(TypeSafeKey<?,V1> key1, V1 value1, TypeSafeKey<?,V2> key2, V2 value2, TypeSafeKey<?,V3> key3, V3 value3, TypeSafeKey<?,V4> key4, V4 value4)
Static factory method for creating an HMap from four given associations.static <V1,V2,V3,V4,V5>
HMaphMap(TypeSafeKey<?,V1> key1, V1 value1, TypeSafeKey<?,V2> key2, V2 value2, TypeSafeKey<?,V3> key3, V3 value3, TypeSafeKey<?,V4> key4, V4 value4, TypeSafeKey<?,V5> key5, V5 value5)
Static factory method for creating an HMap from five given associations.static <V1,V2,V3,V4,V5,V6>
HMaphMap(TypeSafeKey<?,V1> key1, V1 value1, TypeSafeKey<?,V2> key2, V2 value2, TypeSafeKey<?,V3> key3, V3 value3, TypeSafeKey<?,V4> key4, V4 value4, TypeSafeKey<?,V5> key5, V5 value5, TypeSafeKey<?,V6> key6, V6 value6)
Static factory method for creating an HMap from six given associations.static <V1,V2,V3,V4,V5,V6,V7>
HMaphMap(TypeSafeKey<?,V1> key1, V1 value1, TypeSafeKey<?,V2> key2, V2 value2, TypeSafeKey<?,V3> key3, V3 value3, TypeSafeKey<?,V4> key4, V4 value4, TypeSafeKey<?,V5> key5, V5 value5, TypeSafeKey<?,V6> key6, V6 value6, TypeSafeKey<?,V7> key7, V7 value7)
Static factory method for creating an HMap from seven given associations.static <V1,V2,V3,V4,V5,V6,V7,V8>
HMaphMap(TypeSafeKey<?,V1> key1, V1 value1, TypeSafeKey<?,V2> key2, V2 value2, TypeSafeKey<?,V3> key3, V3 value3, TypeSafeKey<?,V4> key4, V4 value4, TypeSafeKey<?,V5> key5, V5 value5, TypeSafeKey<?,V6> key6, V6 value6, TypeSafeKey<?,V7> key7, V7 value7, TypeSafeKey<?,V8> key8, V8 value8)
Static factory method for creating an HMap from eight given associations.boolean
isEmpty()
Test whether thisHMap
is empty.java.util.Iterator<Tuple2<TypeSafeKey<?,?>,java.lang.Object>>
iterator()
java.util.Set<TypeSafeKey<?,?>>
keys()
Retrieve all the mapped keys.<V> HMap
put(TypeSafeKey<?,V> key, V value)
Store a value for the given key.HMap
putAll(HMap hMap)
Store all the key/value mappings inhMap
in this HMap.HMap
remove(TypeSafeKey<?,?> key)
Remove a mapping from this HMap.HMap
removeAll(HMap hMap)
Remove all the key/value mappings inhMap
from this HMap.static <V> HMap
singletonHMap(TypeSafeKey<?,V> key, V value)
Static factory method for creating a singleton HMap.java.util.Map<TypeSafeKey<?,?>,java.lang.Object>
toMap()
Return a standardMap
view of the current snapshot of thisHMap
.java.lang.String
toString()
java.util.Collection<java.lang.Object>
values()
Retrieve all the mapped values.
-
-
-
Field Detail
-
EMPTY
private static final HMap EMPTY
-
table
private final java.util.Map<TypeSafeKey<?,?>,java.lang.Object> table
-
-
Constructor Detail
-
HMap
private HMap(java.util.Map<TypeSafeKey<?,?>,java.lang.Object> table)
-
-
Method Detail
-
get
public <A,B> Maybe<B> get(TypeSafeKey<A,B> key)
Retrieve the value at this key.- Type Parameters:
A
- the value typeB
- the value type- Parameters:
key
- the key- Returns:
- Maybe the value at this key
-
demand
public <V> V demand(TypeSafeKey<?,V> key) throws java.util.NoSuchElementException
Retrieve the value at this key, throwing aNoSuchElementException
if this key is unmapped.- Type Parameters:
V
- the value type- Parameters:
key
- the key- Returns:
- the value at this key
- Throws:
java.util.NoSuchElementException
- if the key is unmapped
-
put
public <V> HMap put(TypeSafeKey<?,V> key, V value)
Store a value for the given key.- Type Parameters:
V
- the value type- Parameters:
key
- the keyvalue
- the value- Returns:
- the updated HMap
-
putAll
public HMap putAll(HMap hMap)
Store all the key/value mappings inhMap
in this HMap.- Parameters:
hMap
- the other HMap- Returns:
- the updated HMap
-
containsKey
public boolean containsKey(TypeSafeKey<?,?> key)
Determine if a key is mapped.- Parameters:
key
- the key- Returns:
- true if the key is mapped; false otherwise
-
remove
public HMap remove(TypeSafeKey<?,?> key)
Remove a mapping from this HMap.- Parameters:
key
- the key- Returns:
- the updated HMap
-
removeAll
public HMap removeAll(HMap hMap)
Remove all the key/value mappings inhMap
from this HMap.- Parameters:
hMap
- the other HMap- Returns:
- the updated HMap
-
isEmpty
public boolean isEmpty()
Test whether thisHMap
is empty.- Returns:
- true if the
HMap
is empty; false otherwise.
-
keys
public java.util.Set<TypeSafeKey<?,?>> keys()
Retrieve all the mapped keys.Note that unlike with
Map.keySet()
, the resulting key set is not "live"; in fact that is, alterations to the resulting key set have no effect on the backingHMap
.- Returns:
- a
Set
of all the mapped keys
-
values
public java.util.Collection<java.lang.Object> values()
Retrieve all the mapped values.- Returns:
- a
List
of all the mapped values
-
toMap
public java.util.Map<TypeSafeKey<?,?>,java.lang.Object> toMap()
Return a standardMap
view of the current snapshot of thisHMap
. Note that updates to either theMap
view or to the originalHMap
do not propagate to the other.- Returns:
- the map view
-
iterator
public java.util.Iterator<Tuple2<TypeSafeKey<?,?>,java.lang.Object>> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<Tuple2<TypeSafeKey<?,?>,java.lang.Object>>
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
alter
private HMap alter(java.util.function.Consumer<java.util.Map<TypeSafeKey<?,?>,java.lang.Object>> alterFn)
-
emptyHMap
public static HMap emptyHMap()
Static factory method for creating an empty HMap.- Returns:
- an empty HMap
-
singletonHMap
public static <V> HMap singletonHMap(TypeSafeKey<?,V> key, V value)
Static factory method for creating a singleton HMap.- Type Parameters:
V
- the only mapped value type- Parameters:
key
- the only mapped keyvalue
- the only mapped value- Returns:
- a singleton HMap
-
hMap
public static <V1,V2> HMap hMap(TypeSafeKey<?,V1> key1, V1 value1, TypeSafeKey<?,V2> key2, V2 value2)
Static factory method for creating an HMap from two given associations.- Type Parameters:
V1
- value1's typeV2
- value2's type- Parameters:
key1
- the first mapped keyvalue1
- the value mapped at key1key2
- the second mapped keyvalue2
- the value mapped at key2- Returns:
- an HMap with the given associations
-
hMap
public static <V1,V2,V3> HMap hMap(TypeSafeKey<?,V1> key1, V1 value1, TypeSafeKey<?,V2> key2, V2 value2, TypeSafeKey<?,V3> key3, V3 value3)
Static factory method for creating an HMap from three given associations.- Type Parameters:
V1
- value1's typeV2
- value2's typeV3
- value3's type- Parameters:
key1
- the first mapped keyvalue1
- the value mapped at key1key2
- the second mapped keyvalue2
- the value mapped at key2key3
- the third mapped keyvalue3
- the value mapped at key3- Returns:
- an HMap with the given associations
-
hMap
public static <V1,V2,V3,V4> HMap hMap(TypeSafeKey<?,V1> key1, V1 value1, TypeSafeKey<?,V2> key2, V2 value2, TypeSafeKey<?,V3> key3, V3 value3, TypeSafeKey<?,V4> key4, V4 value4)
Static factory method for creating an HMap from four given associations.- Type Parameters:
V1
- value1's typeV2
- value2's typeV3
- value3's typeV4
- value4's type- Parameters:
key1
- the first mapped keyvalue1
- the value mapped at key1key2
- the second mapped keyvalue2
- the value mapped at key2key3
- the third mapped keyvalue3
- the value mapped at key3key4
- the fourth mapped keyvalue4
- the value mapped at key4- Returns:
- an HMap with the given associations
-
hMap
public static <V1,V2,V3,V4,V5> HMap hMap(TypeSafeKey<?,V1> key1, V1 value1, TypeSafeKey<?,V2> key2, V2 value2, TypeSafeKey<?,V3> key3, V3 value3, TypeSafeKey<?,V4> key4, V4 value4, TypeSafeKey<?,V5> key5, V5 value5)
Static factory method for creating an HMap from five given associations.- Type Parameters:
V1
- value1's typeV2
- value2's typeV3
- value3's typeV4
- value4's typeV5
- value5's type- Parameters:
key1
- the first mapped keyvalue1
- the value mapped at key1key2
- the second mapped keyvalue2
- the value mapped at key2key3
- the third mapped keyvalue3
- the value mapped at key3key4
- the fourth mapped keyvalue4
- the value mapped at key4key5
- the fifth mapped keyvalue5
- the value mapped at key5- Returns:
- an HMap with the given associations
-
hMap
public static <V1,V2,V3,V4,V5,V6> HMap hMap(TypeSafeKey<?,V1> key1, V1 value1, TypeSafeKey<?,V2> key2, V2 value2, TypeSafeKey<?,V3> key3, V3 value3, TypeSafeKey<?,V4> key4, V4 value4, TypeSafeKey<?,V5> key5, V5 value5, TypeSafeKey<?,V6> key6, V6 value6)
Static factory method for creating an HMap from six given associations.- Type Parameters:
V1
- value1's typeV2
- value2's typeV3
- value3's typeV4
- value4's typeV5
- value5's typeV6
- value6's type- Parameters:
key1
- the first mapped keyvalue1
- the value mapped at key1key2
- the second mapped keyvalue2
- the value mapped at key2key3
- the third mapped keyvalue3
- the value mapped at key3key4
- the fourth mapped keyvalue4
- the value mapped at key4key5
- the fifth mapped keyvalue5
- the value mapped at key5key6
- the sixth mapped keyvalue6
- the value mapped at key6- Returns:
- an HMap with the given associations
-
hMap
public static <V1,V2,V3,V4,V5,V6,V7> HMap hMap(TypeSafeKey<?,V1> key1, V1 value1, TypeSafeKey<?,V2> key2, V2 value2, TypeSafeKey<?,V3> key3, V3 value3, TypeSafeKey<?,V4> key4, V4 value4, TypeSafeKey<?,V5> key5, V5 value5, TypeSafeKey<?,V6> key6, V6 value6, TypeSafeKey<?,V7> key7, V7 value7)
Static factory method for creating an HMap from seven given associations.- Type Parameters:
V1
- value1's typeV2
- value2's typeV3
- value3's typeV4
- value4's typeV5
- value5's typeV6
- value6's typeV7
- value7's type- Parameters:
key1
- the first mapped keyvalue1
- the value mapped at key1key2
- the second mapped keyvalue2
- the value mapped at key2key3
- the third mapped keyvalue3
- the value mapped at key3key4
- the fourth mapped keyvalue4
- the value mapped at key4key5
- the fifth mapped keyvalue5
- the value mapped at key5key6
- the sixth mapped keyvalue6
- the value mapped at key6key7
- the seventh mapped keyvalue7
- the value mapped at key7- Returns:
- an HMap with the given associations
-
hMap
public static <V1,V2,V3,V4,V5,V6,V7,V8> HMap hMap(TypeSafeKey<?,V1> key1, V1 value1, TypeSafeKey<?,V2> key2, V2 value2, TypeSafeKey<?,V3> key3, V3 value3, TypeSafeKey<?,V4> key4, V4 value4, TypeSafeKey<?,V5> key5, V5 value5, TypeSafeKey<?,V6> key6, V6 value6, TypeSafeKey<?,V7> key7, V7 value7, TypeSafeKey<?,V8> key8, V8 value8)
Static factory method for creating an HMap from eight given associations.- Type Parameters:
V1
- value1's typeV2
- value2's typeV3
- value3's typeV4
- value4's typeV5
- value5's typeV6
- value6's typeV7
- value7's typeV8
- value8's type- Parameters:
key1
- the first mapped keyvalue1
- the value mapped at key1key2
- the second mapped keyvalue2
- the value mapped at key2key3
- the third mapped keyvalue3
- the value mapped at key3key4
- the fourth mapped keyvalue4
- the value mapped at key4key5
- the fifth mapped keyvalue5
- the value mapped at key5key6
- the sixth mapped keyvalue6
- the value mapped at key6key7
- the seventh mapped keyvalue7
- the value mapped at key7key8
- the eighth mapped keyvalue8
- the value mapped at key8- Returns:
- an HMap with the given associations
-
-