Package de.bernd_michaely.common.util
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>
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:
- It provides
O(1)time concatenation. - It supports
nullvalues. - It is ordered (e.g.
iterator()returns the elements in the order they were added). - It is not synchronized.
- It is
Cloneable. - The iterator supports the
Iterator.remove()operation inO(1)time. - The
Spliteratoris optimized for lower temporary memory usage compared to the default Spliterator. It isSpliterator.ORDERED|Spliterator.SIZED|Spliterator.SUBSIZED.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new empty ConcatCollection instance.ConcatCollection(Collection<? extends T> collection) Creates a new ConcatCollection instance and adds all elements of the given collection. -
Method Summary
Modifier and TypeMethodDescriptionbooleanvoidclear()clone()Returns a shallow copy of this collection.voidconcat(ConcatCollection<T> other) Returns the concatenation of this and the other collection.booleanReturns true, if an internal cycle is detected.booleanCompares this collection with the given object.inthashCode()iterator()intsize()Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Constructor Details
-
ConcatCollection
public ConcatCollection()Creates a new empty ConcatCollection instance. -
ConcatCollection
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
- Specified by:
iteratorin interfaceCollection<T>- Specified by:
iteratorin interfaceIterable<T>- Specified by:
iteratorin classAbstractCollection<T>
-
spliterator
NOTE: While the
iteratorsupports a saveremoveoperation, 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:
spliteratorin interfaceCollection<T>- Specified by:
spliteratorin interfaceIterable<T>- Throws:
ConcurrentModificationException- if the collections change during comparison
-
size
public int size()- Specified by:
sizein interfaceCollection<T>- Specified by:
sizein classAbstractCollection<T>
-
add
- Specified by:
addin interfaceCollection<T>- Overrides:
addin classAbstractCollection<T>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<T>- Overrides:
clearin classAbstractCollection<T>
-
concat
Returns the concatenation of this and the other collection. The concatenation is performed inO(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 isconcatenatedtwice. This method is primarily for testing and debugging purposes.- Returns:
- true, if an internal cycle is detected
- See Also:
-
equals
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:
equalsin interfaceCollection<T>- Overrides:
equalsin classObject- 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:
hashCodein interfaceCollection<T>- Overrides:
hashCodein classObject
-
clone
Returns a shallow copy of this collection.- Overrides:
clonein classObject- Returns:
- a cloned collection
- Throws:
CloneNotSupportedException- this class will never throw a CloneNotSupportedException, whereas a specialized class might do so
-