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:
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
- Author:
- Martin Desruisseaux (IRD, Geomatys)
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidCaches a new element.cached()Returns an unmodifiable view over the elements cached so far.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 SetOfUnknownSize
equals, isSizeKnown, removeAll, spliterator, toArray, toArrayMethods inherited from class AbstractSet
hashCodeMethods inherited from class AbstractCollection
add, addAll, clear, contains, containsAll, remove, retainAll, toStringMethods inherited from interface Collection
parallelStream, removeIf, stream
-
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
-
-
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
-
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
-
iterator
Returns an iterator over the elements contained in this set. This is not the same iterator than the one given to the constructor.
-