Class FileUtil

java.lang.Object
de.murmelmeister.murmelapi.utils.FileUtil

public final class FileUtil extends Object
Manage the files (Thread-safe)
  • Field Details

    • FILE_LOCKS

      private static final Map<String,ReentrantLock> FILE_LOCKS
    • FILES

      private static final Map<String,File> FILES
    • GSON

      private static final com.google.gson.Gson GSON
  • Constructor Details

    • FileUtil

      public FileUtil()
  • Method Details

    • getLockForFile

      private static ReentrantLock getLockForFile(String fileName)
      Returns the lock associated with the specified file name. If a lock does not exist for the file name, a new lock is created and associated with the file name. The method uses a ConcurrentHashMap to store the file locks, ensuring thread-safe access to the locks.
      Parameters:
      fileName - the name of the file
      Returns:
      the lock associated with the file name
    • createFile

      public static File createFile(org.slf4j.Logger logger, String path, String fileName)
      Creates a new file with the specified path and file name. If the parent directory does not exist, it will be created. If the file already exists, no new file will be created. This method is thread-safe, as it locks the file using a unique lock obtained from the getLockForFile method.
      Parameters:
      logger - the logger to log warning and error messages
      path - the path to the parent directory where the file will be created
      fileName - the name of the file to be created
      Returns:
      the created file
      Throws:
      RuntimeException - if an I/O error occurs during file creation
      See Also:
    • existsFile

      public static boolean existsFile(String path, String fileName)
      Checks whether a file with the specified path and file name exists. This method ensures thread-safe access to the file by acquiring a lock associated with the file name.
      Parameters:
      path - the path to the directory where the file is expected to be located
      fileName - the name of the file to check for existence
      Returns:
      true if the file exists, false otherwise
    • loadJson

      public static <T> T loadJson(org.slf4j.Logger logger, String path, String fileName, Class<T> clazz)
      Loads JSON data from a specified file and deserializes it into an object of the given class. This method ensures thread-safe access to the file using a unique lock associated with the file name.
      Type Parameters:
      T - the type of the object to be deserialized
      Parameters:
      logger - the logger to log warning and error messages
      path - the path to the parent directory where the file is located
      fileName - the name of the file containing the JSON content
      clazz - the class of the object to be deserialized
      Returns:
      an object of type T deserialized from the JSON content of the file
    • saveJson

      public static <T> void saveJson(org.slf4j.Logger logger, String fileName, T json)
      Serializes the specified object into its JSON representation and writes it to a file with the given name. The file must be pre-loaded into the FILES map, otherwise an error will be logged. This method ensures thread-safe access by acquiring a lock associated with the file.
      Type Parameters:
      T - the type of the object to be serialized
      Parameters:
      logger - the logger to log error messages
      fileName - the name of the file to which the JSON representation of the object will be written
      json - the object to be serialized to JSON format
    • loadConfiguration

      public static MurmelConfiguration loadConfiguration(org.slf4j.Logger logger, String path, String fileName)
      Loads the configuration for a given file. This method ensures thread-safe access by acquiring a lock associated with the file name.
      Parameters:
      logger - the logger to log warning and error messages
      path - the path to the directory where the configuration file is located
      fileName - the name of the configuration file
      Returns:
      a MurmelConfiguration object with the loaded configuration data
    • loadConfiguration

      public static MurmelConfiguration loadConfiguration(org.slf4j.Logger logger, File file)
      Loads the configuration for a given file.
      Parameters:
      logger - the logger to log warning and error messages
      file - the configuration file
      Returns:
      a MurmelConfiguration object with the loaded configuration data
    • saveConfiguration

      public static void saveConfiguration(org.slf4j.Logger logger, MurmelConfiguration config, String fileName)
      Saves the given configuration to a file. This method ensures thread-safe access by acquiring a lock associated with the file name.
      Parameters:
      logger - the logger to log error messages
      config - the configuration object to be saved
      fileName - the name of the file to which the configuration will be saved
    • saveConfiguration

      public static void saveConfiguration(org.slf4j.Logger logger, MurmelConfiguration config, File file)
      Saves the given configuration to a specified file. This method ensures thread-safe access by acquiring a lock associated with the file name.
      Parameters:
      logger - the logger to log error messages
      config - the configuration object to be saved
      file - the file to which the configuration will be saved
    • loadProperties

      public static Properties loadProperties(File file)
      Load a file.
      Parameters:
      file - File
      Returns:
      Properties
    • toJson

      public static <T> void toJson(File file, T json)
      Serializes the specified object into its JSON representation and writes it to the provided file. This method ensures thread-safe file writing by acquiring a lock associated with the file name.
      Type Parameters:
      T - the type of the object to be serialized
      Parameters:
      file - the file to which the JSON representation of the object will be written
      json - the object to be serialized to JSON format
    • fromJson

      public static <T> T fromJson(File file, Class<T> clazz)
      Deserializes the JSON content of a specified file into an object of the given class.
      Type Parameters:
      T - the type of the object to be deserialized
      Parameters:
      file - the file containing the JSON content
      clazz - the class of the object to be deserialized
      Returns:
      an object of type T deserialized from the JSON content of the file
      Throws:
      RuntimeException - if an I/O error occurs during file reading or deserialization