Skip to content

Commit

Permalink
add copy to Configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
TureBentzin committed Feb 10, 2023
1 parent 1794c23 commit 1a82f8f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
10 changes: 10 additions & 0 deletions API/src/main/java/net/juligames/core/api/config/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,16 @@ public interface Configuration extends Comparable<Configuration> {

int size();

void copyAndAppendContentTo(Configuration configuration);
void copyAndOverrideContentTo(Configuration configuration);
Configuration copy(String name);
Configuration copyToOffline(String name);

Configuration copyToOffline();


void appendAll(Collection<Configuration> configurations);


@Override
default int compareTo(@NotNull Configuration o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,7 @@ default <T> void spiltAndWrite(Collection<T> collection, Interpreter<T> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -699,6 +701,48 @@ public int size() {
return data.size();
}

@Override
public void copyAndAppendContentTo(@NotNull Configuration configuration) {
IMap<String, String> hazel = hazel();
Set<String> keySet = configuration.keySet();
for (Map.Entry<String, String> 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<Configuration> configurations) {
for (Configuration configuration : configurations) {
configuration.copyAndAppendContentTo(this);
}
}

public final @NotNull String getConjoinedDescription() {
return getName() + "\n" + header_comment;
}
Expand All @@ -723,4 +767,9 @@ public void updateName(String newName) {
public String toString() {
return name;
}

@ApiStatus.Internal
private void clear() {
hazel().clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -675,5 +675,46 @@ public <T> Collection<T> getCollection(String keyspace, Interpreter<T> interpret
public <T> Collection<T> getCollection(@NotNull Supplier<String> keyspace, Interpreter<T> interpreter) {
return getCollection(keyspace.get(), interpreter);
}


@Override
public void copyAndAppendContentTo(@NotNull Configuration configuration) {
Set<String> 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<Configuration> configurations) {
for (Configuration configuration : configurations) {
configuration.copyAndAppendContentTo(this);
}
}
}

0 comments on commit 1a82f8f

Please sign in to comment.