Package io.bytestreams.codec.core
Class SequentialObjectCodec.Builder<T>
java.lang.Object
io.bytestreams.codec.core.SequentialObjectCodec.Builder<T>
- Enclosing class:
SequentialObjectCodec<T>
Builder for constructing a SequentialObjectCodec.
-
Method Summary
Modifier and TypeMethodDescriptionbuild()Builds the SequentialObjectCodec.Adds a field to the codec using a FieldSpec.Adds a required field to the codec.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.
-
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 valuegetter- function to extract the field value for encodingsetter- 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 valuegetter- function to extract the field value for encodingsetter- consumer to set the field value when decodingpresence- predicate to determine if field is present- Returns:
- this builder
-
field
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
Builds the SequentialObjectCodec.- Returns:
- the constructed codec
- Throws:
IllegalArgumentException- if no fields were added
-