Class BaseMapperOperator

java.lang.Object
me.a8kj.config.template.experimental.mapper.BaseMapperOperator
All Implemented Interfaces:
CodecRegistry, EntityLoader, EntitySaver, MapperOperatorContract
Direct Known Subclasses:
DefaultMapperOperator

public abstract class BaseMapperOperator extends Object implements MapperOperatorContract
An abstract base implementation of MapperOperatorContract that handles codec registration and provides core logic for entity serialization and deserialization. *

Note: This is an experimental feature and is currently considered unstable. This class facilitates the conversion between domain entities and flattened memory structures by orchestrating the interaction between CodecAdapter and MapStructureUtils.

*

The class uses a thread-safe registry to store codecs and supports polymorphic codec lookups using type compatibility checks.

Since:
0.3
Author:
a8kj7sea
  • Constructor Details

    • BaseMapperOperator

      public BaseMapperOperator()
  • Method Details

    • registerCodec

      public <T> void registerCodec(Class<T> type, CodecAdapter<T,Map<String,Object>> codec)
      Registers a codec for a specific class type.

      Once registered, the mapping system will use this codec whenever it encounters the specified type during serialization (saving) or deserialization (loading).

      Registers a codec in the internal thread-safe map for future use in load/save operations.

      Specified by:
      registerCodec in interface CodecRegistry
      Type Parameters:
      T - the domain object type.
      Parameters:
      type - the class literal of the type to associate with the codec.
      codec - the CodecAdapter containing the serialization and deserialization logic.
    • findCodec

      protected <T> CodecAdapter<T,Map<String,Object>> findCodec(Class<T> type)
      Attempts to find a suitable codec for the specified class type.

      This method searches for a codec registered for the exact type or a supertype (via Class.isAssignableFrom(Class)).

      Type Parameters:
      T - the entity type.
      Parameters:
      type - the class literal to lookup.
      Returns:
      the associated CodecAdapter, or null if no compatible codec is registered.
    • load

      public <T> Optional<T> load(DataMemory<String> memory, String path, Class<T> type)
      Loads and reconstructs an entity from the provided DataMemory based on a specific path.

      This implementation unflattens the data from the memory into a Map structure before passing it to the appropriate codec for deserialization.

      Specified by:
      load in interface EntityLoader
      Type Parameters:
      T - the expected type of the reconstructed entity.
      Parameters:
      memory - the source memory implementation containing the flattened data.
      path - the base configuration path (e.g., "database.mysql") where the entity's data is stored.
      type - the class literal of the entity to be reconstructed.
      Returns:
      an Optional containing the reconstructed entity, or Optional.empty() if the data is missing or incompatible.
    • save

      public <T> void save(DataMemory<String> memory, String path, T entity)
      Serializes and saves an entity into the provided DataMemory under a specific path.

      This implementation serializes the entity into a Map using its registered codec and then flattens that Map into the provided memory at the specified path.

      Specified by:
      save in interface EntitySaver
      Type Parameters:
      T - the type of the entity being saved.
      Parameters:
      memory - the target memory implementation where data will be stored.
      path - the base configuration path (e.g., "database.mysql") under which the entity's fields will be flattened.
      entity - the domain object instance to persist.