Package de.murmelmeister.murmelapi.utils
Class FileUtil
java.lang.Object
de.murmelmeister.murmelapi.utils.FileUtil
Manage the files
(Thread-safe)
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Map<String, ReentrantLock> private static final com.google.gson.Gson -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic FilecreateFile(org.slf4j.Logger logger, String path, String fileName) Creates a new file with the specified path and file name.static booleanexistsFile(String path, String fileName) Checks whether a file with the specified path and file name exists.static <T> TDeserializes the JSON content of a specified file into an object of the given class.private static ReentrantLockgetLockForFile(String fileName) Returns the lock associated with the specified file name.static MurmelConfigurationloadConfiguration(org.slf4j.Logger logger, File file) Loads the configuration for a given file.static MurmelConfigurationloadConfiguration(org.slf4j.Logger logger, String path, String fileName) Loads the configuration for a given file.static <T> TLoads JSON data from a specified file and deserializes it into an object of the given class.static PropertiesloadProperties(File file) Load a file.static voidsaveConfiguration(org.slf4j.Logger logger, MurmelConfiguration config, File file) Saves the given configuration to a specified file.static voidsaveConfiguration(org.slf4j.Logger logger, MurmelConfiguration config, String fileName) Saves the given configuration to a file.static <T> voidSerializes the specified object into its JSON representation and writes it to a file with the given name.static <T> voidSerializes the specified object into its JSON representation and writes it to the provided file.
-
Field Details
-
FILE_LOCKS
-
FILES
-
GSON
private static final com.google.gson.Gson GSON
-
-
Constructor Details
-
FileUtil
public FileUtil()
-
-
Method Details
-
getLockForFile
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
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 messagespath- the path to the parent directory where the file will be createdfileName- 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
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 locatedfileName- the name of the file to check for existence- Returns:
trueif the file exists,falseotherwise
-
loadJson
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 messagespath- the path to the parent directory where the file is locatedfileName- the name of the file containing the JSON contentclazz- the class of the object to be deserialized- Returns:
- an object of type T deserialized from the JSON content of the file
-
saveJson
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 messagesfileName- the name of the file to which the JSON representation of the object will be writtenjson- 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 messagespath- the path to the directory where the configuration file is locatedfileName- the name of the configuration file- Returns:
- a MurmelConfiguration object with the loaded configuration data
-
loadConfiguration
Loads the configuration for a given file.- Parameters:
logger- the logger to log warning and error messagesfile- 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 messagesconfig- the configuration object to be savedfileName- 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 messagesconfig- the configuration object to be savedfile- the file to which the configuration will be saved
-
loadProperties
Load a file.- Parameters:
file- File- Returns:
- Properties
-
toJson
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 writtenjson- the object to be serialized to JSON format
-
fromJson
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 contentclazz- 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
-