public class BinaryIntReference extends AbstractReference implements net.openhft.chronicle.core.values.IntValue
BytesStore
.
This class provides various operations to access and manipulate a single integer in binary form. The integer is stored in a BytesStore, and this class provides methods for atomic operations, reading/writing the value, and managing its state.
The class also supports volatile reads, ordered writes, and compare-and-swap operations. The maximum size of the backing storage is 4 bytes, corresponding to a 32-bit integer.
Example usage:
BytesStore bytesStore = BytesStore.nativeStoreWithFixedCapacity(32); try (BinaryIntReference ref = new BinaryIntReference()) { ref.bytesStore(bytesStore, 16, 4); ref.setValue(10); int value = ref.getVolatileValue(); }
Note: This class is not thread-safe. External synchronization may be necessary if instances are shared between threads.
BytesStore
,
IntValue
Modifier and Type | Field and Description |
---|---|
static int |
INT_NOT_COMPLETE |
bytes, offset
Constructor and Description |
---|
BinaryIntReference() |
Modifier and Type | Method and Description |
---|---|
int |
addAtomicValue(int delta)
Adds a delta to the current 32-bit integer value atomically and returns the result.
|
int |
addValue(int delta)
Adds a delta to the current 32-bit integer value and returns the result.
|
void |
bytesStore(@NotNull BytesStore bytes,
long offset,
long length)
Sets the BytesStore which this reference points to.
|
boolean |
compareAndSwapValue(int expected,
int value)
Atomically sets the 32-bit integer value to the given updated value if the current value is
equal to the expected value.
|
int |
getValue()
Retrieves the 32-bit integer value from the BytesStore.
|
int |
getVolatileValue()
Retrieves the 32-bit integer value using volatile memory semantics.
|
long |
maxSize()
Returns the maximum size of this reference in bytes (4 bytes for a 32-bit integer).
|
void |
setOrderedValue(int value)
Sets the 32-bit integer value using ordered or lazy set memory semantics.
|
void |
setValue(int value)
Sets the 32-bit integer value in the BytesStore.
|
@NotNull String |
toString()
Returns a string representation of this BinaryIntReference.
|
acceptNewBytesStore, address, bytesStore, lock, offset, performClose, tryLock
assertCloseable, assertCloseablesClosed, close, createdHere, disableCloseableTracing, enableCloseableTracing, gcAndWaitForCloseablesToClose, isClosed, isClosing, isInUserThread, referenceId, shouldPerformCloseInBackground, shouldWaitForClosed, singleThreadedCheckDisabled, singleThreadedCheckDisabled, singleThreadedCheckReset, threadSafetyCheck, throwExceptionIfClosed, throwExceptionIfClosedInSetter, unmonitor, waitForCloseablesToClose, waitForClosed, warnAndCloseIfNotClosed
public static final int INT_NOT_COMPLETE
public void bytesStore(@NotNull @NotNull BytesStore bytes, long offset, long length) throws IllegalStateException, IllegalArgumentException, BufferOverflowException
bytesStore
in interface Byteable
bytesStore
in class AbstractReference
bytes
- the BytesStoreoffset
- the offset within the BytesStorelength
- the length of the valueIllegalArgumentException
- If the length is not equal to maxSizeBufferOverflowException
- If the offset is too largenet.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe wayIllegalStateException
public long maxSize()
@NotNull public @NotNull String toString()
toString
in class net.openhft.chronicle.core.io.AbstractCloseable
public int getValue() throws IllegalStateException, BufferUnderflowException
getValue
in interface net.openhft.chronicle.core.values.IntValue
BufferUnderflowException
- If the offset is too largenet.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe wayIllegalStateException
public void setValue(int value) throws IllegalStateException, BufferOverflowException
setValue
in interface net.openhft.chronicle.core.values.IntValue
value
- the 32-bit integer value to setBufferOverflowException
- If the offset is too largenet.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe wayIllegalStateException
public int getVolatileValue() throws IllegalStateException, BufferUnderflowException
getVolatileValue
in interface net.openhft.chronicle.core.values.IntValue
BufferUnderflowException
- If the offset is too largenet.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe wayIllegalStateException
public void setOrderedValue(int value) throws IllegalStateException, BufferOverflowException
setOrderedValue
in interface net.openhft.chronicle.core.values.IntValue
value
- the 32-bit integer value to setBufferOverflowException
- If the offset is too largenet.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe wayIllegalStateException
public int addValue(int delta) throws IllegalStateException, BufferUnderflowException
addValue
in interface net.openhft.chronicle.core.values.IntValue
delta
- the value to addBufferUnderflowException
- If the offset is too largenet.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe wayIllegalStateException
public int addAtomicValue(int delta) throws IllegalStateException, BufferUnderflowException
addAtomicValue
in interface net.openhft.chronicle.core.values.IntValue
delta
- the value to addBufferUnderflowException
- If the offset is too largenet.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe wayIllegalStateException
public boolean compareAndSwapValue(int expected, int value) throws IllegalStateException, BufferOverflowException
compareAndSwapValue
in interface net.openhft.chronicle.core.values.IntValue
expected
- the expected 32-bit integer valuevalue
- the new 32-bit integer valueBufferOverflowException
- If the offset is too largenet.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe wayIllegalStateException
Copyright © 2023. All rights reserved.