Class PairSeqCodec<T>

java.lang.Object
zeroecho.core.marshal.PairSeqCodec<T>
Type Parameters:
T - domain type that follows the marshalling and unmarshalling conventions
All Implemented Interfaces:
Codec<T,PairSeq>

public final class PairSeqCodec<T> extends Object implements Codec<T,PairSeq>
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() returning PairSeq.
  • Either a public static factory unmarshal(PairSeq) or a public constructor T(PairSeq).

Conventions

  • Marshalling: calls value.marshal() and requires its return type to be PairSeq.
  • Unmarshalling: prefers a public static unmarshal(PairSeq) on T; if absent, falls back to a public constructor T(PairSeq).
  • Failures in lookup or invocation are reported as IllegalStateException with 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-safe ClassValue caches do not retain otherwise unreachable class loaders.
Since:
1.0
  • Constructor Details

    • PairSeqCodec

      public PairSeqCodec(Class<T> type)
      Creates a codec bound to a specific domain type.
      Parameters:
      type - domain class that provides marshal() and either static unmarshal(PairSeq) or T(PairSeq)
      Throws:
      NullPointerException - if type is null
  • Method Details

    • marshal

      public PairSeq marshal(T value)
      Encodes a domain object to PairSeq by invoking its marshal() method.

      The domain object's marshal() method must be public (or otherwise accessible) and return PairSeq. Any exception thrown by the target method is wrapped in IllegalStateException.

      Specified by:
      marshal in interface Codec<T,PairSeq>
      Parameters:
      value - domain object to encode
      Returns:
      the PairSeq produced by value.marshal()
      Throws:
      NullPointerException - if value is null
      IllegalStateException - if no marshal() method exists, if it does not return PairSeq, or if the invocation fails
    • unmarshal

      public T unmarshal(PairSeq repr)
      Decodes a PairSeq back into a domain object by convention.

      Resolution order:

      1. Invoke a public static unmarshal(PairSeq) on type, if present.
      2. Otherwise, invoke a public constructor type(PairSeq).

      If neither entrypoint is available, or invocation fails, an IllegalStateException is thrown with the original cause attached for diagnostics.

      Specified by:
      unmarshal in interface Codec<T,PairSeq>
      Parameters:
      repr - serialized representation to decode
      Returns:
      reconstructed domain object
      Throws:
      NullPointerException - if repr is null
      IllegalStateException - if neither a suitable static factory nor constructor exists, or if either invocation fails