Class SequentialObjectCodec.Builder<T>

java.lang.Object
io.bytestreams.codec.core.SequentialObjectCodec.Builder<T>
Enclosing class:
SequentialObjectCodec<T>

public static class SequentialObjectCodec.Builder<T> extends Object
Builder for constructing a SequentialObjectCodec.
  • Method Details

    • field

      public <V> SequentialObjectCodec.Builder<T> field(String name, Codec<V> codec, Function<? super T, V> getter, BiConsumer<? super T, V> setter)
      Adds a required field to the codec.

      The getter and setter may be declared on a supertype of T.

      Type Parameters:
      V - the field value type
      Parameters:
      name - the field name (used in error messages)
      codec - the codec for this field's value
      getter - function to extract the field value for encoding
      setter - consumer to set the field value when decoding
      Returns:
      this builder
    • constant

      public <V> SequentialObjectCodec.Builder<T> constant(String name, Codec<V> codec, V value)
      Adds a fixed-value field that is present on the wire but not stored on the object.

      Writes value on encode. On decode, reads a value, verifies it matches, and discards it. Intended for record type indicators, magic numbers and protocol version markers — values the format requires but the domain object has no reason to carry.

      Codecs.<BatchTrailer>sequential(BatchTrailer::new)
          .constant("type", Codecs.ascii(2), "BT")
          .field("count", Codecs.uint16(), BatchTrailer::getCount, BatchTrailer::setCount)
          .build();
      

      Comparison is by content, so byte[] constants behave as expected and are reported as hex on mismatch. A failure carries the field path like any other decode error: field [batch.trailer.type]: expected constant [BT] but got [XX].

      The constant is held by reference and never copied, since an arbitrary V cannot be. Mutating it after the codec is built changes what the codec writes and accepts, so pass a value nothing else will modify.

      Type Parameters:
      V - the constant's value type
      Parameters:
      name - the field name (used in error messages)
      codec - the codec for the constant's wire format
      value - the expected constant value
      Returns:
      this builder
      Throws:
      NullPointerException - if any argument is null
    • constant

      public <V> SequentialObjectCodec.Builder<T> constant(String name, Codec<V> codec, V value, Predicate<V> accepts)
      Adds a fixed-value field that is written on encode but matched loosely on decode.

      Use when the wire format tolerates variants of the same constant, such as a fixed-width type indicator that may or may not be padded. value is always what gets written.

      value must itself satisfy accepts, since it is checked on encode like any other value. This is verified here rather than left to fail at encode time.

      Type Parameters:
      V - the constant's value type
      Parameters:
      name - the field name (used in error messages)
      codec - the codec for the constant's wire format
      value - the value written on encode
      accepts - the condition a decoded value must satisfy
      Returns:
      this builder
      Throws:
      NullPointerException - if any argument is null
      IllegalArgumentException - if value does not satisfy accepts
    • field

      public <V> SequentialObjectCodec.Builder<T> field(String name, Codec<V> codec, Function<? super T, V> getter, BiConsumer<? super T, V> setter, Predicate<? super T> presence)
      Adds a field to the codec with a presence predicate.

      The presence predicate determines whether the field should be encoded/decoded. If the predicate returns false, the field is skipped. Note that during decoding, the predicate can only reference fields that have already been decoded (earlier in the field order).

      The getter, setter and presence predicate may be declared on a supertype of T.

      Type Parameters:
      V - the field value type
      Parameters:
      name - the field name (used in error messages)
      codec - the codec for this field's value
      getter - function to extract the field value for encoding
      setter - consumer to set the field value when decoding
      presence - predicate to determine if field is present
      Returns:
      this builder
    • field

      public <V> SequentialObjectCodec.Builder<T> field(FieldSpec<? super T, V> spec)
      Adds a field to the codec using a FieldSpec.

      The field's presence is determined by FieldSpec.presence(), which defaults to always true (required field).

      The spec may be declared on a supertype of T, so a spec written for a base class can be reused by the codec of any subclass.

      Type Parameters:
      V - the field value type
      Parameters:
      spec - the field specification
      Returns:
      this builder
    • build

      public SequentialObjectCodec<T> build()
      Builds the SequentialObjectCodec.
      Returns:
      the constructed codec
      Throws:
      IllegalArgumentException - if no fields were added