Class TripleCodec<A,B,C>

java.lang.Object
io.bytestreams.codec.core.TripleCodec<A,B,C>
Type Parameters:
A - the type of the first value
B - the type of the second value
C - 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 Details

    • encode

      public EncodeResult encode(Triple<A,B,C> value, OutputStream output) throws IOException
      Description copied from interface: Codec
      Encodes the given value and writes it to the output stream.
      Specified by:
      encode in interface Codec<A>
      Parameters:
      value - the value to encode
      output - 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

      public Triple<A,B,C> decode(InputStream input) throws IOException
      Description copied from interface: Codec
      Decodes a value from the input stream.
      Specified by:
      decode in interface Codec<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

      public Object inspect(Triple<A,B,C> value)
      Description copied from interface: Inspectable
      Returns a structured representation of the given value.
      Specified by:
      inspect in interface Inspectable<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 values
      getFirst - extracts the first value from the domain type
      getSecond - extracts the second value from the domain type
      getThird - extracts the third value from the domain type
      Returns:
      a codec for the domain type
      Throws:
      NullPointerException - if any argument is null