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 Type
    Method
    Description
    int
    Returns the total size in bytes of the data structure this flyweight represents.
    boolean
    Checks if this flyweight is currently wrapped around a valid segment.
    Returns the underlying MemorySegment this flyweight is currently pointing to.
    void
    Validates that this flyweight is in a valid state (has a non-null segment).
    void
    wrap(MemorySegment segment, long offset)
    Wraps a memory segment at a specific offset to make this flyweight point to the underlying data.
    void
    Writes the data represented by this flyweight to the given writer.
  • Method Details

    • wrap

      void wrap(MemorySegment segment, long offset)
      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

      void writeTo(BinaryWriter writer)
      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.