Class ChannelBuffer<T>

java.lang.Object
io.activej.csp.queue.ChannelBuffer<T>
Type Parameters:
T - the type of values that are stored in the buffer
All Implemented Interfaces:
io.activej.async.process.AsyncCloseable, ChannelConsumerTransformer<T,ChannelConsumer<T>>, ChannelSupplierTransformer<T,ChannelSupplier<T>>, ChannelTransformer<T,T>, ChannelQueue<T>

public final class ChannelBuffer<T> extends Object implements ChannelQueue<T>
Represents a queue of elements which you can put and take. In order to mark if an object is pending put or take to/from the queue, there are corresponding put and take SettablePromises.
  • Field Summary

    Fields inherited from interface io.activej.async.process.AsyncCloseable

    STATIC
  • Constructor Summary

    Constructors
    Constructor
    Description
    ChannelBuffer(int bufferSize)
     
    ChannelBuffer(int bufferMinSize, int bufferMaxSize)
    Creates a ChannelBuffer with the buffer size of the next highest power of 2 (for example, if bufferMinxSize = 113, a buffer of 128 elements will be created).
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(T item)
    Adds provided item to the buffer and resets current take.
    void
    closeEx(@NotNull Exception e)
    Closes the buffer if this exception is not null.
    @Nullable Exception
     
    boolean
    Checks if this buffer contains elements.
    boolean
    Checks if this buffer has fewer elements than bufferMinSize.
    boolean
    Checks if this buffer is saturated by comparing its current size with bufferMaxSize.
    Returns the head of the buffer if it is not empty, otherwise returns null.
    io.activej.promise.Promise<Void>
    put(T item)
    Puts value in this buffer and increases tail value.
    int
    Returns amount of elements in this buffer.
    io.activej.promise.Promise<T>
    Returns a promise of the head of the buffer if it is not empty.
    boolean
    Checks if this buffer will have fewer elements than bufferMinSize, if at least one more element will be taken, by comparing its current size with bufferMinSize.
    boolean
    Checks if this buffer will be saturated if at least one more element will be added, by comparing its current size with bufferMaxSize.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface io.activej.async.process.AsyncCloseable

    close

    Methods inherited from interface io.activej.csp.queue.ChannelQueue

    getConsumer, getConsumer, getSupplier, transform, transform
  • Constructor Details

    • ChannelBuffer

      public ChannelBuffer(int bufferSize)
      See Also:
    • ChannelBuffer

      public ChannelBuffer(int bufferMinSize, int bufferMaxSize)
      Creates a ChannelBuffer with the buffer size of the next highest power of 2 (for example, if bufferMinxSize = 113, a buffer of 128 elements will be created).
      Parameters:
      bufferMinSize - a minimal amount of elements in the buffer
      bufferMaxSize - a max amount of elements in the buffer
  • Method Details

    • isSaturated

      public boolean isSaturated()
      Checks if this buffer is saturated by comparing its current size with bufferMaxSize.
      Specified by:
      isSaturated in interface ChannelQueue<T>
      Returns:
      true if this buffer size is greater than bufferMaxSize, otherwise returns false
    • willBeSaturated

      public boolean willBeSaturated()
      Checks if this buffer will be saturated if at least one more element will be added, by comparing its current size with bufferMaxSize.
      Returns:
      true if this buffer size is bigger or equal to the bufferMaxSize, otherwise returns false
    • isExhausted

      public boolean isExhausted()
      Checks if this buffer has fewer elements than bufferMinSize.
      Specified by:
      isExhausted in interface ChannelQueue<T>
      Returns:
      true if this buffer size is smaller than bufferMinSize, otherwise returns false
    • willBeExhausted

      public boolean willBeExhausted()
      Checks if this buffer will have fewer elements than bufferMinSize, if at least one more element will be taken, by comparing its current size with bufferMinSize.
      Returns:
      true if this buffer size is smaller or equal to the bufferMinSize, otherwise returns false
    • isEmpty

      public boolean isEmpty()
      Checks if this buffer contains elements.
      Returns:
      true if tail and head values are equal, otherwise false
    • size

      public int size()
      Returns amount of elements in this buffer.
    • add

      public void add(@Nullable T item) throws Exception
      Adds provided item to the buffer and resets current take.
      Throws:
      Exception
    • poll

      @Nullable public T poll() throws Exception
      Returns the head of the buffer if it is not empty, otherwise returns null. Increases the value of head.

      If the buffer will have fewer elements than bufferMinSize after this poll and put promise is not null, put will be set null after the poll.

      If current exception is not null, it will be thrown.

      Returns:
      head element of this buffer index if the buffer is not empty, otherwise null
      Throws:
      Exception - if current exception is not null
    • put

      public io.activej.promise.Promise<Void> put(@Nullable T item)
      Puts value in this buffer and increases tail value.

      Current put must be null. If current exception is not null, a promise of this exception will be returned and the value will be recycled.

      If this take is not null, the value will be set directly to the set, without adding to the buffer.

      Specified by:
      put in interface ChannelQueue<T>
      Parameters:
      item - a value passed to the buffer
      Returns:
      promise of null or exception as a marker of completion
    • take

      public io.activej.promise.Promise<T> take()
      Returns a promise of the head of the buffer if it is not empty.

      If this buffer will be exhausted after this take and put promise is not null, put will be set null after the poll.

      Current take must be null. If current exception is not null, a promise of this exception will be returned.

      Specified by:
      take in interface ChannelQueue<T>
      Returns:
      promise of element taken from the buffer
    • closeEx

      public void closeEx(@NotNull @NotNull Exception e)
      Closes the buffer if this exception is not null. Recycles all elements of the buffer and sets elements, put and take to null.
      Specified by:
      closeEx in interface io.activej.async.process.AsyncCloseable
      Parameters:
      e - exception that is used to close buffer with
    • getException

      @Nullable public @Nullable Exception getException()