Skip to content

Commit 8592ac7

Browse files
authored
Add String based value querying and setting to DataView (#2611)
* Add String based value querying and setting to DataView * Add DataView#streamRootKeys
1 parent fdb6cc3 commit 8592ac7

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/main/java/org/spongepowered/api/data/persistence/DataContainer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ static DataContainer createNew(SafetyMode safety) {
5858
@Override
5959
DataContainer set(DataQuery path, Object value);
6060

61+
@Override
62+
default DataContainer set(String key, Object value) {
63+
return this.set(DataQuery.of(key), value);
64+
}
65+
6166
@Override
6267
DataContainer remove(DataQuery path);
6368
}

src/main/java/org/spongepowered/api/data/persistence/DataView.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Objects;
3939
import java.util.Optional;
4040
import java.util.Set;
41+
import java.util.stream.Stream;
4142

4243
/**
4344
* Represents an object of data represented by a map.
@@ -106,6 +107,15 @@ public interface DataView {
106107
*/
107108
Set<DataQuery> keys(boolean deep);
108109

110+
/**
111+
* Gets a Stream containing all root keys for this {@link DataView}.
112+
*
113+
* @return Stream of root keys of this container
114+
*/
115+
default Stream<String> streamRootKeys() {
116+
return this.keys(false).stream().map(k -> k.parts().getFirst());
117+
}
118+
109119
/**
110120
* Gets a Map containing all keys and their values for this {@link DataView}.
111121
*
@@ -121,6 +131,15 @@ public interface DataView {
121131
*/
122132
Map<DataQuery, Object> values(boolean deep);
123133

134+
/**
135+
* Gets a Stream containing all root keys and their values for this {@link DataView}.
136+
*
137+
* @return Stream of root keys and values of this container
138+
*/
139+
default Stream<Map.Entry<String, Object>> streamRootValues() {
140+
return this.values(false).entrySet().stream().map(entry -> Map.entry(entry.getKey().parts().getFirst(), entry.getValue()));
141+
}
142+
124143
/**
125144
* Returns whether this {@link DataView} contains the given path.
126145
*
@@ -158,6 +177,17 @@ public interface DataView {
158177
*/
159178
DataView set(DataQuery path, Object value);
160179

180+
/**
181+
* Sets the given Object value to this {@link DataView}'s key.
182+
*
183+
* @param key The key of the object to set
184+
* @param value The value of the data
185+
* @return This view, for chaining
186+
*/
187+
default DataView set(String key, Object value) {
188+
return this.set(DataQuery.of(key), value);
189+
}
190+
161191
/**
162192
* Removes the data associated to the given path relative to this
163193
* {@link DataView}'s path.

0 commit comments

Comments
 (0)