#355 document, abstract base, traits and example domain
This commit is contained in:
parent
c1187ae0d7
commit
913411773f
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.iluwatar.abstractdocument;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public interface Document {
|
||||
|
||||
/**
|
||||
* Puts the value related to the key
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @return Void
|
||||
*/
|
||||
Void put(String key, Object value);
|
||||
|
||||
/**
|
||||
* Gets the value for the key
|
||||
*
|
||||
* @param key
|
||||
* @return value or null
|
||||
*/
|
||||
Object get(String key);
|
||||
|
||||
/**
|
||||
* Gets the stream of child documents
|
||||
*
|
||||
* @param key
|
||||
* @param constructor
|
||||
* @return child documents
|
||||
*/
|
||||
<T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor);
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.iluwatar.abstractdocument.domain;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.iluwatar.abstractdocument.AbstractDocument;
|
||||
|
||||
public class Car extends AbstractDocument implements HasModel, HasPrice, HasParts {
|
||||
|
||||
protected Car() {
|
||||
super(new HashMap<String, Object>());
|
||||
}
|
||||
|
||||
protected Car(Map<String,Object> properties) {
|
||||
super(properties);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.iluwatar.abstractdocument.domain;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import com.iluwatar.abstractdocument.Document;
|
||||
|
||||
public interface HasModel extends Document {
|
||||
|
||||
default Optional<String> getModel() {
|
||||
return Optional.ofNullable((String) get("model"));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.iluwatar.abstractdocument.domain;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.iluwatar.abstractdocument.Document;
|
||||
|
||||
public interface HasParts extends Document {
|
||||
|
||||
default Stream<Part> getParts() {
|
||||
return children("parts", Part::new);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.iluwatar.abstractdocument.domain;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import com.iluwatar.abstractdocument.Document;
|
||||
|
||||
public interface HasPrice extends Document {
|
||||
|
||||
default Optional<Number> getPartner() {
|
||||
return Optional.ofNullable((Number) get("price"));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.iluwatar.abstractdocument.domain;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.iluwatar.abstractdocument.AbstractDocument;
|
||||
|
||||
public class Part extends AbstractDocument implements HasModel, HasPrice {
|
||||
|
||||
protected Part() {
|
||||
super(new HashMap<String, Object>());
|
||||
}
|
||||
|
||||
protected Part(Map<String, Object> properties) {
|
||||
super(properties);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user