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<T,V> getter, BiConsumer<T,V> setter)
      Adds a required field to the codec.
      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
    • field

      public <V> SequentialObjectCodec.Builder<T> field(String name, Codec<V> codec, Function<T,V> getter, BiConsumer<T,V> setter, Predicate<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).

      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<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).

      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