Class ListIterators

java.lang.Object
eu.bandm.tools.util.java.ListIterators

public abstract class ListIterators extends Object
Library of operations for working with list iterators.
See Also:
  • Method Details

    • map

      public static <A, B> ListIterator<B> map(Function<? super A,? extends B> fun, ListIterator<? extends A> elems)
      Returns a view on a list iterator that has all elements transformed by a given function.

      Changes to the underlying iterator are reflected in the result.

      Calls to remove are passed through to the underlying iterator; add and set are not supported.

      Type Parameters:
      A - the source element type
      B - the target element type
      Parameters:
      fun - a transformer function
      elems - some elements
      Returns:
      a list iterator where each element is computed on the fly by applying fun to the corresponding element of elems
      Throws:
      NullPointerException - if fun or elems is null
    • unmodifiableListIterator

      public static <A> ListIterator<A> unmodifiableListIterator(ListIterator<? extends A> elems)
      Returns an unmodifiable view on a list iterator.

      Changes to the underlying iterator are reflected in the result.

      The operations remove, add and set are not supported.

      Type Parameters:
      A - the element type
      Parameters:
      elems - some elements
      Returns:
      a list iterator that has the same elements as elems but allows no modification
      Throws:
      NullPointerException - if elems is null
    • concat

      public static <A> ListIterator<A> concat(ListIterator<A> i1, ListIterator<A> i2)
      Returns a view on a list iterator that has all elements of two list iterators one after the other.

      The concatenated list iterator starts at the position of the first given list iterator. The second list iterator is rewound to index zero before this method returns.

      Changes to the underlying iterators are reflected in the result.

      Calls to remove and set are passed through to either of the underlying iterators; add is not supported.

      Type Parameters:
      A - the element type
      Parameters:
      i1 - the first list iterator
      i2 - the second list iterator
      Returns:
      a list iterator that traverses all elements of i1 followed by all elements of i2
      Throws:
      NullPointerException - if i1 or i2 is null
    • concat

      public static <A> ListIterator<A> concat(ListIterator<A> i1, ListIterator<A> i2, boolean first)
      Returns a view on a list iterator that has all elements of two list iterators one after the other.

      The concatenated list iterator starts at the position of one of the given list iterators, at the caller's choice. The other list iterator is wound to the appropriate extremal position (the first to maximal index, the second to zero) before this method returns.

      Changes to the underlying iterators are reflected in the result.

      Calls to remove and set are passed through to either of the underlying iterator; add is not supported.

      Type Parameters:
      A - the element type
      Parameters:
      i1 - the first list iterator
      i2 - the second list iterator
      first - true if the resulting list iterator should start at the position of l1, false if it should start at l2
      Returns:
      a list iterator that traverses all elements of i1 followed by all elements of i2
      Throws:
      NullPointerException - if i1 or i2 is null
    • reverse

      public static <A> ListIterator<A> reverse(ListIterator<A> i, IntSupplier size)
      Returns a view on a list iterator that has all elements in reverse order.

      Changes to the underlying iterator are reflected in the result.

      Calls to remove, add and set are passed through to the underlying iterator.

      Dynamic tracking of index positions requires a callback that can determine the current size of the underlying list. Often a method reference mylist::size will do.

      Type Parameters:
      A - the element type
      Parameters:
      i - a list iterator
      size - a callback that returns the current length of the underlying list
      Returns:
      a list iterator where the elements are backwards with respect to i
      Throws:
      NullPointerException - if i or size is null
    • zip

      public static <A, B, C> ListIterator<C> zip(BiFunction<? super A,? super B,? extends C> fun, ListIterator<? extends A> left, ListIterator<? extends B> right)
      Returns a view on two list iterators that has elements combined pairwise by a given function.

      The two underlying iterators are moved in lockstep by next and previous. Whether there are adjacent elements according to hasPrevious and hasNext is determined by first; a NoSuchElementException may be thrown if second has fewer elements on either side.

      Changes to the underlying iterators are reflected in the result.

      Calls to remove are passed through to the underlying iterator; add and set are not supported.

      Type Parameters:
      A - the first source element type
      B - the second source element type
      C - the target element type
      Parameters:
      fun - a combiner function
      left - the first list iterator
      right - the second list iterator
      Returns:
      a list iterator where each element is computed on the fly by applying fun to the corresponding elements of first and second
      Throws:
      NullPointerException - if fun, first or second is null