diff --git a/API/src/main/java/net/juligames/core/api/config/Configuration.java b/API/src/main/java/net/juligames/core/api/config/Configuration.java index dd977b59..8ce859f0 100644 --- a/API/src/main/java/net/juligames/core/api/config/Configuration.java +++ b/API/src/main/java/net/juligames/core/api/config/Configuration.java @@ -204,6 +204,16 @@ public interface Configuration extends Comparable { int size(); + void copyAndAppendContentTo(Configuration configuration); + void copyAndOverrideContentTo(Configuration configuration); + Configuration copy(String name); + Configuration copyToOffline(String name); + + Configuration copyToOffline(); + + + void appendAll(Collection configurations); + @Override default int compareTo(@NotNull Configuration o) { diff --git a/API/src/main/java/net/juligames/core/api/config/ConfigurationAPI.java b/API/src/main/java/net/juligames/core/api/config/ConfigurationAPI.java index cb37d513..2618d9b1 100644 --- a/API/src/main/java/net/juligames/core/api/config/ConfigurationAPI.java +++ b/API/src/main/java/net/juligames/core/api/config/ConfigurationAPI.java @@ -102,4 +102,7 @@ default void spiltAndWrite(Collection collection, Interpreter interpre * @return initialized Properties to be used with the {@link ConfigurationAPI} */ Properties initializeProperties(@NotNull String name, @Nullable String header); + + @ApiStatus.Experimental + Configuration merge(Configuration c1, Configuration c2); } diff --git a/Core/src/main/java/net/juligames/core/config/CoreConfiguration.java b/Core/src/main/java/net/juligames/core/config/CoreConfiguration.java index d5e9787a..6c654ae9 100644 --- a/Core/src/main/java/net/juligames/core/config/CoreConfiguration.java +++ b/Core/src/main/java/net/juligames/core/config/CoreConfiguration.java @@ -2,9 +2,11 @@ import com.hazelcast.map.IMap; import net.juligames.core.Core; +import net.juligames.core.api.API; import net.juligames.core.api.config.Configuration; import net.juligames.core.api.config.Interpreter; import net.juligames.core.api.misc.TriConsumer; +import org.checkerframework.checker.index.qual.IndexFor; import org.checkerframework.checker.optional.qual.MaybePresent; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -699,6 +701,48 @@ public int size() { return data.size(); } + @Override + public void copyAndAppendContentTo(@NotNull Configuration configuration) { + IMap hazel = hazel(); + Set keySet = configuration.keySet(); + for (Map.Entry stringStringEntry : hazel) { + if(!keySet.contains(stringStringEntry.getKey())) + configuration.setString(stringStringEntry.getKey(),stringStringEntry.getValue()); + } + } + + @Override + public void copyAndOverrideContentTo(@NotNull Configuration configuration) { + hazel().forEach(configuration::setString); + } + + @Override + public Configuration copyToOffline() { + return copyToOffline(getName()); + } + + @Override + public Configuration copy(String name) { + CoreConfiguration configuration = Core.getInstance().getConfigurationApi().getOrCreate(name); + configuration.clear(); + copyAndOverrideContentTo(configuration); + return configuration; + } + + @Override + public Configuration copyToOffline(String name) { + OfflineConfiguration offlineConfiguration = new OfflineConfiguration(name); + copyAndOverrideContentTo(offlineConfiguration); + return offlineConfiguration; + } + + @Override + public void appendAll(@NotNull Collection configurations) { + for (Configuration configuration : configurations) { + configuration.copyAndAppendContentTo(this); + } + } + public final @NotNull String getConjoinedDescription() { return getName() + "\n" + header_comment; } @@ -723,4 +767,9 @@ public void updateName(String newName) { public String toString() { return name; } + + @ApiStatus.Internal + private void clear() { + hazel().clear(); + } } diff --git a/Core/src/main/java/net/juligames/core/config/CoreConfigurationApi.java b/Core/src/main/java/net/juligames/core/config/CoreConfigurationApi.java index 44b9b4b9..703238e4 100644 --- a/Core/src/main/java/net/juligames/core/config/CoreConfigurationApi.java +++ b/Core/src/main/java/net/juligames/core/config/CoreConfigurationApi.java @@ -22,7 +22,7 @@ public class CoreConfigurationApi implements ConfigurationAPI { public static final String DATABASE_CONFIG_NAME = "database"; @Override - public Configuration getOrCreate(String name) { + public CoreConfiguration getOrCreate(String name) { return new CoreConfiguration(name); } @@ -34,12 +34,12 @@ public Configuration getOrCreate(String name) { * @apiNote configuration_name is the reserved key for the name!! */ @Override - public Configuration getOrCreate(Properties defaults) { + public CoreConfiguration getOrCreate(Properties defaults) { return CoreConfiguration.fromProperties(defaults); } @Override - public Configuration master() { + public CoreConfiguration master() { return getOrCreate(MASTER_CONFIG_NAME); } @@ -128,4 +128,10 @@ public Properties initializeProperties(@NotNull String name, @Nullable String he defaults.setProperty("configuration_header", header); return defaults; } + + @Override + public Configuration merge(@NotNull Configuration c1, Configuration c2) { + c1.copyAndAppendContentTo(c2); + return c1; + } } diff --git a/Core/src/main/java/net/juligames/core/config/OfflineConfiguration.java b/Core/src/main/java/net/juligames/core/config/OfflineConfiguration.java index 1add13f8..9c9fde58 100644 --- a/Core/src/main/java/net/juligames/core/config/OfflineConfiguration.java +++ b/Core/src/main/java/net/juligames/core/config/OfflineConfiguration.java @@ -675,5 +675,46 @@ public Collection getCollection(String keyspace, Interpreter interpret public Collection getCollection(@NotNull Supplier keyspace, Interpreter interpreter) { return getCollection(keyspace.get(), interpreter); } + + + @Override + public void copyAndAppendContentTo(@NotNull Configuration configuration) { + Set keySet = configuration.keySet(); + data.forEach((key, value) -> { + if (!keySet.contains(key)) + configuration.setString(key, value); + }); + } + + @Override + public void copyAndOverrideContentTo(@NotNull Configuration configuration) { + data.forEach(configuration::setString); + } + + @Override + public Configuration copyToOffline() { + return copyToOffline(getName()); + } + + @Override + public Configuration copy(String name) { + Core.getInstance().getCoreLogger().warning("OfflineConfiguration#copy(String) is not supported -" + + " OfflineConfiguration#copyToOffline(String) will be used instead"); + return copyToOffline(name); + } + + @Override + public Configuration copyToOffline(String name) { + OfflineConfiguration offlineConfiguration = new OfflineConfiguration(name); + copyAndOverrideContentTo(offlineConfiguration); + return offlineConfiguration; + } + + @Override + public void appendAll(@NotNull Collection configurations) { + for (Configuration configuration : configurations) { + configuration.copyAndAppendContentTo(this); + } + } }