Package io.bytestreams.codec.core
Class TripleCodec<A,B,C>
java.lang.Object
io.bytestreams.codec.core.TripleCodec<A,B,C>
- Type Parameters:
A- the type of the first valueB- the type of the second valueC- the type of the third value
- All Implemented Interfaces:
Codec<Triple<A,,B, C>> Inspectable<Triple<A,B, C>>
public class TripleCodec<A,B,C>
extends Object
implements Codec<Triple<A,B,C>>, Inspectable<Triple<A,B,C>>
A codec that encodes and decodes a triple of values sequentially.
The wire format is [first][second][third]. Use as(TriFunction, Function, Function, Function) to map the triple to a domain type without exposing the internal
representation.
Example usage:
Codec<Color> codec = Codecs.triple(Codecs.uint8(), Codecs.uint8(), Codecs.uint8())
.as(Color::new, Color::r, Color::g, Color::b);
-
Method Summary
Modifier and TypeMethodDescription<V> Codec<V> as(TriFunction<A, B, C, V> constructor, Function<V, A> getFirst, Function<V, B> getSecond, Function<V, C> getThird) Maps this triple codec to a domain type.decode(InputStream input) Decodes a value from the input stream.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(TriFunction<A, B, C, V> constructor, Function<V, A> getFirst, Function<V, B> getSecond, Function<V, C> getThird) Maps this triple codec to a domain type.- Type Parameters:
V- the domain type- Parameters:
constructor- constructs the domain type from the three valuesgetFirst- extracts the first value from the domain typegetSecond- extracts the second value from the domain typegetThird- extracts the third value from the domain type- Returns:
- a codec for the domain type
- Throws:
NullPointerException- if any argument is null
-