Interface Codec<T,R>
- Type Parameters:
T- domain type to encode and decodeR- 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
TandRare application-defined; for example,Codec<MyMessage, byte[]>orCodec<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
nullvalues 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
-
Method Details
-
marshal
Converts a domain value into its serialized representation.- Parameters:
value- domain object to encode- Returns:
- representation corresponding to
value - Throws:
IllegalArgumentException- ifvalueis invalid or cannot be encoded
-
unmarshal
Converts a serialized representation back into the domain object.- Parameters:
repr- representation to decode- Returns:
- domain object reconstructed from
repr - Throws:
IllegalArgumentException- ifrepris malformed or cannot be decoded
-