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, ornullif the iteration did not started yet.private intThe number of valid elements in thecachedElementsarray.The type of service to request withServiceLoader, ornullif unknown.The iterator to use for filling this set, ornullif the iteration did not started yet or is finished. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidCaches a new element.cached()Returns an unmodifiable view over the elements cached so far.private booleanReturnstrueif thesourceIteratoris non-null and have more elements to return, or if we initialized the cache with some elements declared byinitialValues().private booleanCreates thecachedElementsarray.(package private) final booleanexists(int index) Returnstrueif an element exists at the given index.(package private) final Eget(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 booleanisEmpty()Tests if this set has no element.iterator()Returns an iterator over the elements contained in this set.protected EReturns the next element from the given iterator.voidreload()Notifies thisLazySetthat it should re-fetch the elements from the source given at construction time.final intsize()Returns the number of elements in this set.Methods inherited from class org.apache.sis.internal.util.SetOfUnknownSize
equals, isSizeKnown, removeAll, spliterator, toArray, toArrayMethods inherited from class java.util.AbstractSet
hashCodeMethods inherited from class java.util.AbstractCollection
add, addAll, clear, contains, containsAll, remove, retainAll, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream
-
Field Details
-
service
The type of service to request withServiceLoader, ornullif unknown. -
sourceIterator
The iterator to use for filling this set, ornullif the iteration did not started yet or is finished. Those two cases can be distinguished by looking whether thecachedElementsarray is null or not. -
cachedElements
The elements that we cached so far, ornullif 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 thecachedElementsarray. 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 thisLazySetthat 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 sinceLazySetwill not write in that array (LazySetwill create a new array if it needs to add more values).- Returns:
- values to prepend before the source
Iterable, ornullif none. - Since:
- 0.7
-
createCache
private boolean createCache()Creates thecachedElementsarray. This array will contain the elements given byinitialValues()if that method returned a non-null and non-empty array.- Returns:
trueifinitialValues()initialized the set with at least one value.
-
canPullMore
private boolean canPullMore()Returnstrueif thesourceIteratoris 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:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Overrides:
isEmptyin classSetOfUnknownSize<E>- Returns:
trueif 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:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Overrides:
sizein 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 (becauseLazySetwill 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) Returnstrueif 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.
-