Interface Codec<T,R>

Type Parameters:
T - domain type to encode and decode
R - representation type (serialized form)
All Known Implementing Classes:
PairSeqCodec

public interface Codec<T,R>
General-purpose bidirectional codec between a domain type T and a serialized representation R.

A Codec provides two complementary operations:

  • marshal(Object) encodes a domain object into its representation (e.g., object → string, object → byte array).
  • unmarshal(Object) decodes a representation back into the domain object.

Design notes

  • The type parameters T and R are application-defined; for example, Codec<MyMessage, byte[]> or Codec<Key, String>.
  • Codecs are expected to be symmetric: unmarshalling the result of marshalling yields an equivalent domain object, subject to lossiness in the chosen representation.
  • Implementations should document whether null values are permitted, and whether marshalling is deterministic.

Example

Codec<Integer, String> intToString = new Codec<>() {
    public String marshal(Integer value) { return value.toString(); }
    public Integer unmarshal(String repr) { return Integer.parseInt(repr); }
};

String repr = intToString.marshal(42);   // "42"
int val = intToString.unmarshal("42");   // 42
  • Method Summary

    Modifier and Type
    Method
    Description
    marshal(T value)
    Converts a domain value into its serialized representation.
    unmarshal(R repr)
    Converts a serialized representation back into the domain object.
  • Method Details

    • marshal

      R marshal(T value)
      Converts a domain value into its serialized representation.
      Parameters:
      value - domain object to encode
      Returns:
      representation corresponding to value
      Throws:
      IllegalArgumentException - if value is invalid or cannot be encoded
    • unmarshal

      T unmarshal(R repr)
      Converts a serialized representation back into the domain object.
      Parameters:
      repr - representation to decode
      Returns:
      domain object reconstructed from repr
      Throws:
      IllegalArgumentException - if repr is malformed or cannot be decoded