T - the type of the object being internedpublic abstract class AbstractInterner<T> extends Object
The main usage is to reduce the amount of memory used by creating new objects when the same byte sequence is repeatedly decoded into an object.
This cache only guarantees it will provide a String which matches the decoded bytes.
It doesn't guarantee it will always return the same object, nor that different threads will return the same object, though the contents should always be the same.
While not technically thread safe, it should still behave correctly.
Abstract base class for implementing an interning mechanism, which helps in reusing instances of immutable objects. This class is designed to store objects and return previously stored instances that are equal to the required instance.
Note: The interning cache may not always return the same object instance, but the contents of the instances will be equal. *
| Modifier and Type | Field and Description |
|---|---|
protected net.openhft.chronicle.bytes.util.AbstractInterner.InternerEntry<T>[] |
entries
The array storing
InternerEntry objects. |
protected int |
mask
Mask used to hash into
entries, typically capacity - 1. |
protected int |
shift
Shift value used when computing the secondary hash index.
|
protected boolean |
toggle
Flag toggled when choosing between the two hash slots for new entries.
|
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractInterner(int capacity)
Constructor for creating an intern cache with the given capacity.
|
| Modifier and Type | Method and Description |
|---|---|
protected abstract T |
getValue(BytesStore<?,?> bs,
int length)
Converts the bytes from
bs into an instance of T. |
T |
intern(@NotNull Bytes<?> cs)
Interns the specified Bytes object.
|
T |
intern(@NotNull Bytes<?> cs,
int length)
Interns the specified Bytes object of a given length.
|
T |
intern(@NotNull BytesStore<?,?> cs)
Interns the specified BytesStore object.
|
T |
intern(@NotNull BytesStore<?,?> cs,
int length)
Interns the specified Bytes.
|
protected boolean |
toggle()
Toggles the internal toggle state and returns its new value.
|
int |
valueCount()
Returns the number of non-null values in the interner entries.
|
protected final net.openhft.chronicle.bytes.util.AbstractInterner.InternerEntry<T>[] entries
InternerEntry objects. Concurrent modifications
should be externally synchronised as no locking is performed here.protected final int mask
entries, typically capacity - 1.protected final int shift
protected boolean toggle
protected AbstractInterner(int capacity)
1 << 30.
The resulting structure is not inherently thread safe.capacity - the desired capacity for the intern cacheIllegalArgumentException - if capacity is negativepublic T intern(@NotNull @NotNull Bytes<?> cs) throws net.openhft.chronicle.core.io.IORuntimeException, BufferUnderflowException, IllegalStateException
cs - the Bytes object to internnet.openhft.chronicle.core.io.IORuntimeException - If an I/O error occursNullPointerException - if cs is nullBufferUnderflowException - If there is not enough data in the buffernet.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 wayIllegalStateExceptionpublic T intern(@NotNull @NotNull BytesStore<?,?> cs) throws net.openhft.chronicle.core.io.IORuntimeException, BufferUnderflowException, IllegalStateException
cs - the BytesStore object to internNullPointerException - if cs is nullnet.openhft.chronicle.core.io.IORuntimeException - If an I/O error occursBufferUnderflowException - If there is not enough data in the buffernet.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 wayIllegalStateExceptionpublic T intern(@NotNull @NotNull Bytes<?> cs, int length) throws net.openhft.chronicle.core.io.IORuntimeException, BufferUnderflowException, IllegalStateException
cs - the Bytes object to internlength - the length of the Bytes object to internNullPointerException - if cs is nullnet.openhft.chronicle.core.io.IORuntimeException - If an I/O error occursBufferUnderflowException - If there is not enough data in the buffernet.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 wayIllegalStateExceptionpublic T intern(@NotNull @NotNull BytesStore<?,?> cs, int length) throws net.openhft.chronicle.core.io.IORuntimeException, BufferUnderflowException, IllegalStateException
cs - the Bytes to internlength - number of bytes to read from csNullPointerException - if cs is nullnet.openhft.chronicle.core.io.IORuntimeException - If an I/O error occursBufferUnderflowException - If there is not enough data in the buffernet.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@NotNull protected abstract T getValue(BytesStore<?,?> bs, int length) throws net.openhft.chronicle.core.io.IORuntimeException, IllegalStateException, BufferUnderflowException
bs into an instance of T. The
implementation must read exactly length bytes starting from
bs.readPosition() without modifying that position.bs - the bytes store supplying the datalength - the number of bytes to readnet.openhft.chronicle.core.io.IORuntimeException - if an I/O error occursBufferUnderflowException - if there is insufficient data availablenet.openhft.chronicle.core.io.ClosedIllegalStateException - if the resource has been released or closednet.openhft.chronicle.core.io.ThreadingIllegalStateException - if accessed by multiple threads unsafelyIllegalStateExceptionprotected boolean toggle()
public int valueCount()
Copyright © 2026 Chronicle Software Ltd. All rights reserved.