Class Annotaml<T>

  • Type Parameters:
    T - The type of the class to generate the YAML file for.

    public class Annotaml<T>
    extends java.lang.Object
    Annotaml is a library for generating YAML files from Java classes.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Annotaml.LoaderOptions
      Provides options for serializing and deserializing YamlFile annotated objects.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Annotaml​(T yamlObject, @NotNull java.io.File outputFile)
      Create a new Annotaml instance.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected static @NotNull java.lang.String convertToSnakeCase​(@NotNull java.lang.String value)
      Converts a string to snake_case
      static <T> java.util.Optional<java.lang.Integer> getVersionNumber​(@NotNull java.io.File file, @NotNull java.lang.Class<T> classType)
      Get the version number of the YAML file.
      static <T> java.util.Optional<java.lang.Integer> getVersionNumber​(@NotNull java.io.InputStream inputStream, @NotNull java.lang.Class<T> classType)
      Get the version number of the YAML file.
      static <T> T load​(@NotNull java.io.File file, @NotNull java.lang.Class<T> classType)
      Loads a YAML file into an object.
      static <T> T load​(@NotNull java.io.InputStream inputStream, @NotNull java.lang.Class<T> classType)
      Loads a YAML file into an object.
      static <T> T reload​(@NotNull java.io.File file, @NotNull java.io.InputStream defaultsInputStream, @NotNull java.lang.Class<T> classType, @NotNull Annotaml.LoaderOptions options)
      Reloads a YAML file from a File into a Annotaml typed object, doing a few things: Loads the defaults from the provided resource InputStream Checks if the file exists; if not, writes the file and returns the defaults If the file exists, copy and write defaults over if copyDefaults is true Read the file from disk, parse the YAML into the object and return it
      static <T> T reload​(@NotNull java.io.File file, T defaults, Annotaml.LoaderOptions options)
      Reloads a YAML file from a File into a Annotaml typed object, doing a few things: Checks if the file exists; if not, writes the file and returns the defaults If the file exists, copy and write defaults over if copyDefaults is true Read the file from disk, parse the YAML into the object and return it
      static <T> void save​(T yamlObject, @NotNull java.io.File outputFile)
      Write a YamlFile annotated object to a YAML file.
      protected void writeYaml()
      Serializes and writes YamlFile annotated object to a YAML file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Annotaml

        protected Annotaml​(@NotNull
                           T yamlObject,
                           @NotNull
                           @NotNull java.io.File outputFile)
        Create a new Annotaml instance.
        Parameters:
        yamlObject - The class to generate the YAML file for.
        outputFile - The file to generate the YAML file for.
    • Method Detail

      • reload

        public static <T> T reload​(@NotNull
                                   @NotNull java.io.File file,
                                   @NotNull
                                   T defaults,
                                   Annotaml.LoaderOptions options)
                            throws AnnotamlException
        Reloads a YAML file from a File into a Annotaml typed object, doing a few things:
        1. Checks if the file exists; if not, writes the file and returns the defaults
        2. If the file exists, copy and write defaults over if copyDefaults is true
        3. Read the file from disk, parse the YAML into the object and return it
        Type Parameters:
        T - the type of the class to load to/from the File
        Parameters:
        file - The target file that will be reloaded.
        defaults - The object with defaults to use if not set/if the file doesn't exist.
        options - Annotaml.LoaderOptions to use for (re)loading the file.
        Returns:
        the YAML-parsed contents of the File on disk
        Throws:
        AnnotamlException - If an error occurs saving or loading the object to/from YAML
      • reload

        public static <T> T reload​(@NotNull
                                   @NotNull java.io.File file,
                                   @NotNull
                                   @NotNull java.io.InputStream defaultsInputStream,
                                   @NotNull
                                   @NotNull java.lang.Class<T> classType,
                                   @NotNull
                                   @NotNull Annotaml.LoaderOptions options)
                            throws AnnotamlException
        Reloads a YAML file from a File into a Annotaml typed object, doing a few things:
        1. Loads the defaults from the provided resource InputStream
        2. Checks if the file exists; if not, writes the file and returns the defaults
        3. If the file exists, copy and write defaults over if copyDefaults is true
        4. Read the file from disk, parse the YAML into the object and return it
        Type Parameters:
        T - the type of the class to load to/from the File
        Parameters:
        file - The target file that will be reloaded.
        defaultsInputStream - The InputStream of a yaml document to read default values from.
        classType - The type of the class to load to/from the File
        options - Annotaml.LoaderOptions to use for (re)loading the file.
        Returns:
        the YAML-parsed contents of the File on disk
        Throws:
        AnnotamlException - If an error occurs saving or loading the object to/from YAML
      • save

        public static <T> void save​(@NotNull
                                    T yamlObject,
                                    @NotNull
                                    @NotNull java.io.File outputFile)
                             throws AnnotamlException
        Write a YamlFile annotated object to a YAML file.
        Type Parameters:
        T - The type of the object to save.
        Parameters:
        yamlObject - The object to save.
        outputFile - The file to save to.
        Throws:
        AnnotamlException - If an error occurs saving the YAML file.
      • load

        public static <T> T load​(@NotNull
                                 @NotNull java.io.File file,
                                 @NotNull
                                 @NotNull java.lang.Class<T> classType)
                          throws AnnotamlException
        Loads a YAML file into an object. The class to deserialize must be a YamlFile annotated object and have a zero-argument constructor
        Type Parameters:
        T - The type of the object to load into
        Parameters:
        file - The file to load
        classType - The class to load the file into
        Returns:
        The object loaded from the file
        Throws:
        AnnotamlException - If there is an error loading the file
      • load

        public static <T> T load​(@NotNull
                                 @NotNull java.io.InputStream inputStream,
                                 @NotNull
                                 @NotNull java.lang.Class<T> classType)
                          throws AnnotamlException
        Loads a YAML file into an object. The class to deserialize must be a YamlFile annotated object and have a zero-argument constructor
        Type Parameters:
        T - The type of the object to load into
        Parameters:
        inputStream - InputStream of the file to load
        classType - The class to load the file into
        Returns:
        The object loaded from the file
        Throws:
        AnnotamlException - If there is an error loading the file
      • getVersionNumber

        public static <T> java.util.Optional<java.lang.Integer> getVersionNumber​(@NotNull
                                                                                 @NotNull java.io.File file,
                                                                                 @NotNull
                                                                                 @NotNull java.lang.Class<T> classType)
                                                                          throws AnnotamlException
        Get the version number of the YAML file.
        Type Parameters:
        T - The type of the class to get the version number of.
        Parameters:
        file - The file to get the version number of.
        classType - The type of the class to get the version number of.
        Returns:
        The version number of the YAML file.
        Throws:
        AnnotamlException - If an error occurs reading the YAML file.
      • getVersionNumber

        public static <T> java.util.Optional<java.lang.Integer> getVersionNumber​(@NotNull
                                                                                 @NotNull java.io.InputStream inputStream,
                                                                                 @NotNull
                                                                                 @NotNull java.lang.Class<T> classType)
                                                                          throws AnnotamlException
        Get the version number of the YAML file.
        Type Parameters:
        T - The type of the class to get the version number of.
        Parameters:
        inputStream - InputStream of the file to get the version number of.
        classType - The type of the class to get the version number of.
        Returns:
        The version number of the YAML file.
        Throws:
        AnnotamlException - If an error occurs reading the YAML file.
      • convertToSnakeCase

        @NotNull
        protected static @NotNull java.lang.String convertToSnakeCase​(@NotNull
                                                                      @NotNull java.lang.String value)
        Converts a string to snake_case
        Parameters:
        value - the string to convert
        Returns:
        the converted string