Class ListLens
- java.lang.Object
-
- com.jnape.palatable.lambda.optics.lenses.ListLens
-
public final class ListLens extends java.lang.Object
Lenses that operate onList
s.
-
-
Constructor Summary
Constructors Modifier Constructor Description private
ListLens()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <X> Lens.Simple<java.util.List<X>,java.util.List<X>>
asCopy()
Convenience static factory method for creating a lens over a copy of a list.static <X> Lens.Simple<java.util.List<X>,Maybe<X>>
elementAt(int index)
Convenience static factory method for creating a lens that focuses on an element in a list at a particular index.static <X> Lens.Simple<java.util.List<X>,X>
elementAt(int index, X defaultValue)
Convenience static factory method for creating a lens that focuses on an element in a list at a particular index, returningdefaultValue
if there is no value at that index.
-
-
-
Method Detail
-
asCopy
public static <X> Lens.Simple<java.util.List<X>,java.util.List<X>> asCopy()
Convenience static factory method for creating a lens over a copy of a list. Useful for composition to avoid mutating a list reference.- Type Parameters:
X
- the list element type- Returns:
- a lens that focuses on copies of lists
-
elementAt
public static <X> Lens.Simple<java.util.List<X>,Maybe<X>> elementAt(int index)
Convenience static factory method for creating a lens that focuses on an element in a list at a particular index. Wraps result in aMaybe
to handle null values or indexes that fall outside of list boundaries.- Type Parameters:
X
- the list element type- Parameters:
index
- the index to focus on- Returns:
- Maybe the element at the index
-
elementAt
public static <X> Lens.Simple<java.util.List<X>,X> elementAt(int index, X defaultValue)
Convenience static factory method for creating a lens that focuses on an element in a list at a particular index, returningdefaultValue
if there is no value at that index.Note that this lens is NOT lawful, since "putting back what you got changes nothing" fails for any value
B
whereS
is the empty list- Type Parameters:
X
- the list element type- Parameters:
index
- the index to focus ondefaultValue
- the value to use if there is no element at index- Returns:
- the element at the index, or defaultValue
-
-