Class DataObject

java.lang.Object
io.bytestreams.codec.core.DataObject
Direct Known Subclasses:
SimpleData

public abstract class DataObject extends Object
Abstract map-backed data object for use with sequential codecs.

Fields are stored by string key in insertion order. Access is protected — subclasses control which fields are exposed and whether they are mutable.

Use field(String, Codec) to create FieldSpec instances that provide type-safe access and integrate with SequentialObjectCodec.Builder.

For open access with no restrictions, use SimpleData. For typed, controlled access, subclass directly:


 class Message extends DataObject {
   static final FieldSpec<Message, Integer> ID = field("id", Codecs.uint16());

   public int getId() { return get(ID); }
   // no setter — read-only to consumers, codec can still set via FieldSpec
 }
 
  • Constructor Details

    • DataObject

      public DataObject()
  • Method Details

    • get

      protected <V> V get(String key)
    • set

      protected void set(String key, Object value)
    • get

      protected <V> V get(FieldSpec<? extends DataObject,V> spec)
      Gets a field value using a FieldSpec. Delegates to FieldSpec.get(Object).
    • set

      protected <V> void set(FieldSpec<? extends DataObject,V> spec, V value)
      Sets a field value using a FieldSpec. Delegates to FieldSpec.set(Object, Object).
    • field

      public static <T extends DataObject, V> FieldSpec<T,V> field(String name, Codec<V> codec)
      Creates a FieldSpec backed by the object's internal map.
      Type Parameters:
      T - the DataObject subclass type
      V - the field value type
      Parameters:
      name - the field name (used as map key)
      codec - the codec for the field value
      Returns:
      a new FieldSpec
    • field

      public static <T extends DataObject, V> FieldSpec<T,V> field(String name, Codec<V> codec, Predicate<T> presence)
      Creates a FieldSpec backed by the object's internal map with a presence predicate.
      Type Parameters:
      T - the DataObject subclass type
      V - the field value type
      Parameters:
      name - the field name (used as map key)
      codec - the codec for the field value
      presence - predicate to determine if field is present
      Returns:
      a new FieldSpec
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object