From 7bb7fa6c9a73fbace4efb19d95a6c9a08d41aebb Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 8 Jul 2024 14:31:32 +0200 Subject: [PATCH] ConfigManager --- pom.xml | 2 +- .../file/ConfigurationManager.java | 116 ++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/main/java/me/finn/unlegitlibrary/file/ConfigurationManager.java diff --git a/pom.xml b/pom.xml index d3676ae..6d73a8e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.finn.unlegitlibrary unlegitlibrary - 1.0 + 1.1 21 diff --git a/src/main/java/me/finn/unlegitlibrary/file/ConfigurationManager.java b/src/main/java/me/finn/unlegitlibrary/file/ConfigurationManager.java new file mode 100644 index 0000000..6f79aff --- /dev/null +++ b/src/main/java/me/finn/unlegitlibrary/file/ConfigurationManager.java @@ -0,0 +1,116 @@ +package me.finn.unlegitlibrary.file; + +import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider; + +import java.io.*; +import java.util.*; +import java.util.stream.Collectors; + +public class ConfigurationManager extends DefaultMethodsOverrider { + private final Properties properties = new Properties(); + private final File configFile; + + public ConfigurationManager(File configFile) { + this.configFile = configFile; + } + + public final boolean isSet(String key) { + return properties.containsKey(key); + } + + public final void loadProperties() throws IOException { + try (FileInputStream in = new FileInputStream(configFile)) { + properties.load(in); + } + } + + public final void saveProperties() throws IOException { + try (FileOutputStream out = new FileOutputStream(configFile)) { + properties.store(out, null); + } + } + + public final long getLong(String key) { + return Long.parseLong(properties.getProperty(key)); + } + + public final double getDouble(String key) { + return Double.parseDouble(properties.getProperty(key)); + } + + public final float getFloat(String key) { + return Float.parseFloat(properties.getProperty(key)); + } + + public final int getInt(String key) { + return Integer.parseInt(properties.getProperty(key)); + } + + public final Object getObject(String key) { + return (Object) properties.getProperty(key); + } + + public Map getMap(String key) { + String value = properties.getProperty(key); + Map map = new HashMap<>(); + + if (value != null) { + String[] entries = value.split(","); + + for (String entry : entries) { + String[] pair = entry.split("="); + if (pair.length == 2) map.put(pair[0], pair[1]); + } + } + + return map; + } + + public final String getString(String key) { + return properties.getProperty(key); + } + + public final List getList(String key) { + String value = properties.getProperty(key); + + if (value != null) return Arrays.asList(value.split(",")); + else return new ArrayList<>(); + } + + public final void set(String key, long value) { + properties.setProperty(key, String.valueOf(value)); + } + + public final void set(String key, int value) { + properties.setProperty(key, String.valueOf(value)); + } + + public final void set(String key, double value) { + properties.setProperty(key, String.valueOf(value)); + } + + public final void set(String key, float value) { + properties.setProperty(key, String.valueOf(value)); + } + + public final void set(String key, Object value) { + properties.setProperty(key, String.valueOf(value)); + } + + public final void set(String key, String value) { + properties.setProperty(key, value); + } + + public final void set(String key, Map map) { + String value = map.entrySet().stream() + .map(entry -> entry.getKey() + "=" + entry.getValue()) + .collect(Collectors.joining(",")); + + properties.setProperty(key, value); + } + + public final void set(String key, List list) { + String value = list.stream().collect(Collectors.joining(",")); + properties.setProperty(key, value); + } +} \ No newline at end of file