Class MapLens
java.lang.Object
com.jnape.palatable.lambda.optics.lenses.MapLens
Lenses that operate on
Map
s.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
V> Lens.Simple <Map<K, V>, Map<K, V>> asCopy()
A lens that focuses on a copy of a Map.A lens that focuses on a copy of aMap
as a subtypeM
.static <K,
V> Lens.Simple <Map<K, V>, Map<V, K>> inverted()
A lens that focuses on the inverse of a map (keys and values swapped).static <K,
V> Lens.Simple <Map<K, V>, Set<K>> keys()
A lens that focuses on the keys of a map.static <K,
V, V2> Lens.Simple <Map<K, V>, Map<K, V2>> mappingValues
(Iso<V, V, V2, V2> iso) A lens that focuses on a map while mapping its values with the mappingIso
.A lens that focuses on a value at a key in a map, as aMaybe
, and produces a subtypeM
on the way back out.static <K,
V> Lens.Simple <Map<K, V>, Maybe<V>> valueAt
(K k) A lens that focuses on a value at a key in a map, as aMaybe
.static <K,
V> Lens.Simple <Map<K, V>, V> valueAt
(K k, V defaultValue) A lens that focuses on a value at a key in a map, falling back todefaultV
if the value is missing.static <K,
V> Lens.Simple <Map<K, V>, Collection<V>> values()
A lens that focuses on the values of a map.
-
Constructor Details
-
MapLens
private MapLens()
-
-
Method Details
-
asCopy
public static <M extends Map<K,V>, Lens<Map<K,K, V> V>, asCopyM, M, M> (Fn1<? super Map<K, V>, ? extends M> copyFn) A lens that focuses on a copy of aMap
as a subtypeM
. Useful for composition to avoid mutating a map reference.- Type Parameters:
M
- the map subtypeK
- the key typeV
- the value type- Parameters:
copyFn
- the copy function- Returns:
- a lens that focuses on copies of maps as a specific subtype
-
asCopy
A lens that focuses on a copy of a Map. Useful for composition to avoid mutating a map reference.- Type Parameters:
K
- the key typeV
- the value type- Returns:
- a lens that focuses on copies of maps
-
valueAt
public static <M extends Map<K,V>, Lens<Map<K,K, V> V>, valueAtM, Maybe<V>, Maybe<V>> (Fn1<? super Map<K, V>, ? extends M> copyFn, K k) A lens that focuses on a value at a key in a map, as aMaybe
, and produces a subtypeM
on the way back out.- Type Parameters:
M
- the map subtypeK
- the key typeV
- the value type- Parameters:
copyFn
- the copy functionk
- the key to focus on- Returns:
- a lens that focuses on the value at key, as a
Maybe
-
valueAt
A lens that focuses on a value at a key in a map, as aMaybe
.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
k
- the key to focus on- Returns:
- a lens that focuses on the value at key, as a
Maybe
-
valueAt
A lens that focuses on a value at a key in a map, falling back todefaultV
if the value is missing.Note that this lens is NOT lawful, since "putting back what you got changes nothing" fails for any value
B
whereS
is the empty map- Type Parameters:
K
- the key typeV
- the value type- Parameters:
k
- the key to focus ondefaultValue
- the default value to use in case of a missing value at key- Returns:
- a lens that focuses on the value at the key
-
keys
A lens that focuses on the keys of a map.- Type Parameters:
K
- the key typeV
- the value type- Returns:
- a lens that focuses on the keys of a map
-
values
A lens that focuses on the values of a map. In the case of updating the map, only the entries with a value listed in the update collection of values are kept.Note that this lens is NOT lawful, since "you get back what you put in" fails for all values
B
that represent a non-surjective superset of the existing values inS
.- Type Parameters:
K
- the key typeV
- the value type- Returns:
- a lens that focuses on the values of a map
-
inverted
A lens that focuses on the inverse of a map (keys and values swapped). In the case of multiple equal values becoming keys, the last one wins.Note that this lens is very likely to NOT be lawful, since "you get back what you put in" will fail for any keys that map to the same value.
- Type Parameters:
K
- the key typeV
- the value type- Returns:
- a lens that focuses on the inverse of a map
-
mappingValues
A lens that focuses on a map while mapping its values with the mappingIso
.Note that for this lens to be lawful,
iso
must be lawful.- Type Parameters:
K
- the key typeV
- the unfocused map value typeV2
- the focused map value type- Parameters:
iso
- the mappingIso
- Returns:
- a lens that focuses on a map while mapping its values
-