-
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
28c9f23
commit aec4c02
Showing
25 changed files
with
610 additions
and
259 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
124 changes: 124 additions & 0 deletions
124
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,124 @@ | ||
/* | ||
* Copyright 2019-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
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); | ||
} |
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
75 changes: 75 additions & 0 deletions
75
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,75 @@ | ||
/* | ||
* Copyright 2019-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
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); | ||
} | ||
} |
Oops, something went wrong.