Class ConcatCollection<T>

java.lang.Object
java.util.AbstractCollection<T>
de.bernd_michaely.common.util.ConcatCollection<T>
Type Parameters:
T - the type of the collection elements
All Implemented Interfaces:
Serializable, Cloneable, Iterable<T>, Collection<T>

public class ConcatCollection<T> extends AbstractCollection<T> implements Cloneable, Serializable
A lightweight Collection which provides an efficient concatenation for two instances of this type. At the time of this writing Collections provide concatenation only through Collection.addAll(collection) in O(n) time. Also note that Stream.concat(Stream, Stream) discourages repeated concatenation. The Collection is useful e.g. for accumulating partial results in the combine phase of RecursiveTasks, when subtasks return a collection of elements which are to be combined into one flat collection.

The characteristics of this implementation include the following:

See Also:
  • Constructor Details

    • ConcatCollection

      public ConcatCollection()
      Creates a new empty ConcatCollection instance.
    • ConcatCollection

      public ConcatCollection(Collection<? extends T> collection)
      Creates a new ConcatCollection instance and adds all elements of the given collection.
      Parameters:
      collection - if not null, all elements of the collection are added to this ConcatCollection
  • Method Details

    • iterator

      public Iterator<T> iterator()
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in class AbstractCollection<T>
    • spliterator

      public Spliterator<T> spliterator()

      NOTE: While the iterator supports a save remove operation, the Spliterator does not! This collection must not be changed during spliteration.

      Implementation note:

      This Spliterator is more memory efficient than the default Spliterators.spliterator(Collection, int) by avoiding to copy all elements into an array and directly operating on internal node objects.

      Specified by:
      spliterator in interface Collection<T>
      Specified by:
      spliterator in interface Iterable<T>
      Throws:
      ConcurrentModificationException - if the collections change during comparison
    • size

      public int size()
      Specified by:
      size in interface Collection<T>
      Specified by:
      size in class AbstractCollection<T>
    • add

      public boolean add(T newElement)
      Specified by:
      add in interface Collection<T>
      Overrides:
      add in class AbstractCollection<T>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<T>
      Overrides:
      clear in class AbstractCollection<T>
    • concat

      public void concat(ConcatCollection<T> other)
      Returns the concatenation of this and the other collection. The concatenation is performed in O(1) time by direct connection of internal nodes. Note, that changes made to the other collection after the concatenation will be reflected in the resulting collection.

      NOTE: The caller is responsible for not concatenating the same ConcatCollection instance twice, otherwise [spl]iteration will result in endless loops!

      Parameters:
      other - the other collection
      See Also:
    • detectCycle

      public boolean detectCycle()
      Returns true, if an internal cycle is detected. A cycle can occur, if the same ConcatCollection is concatenated twice. This method is primarily for testing and debugging purposes.
      Returns:
      true, if an internal cycle is detected
      See Also:
    • equals

      public boolean equals(Object obj)
      Compares this collection with the given object. Returns true, if the other object is of the same type, contains the same number of elements and the equals method for the contained elements in the iterated order pairwise returns true. Returns false otherwise.
      Specified by:
      equals in interface Collection<T>
      Overrides:
      equals in class Object
      Parameters:
      obj - the other object to compare
      Returns:
      true, if the other object is a ConcatCollection with the same content
      Throws:
      ConcurrentModificationException - if the collections change during comparison
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<T>
      Overrides:
      hashCode in class Object
    • clone

      Returns a shallow copy of this collection.
      Overrides:
      clone in class Object
      Returns:
      a cloned collection
      Throws:
      CloneNotSupportedException - this class will never throw a CloneNotSupportedException, whereas a specialized class might do so