Class LazySet<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
org.apache.sis.internal.util.SetOfUnknownSize<E>
org.apache.sis.internal.referencing.LazySet<E>
- Type Parameters:
E
- the type of elements in the set.
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,Set<E>
- Direct Known Subclasses:
AuthorityFactories
,Providers
An immutable set built from an iterator, which will be filled only when needed.
This implementation does not check if all elements in the iterator
are really unique; we assume that this condition was already verified by the caller.
One usage of LazySet
is to workaround a ServiceLoader
bug which blocks usage of two
Iterator
instances together: the first iteration must be fully completed or abandoned before we can start
a new iteration. See
DefaultMathTransformFactory()
.
Some usages for this class are to prepend some values before the elements given by the source Iterable
,
or to replace some values when they are loaded. It may also be used for creating filtered sets when used together
with CollectionsExt.filter(…)
.
This class is not thread-safe. Synchronization, if desired, shall be done by the caller.
- Since:
- 0.6
- Version:
- 0.8
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate E[]
The elements that we cached so far, ornull
if the iteration did not started yet.private int
The number of valid elements in thecachedElements
array.The type of service to request withServiceLoader
, ornull
if unknown.The iterator to use for filling this set, ornull
if the iteration did not started yet or is finished. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Caches a new element.cached()
Returns an unmodifiable view over the elements cached so far.private boolean
Returnstrue
if thesourceIterator
is non-null and have more elements to return, or if we initialized the cache with some elements declared byinitialValues()
.private boolean
Creates thecachedElements
array.(package private) final boolean
exists
(int index) Returnstrue
if an element exists at the given index.(package private) final E
get
(int index) Returns the element at the specified position in this set.protected E[]
Hook for subclasses that want to prepend some values before the sourceIterable
.final boolean
isEmpty()
Tests if this set has no element.iterator()
Returns an iterator over the elements contained in this set.protected E
Returns the next element from the given iterator.void
reload()
Notifies thisLazySet
that it should re-fetch the elements from the source given at construction time.final int
size()
Returns the number of elements in this set.Methods inherited from class org.apache.sis.internal.util.SetOfUnknownSize
equals, isSizeKnown, removeAll, spliterator, toArray, toArray
Methods inherited from class java.util.AbstractSet
hashCode
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, stream, toArray
-
Field Details
-
service
The type of service to request withServiceLoader
, ornull
if unknown. -
sourceIterator
The iterator to use for filling this set, ornull
if the iteration did not started yet or is finished. Those two cases can be distinguished by looking whether thecachedElements
array is null or not. -
cachedElements
The elements that we cached so far, ornull
if the iteration did not started yet. After the iteration started, this array will grow as needed.- See Also:
-
numCached
private int numCachedThe number of valid elements in thecachedElements
array. This counter will be incremented as long as there is more elements returned bysourceIterator
.
-
-
Constructor Details
-
LazySet
Constructs a set to be filled by the elements from the specified source. Iteration will start only when first needed, and at most one iteration will be performed (unlessreload()
is invoked).- Parameters:
service
- the type of service to request withServiceLoader
.
-
LazySet
Constructs a set to be filled using the specified iterator. Iteration with the given iterator will occur only when needed.- Parameters:
iterator
- the iterator to use for filling this set.
-
-
Method Details
-
reload
public void reload()Notifies thisLazySet
that it should re-fetch the elements from the source given at construction time. -
initialValues
Hook for subclasses that want to prepend some values before the sourceIterable
. This method is invoked only when first needed. It is safe to return a shared array sinceLazySet
will not write in that array (LazySet
will create a new array if it needs to add more values).- Returns:
- values to prepend before the source
Iterable
, ornull
if none. - Since:
- 0.7
-
createCache
private boolean createCache()Creates thecachedElements
array. This array will contain the elements given byinitialValues()
if that method returned a non-null and non-empty array.- Returns:
true
ifinitialValues()
initialized the set with at least one value.
-
canPullMore
private boolean canPullMore()Returnstrue
if thesourceIterator
is non-null and have more elements to return, or if we initialized the cache with some elements declared byinitialValues()
. -
isEmpty
public final boolean isEmpty()Tests if this set has no element.- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceSet<E>
- Overrides:
isEmpty
in classSetOfUnknownSize<E>
- Returns:
true
if this set has no element.
-
size
public final int size()Returns the number of elements in this set. Invoking this method forces the set to immediately iterates through all remaining elements.- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceSet<E>
- Overrides:
size
in classSetOfUnknownSize<E>
- Returns:
- number of elements in the iterator.
-
next
Returns the next element from the given iterator. Default implementation returnsIterator.next()
. Subclasses may override if they need to apply additional processing. For example, this method can be used for skipping data, but this approach works only if we have the guarantee that another element exists after the skipped one (becauseLazySet
will not invokeIterator.hasNext()
again).- Parameters:
it
- the iterator from which to get a next value.- Returns:
- the next value (may be
null
).
-
cache
Caches a new element. Subclasses can override this method if they want to substitute the given value by another value.- Parameters:
element
- the element to add to the cache.
-
cached
Returns an unmodifiable view over the elements cached so far. The returned list does not contain any elements that were not yet fetched from the source.- Returns:
- the elements cached so far.
-
exists
final boolean exists(int index) Returnstrue
if an element exists at the given index. The element is not loaded immediately.NOTE: This method is for use by iterators only. It is not suited for more general usage since it does not check for negative index and for skipped elements.
-
get
Returns the element at the specified position in this set.- Parameters:
index
- the index at which to get an element.- Returns:
- the element at the requested index.
-
iterator
Returns an iterator over the elements contained in this set. This is not the same iterator than the one given to the constructor.
-