Package io.activej.csp.queue
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>
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
ConstructorsConstructorDescriptionChannelBuffer(int bufferSize) ChannelBuffer(int bufferMinSize, int bufferMaxSize) Creates a ChannelBuffer with the buffer size of the next highest power of 2 (for example, ifbufferMinxSize = 113, a buffer of 128 elements will be created). -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds provided item to the buffer and resets currenttake.voidCloses the buffer if thisexceptionis notnull.@Nullable ExceptionbooleanisEmpty()Checks if this buffer contains elements.booleanChecks if this buffer has fewer elements thanbufferMinSize.booleanChecks if this buffer is saturated by comparing its current size withbufferMaxSize.poll()Returns the head of the buffer if it is not empty, otherwise returnsnull.io.activej.promise.Promise<Void>Putsvaluein this buffer and increasestailvalue.intsize()Returns amount of elements in this buffer.io.activej.promise.Promise<T>take()Returns a promise of the head of thebufferif it is not empty.booleanChecks if this buffer will have fewer elements thanbufferMinSize, if at least one more element will be taken, by comparing its current size withbufferMinSize.booleanChecks if this buffer will be saturated if at least one more element will be added, by comparing its current size withbufferMaxSize.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.activej.async.process.AsyncCloseable
closeMethods 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, ifbufferMinxSize = 113, a buffer of 128 elements will be created).- Parameters:
bufferMinSize- a minimal amount of elements in the bufferbufferMaxSize- 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 withbufferMaxSize.- Specified by:
isSaturatedin interfaceChannelQueue<T>- Returns:
trueif this buffer size is greater thanbufferMaxSize, otherwise returnsfalse
-
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 withbufferMaxSize.- Returns:
trueif this buffer size is bigger or equal to thebufferMaxSize, otherwise returnsfalse
-
isExhausted
public boolean isExhausted()Checks if this buffer has fewer elements thanbufferMinSize.- Specified by:
isExhaustedin interfaceChannelQueue<T>- Returns:
trueif this buffer size is smaller thanbufferMinSize, otherwise returnsfalse
-
willBeExhausted
public boolean willBeExhausted()Checks if this buffer will have fewer elements thanbufferMinSize, if at least one more element will be taken, by comparing its current size withbufferMinSize.- Returns:
trueif this buffer size is smaller or equal to thebufferMinSize, otherwise returnsfalse
-
isEmpty
public boolean isEmpty()Checks if this buffer contains elements.- Returns:
trueiftailandheadvalues are equal, otherwisefalse
-
size
public int size()Returns amount of elements in this buffer. -
add
Adds provided item to the buffer and resets currenttake.- Throws:
Exception
-
poll
Returns the head of the buffer if it is not empty, otherwise returnsnull. Increases the value ofhead.If the buffer will have fewer elements than
bufferMinSizeafter this poll andputpromise is notnull,putwill be setnullafter the poll.If current
exceptionis notnull, it will be thrown.- Returns:
- head element of this buffer
index if the buffer is not empty, otherwise
null - Throws:
Exception- if currentexceptionis notnull
-
put
Putsvaluein this buffer and increasestailvalue.Current
putmust benull. If currentexceptionis notnull, a promise of this exception will be returned and thevaluewill be recycled.If this
takeis notnull, the value will be set directly to theset, without adding to the buffer.- Specified by:
putin interfaceChannelQueue<T>- Parameters:
item- a value passed to the buffer- Returns:
- promise of
nullorexceptionas a marker of completion
-
take
Returns a promise of the head of thebufferif it is not empty.If this buffer will be exhausted after this take and
putpromise is notnull,putwill be setnullafter the poll.Current
takemust benull. If currentexceptionis notnull, a promise of this exception will be returned.- Specified by:
takein interfaceChannelQueue<T>- Returns:
- promise of element taken from the buffer
-
closeEx
Closes the buffer if thisexceptionis notnull. Recycles all elements of the buffer and setselements,putandtaketonull.- Specified by:
closeExin interfaceio.activej.async.process.AsyncCloseable- Parameters:
e- exception that is used to close buffer with
-
getException
-