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
Collection
s
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
RecursiveTask
s, 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
null
values. - 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
Spliterator
is optimized for lower temporary memory usage compared to the default Spliterator. It isSpliterator.ORDERED
|Spliterator.SIZED
|Spliterator.SUBSIZED
.
- See Also:
-
Constructor Summary
ConstructorDescriptionCreates 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 TypeMethodDescriptionboolean
void
clear()
clone()
Returns a shallow copy of this collection.void
concat
(ConcatCollection<T> other) Returns the concatenation of this and the other collection.boolean
Returns true, if an internal cycle is detected.boolean
Compares this collection with the given object.int
hashCode()
iterator()
int
size()
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods 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:
iterator
in interfaceCollection<T>
- Specified by:
iterator
in interfaceIterable<T>
- Specified by:
iterator
in classAbstractCollection<T>
-
spliterator
NOTE: While the
iterator
supports a saveremove
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 interfaceCollection<T>
- Specified by:
spliterator
in interfaceIterable<T>
- Throws:
ConcurrentModificationException
- if the collections change during comparison
-
size
public int size()- Specified by:
size
in interfaceCollection<T>
- Specified by:
size
in classAbstractCollection<T>
-
add
- Specified by:
add
in interfaceCollection<T>
- Overrides:
add
in classAbstractCollection<T>
-
clear
public void clear()- Specified by:
clear
in interfaceCollection<T>
- Overrides:
clear
in 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 isconcatenated
twice. 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:
equals
in interfaceCollection<T>
- Overrides:
equals
in 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:
hashCode
in interfaceCollection<T>
- Overrides:
hashCode
in classObject
-
clone
Returns a shallow copy of this collection.- Overrides:
clone
in classObject
- Returns:
- a cloned collection
- Throws:
CloneNotSupportedException
- this class will never throw a CloneNotSupportedException, whereas a specialized class might do so
-