Package net.william278.annotaml
Class Annotaml<T>
- java.lang.Object
-
- net.william278.annotaml.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 deserializingYamlFile
annotated objects.
-
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_casestatic <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 aFile
into aAnnotaml
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 ifcopyDefaults
is true Read the file from disk, parse the YAML into the object and return itstatic <T> T
reload(@NotNull java.io.File file, T defaults, Annotaml.LoaderOptions options)
Reloads a YAML file from aFile
into aAnnotaml
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 ifcopyDefaults
is true Read the file from disk, parse the YAML into the object and return itstatic <T> void
save(T yamlObject, @NotNull java.io.File outputFile)
Write aYamlFile
annotated object to a YAML file.protected void
writeYaml()
Serializes and writesYamlFile
annotated object to a YAML file.
-
-
-
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 aFile
into aAnnotaml
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
- Type Parameters:
T
- the type of the class to load to/from theFile
- 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 aFile
into aAnnotaml
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
- Type Parameters:
T
- the type of the class to load to/from theFile
- 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 theFile
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 aYamlFile
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 aYamlFile
annotated object and have a zero-argument constructor- Type Parameters:
T
- The type of the object to load into- Parameters:
file
- The file to loadclassType
- 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 aYamlFile
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 loadclassType
- 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.
-
writeYaml
protected void writeYaml() throws AnnotamlException
Serializes and writesYamlFile
annotated object to a YAML file.- Throws:
AnnotamlException
- If an error occurs during serialization
-
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
-
-