Top Description Inners Fields Constructors Methods
org.jooq.lambda

pack-priv final Class SeqBuffer<T>

extends Object
Class Inheritance
Imports
java.util.ArrayList, .Comparator, .List, .Objects, .Spliterator, java.util.function.Consumer, java.util.stream.Stream

Lazily consumes given Spliterator through Seqs provided by method seq(). This method may be called multiple times, and the returned Seqs may be consumed interchangeably. Instances of this class ARE thread-safe.
Author
Tomasz Linkowski

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
SeqBuffer.BufferSpliterator

Special Spliterator whose tryAdvance method can buffer (i.e. can advance the source spliterator).

Field Summary

Modifier and TypeField and Description
private final List<T>
private volatile boolean
buffering

True while source hasn't reported that it's exhausted.

private final Spliterator<T>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv static <T> SeqBuffer<T>
of(Stream<? extends T> stream)

pack-priv static <T> SeqBuffer<T>
of(Spliterator<T> spliterator)

pack-priv Seq<T>
seq()

Returns a Seq over given source.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bufferback to summary
private final List<T> buffer
bufferingback to summary
private volatile boolean buffering

True while source hasn't reported that it's exhausted. This volatile field acts as a memory barrier for the contents of source and buffer:

sourceback to summary
private final Spliterator<T> source

Constructor Detail

SeqBufferback to summary
private SeqBuffer(Spliterator<T> source)

Method Detail

ofback to summary
pack-priv static <T> SeqBuffer<T> of(Stream<? extends T> stream)
Annotations
@SuppressWarnings:unchecked
ofback to summary
pack-priv static <T> SeqBuffer<T> of(Spliterator<T> spliterator)
seqback to summary
pack-priv Seq<T> seq()

Returns a Seq over given source. Although such Seq is not thread-safe by itself, it interacts in a thread-safe way with its parent SeqBuffer. As a result, each Seq returned by this method can be safely used on a different thread.

org.jooq.lambda back to summary

private Class SeqBuffer.BufferSpliterator

extends Object
implements Spliterator<T>
Class Inheritance
All Implemented Interfaces
java.util.Spliterator

Special Spliterator whose tryAdvance method can buffer (i.e. can advance the source spliterator). Instances of this class are NOT thread-safe but they interact in a thread-safe way with SeqBuffer.

Field Summary

Modifier and TypeField and Description
private int
nextIndex

Index of the element that will be returned upon next call to tryAdvance if such element exists.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private T
private boolean
private boolean
public int
characteristics()

Implements java.util.Spliterator.characteristics.

Returns a set of characteristics of this Spliterator and its elements.

public long
estimateSize()

Implements java.util.Spliterator.estimateSize.

Returns an estimate of the number of elements that would be encountered by a forEachRemaining traversal, or returns Long#MAX_VALUE if infinite, unknown, or too expensive to compute.

private long
estimateSizeDuringBuffering()

Returns the estimate size of this Spliterator.

public Comparator<? super T>
getComparator()

Overrides default java.util.Spliterator.getComparator.

If this Spliterator's source is SORTED by a Comparator, returns that Comparator.

private int
pack-priv SeqBuffer<T>
public boolean
tryAdvance(Consumer<? super T> action)

Implements java.util.Spliterator.tryAdvance.

If a remaining element exists: performs the given action on it, returning true; else returns false.

private boolean
tryAdvanceSource()

Buffers (i.e. advances the source) until an item at nextIndex is added to the buffer, or until the source is exhausted.

private boolean
tryAdvanceThisAtOnce(Consumer<? super T> action)

Called only when buffering has been completed.

private boolean
tryAdvanceThisWithBuffering(Consumer<? super T> action)

Tries to advance this Spliterator to element at nextIndex, buffering source elements into buffer if necessary.

public Spliterator<T>
trySplit()

Implements java.util.Spliterator.trySplit.

If this spliterator can be partitioned, returns a Spliterator covering elements, that will, upon return from this method, not be covered by this Spliterator.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

nextIndexback to summary
private int nextIndex

Index of the element that will be returned upon next call to tryAdvance if such element exists.

Constructor Detail

BufferSpliteratorback to summary
private BufferSpliterator()

Method Detail

advanceThisback to summary
private T advanceThis()
canAdvanceThisAtOnceback to summary
private boolean canAdvanceThisAtOnce()
canAdvanceThisWithBufferingback to summary
private boolean canAdvanceThisWithBuffering()
characteristicsback to summary
public int characteristics()

Implements java.util.Spliterator.characteristics.

Doc from java.util.Spliterator.characteristics.

Returns a set of characteristics of this Spliterator and its elements. The result is represented as ORed values from ORDERED, DISTINCT, SORTED, SIZED, NONNULL, IMMUTABLE, CONCURRENT, SUBSIZED. Repeated calls to characteristics() on a given spliterator, prior to or in-between calls to trySplit, should always return the same result.

If a Spliterator reports an inconsistent set of characteristics (either those returned from a single invocation or across multiple invocations), no guarantees can be made about any computation using this Spliterator.

Annotations
@Override
estimateSizeback to summary
public long estimateSize()

Implements java.util.Spliterator.estimateSize.

Doc from java.util.Spliterator.estimateSize.

Returns an estimate of the number of elements that would be encountered by a forEachRemaining traversal, or returns Long#MAX_VALUE if infinite, unknown, or too expensive to compute.

If this Spliterator is SIZED and has not yet been partially traversed or split, or this Spliterator is SUBSIZED and has not yet been partially traversed, this estimate must be an accurate count of elements that would be encountered by a complete traversal. Otherwise, this estimate may be arbitrarily inaccurate, but must decrease as specified across invocations of trySplit.

Annotations
@Override
estimateSizeDuringBufferingback to summary
private long estimateSizeDuringBuffering()

Returns the estimate size of this Spliterator. Synchronized to get an accurate sum of buffer.size() and source.estimateSize().

getComparatorback to summary
public Comparator<? super T> getComparator()

Overrides default java.util.Spliterator.getComparator.

Doc from java.util.Spliterator.getComparator.

If this Spliterator's source is SORTED by a Comparator, returns that Comparator. If the source is SORTED in natural order, returns null. Otherwise, if the source is not SORTED, throws IllegalStateException.

Annotations
@Override
numberOfElementsLeftInBufferback to summary
private int numberOfElementsLeftInBuffer()
parentSeqBufferback to summary
pack-priv SeqBuffer<T> parentSeqBuffer()
tryAdvanceback to summary
public boolean tryAdvance(Consumer<? super T> action)

Implements java.util.Spliterator.tryAdvance.

Doc from java.util.Spliterator.tryAdvance.

If a remaining element exists: performs the given action on it, returning true; else returns false. If this Spliterator is ORDERED the action is performed on the next element in encounter order. Exceptions thrown by the action are relayed to the caller.

Subsequent behavior of a spliterator is unspecified if the action throws an exception.

Annotations
@Override
tryAdvanceSourceback to summary
private boolean tryAdvanceSource()

Buffers (i.e. advances the source) until an item at nextIndex is added to the buffer, or until the source is exhausted. Guarded by: buffer

tryAdvanceThisAtOnceback to summary
private boolean tryAdvanceThisAtOnce(Consumer<? super T> action)

Called only when buffering has been completed.

tryAdvanceThisWithBufferingback to summary
private boolean tryAdvanceThisWithBuffering(Consumer<? super T> action)

Tries to advance this Spliterator to element at nextIndex, buffering source elements into buffer if necessary. Synchronized on buffer in order to: - obtain accurate buffer.size() - safely copy from source to buffer (if needed) - safely call buffer.get()

trySplitback to summary
public Spliterator<T> trySplit()

Implements java.util.Spliterator.trySplit.

Doc from java.util.Spliterator.trySplit.

If this spliterator can be partitioned, returns a Spliterator covering elements, that will, upon return from this method, not be covered by this Spliterator.

If this Spliterator is ORDERED, the returned Spliterator must cover a strict prefix of the elements.

Unless this Spliterator covers an infinite number of elements, repeated calls to trySplit() must eventually return null. Upon non-null return:

  • the value reported for estimateSize() before splitting, must, after splitting, be greater than or equal to estimateSize() for this and the returned Spliterator; and
  • if this Spliterator is SUBSIZED, then estimateSize() for this spliterator before splitting must be equal to the sum of estimateSize() for this and the returned Spliterator after splitting.

This method may return null for any reason, including emptiness, inability to split after traversal has commenced, data structure constraints, and efficiency considerations.

Annotations
@Override