Skip to content

Commit

Permalink
Desktop: Fixed an issue when configuration set via CLI was persisting
Browse files Browse the repository at this point in the history
  • Loading branch information
krlvm committed Jul 31, 2022
1 parent f07fdc9 commit 7ae84d2
Showing 1 changed file with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> immutableKeys;
private final Collection<String> immutableKeys = new HashSet<>();
private final Map<String, String> preImmutableData = new HashMap<>();

public AdvancedConfiguration() {
this(new ArrayList<>());
}

public AdvancedConfiguration(Collection<String> 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<Map.Entry<String, String>> entries() {
return super.entries().stream()
.filter(entry -> !immutableKeys.contains(entry.getKey()))
.collect(Collectors.toSet());
Set<Map.Entry<String, String>> entries = new HashSet<>();

for (Map.Entry<String, String> 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<String> getImmutableKeys() {
Expand Down

0 comments on commit 7ae84d2

Please sign in to comment.