-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "defaulting" ConfigNode and ConfigLists
* Instances can be obtained by calling ConfigNode#defaulting and ConfigList#defaulting, respectively * "defaulting" nodes/lists will delegate to a secondary "defaults" node or list if a value is not present * Unit tests * Bump version
- Loading branch information
Showing
7 changed files
with
333 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ plugins { | |
} | ||
|
||
group 'com.github.steanky' | ||
version '0.23.2' | ||
version '0.24.0' | ||
|
||
publishing { | ||
publications { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...-core/src/main/java/com/github/steanky/ethylene/core/collection/DefaultingConfigList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.github.steanky.ethylene.core.collection; | ||
|
||
import com.github.steanky.ethylene.core.ConfigElement; | ||
import com.github.steanky.toolkit.collection.Containers; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.UnmodifiableView; | ||
|
||
import java.util.AbstractCollection; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.RandomAccess; | ||
|
||
/** | ||
* An implementation of a "defaulting" ConfigList. | ||
* | ||
* @see ConfigList#defaulting(ConfigList, ConfigList) | ||
*/ | ||
class DefaultingConfigList extends AbstractConfigList implements Immutable, RandomAccess { | ||
private final ConfigList base; | ||
private final ConfigList defaults; | ||
|
||
/** | ||
* Creates a new defaulting ConfigList. | ||
* | ||
* @param base the base list | ||
* @param defaults the default list, only queried if there is no value at a given index in {@code base} | ||
*/ | ||
DefaultingConfigList(@NotNull ConfigList base, @NotNull ConfigList defaults) { | ||
this.base = base; | ||
this.defaults = defaults; | ||
} | ||
|
||
@Override | ||
public @UnmodifiableView @NotNull Collection<ConfigEntry> entryCollection() { | ||
return Containers.mappedView(ConfigEntry::of, elementCollection()); | ||
} | ||
|
||
@Override | ||
public @UnmodifiableView @NotNull Collection<ConfigElement> elementCollection() { | ||
return new AbstractCollection<>() { | ||
@Override | ||
public @NotNull Iterator<ConfigElement> iterator() { | ||
int baseSize = base.size(); | ||
int max = DefaultingConfigList.this.size(); | ||
|
||
return new Iterator<>() { | ||
private int i = 0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return i < max; | ||
} | ||
|
||
@Override | ||
public ConfigElement next() { | ||
int i = this.i++; | ||
if (i < baseSize) { | ||
return base.get(i); | ||
} | ||
|
||
return defaults.get(i); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return DefaultingConfigList.this.size(); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public ConfigElement get(int index) { | ||
if (index < base.size()) { | ||
return base.get(index); | ||
} | ||
|
||
return defaults.get(index); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return Math.max(base.size(), defaults.size()); | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
...-core/src/main/java/com/github/steanky/ethylene/core/collection/DefaultingConfigNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package com.github.steanky.ethylene.core.collection; | ||
|
||
import com.github.steanky.ethylene.core.ConfigElement; | ||
import com.github.steanky.toolkit.collection.Containers; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.UnmodifiableView; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* An implementation of a "defaulting" ConfigNode. | ||
* | ||
* @see ConfigNode#defaulting(ConfigNode, ConfigNode) | ||
*/ | ||
class DefaultingConfigNode extends AbstractConfigNode implements Immutable { | ||
private final ConfigNode base; | ||
private final ConfigNode defaults; | ||
|
||
/** | ||
* Constructs a new defaulting ConfigNode. | ||
* | ||
* @param base the base node, which is queried first | ||
* @param defaults the default node, which is only queried if a requested value is not present in {@code base} | ||
*/ | ||
DefaultingConfigNode(@NotNull ConfigNode base, @NotNull ConfigNode defaults) { | ||
this.base = base; | ||
this.defaults = defaults; | ||
} | ||
|
||
@Override | ||
public @UnmodifiableView @NotNull Collection<ConfigEntry> entryCollection() { | ||
return Containers.mappedView(entry -> (ConfigEntry)entry, entrySet()); | ||
} | ||
|
||
@Override | ||
public @UnmodifiableView @NotNull Collection<ConfigElement> elementCollection() { | ||
return Containers.mappedView(Entry::getValue, entrySet()); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public Set<Entry<String, ConfigElement>> entrySet() { | ||
return new AbstractSet<>() { | ||
@Override | ||
public @NotNull Iterator<Map.Entry<String, ConfigElement>> iterator() { | ||
return new Iterator<>() { | ||
private final Iterator<ConfigEntry> baseIterator = base.entryCollection().iterator(); | ||
private final Iterator<ConfigEntry> defaultsIterator = defaults.entryCollection().iterator(); | ||
|
||
private ConfigEntry nextDefault; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return baseIterator.hasNext() || tryAdvanceDefaults(); | ||
} | ||
|
||
@Override | ||
public ConfigEntry next() { | ||
if (baseIterator.hasNext()) { | ||
return baseIterator.next(); | ||
} | ||
|
||
ConfigEntry nextDefault = this.nextDefault; | ||
if (nextDefault != null) { | ||
this.nextDefault = null; | ||
return nextDefault; | ||
} | ||
|
||
if (tryAdvanceDefaults()) { | ||
nextDefault = this.nextDefault; | ||
this.nextDefault = null; | ||
return nextDefault; | ||
} | ||
|
||
throw new NoSuchElementException(); | ||
} | ||
|
||
private boolean tryAdvanceDefaults() { | ||
while (defaultsIterator.hasNext()) { | ||
ConfigEntry nextDefault = defaultsIterator.next(); | ||
|
||
if (!base.containsKey(nextDefault.getKey())) { | ||
this.nextDefault = nextDefault; | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return DefaultingConfigNode.this.size(); | ||
} | ||
|
||
@Override | ||
public boolean contains(Object o) { | ||
return base.entrySet().contains(o) || defaults.entrySet().contains(o); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public ConfigElement get(Object key) { | ||
ConfigElement element = base.get(key); | ||
if (element != null) { | ||
return element; | ||
} | ||
|
||
return defaults.get(key); | ||
} | ||
|
||
@Override | ||
public boolean containsKey(Object key) { | ||
return base.containsKey(key) || defaults.containsKey(key); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
int size = base.size(); | ||
for (String key : defaults.keySet()) { | ||
if (!base.containsKey(key)) { | ||
size++; | ||
} | ||
} | ||
|
||
return size; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return base.isEmpty() && defaults.isEmpty(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...e/src/test/java/com/github/steanky/ethylene/core/collection/DefaultingConfigListTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.github.steanky.ethylene.core.collection; | ||
|
||
import com.github.steanky.ethylene.core.ConfigPrimitive; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class DefaultingConfigListTest { | ||
@Test | ||
void emptyBase() { | ||
ConfigList base = ConfigList.of(); | ||
ConfigList defaults = ConfigList.of("test"); | ||
|
||
DefaultingConfigList defaultingConfigList = new DefaultingConfigList(base, defaults); | ||
|
||
assertEquals(ConfigPrimitive.of("test"), defaultingConfigList.get(0)); | ||
assertEquals(1, defaultingConfigList.size()); | ||
|
||
assertThrows(IndexOutOfBoundsException.class, () -> defaultingConfigList.get(1)); | ||
|
||
base.addString("test2"); | ||
|
||
assertEquals(ConfigPrimitive.of("test2"), defaultingConfigList.get(0)); | ||
} | ||
|
||
@Test | ||
void itr() { | ||
ConfigList base = ConfigList.of("a", "b", "c"); | ||
ConfigList defaults = ConfigList.of("0", "1", "2", "d", "e", "f"); | ||
|
||
DefaultingConfigList defaultingConfigList = new DefaultingConfigList(base, defaults); | ||
List<String> values = new ArrayList<>(6); | ||
for (ConfigEntry entry : defaultingConfigList.entryCollection()) { | ||
values.add(entry.getValue().asString()); | ||
} | ||
|
||
assertEquals(List.of("a", "b", "c", "d", "e", "f"), values); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...e/src/test/java/com/github/steanky/ethylene/core/collection/DefaultingConfigNodeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.github.steanky.ethylene.core.collection; | ||
|
||
import com.github.steanky.ethylene.core.ConfigElement; | ||
import com.github.steanky.ethylene.core.ConfigPrimitive; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class DefaultingConfigNodeTest { | ||
@Test | ||
void simple() { | ||
ConfigNode base = ConfigNode.of("key", "value"); | ||
ConfigNode defaults = ConfigNode.of("default", 69); | ||
|
||
DefaultingConfigNode defaultingConfigNode = new DefaultingConfigNode(base, defaults); | ||
|
||
assertEquals(ConfigPrimitive.of(69), defaultingConfigNode.get("default")); | ||
assertEquals(2, defaultingConfigNode.size()); | ||
|
||
base.putNumber("default", 420); | ||
|
||
assertEquals(ConfigPrimitive.of(420), defaultingConfigNode.get("default")); | ||
|
||
Set<Map.Entry<String, ConfigElement>> entrySet = new HashSet<>(); | ||
entrySet.addAll(defaultingConfigNode.entrySet()); | ||
|
||
assertEquals(Set.of(Map.entry("key", ConfigPrimitive.of("value")), | ||
Map.entry("default", ConfigPrimitive.of(420))), entrySet); | ||
} | ||
} |