Class AdvancedEnumSerializer<E extends Enum<E>>

java.lang.Object
com.mrivanplays.annotationconfig.core.serialization.AdvancedEnumSerializer<E>
Type Parameters:
E - enum class
All Implemented Interfaces:
FieldTypeSerializer<E>

public final class AdvancedEnumSerializer<E extends Enum<E>> extends Object implements FieldTypeSerializer<E>
Enum serializer, which uses different tricks to serialize enum constants to user-friendly values for configs, and also to read them properly.

Lets be real, every developer tried or is going to try to serialize enum constants, so they are more user-friendly to be configured. Yet everyone who tried wasn't really able to go farther than accepting this input: enum_constant for an enum constant of ENUM_CONSTANT. As already mentioned, this serializer uses tricks in order to accept literally every output possible. Say you have an enum constant of ACCEPT_ONLY_TRUE. This serializer serializes this constant as accept only true, but upon deserialization, the serializer accepts literally any possible way of combining these words. This basically means it will accept only accept true, accept true only, etc., and of course the input as a regular enum constant e.g. ACCEPT_ONLY_TRUE and accept_only_true.

Example: Say you have an enum of:


 public enum Option {
   ACCEPT_TRUE,
   ACCEPT_FALSE,
   ACCEPT_BOTH,
   DO_NOT_ACCEPT_TRUE,
   DO_NOT_ACCEPT_FALSE,
   DO_NOT_ACCEPT_BOTH
 }
 
and you want to use AdvancedEnumSerializer to serialize it, you just register it using the SerializerRegistry. An example of this is:

 SerializerRegistry.INSTANCE.registerSerializer(Option.class, AdvancedEnumSerializer.forEnum(Option.class));
 
Since:
2.0.0
Author:
MrIvanPlays
  • Field Details

  • Method Details

    • forEnum

      public static <E extends Enum<E>> AdvancedEnumSerializer<E> forEnum(Class<E> enumClass)
      Creates a new AdvancedEnumSerializer for the specified enumClass
      Type Parameters:
      E - enum class
      Parameters:
      enumClass - the enum class you want an advanced enum serializer
      Returns:
      advanced enum serializer
      Throws:
      NullPointerException - if null enumClass specified
      IllegalArgumentException - if specified enumClass is not an Enum
    • forEnumWithCondition

      public static <E extends Enum<E>> AdvancedEnumSerializer<E> forEnumWithCondition(Class<E> enumClass, BiPredicate<Integer,List<String>> matchesCondition)
      Creates a new AdvancedEnumSerializer for the specified enumClass with the specified matchesCondition.

      The matches condition is used when the deserializer does a last effort to try and return an enum value from the specified input.

      Type Parameters:
      E - enum class
      Parameters:
      enumClass - the enum class you want an advanced enum serializer
      matchesCondition - matches condition
      Returns:
      advanced enum serializer
      Throws:
      NullPointerException - if null enumClass or null matchesCondition specified
      IllegalArgumentException - if the specified enumClass is not an Enum
    • deserialize

      public E deserialize(DataObject data, Field field)
      AnnotationConfig invokes this call-back method during deserialization when it encounters a field of the specified type.
      Specified by:
      deserialize in interface FieldTypeSerializer<E extends Enum<E>>
      Parameters:
      data - the data we received from the config
      field - the field we will attach this information to later on
      Returns:
      the generic value, the implementation of this interface has specified
    • serialize

      public DataObject serialize(E value, Field field)
      AnnotationConfig invokes this call-back method during serialization when it encounters a field of the specified type.
      Specified by:
      serialize in interface FieldTypeSerializer<E extends Enum<E>>
      Parameters:
      value - the data we need serialized
      field - the field we got the value from
      Returns:
      a serialized object which is useful for dumping into a configuration file