#355 document, abstract base, traits and example domain

This commit is contained in:
qza 2016-06-01 22:20:55 +02:00
parent c1187ae0d7
commit 913411773f
7 changed files with 147 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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"));
}
}

View File

@ -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);
}
}

View File

@ -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"));
}
}

View File

@ -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);
}
}