Module org.refcodes.generator
Package org.refcodes.generator
Class ConcurrentBufferedGeneratorDecorator<T>
java.lang.Object
org.refcodes.generator.ConcurrentBufferedGeneratorDecorator<T>
- Type Parameters:
T
- the generic type of the elements being generated.
- All Implemented Interfaces:
Iterator<T>
,BufferedGenerator<T>
,Generator<T>
,org.refcodes.mixin.DecorateeAccessor<Iterator<T>>
,org.refcodes.mixin.Disposable
,org.refcodes.mixin.Suspendable
public class ConcurrentBufferedGeneratorDecorator<T>
extends Object
implements BufferedGenerator<T>, org.refcodes.mixin.DecorateeAccessor<Iterator<T>>, org.refcodes.mixin.Disposable
The
ConcurrentBufferedGeneratorDecorator
decorates a
Generator
with read-ahead functionality by buffering generated IDs
till a predefined maximum is reached and refilling the buffer periodically
upon dropping below a threshold. A background thread checks the buffer
periodically and takes care of refilling. The background task is paused upon
calling #suspend() and the resources are freed upon calling
dispose()
. The resulting Generator
is thread safe!-
Nested Class Summary
Nested classes/interfaces inherited from interface org.refcodes.mixin.DecorateeAccessor
org.refcodes.mixin.DecorateeAccessor.DecorateeBuilder<DECORATEE extends Object,
B extends org.refcodes.mixin.DecorateeAccessor.DecorateeBuilder<DECORATEE, B>>, org.refcodes.mixin.DecorateeAccessor.DecorateeMutator<DECORATEE extends Object>, org.refcodes.mixin.DecorateeAccessor.DecorateeProperty<DECORATEE extends Object> -
Constructor Summary
ConstructorDescriptionConcurrentBufferedGeneratorDecorator
(Iterator<T> aDecoratee) Constructs theConcurrentBufferedGeneratorDecorator
decorating the providedGenerator
using theDEFAULT_MAX_BUFFER_SIZE
for the per thread read-ahead buffer.ConcurrentBufferedGeneratorDecorator
(Iterator<T> aDecoratee, int aMaxBufferSize, int aBufferThreshold, long aRefillLoopSleepTimeMillis, boolean isAdaptive, boolean isDaemon) Constructs theConcurrentBufferedGeneratorDecorator
decorating the providedGenerator
with the provided properties. -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Iterator
forEachRemaining, remove
-
Constructor Details
-
ConcurrentBufferedGeneratorDecorator
Constructs theConcurrentBufferedGeneratorDecorator
decorating the providedGenerator
using theDEFAULT_MAX_BUFFER_SIZE
for the per thread read-ahead buffer.- Parameters:
aDecoratee
- TheGenerator
to be decorated.
-
ConcurrentBufferedGeneratorDecorator
public ConcurrentBufferedGeneratorDecorator(Iterator<T> aDecoratee, int aMaxBufferSize, int aBufferThreshold, long aRefillLoopSleepTimeMillis, boolean isAdaptive, boolean isDaemon) Constructs theConcurrentBufferedGeneratorDecorator
decorating the providedGenerator
with the provided properties.- Parameters:
aDecoratee
- TheGenerator
to be decorated.aMaxBufferSize
- The size of the read-ahead bufferaBufferThreshold
- The threshold at which the daemon is to begin refilling the buffer till its buffer max size.aRefillLoopSleepTimeMillis
- The loop sleep time in milliseconds to for the refill daemon to wait before testing (and refilling) the buffer.isAdaptive
- True in case the refill loop sleep time is to be adjusted in case after a sleep period the queue is empty.isDaemon
- True in case the daemon is to be started as daemon thread.
-
-
Method Details
-
hasNext
public boolean hasNext()Tests whether theGenerator
is capable of generating aGenerator.next()
item. -
next
Generates a next item. -
getDecoratee
- Specified by:
getDecoratee
in interfaceorg.refcodes.mixin.DecorateeAccessor<T>
-
suspend
public void suspend()- Specified by:
suspend
in interfaceorg.refcodes.mixin.Suspendable
-
dispose
public void dispose()- Specified by:
dispose
in interfaceorg.refcodes.mixin.Disposable
-