Class FileMapper

java.lang.Object
com.mooltiverse.oss.nyx.io.FileMapper

public class FileMapper extends Object
This class is used to load and save data files like configuration or state files.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    load(File file, Class<T> type)
    Unmarshals the content of the given file to an object of the given type.
    static <T> T
    load(URL url, Class<T> type)
    Unmarshals the content of the given URL to an object of the given type.
    static void
    save(String filePath, Object content)
    Marshals the content of the given object to a file represented by the given path.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • load

      public static <T> T load(File file, Class<T> type) throws DataAccessException
      Unmarshals the content of the given file to an object of the given type.
      Type Parameters:
      T - the type of object to unmarshal.
      Parameters:
      file - the file to load from. The file path must end with one of the supported extensions: json, yaml, yml (or JSON is used by default).
      type - the class representing the type of object to unmarshal.
      Returns:
      the object instance unmarshalled from the given file.
      Throws:
      DataAccessException - in case of any exception due to data access
      IllegalArgumentException - if the given file path does not contain a supported extension
    • load

      public static <T> T load(URL url, Class<T> type) throws DataAccessException
      Unmarshals the content of the given URL to an object of the given type.
      Type Parameters:
      T - the type of object to unmarshal.
      Parameters:
      url - the URL to load from. The file path must end with one of the supported extensions: json, yaml, yml (or JSON is used by default).
      type - the class representing the type of object to unmarshal.
      Returns:
      the object instance unmarshalled from the given URL.
      Throws:
      DataAccessException - in case of any exception due to data access
      IllegalArgumentException - if the given file path does not contain a supported extension
    • save

      public static void save(String filePath, Object content) throws DataAccessException
      Marshals the content of the given object to a file represented by the given path.
      Parameters:
      filePath - the path of the file to save to. If it's a relative path it will be considered relative to the current working directory. The file path must end with one of the supported extensions: json, yaml, yml (or JSON is used by default).
      content - the object to marshal.
      Throws:
      DataAccessException - in case of any exception due to data access
      IllegalArgumentException - if the given file path does not contain a supported extension