Skip to content

Commit 9134117

Browse files
committed
iluwatar#355 document, abstract base, traits and example domain
1 parent c1187ae commit 9134117

File tree

7 files changed

+147
-0
lines changed

7 files changed

+147
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.iluwatar.abstractdocument;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.Objects;
6+
import java.util.function.Function;
7+
import java.util.stream.Stream;
8+
9+
public abstract class AbstractDocument implements Document {
10+
11+
private final Map<String, Object> properties;
12+
13+
protected AbstractDocument(Map<String, Object> properties) {
14+
Objects.requireNonNull(properties, "properties map is required");
15+
this.properties = properties;
16+
}
17+
18+
@Override
19+
public Void put(String key, Object value) {
20+
properties.put(key, value);
21+
return null;
22+
}
23+
24+
@Override
25+
public Object get(String key) {
26+
return properties.get(key);
27+
}
28+
29+
@Override
30+
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
31+
return Stream.of(get(key))
32+
.filter(el -> el != null)
33+
.map(el -> (List<Map<String, Object>>) el)
34+
.findFirst().get().stream()
35+
.map(constructor);
36+
}
37+
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.iluwatar.abstractdocument;
2+
3+
import java.util.Map;
4+
import java.util.function.Function;
5+
import java.util.stream.Stream;
6+
7+
public interface Document {
8+
9+
/**
10+
* Puts the value related to the key
11+
*
12+
* @param key
13+
* @param value
14+
* @return Void
15+
*/
16+
Void put(String key, Object value);
17+
18+
/**
19+
* Gets the value for the key
20+
*
21+
* @param key
22+
* @return value or null
23+
*/
24+
Object get(String key);
25+
26+
/**
27+
* Gets the stream of child documents
28+
*
29+
* @param key
30+
* @param constructor
31+
* @return child documents
32+
*/
33+
<T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor);
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.iluwatar.abstractdocument.domain;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import com.iluwatar.abstractdocument.AbstractDocument;
7+
8+
public class Car extends AbstractDocument implements HasModel, HasPrice, HasParts {
9+
10+
protected Car() {
11+
super(new HashMap<String, Object>());
12+
}
13+
14+
protected Car(Map<String,Object> properties) {
15+
super(properties);
16+
}
17+
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.iluwatar.abstractdocument.domain;
2+
3+
import java.util.Optional;
4+
5+
import com.iluwatar.abstractdocument.Document;
6+
7+
public interface HasModel extends Document {
8+
9+
default Optional<String> getModel() {
10+
return Optional.ofNullable((String) get("model"));
11+
}
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.iluwatar.abstractdocument.domain;
2+
3+
import java.util.stream.Stream;
4+
5+
import com.iluwatar.abstractdocument.Document;
6+
7+
public interface HasParts extends Document {
8+
9+
default Stream<Part> getParts() {
10+
return children("parts", Part::new);
11+
}
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.iluwatar.abstractdocument.domain;
2+
3+
import java.util.Optional;
4+
5+
import com.iluwatar.abstractdocument.Document;
6+
7+
public interface HasPrice extends Document {
8+
9+
default Optional<Number> getPartner() {
10+
return Optional.ofNullable((Number) get("price"));
11+
}
12+
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.iluwatar.abstractdocument.domain;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import com.iluwatar.abstractdocument.AbstractDocument;
7+
8+
public class Part extends AbstractDocument implements HasModel, HasPrice {
9+
10+
protected Part() {
11+
super(new HashMap<String, Object>());
12+
}
13+
14+
protected Part(Map<String, Object> properties) {
15+
super(properties);
16+
}
17+
18+
}

0 commit comments

Comments
 (0)