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!
  • Constructor Details

    • ConcurrentBufferedGeneratorDecorator

      public ConcurrentBufferedGeneratorDecorator(Iterator<T> aDecoratee)
      Constructs the ConcurrentBufferedGeneratorDecorator decorating the provided Generator using the DEFAULT_MAX_BUFFER_SIZE for the per thread read-ahead buffer.
      Parameters:
      aDecoratee - The Generator to be decorated.
    • ConcurrentBufferedGeneratorDecorator

      public ConcurrentBufferedGeneratorDecorator(Iterator<T> aDecoratee, int aMaxBufferSize, int aBufferThreshold, long aRefillLoopSleepTimeMillis, boolean isAdaptive, boolean isDaemon)
      Constructs the ConcurrentBufferedGeneratorDecorator decorating the provided Generator with the provided properties.
      Parameters:
      aDecoratee - The Generator to be decorated.
      aMaxBufferSize - The size of the read-ahead buffer
      aBufferThreshold - 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 the Generator is capable of generating a Generator.next() item.
      Specified by:
      hasNext in interface Generator<T>
      Specified by:
      hasNext in interface Iterator<T>
      Returns:
      true, if successful
    • next

      public T next()
      Generates a next item.
      Specified by:
      next in interface Generator<T>
      Specified by:
      next in interface Iterator<T>
      Returns:
      The next item generated
    • getDecoratee

      public Iterator<T> getDecoratee()
      Specified by:
      getDecoratee in interface org.refcodes.mixin.DecorateeAccessor<T>
    • suspend

      public void suspend()
      Specified by:
      suspend in interface org.refcodes.mixin.Suspendable
    • dispose

      public void dispose()
      Specified by:
      dispose in interface org.refcodes.mixin.Disposable