mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-12 02:14:07 +00:00
#355 document, abstract base, traits and example domain
This commit is contained in:
1 parent
c1187ae0d7
commit
913411773f
7 files changed
+147
No files matched your search
@@ -0,0 +1,38 @@
|
||||
package com.iluwatar.abstractdocument;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public abstract class AbstractDocument implements Document {
|
||||
|
||||
private final Map<String, Object> properties;
|
||||
|
||||
protected AbstractDocument(Map<String, Object> properties) {
|
||||
Objects.requireNonNull(properties, "properties map is required");
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void put(String key, Object value) {
|
||||
properties.put(key, value);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(String key) {
|
||||
return properties.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
|
||||
return Stream.of(get(key))
|
||||
.filter(el -> el != null)
|
||||
.map(el -> (List<Map<String, Object>>) el)
|
||||
.findFirst().get().stream()
|
||||
.map(constructor);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user