Class NonEmptyMap<K,V>
java.lang.Object
dmx.fun.NonEmptyMap<K,V>
- Type Parameters:
K- the type of keysV- the type of values
An immutable, non-empty map: a map guaranteed at construction time to contain
at least one entry.
This type makes the non-emptiness constraint part of the static type system.
APIs that require at least one entry can declare NonEmptyMap<K, V> instead of
Map<K, V> and eliminate runtime emptiness checks entirely.
Insertion order is preserved (backed by LinkedHashMap). The first entry
inserted is the headKey() / headValue().
This class is @NullMarked: all keys, values, and parameters are non-null
by default.
-
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsKey(K key) Returnstrueif this map contains an entry forkey.booleanOption<NonEmptyMap<K, V>> filter(BiPredicate<? super K, ? super V> predicate) Returns a newNonEmptyMapcontaining only entries for whichpredicatereturnstrue, wrapped inOption.some(Object).static <K,V> Option <NonEmptyMap<K, V>> Attempts to construct aNonEmptyMapfrom a plainMap.Returns the value associated withkey, orOption.none()if absent.inthashCode()headKey()Returns the guaranteed head key of this map.Returns the value associated with the head key.keySet()Returns aNonEmptySetcontaining all keys of this map in insertion order.<R> NonEmptyMap<R, V> Appliesmapperto every key and returns a newNonEmptyMapwith the mapped keys and the original values.<R> NonEmptyMap<K, R> Appliesmapperto every value and returns a newNonEmptyMapwith the same keys and mapped values.merge(NonEmptyMap<K, V> other, BinaryOperator<V> mergeFunction) Returns a newNonEmptyMapthat is the union of this map andother.static <K,V> NonEmptyMap <K, V> Creates aNonEmptyMapwith the given head entry and additional entries.static <K,V> NonEmptyMap <K, V> singleton(K key, V value) Creates aNonEmptyMapcontaining exactly one entry.intsize()Returns the number of entries in this map.toMap()Returns an unmodifiableMapcontaining all entries (head entry first, then tail entries in insertion order).Converts this map to aNonEmptyListof its entries in insertion order.toString()values()Returns aNonEmptyListcontaining all values of this map in insertion order.
-
Method Details
-
of
Creates aNonEmptyMapwith the given head entry and additional entries.If
restcontainskey, that duplicate is ignored — the providedvalueis always used for the head key.- Type Parameters:
K- the key typeV- the value type- Parameters:
key- the head key; must not benullvalue- the head value; must not benullrest- additional entries; must not benull; keys and values must not benull- Returns:
- a new
NonEmptyMap - Throws:
NullPointerException- ifkey,value,rest, or any key/value insiderestisnull
-
singleton
Creates aNonEmptyMapcontaining exactly one entry.- Type Parameters:
K- the key typeV- the value type- Parameters:
key- the sole key; must not benullvalue- the sole value; must not benull- Returns:
- a singleton
NonEmptyMap - Throws:
NullPointerException- ifkeyorvalueisnull
-
fromMap
Attempts to construct aNonEmptyMapfrom a plainMap.- Type Parameters:
K- the key typeV- the value type- Parameters:
map- the source map; must not benull; keys and values must not benull- Returns:
Option.some(Object)wrapping theNonEmptyMapif the map is non-empty, orOption.none()if the map is empty- Throws:
NullPointerException- ifmapor any key/value isnull
-
headKey
-
headValue
Returns the value associated with the head key.- Returns:
- the head value (never
null)
-
size
public int size()Returns the number of entries in this map. Always ≥ 1.- Returns:
- the size
-
get
Returns the value associated withkey, orOption.none()if absent.- Parameters:
key- the key to look up; must not benull- Returns:
Some(value)if the key is present,Noneotherwise- Throws:
NullPointerException- ifkeyisnull
-
containsKey
Returnstrueif this map contains an entry forkey.- Parameters:
key- the key to test; must not benull- Returns:
trueif the key is present- Throws:
NullPointerException- ifkeyisnull
-
keySet
Returns aNonEmptySetcontaining all keys of this map in insertion order. The head key of this map is the head of the returned set.- Returns:
- a
NonEmptySet<K>of all keys (nevernull)
-
values
Returns aNonEmptyListcontaining all values of this map in insertion order. The head value of this map is the head of the returned list. Duplicate values are preserved (unlike keys, values need not be unique).- Returns:
- a
NonEmptyList<V>of all values (nevernull)
-
toMap
-
mapValues
Appliesmapperto every value and returns a newNonEmptyMapwith the same keys and mapped values.- Type Parameters:
R- the result value type- Parameters:
mapper- a non-null function to apply to each value; must not returnnull- Returns:
- a new
NonEmptyMapwith mapped values - Throws:
NullPointerException- ifmapperisnullor returnsnull
-
mapKeys
Appliesmapperto every key and returns a newNonEmptyMapwith the mapped keys and the original values.If multiple keys map to the same new key, head-wins semantics apply: the head entry is always preserved, and any tail entry whose mapped key collides with the mapped head key is silently dropped.
- Type Parameters:
R- the result key type- Parameters:
mapper- a non-null function to apply to each key; must not returnnull- Returns:
- a new
NonEmptyMapwith mapped keys - Throws:
NullPointerException- ifmapperisnullor returnsnull
-
filter
Returns a newNonEmptyMapcontaining only entries for whichpredicatereturnstrue, wrapped inOption.some(Object). ReturnsOption.none()if no entries pass the predicate.- Parameters:
predicate- a non-null predicate to test each key-value pair- Returns:
Some(filteredMap)if at least one entry passes,Noneotherwise- Throws:
NullPointerException- ifpredicateisnull
-
merge
Returns a newNonEmptyMapthat is the union of this map andother. When both maps contain the same key,mergeFunctionis applied to the two values.- Parameters:
other- the other map; must not benullmergeFunction- function to resolve value conflicts; must not benull; must not returnnull(a null return would violate the non-null value contract and is rejected immediately)- Returns:
- a new
NonEmptyMapcontaining all entries from both maps - Throws:
NullPointerException- ifother,mergeFunction, or the result ofmergeFunctionisnull
-
toNonEmptyList
Converts this map to aNonEmptyListof its entries in insertion order.- Returns:
- a
NonEmptyList<Map.Entry<K, V>>
-
equals
-
hashCode
-
toString
-