Package io.bytestreams.codec.core
Class PairCodec<A,B>
java.lang.Object
io.bytestreams.codec.core.PairCodec<A,B>
- Type Parameters:
A- the type of the first valueB- the type of the second value
- All Implemented Interfaces:
Codec<Pair<A,,B>> Inspectable<Pair<A,B>>
A codec that encodes and decodes a pair of values sequentially.
The wire format is [first][second]. Use as(BiFunction, Function, Function) to
map the pair to a domain type without exposing the internal representation.
Example usage:
Codec<Rectangle> codec = Codecs.pair(Codecs.uint8(), Codecs.uint8())
.as(Rectangle::new, r -> r.width, r -> r.height);
-
Method Summary
Modifier and TypeMethodDescription<V> Codec<V> Maps this pair codec to a domain type.decode(InputStream input) Decodes a value from the input stream.encode(Pair<A, B> value, OutputStream output) Encodes the given value and writes it to the output stream.Returns a structured representation of the given value.
-
Method Details
-
encode
Description copied from interface:CodecEncodes the given value and writes it to the output stream.- Specified by:
encodein interfaceCodec<A>- Parameters:
value- the value to encodeoutput- the output stream to write the encoded bytes to- Returns:
- the encode result containing logical count and bytes written
- Throws:
IOException- if an I/O error occurs during encoding
-
decode
Description copied from interface:CodecDecodes a value from the input stream.- Specified by:
decodein interfaceCodec<A>- Parameters:
input- the input stream to read the encoded bytes from- Returns:
- the decoded value
- Throws:
IOException- if an I/O error occurs during decoding
-
inspect
Description copied from interface:InspectableReturns a structured representation of the given value.- Specified by:
inspectin interfaceInspectable<A>- Parameters:
value- the decoded value to inspect- Returns:
- a structured representation (Map, List, or scalar)
-
as
public <V> Codec<V> as(BiFunction<A, B, V> constructor, Function<V, A> getFirst, Function<V, B> getSecond) Maps this pair codec to a domain type.- Type Parameters:
V- the domain type- Parameters:
constructor- constructs the domain type from the two valuesgetFirst- extracts the first value from the domain typegetSecond- extracts the second value from the domain type- Returns:
- a codec for the domain type
- Throws:
NullPointerException- if any argument is null
-