Class OverWritingBuffer<T>
- java.lang.Object
-
- org.jcsp.util.OverWritingBuffer<T>
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,ChannelDataStore<T>
public class OverWritingBuffer<T> extends java.lang.Object implements ChannelDataStore<T>, java.io.Serializable
This is used to create a buffered object channel that always accepts input, overwriting its last entered data if full.Description
It is an implementation of ChannelDataStore that yields a FIFO buffered semantics for a channel. When empty, the channel blocks readers. When full, a writer will overwrite the latest item written to the channel. See the static construction methods ofChannel(Channel.one2one(org.jcsp.util.ChannelDataStore)etc.).The getState method returns EMPTY or NONEMPTYFULL, but never FULL.
-
-
Field Summary
Fields Modifier and Type Field Description private T[]bufferThe storage for the buffered Objectsprivate intcounterThe number of Objects stored in the Bufferprivate intfirstIndexThe index of the oldest element (when counter > 0)private intlastIndexThe index of the next free element (when counter < buffer.length)private booleanvalueWrittenWhileFull-
Fields inherited from interface org.jcsp.util.ChannelDataStore
EMPTY, FULL, NONEMPTYFULL
-
-
Constructor Summary
Constructors Constructor Description OverWritingBuffer(int size)Construct a new OverWritingBuffer with the specified size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Objectclone()Returns a new (and EMPTY) OverWritingBuffer with the same creation parameters as this one.voidendGet()This ends an extended rendezvous by the reader.Tget()Returns the oldest Object from the OverWritingBuffer and removes it.intgetState()Returns the current state of the OverWritingBuffer.voidput(T value)Puts a new Object into the OverWritingBuffer.voidremoveAll()Deletes all items in the buffer, leaving it empty.TstartGet()This begins an extended rendezvous by the reader.
-
-
-
Field Detail
-
buffer
private final T[] buffer
The storage for the buffered Objects
-
counter
private int counter
The number of Objects stored in the Buffer
-
firstIndex
private int firstIndex
The index of the oldest element (when counter > 0)
-
lastIndex
private int lastIndex
The index of the next free element (when counter < buffer.length)
-
valueWrittenWhileFull
private boolean valueWrittenWhileFull
-
-
Constructor Detail
-
OverWritingBuffer
public OverWritingBuffer(int size)
Construct a new OverWritingBuffer with the specified size.- Parameters:
size- the number of Objects the OverWritingBuffer can store.- Throws:
BufferSizeError- if size is zero or negative. Note: no action should be taken to try/catch this exception - application code generating it is in error and needs correcting.
-
-
Method Detail
-
get
public T get()
Returns the oldest Object from the OverWritingBuffer and removes it.Pre-condition: getState must not currently return EMPTY.
- Specified by:
getin interfaceChannelDataStore<T>- Returns:
- the oldest Object from the OverWritingBuffer
-
put
public void put(T value)
Puts a new Object into the OverWritingBuffer.If OverWritingBuffer is full, the last item previously put into the buffer will be overwritten.
- Specified by:
putin interfaceChannelDataStore<T>- Parameters:
value- the Object to put into the OverWritingBuffer
-
startGet
public T startGet()
This begins an extended rendezvous by the reader. The semantics of an extended rendezvous on an overwrite-newest buffer are slightly complicated, but hopefully intuitive.If the buffer is of size 2 or larger, the semantics are as follows. Beginning an extended rendezvous will return the oldest value in the buffer, but not remove it. If the writer writes to the buffer during the rendezvous, it will grow the buffer and end up overwriting the newest value as normal. At the end of the extended rendezvous, the oldest value is removed.
If the buffer is of size 1, the semantics are identical to those of an
OverWriteOldestBuffer. For a complete description, refer to the documentation for theOverWriteOldestBuffer.startGet()method.- Specified by:
startGetin interfaceChannelDataStore<T>- Returns:
- The oldest value in the buffer at this time
- See Also:
ChannelDataStore.endGet()
-
endGet
public void endGet()
This ends an extended rendezvous by the reader.- Specified by:
endGetin interfaceChannelDataStore<T>- See Also:
startGet()
-
getState
public int getState()
Returns the current state of the OverWritingBuffer.- Specified by:
getStatein interfaceChannelDataStore<T>- Returns:
- the current state of the OverWritingBuffer (EMPTY or NONEMPTYFULL)
-
clone
public java.lang.Object clone()
Returns a new (and EMPTY) OverWritingBuffer with the same creation parameters as this one.Note: Only the size and structure of the OverWritingBuffer is cloned, not any stored data.
- Specified by:
clonein interfaceChannelDataStore<T>- Overrides:
clonein classjava.lang.Object- Returns:
- the cloned instance of this OverWritingBuffer.
-
removeAll
public void removeAll()
Description copied from interface:ChannelDataStoreDeletes all items in the buffer, leaving it empty.- Specified by:
removeAllin interfaceChannelDataStore<T>
-
-