Class Serializer<I,O>

java.lang.Object
me.hsgamer.hscore.serializer.Serializer<I,O>
Type Parameters:
I - the type of the input object
O - the type of the output object

public class Serializer<I,O> extends Object
The serializer
  • Constructor Details

    • Serializer

      public Serializer()
  • Method Details

    • register

      public <T extends O> Serializer<I,O> register(String type, Class<T> outputClass, Function<I,T> outputFunction, Function<T,I> inputFunction)
      Register a new entry
      Type Parameters:
      T - the type of the output
      Parameters:
      type - the type
      outputClass - the class of the output
      outputFunction - the output function
      inputFunction - the input function
      Returns:
      the serializer
      Throws:
      IllegalArgumentException - if the type or the class is already registered
    • register

      public <T extends O> Serializer<I,O> register(Class<T> outputClass, Function<I,T> outputFunction, Function<T,I> inputFunction)
      Register a new entry, without specifying the type. The type will be the class name of the output, or the value of SerializerType if the class is annotated with it.
      Type Parameters:
      T - the type of the output
      Parameters:
      outputClass - the class of the output
      outputFunction - the output function
      inputFunction - the input function
      Returns:
      the serializer
    • register

      public <T extends O> Serializer<I,O> register(Class<T> outputClass)
      Register a new entry, without specifying the type and the input function. The type will be the class name of the output, or the value of SerializerType if the class is annotated with it. The input function will be the method annotated with SerializerInputFunction in the output class, which must be public and non-static. The output function will be the method annotated with SerializerOutputFunction in the output class, which must be public and static.
      Type Parameters:
      T - the type of the output
      Parameters:
      outputClass - the class of the output
      Returns:
      the serializer
      Throws:
      IllegalArgumentException - if the output class does not have the required methods
    • unregister

      public Serializer<I,O> unregister(String type)
      Unregister an entry
      Parameters:
      type - the type
      Returns:
      the serializer
    • unregister

      public Serializer<I,O> unregister(Class<? extends O> outputClass)
      Unregister an entry
      Parameters:
      outputClass - the class of the output
      Returns:
      the serializer
    • deserialize

      public O deserialize(String type, I input)
      Deserialize the input
      Parameters:
      type - the type
      input - the input
      Returns:
      the output
    • serialize

      public Map.Entry<String,I> serialize(O output)
      Serialize the output
      Parameters:
      output - the output
      Returns:
      the entry of the type and the input
    • getRegisteredTypes

      public Map<String,Class<? extends O>> getRegisteredTypes()
      Get the registered types
      Returns:
      the registered types