Class CompositeEncoder<To>

java.lang.Object
io.inverno.mod.base.converter.CompositeEncoder<To>
Type Parameters:
To - the encoded type
All Implemented Interfaces:
Encoder<Object,To>

public class CompositeEncoder<To> extends Object implements Encoder<Object,To>

A composite encoder relies on multiple CompoundEncoder to encode objects.

Such implementation makes it possible to create extensible encoder able to encode various type of objects by composition of many specific compound encoders.

Since:
1.0
Author:
Jeremy Kuhn
See Also:
  • Constructor Details

    • CompositeEncoder

      public CompositeEncoder()

      Creates a composite encoder.

  • Method Details

    • setEncoders

      public void setEncoders(List<CompoundEncoder<?,To>> encoders)

      Sets the compound encoders used to encode objects.

      Parameters:
      encoders - a list of compound encoders
    • setDefaultEncoder

      public void setDefaultEncoder(Encoder<Object,To> defaultEncoder)

      Sets a default encoder to use when no compound encoder is able to encode an object.

      Parameters:
      defaultEncoder - the default decoder
    • getEncoder

      protected <T> CompoundEncoder<T,To> getEncoder(Type type) throws EncoderNotFoundException

      Returns the first compound encoder that can encode the specified type.

      Type Parameters:
      T - the type of object encoded by the returned encoder
      Parameters:
      type - the type to encode
      Returns:
      a compound encoder
      Throws:
      EncoderNotFoundException - if no encoder can encode the specified type
    • encode

      public <T> To encode(T value) throws EncoderNotFoundException
      Description copied from interface: Encoder

      Encodes the specified value to the encoded type.

      Specified by:
      encode in interface Encoder<Object,To>
      Type Parameters:
      T - the type of the decoded object
      Parameters:
      value - the object to encode
      Returns:
      an encoded object
      Throws:
      EncoderNotFoundException
    • encode

      public <T> To encode(T value, Class<T> type) throws ConverterException
      Description copied from interface: Encoder

      Encodes the specified value whose type is represented by the specified class to the encoded type.

      Specified by:
      encode in interface Encoder<Object,To>
      Type Parameters:
      T - the type of the decoded object
      Parameters:
      value - the object to encode
      type - the class of the decoded object
      Returns:
      an encoded object
      Throws:
      ConverterException - if there was an error encoding the value
    • encode

      public <T> To encode(T value, Type type) throws ConverterException
      Description copied from interface: Encoder

      Encodes the specified value whose type is the specified type to the encoded type.

      Specified by:
      encode in interface Encoder<Object,To>
      Type Parameters:
      T - the type of the decoded object
      Parameters:
      value - the object to encode
      type - the type of the decoded object
      Returns:
      an encoded object
      Throws:
      ConverterException - if there was an error encoding the value