From 7ae84d24eea42c68b5ce75abd51fbbe46a439c51 Mon Sep 17 00:00:00 2001 From: krlvm <51774833+krlvm@users.noreply.github.com> Date: Sun, 31 Jul 2022 17:35:44 +0400 Subject: [PATCH] Desktop: Fixed an issue when configuration set via CLI was persisting --- .../configuration/AdvancedConfiguration.java | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/desktop/src/main/java/io/github/krlvm/powertunnel/desktop/configuration/AdvancedConfiguration.java b/desktop/src/main/java/io/github/krlvm/powertunnel/desktop/configuration/AdvancedConfiguration.java index 63dec06e..e0ed03bb 100644 --- a/desktop/src/main/java/io/github/krlvm/powertunnel/desktop/configuration/AdvancedConfiguration.java +++ b/desktop/src/main/java/io/github/krlvm/powertunnel/desktop/configuration/AdvancedConfiguration.java @@ -19,48 +19,52 @@ import io.github.krlvm.powertunnel.configuration.ConfigurationStore; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; public class AdvancedConfiguration extends ConfigurationStore { - private final Collection immutableKeys; + private final Collection immutableKeys = new HashSet<>(); + private final Map preImmutableData = new HashMap<>(); - public AdvancedConfiguration() { - this(new ArrayList<>()); - } - - public AdvancedConfiguration(Collection immutableKeys) { - this.immutableKeys = immutableKeys; - } - - public void protect(String key) { - this.immutableKeys.add(key); + private void saveOriginalKey(String key) { + String originalVal = get(key, null); + if (originalVal != null) { + preImmutableData.put(key, originalVal); + } } public void protect(String key, String value) { this.immutableKeys.add(key); + saveOriginalKey(key); set(key, value); } public void protectInt(String key, int value) { this.immutableKeys.add(key); + saveOriginalKey(key); setInt(key, value); } public void protectBoolean(String key, boolean value) { this.immutableKeys.add(key); + saveOriginalKey(key); setBoolean(key, value); } @Override protected Set> entries() { - return super.entries().stream() - .filter(entry -> !immutableKeys.contains(entry.getKey())) - .collect(Collectors.toSet()); + Set> entries = new HashSet<>(); + + for (Map.Entry entry : super.entries()) { + if (immutableKeys.contains(entry.getKey())) { + if (!preImmutableData.containsKey(entry.getKey())) continue; + entry = new AbstractMap.SimpleEntry<>(entry.getKey(), preImmutableData.get(entry.getKey())); + } + entries.add(entry); + } + + return entries; } public Collection getImmutableKeys() {