Interface FlyweightAccessor
public interface FlyweightAccessor
Defines the contract for a flyweight object.
A flyweight is a lightweight Java object that does not hold data itself. Instead, it wraps a
MemorySegment and provides structured accessor methods (getters/setters) that read from
and write to the underlying memory at specific offsets. This enables zero-copy, object-oriented
data manipulation.
-
Method Summary
Modifier and TypeMethodDescriptionintbyteSize()Returns the total size in bytes of the data structure this flyweight represents.booleanChecks if this flyweight is currently wrapped around a valid segment.segment()Returns the underlying MemorySegment this flyweight is currently pointing to.voidvalidate()Validates that this flyweight is in a valid state (has a non-null segment).voidwrap(MemorySegment segment, long offset) Wraps a memory segment at a specific offset to make this flyweight point to the underlying data.voidwriteTo(BinaryWriter writer) Writes the data represented by this flyweight to the given writer.
-
Method Details
-
wrap
Wraps a memory segment at a specific offset to make this flyweight point to the underlying data. This is the core method for reusing the flyweight object.- Parameters:
segment- The memory segment containing the structured data.offset- The starting position of the data for this flyweight within the segment.
-
segment
MemorySegment segment()Returns the underlying MemorySegment this flyweight is currently pointing to.- Returns:
- The MemorySegment, or null if not wrapped.
-
byteSize
int byteSize()Returns the total size in bytes of the data structure this flyweight represents. This is also known as the "block length".- Returns:
- The size of the structure in bytes.
-
writeTo
Writes the data represented by this flyweight to the given writer.- Parameters:
writer- The writer to serialize the data into.
-
isWrapped
boolean isWrapped()Checks if this flyweight is currently wrapped around a valid segment.- Returns:
- true if wrapped with a non-null segment, false otherwise.
-
validate
void validate()Validates that this flyweight is in a valid state (has a non-null segment).- Throws:
IllegalStateException- if the flyweight is not wrapped.
-