mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 10:38:19 +08:00
feat(holder): Add file type multi configuration. #169
This commit is contained in:
@@ -32,6 +32,11 @@
|
|||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>cc.carm.lib</groupId>
|
||||||
|
<artifactId>configured-feature-file</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package cc.carm.lib.configuration;
|
package cc.carm.lib.configuration.multi;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
+73
@@ -0,0 +1,73 @@
|
|||||||
|
package cc.carm.lib.configuration.multi;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
public abstract class MultiFileConfiguration<K, T> extends MultiConfiguration<K, T> {
|
||||||
|
|
||||||
|
protected final @NotNull File dataFolder;
|
||||||
|
protected final @NotNull String extensionSuffix; // e.g. ".yml"
|
||||||
|
|
||||||
|
protected final ConcurrentHashMap<K, ConfigurationHolder<?>> holders = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
public MultiFileConfiguration(@NotNull File dataFolder, @NotNull String extensionSuffix) {
|
||||||
|
this.dataFolder = dataFolder;
|
||||||
|
this.extensionSuffix = extensionSuffix;
|
||||||
|
|
||||||
|
// Load existing configuration files
|
||||||
|
if (dataFolder.exists() && dataFolder.isDirectory()) {
|
||||||
|
File[] files = dataFolder.listFiles((dir, name) -> name.endsWith(extensionSuffix));
|
||||||
|
if (files != null) {
|
||||||
|
for (File file : files) {
|
||||||
|
String fileName = file.getName();
|
||||||
|
String keyStr = fileName.substring(0, fileName.length() - extensionSuffix.length()); // Remove extension suffix
|
||||||
|
try {
|
||||||
|
holders.put(extractKeyFromFilename(keyStr), loadHolder(file));
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loadAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract K extractKeyFromFilename(@NotNull String fileName);
|
||||||
|
|
||||||
|
public String keyToFilename(@NotNull K key) {
|
||||||
|
return key.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract ConfigurationHolder<?> loadHolder(@NotNull File file);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Map<K, ConfigurationHolder<?>> holders() {
|
||||||
|
return this.holders;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull ConfigurationHolder<?> holder(@NotNull K key) {
|
||||||
|
ConfigurationHolder<?> loaded = holders.get(key);
|
||||||
|
if (loaded != null) return loaded;
|
||||||
|
|
||||||
|
File file = new File(dataFolder, keyToFilename(key) + this.extensionSuffix);
|
||||||
|
ConfigurationHolder<?> created = loadHolder(file);
|
||||||
|
holders.put(key, created);
|
||||||
|
return created;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeHolder(@NotNull K key) {
|
||||||
|
ConfigurationHolder<?> loaded = holders.remove(key);
|
||||||
|
if (loaded == null) return;
|
||||||
|
|
||||||
|
File file = new File(dataFolder, key + ".yml");
|
||||||
|
if (file.exists()) file.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.tests;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.MultiConfiguration;
|
|
||||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
|
||||||
import cc.carm.lib.configuration.source.yaml.YAMLConfigFactory;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
public abstract class ExampleUserStorage<T> extends MultiConfiguration<UUID, T> {
|
|
||||||
|
|
||||||
protected final @NotNull File dataFolder;
|
|
||||||
protected final ConcurrentHashMap<UUID, ConfigurationHolder<?>> holders = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
public ExampleUserStorage(@NotNull File dataFolder) {
|
|
||||||
this.dataFolder = dataFolder;
|
|
||||||
|
|
||||||
// Load existing configuration files
|
|
||||||
if (dataFolder.exists() && dataFolder.isDirectory()) {
|
|
||||||
File[] files = dataFolder.listFiles((dir, name) -> name.endsWith(".yml"));
|
|
||||||
if (files != null) {
|
|
||||||
for (File file : files) {
|
|
||||||
String fileName = file.getName();
|
|
||||||
String uuidStr = fileName.substring(0, fileName.length() - 4); // Remove ".yml"
|
|
||||||
try {
|
|
||||||
UUID uuid = UUID.fromString(uuidStr);
|
|
||||||
ConfigurationHolder<?> holder = YAMLConfigFactory.from(file).build();
|
|
||||||
holders.put(uuid, holder);
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
System.err.println("Invalid UUID in file name: " + fileName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
loadAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Map<UUID, ConfigurationHolder<?>> holders() {
|
|
||||||
return this.holders;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull ConfigurationHolder<?> holder(@NotNull UUID key) {
|
|
||||||
ConfigurationHolder<?> loaded = holders.get(key);
|
|
||||||
if (loaded != null) return loaded;
|
|
||||||
|
|
||||||
ConfigurationHolder<?> created = YAMLConfigFactory.from(new File(dataFolder, key + ".yml")).build();
|
|
||||||
holders.put(key, created);
|
|
||||||
return created;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeHolder(@NotNull UUID key) {
|
|
||||||
ConfigurationHolder<?> loaded = holders.remove(key);
|
|
||||||
if (loaded == null) return;
|
|
||||||
|
|
||||||
File file = new File(dataFolder, key + ".yml");
|
|
||||||
if (file.exists()) file.delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
package cc.carm.lib.configuration.tests;
|
package cc.carm.lib.configuration.tests;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.multi.MultiFileConfiguration;
|
||||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||||
import cc.carm.lib.configuration.source.section.ConfigureSection;
|
import cc.carm.lib.configuration.source.section.ConfigureSection;
|
||||||
|
import cc.carm.lib.configuration.source.yaml.YAMLConfigFactory;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@@ -26,10 +28,11 @@ public class MultiFileTests {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ProfileStorage extends ExampleUserStorage<UserProfile> {
|
public static class ProfileStorage extends MultiFileConfiguration<UUID, UserProfile> {
|
||||||
|
|
||||||
|
|
||||||
public ProfileStorage(@NotNull File dataFolder) {
|
public ProfileStorage(@NotNull File dataFolder) {
|
||||||
super(dataFolder);
|
super(dataFolder, ".yml");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -55,6 +58,16 @@ public class MultiFileTests {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UUID extractKeyFromFilename(@NotNull String fileName) {
|
||||||
|
return UUID.fromString(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ConfigurationHolder<?> loadHolder(@NotNull File file) {
|
||||||
|
return YAMLConfigFactory.from(file).build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user