Class DefaultCasterChain<T,S extends io.fluxzero.common.api.SerializedObject<T>>

java.lang.Object
io.fluxzero.sdk.common.serialization.casting.DefaultCasterChain<T,S>
Type Parameters:
T - the data type of the serialized value (e.g. byte[] or JsonNode)
S - the serialized object wrapper containing the data
All Implemented Interfaces:
Caster<S,S>, CasterChain<S,S>

public class DefaultCasterChain<T,S extends io.fluxzero.common.api.SerializedObject<T>> extends Object implements CasterChain<S,S>
Default implementation of the CasterChain interface used for managing and applying casting operations— typically upcasting or downcasting—on SerializedObject instances.

This class supports flexible runtime registration and execution of type transformers based on annotations such as Upcast and Downcast. It allows the system to evolve serialized object schemas over time while maintaining backward and forward compatibility.

Key Responsibilities

  • Applies registered caster functions based on a combination of object type and revision.
  • Supports both upcasting (increasing version) and downcasting (lowering version) based on configuration.
  • Provides utility methods to create upcasters and downcasters using optional format conversion logic.
  • Allows chaining via intercept() for use with ConvertingSerializedObject wrappers.

Usage


 CasterChain<SerializedObject<byte[]>, SerializedObject<?>> upcaster =
     DefaultCasterChain.createUpcaster(candidates, JsonNode.class);

 Stream<SerializedObject<byte[]>> transformed = upcaster.cast(inputStream, latestRevision);
 

Design Notes

  • Backed by a Map<DataRevision, AnnotatedCaster<T>> which ensures casting operations are deterministic.
  • When multiple methods would match the same revision, an exception is thrown to prevent ambiguous resolution.
  • Supports optional format conversion using a Converter via DefaultCasterChain.ConvertingSerializedObject wrapper.
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected static class 
    A SerializedObject wrapper that applies a Converter to translate data from one format to another.
    protected static class 
    Represents a unique combination of serialized object type and revision, used as a key in the caster registry.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    DefaultCasterChain(Collection<?> casterCandidates, Class<T> dataType, boolean down)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    cast(Stream<? extends S> input, Integer desiredRevision)
    Casts the given stream of input values to the output format, optionally considering a target revision.
    protected static <BEFORE, INTERNAL>
    CasterChain<io.fluxzero.common.api.SerializedObject<BEFORE>,io.fluxzero.common.api.SerializedObject<?>>
    create(Collection<?> casterCandidates, io.fluxzero.common.serialization.Converter<BEFORE,INTERNAL> converter, boolean down)
     
    protected static <T, S extends io.fluxzero.common.api.SerializedObject<T>>
    CasterChain<S,S>
    create(Collection<?> casterCandidates, Class<T> dataType, boolean down)
     
    static <T, S extends io.fluxzero.common.api.SerializedObject<T>>
    CasterChain<S,S>
    createDowncaster(Collection<?> casterCandidates, Class<T> dataType)
     
    static <BEFORE, INTERNAL>
    CasterChain<io.fluxzero.common.api.SerializedObject<BEFORE>,io.fluxzero.common.api.SerializedObject<?>>
    createUpcaster(Collection<?> casterCandidates, io.fluxzero.common.serialization.Converter<BEFORE,INTERNAL> converter)
     
    static <T, S extends io.fluxzero.common.api.SerializedObject<T>>
    CasterChain<S,S>
    createUpcaster(Collection<?> casterCandidates, Class<T> dataType)
     
    io.fluxzero.common.Registration
    Registers one or more objects that may contain casting logic (e.g. annotated methods or implementations).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface io.fluxzero.sdk.common.serialization.casting.Caster

    cast

    Methods inherited from interface io.fluxzero.sdk.common.serialization.casting.CasterChain

    intercept
  • Constructor Details

    • DefaultCasterChain

      protected DefaultCasterChain(Collection<?> casterCandidates, Class<T> dataType, boolean down)
  • Method Details

    • createUpcaster

      public static <BEFORE, INTERNAL> CasterChain<io.fluxzero.common.api.SerializedObject<BEFORE>,io.fluxzero.common.api.SerializedObject<?>> createUpcaster(Collection<?> casterCandidates, io.fluxzero.common.serialization.Converter<BEFORE,INTERNAL> converter)
    • createUpcaster

      public static <T, S extends io.fluxzero.common.api.SerializedObject<T>> CasterChain<S,S> createUpcaster(Collection<?> casterCandidates, Class<T> dataType)
    • createDowncaster

      public static <T, S extends io.fluxzero.common.api.SerializedObject<T>> CasterChain<S,S> createDowncaster(Collection<?> casterCandidates, Class<T> dataType)
    • create

      protected static <BEFORE, INTERNAL> CasterChain<io.fluxzero.common.api.SerializedObject<BEFORE>,io.fluxzero.common.api.SerializedObject<?>> create(Collection<?> casterCandidates, io.fluxzero.common.serialization.Converter<BEFORE,INTERNAL> converter, boolean down)
    • create

      protected static <T, S extends io.fluxzero.common.api.SerializedObject<T>> CasterChain<S,S> create(Collection<?> casterCandidates, Class<T> dataType, boolean down)
    • registerCasterCandidates

      public io.fluxzero.common.Registration registerCasterCandidates(Object... candidates)
      Description copied from interface: CasterChain
      Registers one or more objects that may contain casting logic (e.g. annotated methods or implementations). These candidates are inspected and included into the chain if applicable.
      Specified by:
      registerCasterCandidates in interface CasterChain<T,S extends io.fluxzero.common.api.SerializedObject<T>>
      Parameters:
      candidates - one or more caster providers
      Returns:
      a Registration that can be used to remove the registered casters
    • cast

      public Stream<S> cast(Stream<? extends S> input, Integer desiredRevision)
      Description copied from interface: Caster
      Casts the given stream of input values to the output format, optionally considering a target revision.
      Specified by:
      cast in interface Caster<T,S extends io.fluxzero.common.api.SerializedObject<T>>
      Parameters:
      input - the input stream of values
      desiredRevision - the target revision number (nullable)
      Returns:
      a stream of transformed output values