Class SimpleEncodingRegistry

java.lang.Object
io.permazen.encoding.SimpleEncodingRegistry
All Implemented Interfaces:
EncodingRegistry
Direct Known Subclasses:
DefaultEncodingRegistry

public class SimpleEncodingRegistry extends Object implements EncodingRegistry
A straightforward EncodingRegistry implementation that creates object array types on demand.

The add() method only accepts non-array types and primitive array types. All other array types are automatically created on demand via buildArrayEncoding().

  • Field Details

    • log

      protected final Logger log
  • Constructor Details

    • SimpleEncodingRegistry

      public SimpleEncodingRegistry()
  • Method Details

    • add

      public boolean add(Encoding<?> encoding)
      Add a new Encoding to this registry.

      The type's encoding ID must not contain any array dimensions except for single dimension primitive array types.

      Parameters:
      encoding - the Encoding to registre
      Returns:
      true if it was added, false if it was already registered
      Throws:
      IllegalArgumentException - if encoding is null
      IllegalArgumentException - if encoding's encoding ID is null (i.e., encoding is anonymous)
      IllegalArgumentException - if encoding's encoding ID conflicts with an existing, but different, encoding
      IllegalArgumentException - if encoding's encoding ID has one or more array dimensions
    • buildArrayEncoding

      public static <E> Encoding<E[]> buildArrayEncoding(Encoding<E> elementEncoding)
      Build an array encoding for the given element encoding using Permazen's default array encoding.

      The element encoding must represent a non-primitive type. This method uses the generic array encoding provided by ObjectArrayEncoding, wrapped via NullSafeEncoding.

      If elementEncoding is anonymous, so will be the returned encoding, otherwise the returned encoding's EncodingId will be equal to elementEncoding.getArrayId().

      Parameters:
      elementEncoding - element encoding
      Returns:
      corresponding array encoding
      Throws:
      IllegalArgumentException - if elementEncoding is null
      IllegalArgumentException - if elementEncoding encodes a primitive type
      IllegalArgumentException - if elementEncoding encodes an array type with 255 dimensions
    • getEncoding

      public Encoding<?> getEncoding(EncodingId encodingId)
      Description copied from interface: EncodingRegistry
      Get the Encoding with the given encoding ID in this registry.
      Specified by:
      getEncoding in interface EncodingRegistry
      Parameters:
      encodingId - encoding ID
      Returns:
      corresponding Encoding, if any, otherwise null
    • getEncodings

      public <T> List<Encoding<T>> getEncodings(TypeToken<T> typeToken)
      Description copied from interface: EncodingRegistry
      Get all of the Encodings in this registry that supports values of the given Java type.

      The Java type must exactly match the Encoding's supported Java type.

      Specified by:
      getEncodings in interface EncodingRegistry
      Type Parameters:
      T - field value type
      Parameters:
      typeToken - field value type
      Returns:
      unmodifiable list of Encodings supporting Java values of type typeToken, possibly empty
    • register

      protected void register(EncodingId encodingId, Encoding<?> encoding)
      Register a new Encoding.
      Parameters:
      encodingId - encoding ID under which to register the new encoding
      encoding - the new encoding to register
      Throws:
      IllegalArgumentException - if either parameter is null
      IllegalArgumentException - if there is already a encoding registered under encodingId
      IllegalArgumentException - if encoding is anonymous or has an encoding ID different from encodingId