Class PairSeqCodec<T>
java.lang.Object
zeroecho.core.marshal.PairSeqCodec<T>
- Type Parameters:
T- domain type that follows the marshalling and unmarshalling conventions
Reflection-based
Codec that marshals and unmarshals domain objects to
and from PairSeq.
A PairSeqCodec adapts any domain type T to the
PairSeq representation by convention, using reflection. The domain
type must implement one or both of the following:
- Instance method
marshal()returningPairSeq. - Either a public static factory
unmarshal(PairSeq)or a public constructorT(PairSeq).
Conventions
- Marshalling: calls
value.marshal()and requires its return type to bePairSeq. - Unmarshalling: prefers a public static
unmarshal(PairSeq)onT; if absent, falls back to a public constructorT(PairSeq). - Failures in lookup or invocation are reported as
IllegalStateExceptionwith the cause attached.
Example
public final class User {
private final String id;
private final String name;
public User(String id, String name) {
this.id = id; this.name = name;
}
// Required by PairSeqCodec for marshalling:
public PairSeq marshal() {
return PairSeq.of("id", id, "name", name);
}
// Preferred unmarshal entrypoint:
public static User unmarshal(PairSeq ps) {
PairSeq.Cursor c = ps.cursor();
String id = null, name = null;
while (c.next()) {
if ("id".equals(c.key())) id = c.value();
else if ("name".equals(c.key())) name = c.value();
}
return new User(id, name);
}
}
PairSeqCodec<User> codec = new PairSeqCodec<>(User.class);
PairSeq repr = codec.marshal(new User("u-1", "Alice"));
User u = codec.unmarshal(repr);
Thread-safety
Instances are immutable and thread-safe. Public accessors are resolved once per runtime class and operation type, then invoked through cached method handles. The unload-safeClassValue
caches do not retain otherwise unreachable class loaders.- Since:
- 1.0
-
Constructor Summary
ConstructorsConstructorDescriptionPairSeqCodec(Class<T> type) Creates a codec bound to a specific domain type. -
Method Summary
-
Constructor Details
-
PairSeqCodec
Creates a codec bound to a specific domain type.- Parameters:
type- domain class that providesmarshal()and eitherstatic unmarshal(PairSeq)orT(PairSeq)- Throws:
NullPointerException- iftypeisnull
-
-
Method Details
-
marshal
Encodes a domain object toPairSeqby invoking itsmarshal()method.The domain object's
marshal()method must be public (or otherwise accessible) and returnPairSeq. Any exception thrown by the target method is wrapped inIllegalStateException.- Specified by:
marshalin interfaceCodec<T,PairSeq> - Parameters:
value- domain object to encode- Returns:
- the
PairSeqproduced byvalue.marshal() - Throws:
NullPointerException- ifvalueisnullIllegalStateException- if nomarshal()method exists, if it does not returnPairSeq, or if the invocation fails
-
unmarshal
Decodes aPairSeqback into a domain object by convention.Resolution order:
- Invoke a public static
unmarshal(PairSeq)ontype, if present. - Otherwise, invoke a public constructor
type(PairSeq).
If neither entrypoint is available, or invocation fails, an
IllegalStateExceptionis thrown with the original cause attached for diagnostics.- Specified by:
unmarshalin interfaceCodec<T,PairSeq> - Parameters:
repr- serialized representation to decode- Returns:
- reconstructed domain object
- Throws:
NullPointerException- ifreprisnullIllegalStateException- if neither a suitable static factory nor constructor exists, or if either invocation fails
- Invoke a public static
-