-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1001 Implement object- and list based property access
- Loading branch information
1 parent
879a058
commit e1dc83b
Showing
26 changed files
with
531 additions
and
244 deletions.
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
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
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
108 changes: 108 additions & 0 deletions
108
hartshorn-properties/src/main/java/org/dockbox/hartshorn/properties/AbstractMapProperty.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,108 @@ | ||
package org.dockbox.hartshorn.properties; | ||
|
||
import org.dockbox.hartshorn.properties.list.SimpleListProperty; | ||
import org.dockbox.hartshorn.properties.parse.support.ValueConfiguredPropertyParser; | ||
import org.dockbox.hartshorn.properties.value.SimpleValueProperty; | ||
import org.dockbox.hartshorn.properties.value.StandardValuePropertyParsers; | ||
import org.dockbox.hartshorn.util.option.Option; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import java.util.function.BiPredicate; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
public abstract class AbstractMapProperty<T> { | ||
|
||
private final Map<String, ConfiguredProperty> properties; | ||
private final String name; | ||
|
||
protected AbstractMapProperty(String name, Map<String, ConfiguredProperty> properties) { | ||
this.properties = new TreeMap<>(properties); | ||
this.name = name; | ||
} | ||
|
||
protected Map<String, ConfiguredProperty> properties() { | ||
return this.properties; | ||
} | ||
|
||
public String name() { | ||
return this.name; | ||
} | ||
|
||
public Option<ValueProperty> get(T key) { | ||
return Option.of(this.properties.get(valueAccessor(key))) | ||
.flatMap(ValueConfiguredPropertyParser.INSTANCE::parse); | ||
} | ||
|
||
protected abstract String valueAccessor(T key); | ||
|
||
public Option<ObjectProperty> object(T key) { | ||
Map<String, ConfiguredProperty> propertyMap = this.collectToMap(key, (name, property) -> { | ||
return name.startsWith(this.accessor(key) + "."); | ||
}).entrySet().stream().collect(Collectors.toMap( | ||
// Strip trailing . from key | ||
entry -> entry.getKey().substring(1), | ||
Map.Entry::getValue | ||
)); | ||
ObjectProperty property = new MapObjectProperty(this.name() + this.accessor(key), propertyMap); | ||
return Option.of(property); | ||
} | ||
|
||
protected abstract String accessor(T key); | ||
|
||
public Option<ListProperty> list(T key) { | ||
return this.list(key, value -> { | ||
Option<String[]> values = StandardValuePropertyParsers.STRING_LIST.parse(value); | ||
List<String> valueList = values.stream().flatMap(Arrays::stream).toList(); | ||
List<Property> listProperties = new ArrayList<>(); | ||
for (int i = 0; i < valueList.size(); i++) { | ||
listProperties.add(i, new SimpleValueProperty(this.name() + this.accessor(key) + "[" + i + "]", valueList.get(i))); | ||
} | ||
return new SimpleListProperty(this.name() + this.accessor(key), listProperties); | ||
}); | ||
} | ||
|
||
public Option<ListProperty> list(T key, Function<ValueProperty, ListProperty> singleValueMapper) { | ||
if (this.contains(key)) { | ||
return this.get(key).map(singleValueMapper); | ||
} else { | ||
Map<String, ConfiguredProperty> propertyMap = this.collectToMap(key, (name, property) -> { | ||
return name.startsWith(key + "["); | ||
}); | ||
ListProperty property = new MapListProperty(this.name() + this.accessor(key), propertyMap); | ||
return Option.of(property); | ||
} | ||
} | ||
|
||
public boolean contains(T key) { | ||
if (this.properties().containsKey(this.valueAccessor(key))) { | ||
return true; | ||
} else { | ||
String accessor = this.accessor(key); | ||
return this.properties().keySet().stream().anyMatch(propertyKey -> | ||
propertyKey.startsWith(accessor + ".") || propertyKey.startsWith(accessor + "[") | ||
); | ||
} | ||
} | ||
|
||
public List<ConfiguredProperty> find(BiPredicate<String, ConfiguredProperty> predicate) { | ||
return this.properties().entrySet().stream() | ||
.filter(entry -> predicate.test(entry.getKey(), entry.getValue())) | ||
.map(Map.Entry::getValue) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private Map<String, ConfiguredProperty> collectToMap(T key, BiPredicate<String, ConfiguredProperty> predicate) { | ||
return this.find(predicate).stream() | ||
.collect(Collectors.toMap( | ||
property -> this.key(key, property), | ||
Function.identity() | ||
)); | ||
} | ||
|
||
protected abstract String key(T prefix, ConfiguredProperty property); | ||
} |
14 changes: 10 additions & 4 deletions
14
hartshorn-properties/src/main/java/org/dockbox/hartshorn/properties/ListProperty.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 |
---|---|---|
@@ -1,13 +1,19 @@ | ||
package org.dockbox.hartshorn.properties; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import org.dockbox.hartshorn.properties.list.ListPropertyParser; | ||
import org.dockbox.hartshorn.util.option.Option; | ||
|
||
import java.util.Collection; | ||
|
||
public non-sealed interface ListProperty extends Property { | ||
|
||
List<Property> elements(); | ||
int size(); | ||
|
||
Option<ValueProperty> get(int index); | ||
|
||
Option<ObjectProperty> object(int index); | ||
|
||
Option<ListProperty> list(int index); | ||
|
||
<T> Collection<T> parse(ListPropertyParser<T> parser); | ||
} |
59 changes: 59 additions & 0 deletions
59
hartshorn-properties/src/main/java/org/dockbox/hartshorn/properties/MapListProperty.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,59 @@ | ||
package org.dockbox.hartshorn.properties; | ||
|
||
import org.dockbox.hartshorn.properties.list.ListPropertyParser; | ||
import org.dockbox.hartshorn.util.option.Option; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
public class MapListProperty extends AbstractMapProperty<Integer> implements ListProperty { | ||
|
||
public MapListProperty(String name, Map<String, ConfiguredProperty> properties) { | ||
super(name, properties); | ||
} | ||
|
||
@Override | ||
protected String valueAccessor(Integer key) { | ||
return "[" + key + "]"; | ||
} | ||
|
||
@Override | ||
protected String accessor(Integer key) { | ||
return "[" + key + "]"; | ||
} | ||
|
||
@Override | ||
protected String key(Integer prefix, ConfiguredProperty property) { | ||
return property.name().substring(this.name().length() + this.accessor(prefix).length()); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return this.properties().keySet().stream() | ||
.map(key -> key.split("\\[")[1].split("]")[0]) | ||
.map(Integer::parseInt) | ||
.max(Integer::compareTo) | ||
.map(i -> i + 1) // Add one to get the size, as the max index is zero-based | ||
.orElse(0); | ||
} | ||
|
||
@Override | ||
public Option<ValueProperty> get(int index) { | ||
return this.get(Integer.valueOf(index)); | ||
} | ||
|
||
@Override | ||
public Option<ObjectProperty> object(int index) { | ||
return this.object(Integer.valueOf(index)); | ||
} | ||
|
||
@Override | ||
public Option<ListProperty> list(int index) { | ||
return this.list(Integer.valueOf(index)); | ||
} | ||
|
||
@Override | ||
public <T> Collection<T> parse(ListPropertyParser<T> parser) { | ||
return parser.parse(this); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
hartshorn-properties/src/main/java/org/dockbox/hartshorn/properties/MapObjectProperty.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,45 @@ | ||
package org.dockbox.hartshorn.properties; | ||
|
||
import org.dockbox.hartshorn.properties.object.ObjectPropertyParser; | ||
import org.dockbox.hartshorn.util.option.Option; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class MapObjectProperty extends AbstractMapProperty<String> implements ObjectProperty { | ||
|
||
public MapObjectProperty(String name, Map<String, ConfiguredProperty> properties) { | ||
super(name, properties); | ||
} | ||
|
||
@Override | ||
protected String valueAccessor(String key) { | ||
return key; | ||
} | ||
|
||
@Override | ||
protected String accessor(String key) { | ||
return this.name().isBlank() | ||
? key | ||
: "." + key; | ||
} | ||
|
||
@Override | ||
protected String key(String prefix, ConfiguredProperty property) { | ||
// Remove prefix from name | ||
return property.name().substring(this.name().length() + this.accessor(prefix).length()); | ||
} | ||
|
||
@Override | ||
public List<String> keys() { | ||
return this.properties().keySet().stream() | ||
.map(key -> key.split("\\.")[0]) | ||
.distinct() | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public <T> Option<T> parse(ObjectPropertyParser<T> parser) { | ||
return parser.parse(this); | ||
} | ||
} |
Oops, something went wrong.