Class DefaultCasterChain<T, S extends 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 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:
  • Constructor Details

    • DefaultCasterChain

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

    • createUpcaster

      public static <BEFORE,INTERNAL> CasterChain<SerializedObject<BEFORE>, SerializedObject<?>> createUpcaster(Collection<?> casterCandidates, Converter<BEFORE,INTERNAL> converter)
    • createUpcaster

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

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

      protected static <BEFORE,INTERNAL> CasterChain<SerializedObject<BEFORE>, SerializedObject<?>> create(Collection<?> casterCandidates, Converter<BEFORE,INTERNAL> converter, boolean down)
    • create

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

      public 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 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 SerializedObject<T>>
      Parameters:
      input - the input stream of values
      desiredRevision - the target revision number (nullable)
      Returns:
      a stream of transformed output values
    • castFirstOrNull

      public S castFirstOrNull(S input, Integer desiredRevision)
      Description copied from interface: CasterChain
      Casts a single input and returns the first resulting output, or null if the input is dropped.

      This is equivalent to cast(Stream.of(input), rev).findAny().orElse(null), but implementations may optimize the common no-caster path without constructing a stream pipeline.

      Specified by:
      castFirstOrNull in interface CasterChain<T, S extends SerializedObject<T>>
      Parameters:
      input - the input value
      desiredRevision - the target revision number (nullable)
      Returns:
      the first casted result, or null when no result is produced