Class AbstractIterationSet<E>
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,Set<E>
- Direct Known Subclasses:
AbstractNavigableSet
,ConvertedSet
Set
implementations based on database entries.
This class assumes the following:
- The size of the set is not cached, i.e., it requires an actual enumeration of all of the set's elements; and
- Iteration may utilize a resource that needs to be closed
As a result:
AbstractSet
methods that rely onsize()
are overridden with implementations that avoid the use ofsize()
where possible; anditerator()
returns aCloseableIterator
stream()
, which is based oniterator()
, arranges (viaStream.onClose()
) for the iterator to be closed onStream.close()
.
iterator()
and stream()
.
For a read-only implementation, subclasses should implement contains()
and iterator()
.
For a mutable implementation, subclasses should also implement add()
, remove()
,
and clear()
, and make the iterator()
mutable.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected Spliterator<E>
buildSpliterator
(Iterator<E> iterator) Build aSpliterator
appropriate for this set from the given instance iterator.boolean
Overridden inAbstractIterationSet
to minimize the use ofsize()
.int
hashCode()
boolean
isEmpty()
Overridden inAbstractIterationSet
to minimize the use ofsize()
.abstract CloseableIterator<E>
iterator()
int
size()
Calculate size.Overridden inAbstractIterationSet
to avoid the use ofsize()
.stream()
Object[]
toArray()
Overridden inAbstractIterationSet
to minimize the use ofsize()
.<T> T[]
toArray
(T[] array) Overridden inAbstractIterationSet
to minimize the use ofsize()
.Methods inherited from class java.util.AbstractSet
removeAll
Methods inherited from class java.util.AbstractCollection
add, addAll, clear, contains, containsAll, remove, retainAll, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, toArray
-
Constructor Details
-
AbstractIterationSet
protected AbstractIterationSet()
-
-
Method Details
-
iterator
-
size
public int size()Calculate size.The implementation in
AbstractIterationSet
iterates through all of the elements.- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceSet<E>
- Specified by:
size
in classAbstractCollection<E>
-
equals
Overridden inAbstractIterationSet
to minimize the use ofsize()
.- Specified by:
equals
in interfaceCollection<E>
- Specified by:
equals
in interfaceSet<E>
- Overrides:
equals
in classAbstractSet<E>
-
hashCode
public int hashCode()- Specified by:
hashCode
in interfaceCollection<E>
- Specified by:
hashCode
in interfaceSet<E>
- Overrides:
hashCode
in classAbstractSet<E>
-
isEmpty
public boolean isEmpty()Overridden inAbstractIterationSet
to minimize the use ofsize()
.- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceSet<E>
- Overrides:
isEmpty
in classAbstractCollection<E>
-
toArray
Overridden inAbstractIterationSet
to minimize the use ofsize()
.- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceSet<E>
- Overrides:
toArray
in classAbstractCollection<E>
-
toArray
public <T> T[] toArray(T[] array) Overridden inAbstractIterationSet
to minimize the use ofsize()
.- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceSet<E>
- Overrides:
toArray
in classAbstractCollection<E>
-
spliterator
Overridden inAbstractIterationSet
to avoid the use ofsize()
.Note: the underlying
CloseableIterator
is not closed when this method is used. Prefer usingbuildSpliterator(java.util.Iterator<E>)
and arranging for the iterator to be closed separately. -
buildSpliterator
Build aSpliterator
appropriate for this set from the given instance iterator.Implementations should probably use
Spliterators.spliteratorUnknownSize()
unless the size is known.- Parameters:
iterator
- a new iterator returned from {#link #iterator}
-
stream
The implementation in
AbstractIterationSet
build a stream from the results fromiterator()
andbuildSpliterator()
, and marks the iterator for close viaStream.onClose()
.
-