Class SequentialObjectCodec.Builder<T>
- Enclosing class:
SequentialObjectCodec<T>
-
Method Summary
Modifier and TypeMethodDescriptionbuild()Builds the SequentialObjectCodec.Adds a fixed-value field that is present on the wire but not stored on the object.Adds a fixed-value field that is written on encode but matched loosely on decode.Adds a field to the codec using a FieldSpec.Adds a required field to the codec.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.
-
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 valuegetter- function to extract the field value for encodingsetter- consumer to set the field value when decoding- Returns:
- this builder
-
constant
Adds a fixed-value field that is present on the wire but not stored on the object.Writes
valueon 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
Vcannot 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 formatvalue- 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.
valueis always what gets written.valuemust itself satisfyaccepts, 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 formatvalue- the value written on encodeaccepts- the condition a decoded value must satisfy- Returns:
- this builder
- Throws:
NullPointerException- if any argument is nullIllegalArgumentException- ifvaluedoes not satisfyaccepts
-
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 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).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
Builds the SequentialObjectCodec.- Returns:
- the constructed codec
- Throws:
IllegalArgumentException- if no fields were added
-