Class ListLens


  • public final class ListLens
    extends java.lang.Object
    Lenses that operate on Lists.
    • 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, returning defaultValue if there is no value at that index.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ListLens

        private ListLens()
    • 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 a Maybe 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, returning defaultValue 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 where S is the empty list

        Type Parameters:
        X - the list element type
        Parameters:
        index - the index to focus on
        defaultValue - the value to use if there is no element at index
        Returns:
        the element at the index, or defaultValue