Skip to content

Commit

Permalink
feat: Configuration#getKeyList
Browse files Browse the repository at this point in the history
  • Loading branch information
Siroshun09 committed Aug 2, 2021
1 parent 3d8d1bc commit c79e190
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ public interface Configuration {
*/
@NotNull @Unmodifiable Set<String> getPaths();

/**
* Gets the list of root keys included in this {@link Configuration}.
*
* This method may not return to the correct order depending on the implementation.
*
* @return list of root keys
*/
@NotNull @Unmodifiable List<String> getKeyList();

/**
* Gets the set of root objects included in this {@link Configuration}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jetbrains.annotations.Unmodifiable;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -72,7 +73,7 @@ public class MappedConfiguration extends AbstractConfiguration {

var map = new LinkedHashMap<>();

for (var key : config.getPaths()) {
for (var key : config.getKeyList()) {
var value = config.get(key);

if (value instanceof Configuration) {
Expand Down Expand Up @@ -177,6 +178,13 @@ public void set(@NotNull String path, @Nullable Object value) {
.collect(Collectors.toUnmodifiableSet());
}

@Override
public @NotNull @Unmodifiable List<String> getKeyList() {
return map.keySet().stream()
.map(object -> object instanceof String ? (String) object : object.toString())
.collect(Collectors.toUnmodifiableList());
}

@Override
public @NotNull @Unmodifiable Set<Object> getValues() {
return map.values().stream().collect(Collectors.toUnmodifiableSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
Expand Down Expand Up @@ -86,6 +87,14 @@ public void set(@NotNull String path, @Nullable Object value) {
.collect(Collectors.toUnmodifiableSet());
}

@Override
public @NotNull @Unmodifiable List<String> getKeyList() {
return properties.keySet()
.stream()
.map(object -> object instanceof String ? (String) object : object.toString())
.collect(Collectors.toUnmodifiableList());
}

@Override
public @NotNull @Unmodifiable Set<Object> getValues() {
return properties.values().stream().collect(Collectors.toUnmodifiableSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
import java.util.Set;

Expand Down Expand Up @@ -83,6 +84,11 @@ public void set(@NotNull String path, @Nullable Object value) {
return Set.of("key", "map");
}

@Override
public @NotNull @Unmodifiable List<String> getKeyList() {
return List.of("key", "map");
}

@Override
public @NotNull @Unmodifiable Set<Object> getValues() {
return Collections.emptySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -127,6 +128,11 @@ public void set(@NotNull String path, @Nullable Object value) {
return config != null ? config.getPaths() : Collections.emptySet();
}

@Override
public @NotNull @Unmodifiable List<String> getKeyList() {
return config.getKeyList();
}

@Override
public @NotNull @Unmodifiable Set<Object> getValues() {
return config != null ? config.getValues() : Collections.emptySet();
Expand Down Expand Up @@ -165,7 +171,7 @@ public void save() throws IOException {
var map = new LinkedHashMap<>();

if (config != null) {
for (var key : config.getPaths()) {
for (var key : config.getKeyList()) {
map.put(key, config.get(key));
}
}
Expand Down

0 comments on commit c79e190

Please sign in to comment.