Class CollectionViews

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

public abstract class CollectionViews extends Object
Library of view collections, which allow a transformed access to a collection.

These features should not be used deeply nested, because the freqency and time of the evaluation of the contained operations soon may appear confusing. When a collection shall be processed element by element, the genuine Java streams framework is often preferable.

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static <A, B, C extends B>
    Map<A,B>
    castValues(Class<? extends C> cls, Map<A,C> things)
    Returns a view on a given collection that modifies the type of each element by casting.
    static <A, B> Collection<B>
    map(Function<? super A,? extends B> fun, Collection<? extends A> things)
    Returns a life view on a given collection that transforms each element by a given function.
    static <A, B, C> Map.Entry<A,C>
    map(Function<? super B,? extends C> fun, Map.Entry<A,B> e)
    Returns a new map entry with an immutable value from the transformed value of the given entry.
    static <A, B, C> Map<A,C>
    map(Function<? super B,? extends C> fun, Map<A,B> things)
    Returns a life view on a given map that transforms for each key the value with a given function.
    static <A, B> Map<A,B>
    toGraph(Function<? super A,? extends B> fun, Set<? extends A> dom)
    Returns a map with a given key set and values computed by a function.
    static <A, B, C> Map<B,C>
    toSpan(Function<? super A,? extends B> left, Function<? super A,? extends C> right, Collection<? extends A> gen)
    Creates a map by generating entries from generating elements using two functions.
    static <A, B, C> Map<B,C>
    toSpan(Function<? super A,? extends B> left, Function<? super A,? extends C> right, BiFunction<? super C,? super C,? extends C> merge, Collection<? extends A> gen)
    Creates a map by generating entries from generating elements using two functions.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • map

      public static <A, B> Collection<B> map(Function<? super A,? extends B> fun, Collection<? extends A> things)
      Returns a life view on a given collection that transforms each element by a given function.

      Changes to the backing collection are reflected by the resulting view. Removes through the iterator of the resulting collection are reflected by the backing collection.

      Type Parameters:
      A - the type of given elements
      B - the type of transformed elements
      Parameters:
      fun - the transformer function
      things - the collection to transform
      Returns:
      an collection that has, for each current element of things, a corresponding element transformed by fun
    • map

      public static <A, B, C> Map<A,C> map(Function<? super B,? extends C> fun, Map<A,B> things)
      Returns a life view on a given map that transforms for each key the value with a given function.

      Changes to the backing collection are reflected by the resulting view. Removes through the iterator of the resulting collection are reflected by the backing collection. Assignments to the values of an entry from the entry set of the resulting collection are not allowed.

      Type Parameters:
      A - the type of keys
      B - the type of given values
      C - the type of transformed values
      Parameters:
      fun - the transformer function
      things - the map to transform
      Returns:
      an unmodifiable map that has, for each current value of things, a corresponding value ad-hoc transformed by fun, for the same key
    • map

      public static <A, B, C> Map.Entry<A,C> map(Function<? super B,? extends C> fun, Map.Entry<A,B> e)
      Returns a new map entry with an immutable value from the transformed value of the given entry.
      Type Parameters:
      A - the type of the key
      B - the type of the given value
      C - the type of the transformed value
      Parameters:
      fun - the transformer function
      e - a map entry
      Returns:
      a map entry that has the same key as e and a value resulting from applying fun to the value of e
    • toSpan

      public static <A, B, C> Map<B,C> toSpan(Function<? super A,? extends B> left, Function<? super A,? extends C> right, Collection<? extends A> gen)
      Creates a map by generating entries from generating elements using two functions.
      Type Parameters:
      A - the type of generating elemens
      B - the type of keys
      C - the type of values
      Parameters:
      left - the key-generating function
      right - the value-generating function
      gen - the generating elements
      Returns:
      an unmodifiable map that contains, for each generating element, the computed entry
      Throws:
      IllegalArgumentException - if two generating elements result in the same key but different values
    • toSpan

      public static <A, B, C> Map<B,C> toSpan(Function<? super A,? extends B> left, Function<? super A,? extends C> right, BiFunction<? super C,? super C,? extends C> merge, Collection<? extends A> gen)
      Creates a map by generating entries from generating elements using two functions.
      Type Parameters:
      A - the type of generating elemens
      B - the type of keys
      C - the type of values
      Parameters:
      left - the key-generating function
      right - the value-generating function
      merge - an operation to reconcile values if two generating elements result in the same key
      gen - the generating elements
      Returns:
      an unmodifiable map that contains, for each generating element, the computed entry
    • toGraph

      public static <A, B> Map<A,B> toGraph(Function<? super A,? extends B> fun, Set<? extends A> dom)
      Returns a map with a given key set and values computed by a function.
      Type Parameters:
      A - the type of keys
      B - the type of values
      Parameters:
      fun - a function to compute values from keys
      dom - the key set
      Returns:
      a new map that maps all keys in dom to values computed with fun
    • castValues

      public static <A, B, C extends B> Map<A,B> castValues(Class<? extends C> cls, Map<A,C> things)
      Returns a view on a given collection that modifies the type of each element by casting.

      Changes to the underlying collection are reflected by the resulting view.

      Type Parameters:
      A - the type of keys
      B - the actual type of elements
      C - the apparent type of elements
      Parameters:
      cls - the class to cast to
      things - the map to transform
      Returns:
      an unmodifiable map that has, for each current value of things, a cast reference of type cls, for the same key