Skip to content

Commit 4acdfe3

Browse files
authored
Merge pull request #4 from iluwatar/master
re-sync from base
2 parents 2963418 + cc571f4 commit 4acdfe3

File tree

753 files changed

+8243
-5997
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

753 files changed

+8243
-5997
lines changed

abstract-document/src/main/java/com/iluwatar/abstractdocument/AbstractDocument.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323

2424
package com.iluwatar.abstractdocument;
2525

26+
import java.util.Collection;
2627
import java.util.List;
2728
import java.util.Map;
2829
import java.util.Objects;
29-
import java.util.Optional;
3030
import java.util.function.Function;
3131
import java.util.stream.Stream;
3232

3333
/**
34-
* Abstract implementation of Document interface
34+
* Abstract implementation of Document interface.
3535
*/
3636
public abstract class AbstractDocument implements Document {
3737

@@ -55,16 +55,21 @@ public Object get(String key) {
5555

5656
@Override
5757
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
58-
Optional<List<Map<String, Object>>> any = Stream.of(get(key)).filter(Objects::nonNull)
59-
.map(el -> (List<Map<String, Object>>) el).findAny();
60-
return any.map(maps -> maps.stream().map(constructor)).orElseGet(Stream::empty);
58+
return Stream.ofNullable(get(key))
59+
.filter(Objects::nonNull)
60+
.map(el -> (List<Map<String, Object>>) el)
61+
.findAny()
62+
.stream()
63+
.flatMap(Collection::stream)
64+
.map(constructor);
6165
}
6266

6367
@Override
6468
public String toString() {
6569
var builder = new StringBuilder();
6670
builder.append(getClass().getName()).append("[");
67-
properties.forEach((key, value) -> builder.append("[").append(key).append(" : ").append(value).append("]"));
71+
properties.forEach((key, value) -> builder.append("[").append(key).append(" : ").append(value)
72+
.append("]"));
6873
builder.append("]");
6974
return builder.toString();
7075
}

abstract-document/src/main/java/com/iluwatar/abstractdocument/App.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,58 +25,59 @@
2525

2626
import com.iluwatar.abstractdocument.domain.Car;
2727
import com.iluwatar.abstractdocument.domain.enums.Property;
28-
import org.slf4j.Logger;
29-
import org.slf4j.LoggerFactory;
30-
3128
import java.util.List;
3229
import java.util.Map;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
3332

3433
/**
35-
* The Abstract Document pattern enables handling additional, non-static
36-
* properties. This pattern uses concept of traits to enable type safety and
37-
* separate properties of different classes into set of interfaces.
38-
* <p>
39-
* <p>
40-
* In Abstract Document pattern,({@link AbstractDocument}) fully implements
41-
* {@link Document}) interface. Traits are then defined to enable access to
42-
* properties in usual, static way.
34+
* The Abstract Document pattern enables handling additional, non-static properties. This pattern
35+
* uses concept of traits to enable type safety and separate properties of different classes into
36+
* set of interfaces.
37+
*
38+
* <p>In Abstract Document pattern,({@link AbstractDocument}) fully implements {@link Document})
39+
* interface. Traits are then defined to enable access to properties in usual, static way.
4340
*/
4441
public class App {
4542

4643
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
4744

4845
/**
49-
* Executes the App
46+
* Executes the App.
5047
*/
5148
public App() {
5249
LOGGER.info("Constructing parts and car");
5350

5451
var wheelProperties = Map.of(
55-
Property.TYPE.toString(), "wheel",
56-
Property.MODEL.toString(), "15C",
57-
Property.PRICE.toString(), 100L);
52+
Property.TYPE.toString(), "wheel",
53+
Property.MODEL.toString(), "15C",
54+
Property.PRICE.toString(), 100L);
5855

5956
var doorProperties = Map.of(
60-
Property.TYPE.toString(), "door",
61-
Property.MODEL.toString(), "Lambo",
62-
Property.PRICE.toString(), 300L);
57+
Property.TYPE.toString(), "door",
58+
Property.MODEL.toString(), "Lambo",
59+
Property.PRICE.toString(), 300L);
6360

6461
var carProperties = Map.of(
65-
Property.MODEL.toString(), "300SL",
66-
Property.PRICE.toString(), 10000L,
67-
Property.PARTS.toString(), List.of(wheelProperties, doorProperties));
62+
Property.MODEL.toString(), "300SL",
63+
Property.PRICE.toString(), 10000L,
64+
Property.PARTS.toString(), List.of(wheelProperties, doorProperties));
6865

6966
var car = new Car(carProperties);
7067

7168
LOGGER.info("Here is our car:");
72-
LOGGER.info("-> model: {}", car.getModel().get());
73-
LOGGER.info("-> price: {}", car.getPrice().get());
69+
LOGGER.info("-> model: {}", car.getModel().orElseThrow());
70+
LOGGER.info("-> price: {}", car.getPrice().orElseThrow());
7471
LOGGER.info("-> parts: ");
75-
car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}", p.getType().get(), p.getModel().get(), p.getPrice().get()));
72+
car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}",
73+
p.getType().orElse(null),
74+
p.getModel().orElse(null),
75+
p.getPrice().orElse(null))
76+
);
7677
}
7778

7879
/**
79-
* Program entry point
80+
* Program entry point.
8081
*
8182
* @param args command line args
8283
*/

abstract-document/src/main/java/com/iluwatar/abstractdocument/Document.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
import java.util.stream.Stream;
2929

3030
/**
31-
* Document interface
31+
* Document interface.
3232
*/
3333
public interface Document {
3434

3535
/**
36-
* Puts the value related to the key
36+
* Puts the value related to the key.
3737
*
3838
* @param key element key
3939
* @param value element value
@@ -42,15 +42,15 @@ public interface Document {
4242
Void put(String key, Object value);
4343

4444
/**
45-
* Gets the value for the key
45+
* Gets the value for the key.
4646
*
4747
* @param key element key
4848
* @return value or null
4949
*/
5050
Object get(String key);
5151

5252
/**
53-
* Gets the stream of child documents
53+
* Gets the stream of child documents.
5454
*
5555
* @param key element key
5656
* @param constructor constructor of child class

abstract-document/src/main/java/com/iluwatar/abstractdocument/domain/Car.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@
2323

2424
package com.iluwatar.abstractdocument.domain;
2525

26-
import java.util.Map;
27-
2826
import com.iluwatar.abstractdocument.AbstractDocument;
27+
import java.util.Map;
2928

3029
/**
31-
* Car entity
30+
* Car entity.
3231
*/
3332
public class Car extends AbstractDocument implements HasModel, HasPrice, HasParts {
3433

abstract-document/src/main/java/com/iluwatar/abstractdocument/domain/HasModel.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@
2323

2424
package com.iluwatar.abstractdocument.domain;
2525

26-
import java.util.Optional;
27-
2826
import com.iluwatar.abstractdocument.Document;
2927
import com.iluwatar.abstractdocument.domain.enums.Property;
28+
import java.util.Optional;
3029

3130
/**
32-
* HasModel trait for static access to 'model' property
31+
* HasModel trait for static access to 'model' property.
3332
*/
3433
public interface HasModel extends Document {
3534

abstract-document/src/main/java/com/iluwatar/abstractdocument/domain/HasParts.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@
2323

2424
package com.iluwatar.abstractdocument.domain;
2525

26-
import java.util.stream.Stream;
27-
2826
import com.iluwatar.abstractdocument.Document;
2927
import com.iluwatar.abstractdocument.domain.enums.Property;
28+
import java.util.stream.Stream;
3029

3130
/**
32-
* HasParts trait for static access to 'parts' property
31+
* HasParts trait for static access to 'parts' property.
3332
*/
3433
public interface HasParts extends Document {
3534

abstract-document/src/main/java/com/iluwatar/abstractdocument/domain/HasPrice.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@
2323

2424
package com.iluwatar.abstractdocument.domain;
2525

26-
import java.util.Optional;
27-
2826
import com.iluwatar.abstractdocument.Document;
2927
import com.iluwatar.abstractdocument.domain.enums.Property;
28+
import java.util.Optional;
3029

3130
/**
32-
* HasPrice trait for static access to 'price' property
31+
* HasPrice trait for static access to 'price' property.
3332
*/
3433
public interface HasPrice extends Document {
3534

abstract-document/src/main/java/com/iluwatar/abstractdocument/domain/HasType.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@
2323

2424
package com.iluwatar.abstractdocument.domain;
2525

26-
import java.util.Optional;
27-
2826
import com.iluwatar.abstractdocument.Document;
2927
import com.iluwatar.abstractdocument.domain.enums.Property;
28+
import java.util.Optional;
3029

3130
/**
32-
* HasType trait for static access to 'type' property
31+
* HasType trait for static access to 'type' property.
3332
*/
3433
public interface HasType extends Document {
3534

abstract-document/src/main/java/com/iluwatar/abstractdocument/domain/Part.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@
2323

2424
package com.iluwatar.abstractdocument.domain;
2525

26-
import java.util.Map;
27-
2826
import com.iluwatar.abstractdocument.AbstractDocument;
27+
import java.util.Map;
2928

3029
/**
31-
* Part entity
30+
* Part entity.
3231
*/
3332
public class Part extends AbstractDocument implements HasType, HasModel, HasPrice {
3433

abstract-document/src/main/java/com/iluwatar/abstractdocument/domain/enums/Property.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
package com.iluwatar.abstractdocument.domain.enums;
2525

2626
/**
27-
*
28-
* Enum To Describe Property type
29-
*
27+
* Enum To Describe Property type.
3028
*/
3129
public enum Property {
3230

abstract-document/src/test/java/com/iluwatar/abstractdocument/AbstractDocumentTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323

2424
package com.iluwatar.abstractdocument;
2525

26-
import org.junit.jupiter.api.Test;
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
import static org.junit.jupiter.api.Assertions.assertNotNull;
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
2729

2830
import java.util.HashMap;
2931
import java.util.List;
3032
import java.util.Map;
31-
import java.util.stream.Stream;
32-
33-
import static org.junit.jupiter.api.Assertions.*;
33+
import org.junit.jupiter.api.Test;
3434

3535
/**
3636
* AbstractDocument test class
@@ -61,22 +61,22 @@ public void shouldRetrieveChildren() {
6161

6262
document.put(KEY, children);
6363

64-
Stream<DocumentImplementation> childrenStream = document.children(KEY, DocumentImplementation::new);
64+
var childrenStream = document.children(KEY, DocumentImplementation::new);
6565
assertNotNull(children);
6666
assertEquals(2, childrenStream.count());
6767
}
6868

6969
@Test
7070
public void shouldRetrieveEmptyStreamForNonExistingChildren() {
71-
Stream<DocumentImplementation> children = document.children(KEY, DocumentImplementation::new);
71+
var children = document.children(KEY, DocumentImplementation::new);
7272
assertNotNull(children);
7373
assertEquals(0, children.count());
7474
}
7575

7676
@Test
7777
public void shouldIncludePropsInToString() {
78-
Map<String, Object> props = Map.of(KEY, VALUE);
79-
DocumentImplementation document = new DocumentImplementation(props);
78+
var props = Map.of(KEY, (Object) VALUE);
79+
var document = new DocumentImplementation(props);
8080
assertTrue(document.toString().contains(KEY));
8181
assertTrue(document.toString().contains(VALUE));
8282
}

abstract-document/src/test/java/com/iluwatar/abstractdocument/DomainTest.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@
2323

2424
package com.iluwatar.abstractdocument;
2525

26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
2628
import com.iluwatar.abstractdocument.domain.Car;
2729
import com.iluwatar.abstractdocument.domain.Part;
2830
import com.iluwatar.abstractdocument.domain.enums.Property;
29-
import org.junit.jupiter.api.Test;
30-
3131
import java.util.List;
3232
import java.util.Map;
33-
34-
import static org.junit.jupiter.api.Assertions.assertEquals;
33+
import org.junit.jupiter.api.Test;
3534

3635
/**
3736
* Test for Part and Car
@@ -47,27 +46,27 @@ public class DomainTest {
4746

4847
@Test
4948
public void shouldConstructPart() {
50-
Map<String, Object> partProperties = Map.of(
51-
Property.TYPE.toString(), TEST_PART_TYPE,
52-
Property.MODEL.toString(), TEST_PART_MODEL,
53-
Property.PRICE.toString(), TEST_PART_PRICE);
54-
Part part = new Part(partProperties);
55-
56-
assertEquals(TEST_PART_TYPE, part.getType().get());
57-
assertEquals(TEST_PART_MODEL, part.getModel().get());
58-
assertEquals(TEST_PART_PRICE, part.getPrice().get());
49+
var partProperties = Map.of(
50+
Property.TYPE.toString(), TEST_PART_TYPE,
51+
Property.MODEL.toString(), TEST_PART_MODEL,
52+
Property.PRICE.toString(), (Object) TEST_PART_PRICE
53+
);
54+
var part = new Part(partProperties);
55+
assertEquals(TEST_PART_TYPE, part.getType().orElseThrow());
56+
assertEquals(TEST_PART_MODEL, part.getModel().orElseThrow());
57+
assertEquals(TEST_PART_PRICE, part.getPrice().orElseThrow());
5958
}
6059

6160
@Test
6261
public void shouldConstructCar() {
63-
Map<String, Object> carProperties = Map.of(
64-
Property.MODEL.toString(), TEST_CAR_MODEL,
65-
Property.PRICE.toString(), TEST_CAR_PRICE,
66-
Property.PARTS.toString(), List.of(Map.of(), Map.of()));
67-
Car car = new Car(carProperties);
68-
69-
assertEquals(TEST_CAR_MODEL, car.getModel().get());
70-
assertEquals(TEST_CAR_PRICE, car.getPrice().get());
62+
var carProperties = Map.of(
63+
Property.MODEL.toString(), TEST_CAR_MODEL,
64+
Property.PRICE.toString(), TEST_CAR_PRICE,
65+
Property.PARTS.toString(), List.of(Map.of(), Map.of())
66+
);
67+
var car = new Car(carProperties);
68+
assertEquals(TEST_CAR_MODEL, car.getModel().orElseThrow());
69+
assertEquals(TEST_CAR_PRICE, car.getPrice().orElseThrow());
7170
assertEquals(2, car.getParts().count());
7271
}
7372

0 commit comments

Comments
 (0)