Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1fa8a60
Sharding Pattern (#1056)
Azureyjt Nov 8, 2019
6d1c0b1
Resolves checkstyle errors for ambassador, async-method-invocation, b…
anuragagarwal561994 Nov 9, 2019
efc17fc
Resolves checkstyle errors for business-delegate, bytecode, caching (…
anuragagarwal561994 Nov 9, 2019
31f27a7
Resolves checkstyle errors for callback, chain, circuit-breaker (#1060)
anuragagarwal561994 Nov 9, 2019
2f49648
Resolves checkstyle errors for collection-pipeline, command, commande…
anuragagarwal561994 Nov 9, 2019
4f9ee01
Resolves checkstyle errors for converter, cqrs (#1063)
anuragagarwal561994 Nov 10, 2019
dda0953
Resolves checkstyle errors for guarded-suspension, half-sync-half-asy…
anuragagarwal561994 Nov 10, 2019
7f06f3b
Resolves checkstyle errors for intercepting-filter, interpreter, iter…
anuragagarwal561994 Nov 10, 2019
eae09fc
Resolves checkstyle errors for api-gateway, lazy-loading, leader-elec…
anuragagarwal561994 Nov 10, 2019
01e489c
Resolves checkstyle errors for dao data-bus data-locality data-mapper…
anuragagarwal561994 Nov 10, 2019
f2c91eb
Resolves checkstyle errors for delegation dependency-injection dirty-…
anuragagarwal561994 Nov 10, 2019
7c888e8
Resolves checkstyle errors for eip-* (#1069)
anuragagarwal561994 Nov 10, 2019
5ae2ce6
Resolves checkstyle errors for event-* (#1070)
anuragagarwal561994 Nov 10, 2019
4dae1fa
Resolves checkstyle errors for execute-around extension-objects (#1071)
anuragagarwal561994 Nov 10, 2019
9c8ad44
Resolves checkstyle errors for patterns starting with letter r (#1072)
anuragagarwal561994 Nov 10, 2019
b92eb52
Resolves checkstyle errors for template-method thread-pool throttling…
anuragagarwal561994 Nov 10, 2019
f0f0143
Resolves checkstyle errors for trampoline twin typeobjectpattern unit…
anuragagarwal561994 Nov 10, 2019
c441831
Java 11 migration: ambassador async-method-invocation balking bridge …
anuragagarwal561994 Nov 11, 2019
329479d
#984 update Ambassador readme
iluwatar Nov 11, 2019
0272d71
#984 update Bridge readme
iluwatar Nov 11, 2019
2628cc0
#984 update Builder readme
iluwatar Nov 11, 2019
c954a43
Resolves checkstyle errors for facade factory-kit spatial-partition s…
anuragagarwal561994 Nov 11, 2019
37599eb
Resolves checkstyle errors for feature-toggle fluentinterface flux fl…
anuragagarwal561994 Nov 11, 2019
3907951
Resolves checkstyle issues for semaphore servant serverless service-l…
anuragagarwal561994 Nov 11, 2019
1e76d91
Resolves checkstyle errors for abstract-document abstract-factory acy…
anuragagarwal561994 Nov 11, 2019
6ef840f
Resolves checkstyle errors for naked-objects null-object object-mothe…
anuragagarwal561994 Nov 12, 2019
33ea733
Java 11 migration: patterns (remaining b-c) (#1081)
anuragagarwal561994 Nov 12, 2019
3c57bf7
#984 #987 update readmes
iluwatar Nov 12, 2019
f04fc3c
Java 11 migration: patterns starting with a (#1084)
anuragagarwal561994 Nov 13, 2019
160b737
Add another real world example for Builder
iluwatar Nov 13, 2019
50467c9
Java 11 migration: patterns (t-v) (#1085)
anuragagarwal561994 Nov 14, 2019
cc571f4
Saga pattern (#1062)
besok Nov 14, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@

package com.iluwatar.abstractdocument;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

/**
* Abstract implementation of Document interface
* Abstract implementation of Document interface.
*/
public abstract class AbstractDocument implements Document {

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

@Override
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
Optional<List<Map<String, Object>>> any = Stream.of(get(key)).filter(Objects::nonNull)
.map(el -> (List<Map<String, Object>>) el).findAny();
return any.map(maps -> maps.stream().map(constructor)).orElseGet(Stream::empty);
return Stream.ofNullable(get(key))
.filter(Objects::nonNull)
.map(el -> (List<Map<String, Object>>) el)
.findAny()
.stream()
.flatMap(Collection::stream)
.map(constructor);
}

@Override
public String toString() {
var builder = new StringBuilder();
builder.append(getClass().getName()).append("[");
properties.forEach((key, value) -> builder.append("[").append(key).append(" : ").append(value).append("]"));
properties.forEach((key, value) -> builder.append("[").append(key).append(" : ").append(value)
.append("]"));
builder.append("]");
return builder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,59 @@

import com.iluwatar.abstractdocument.domain.Car;
import com.iluwatar.abstractdocument.domain.enums.Property;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

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

/**
* Executes the App
* Executes the App.
*/
public App() {
LOGGER.info("Constructing parts and car");

var wheelProperties = Map.of(
Property.TYPE.toString(), "wheel",
Property.MODEL.toString(), "15C",
Property.PRICE.toString(), 100L);
Property.TYPE.toString(), "wheel",
Property.MODEL.toString(), "15C",
Property.PRICE.toString(), 100L);

var doorProperties = Map.of(
Property.TYPE.toString(), "door",
Property.MODEL.toString(), "Lambo",
Property.PRICE.toString(), 300L);
Property.TYPE.toString(), "door",
Property.MODEL.toString(), "Lambo",
Property.PRICE.toString(), 300L);

var carProperties = Map.of(
Property.MODEL.toString(), "300SL",
Property.PRICE.toString(), 10000L,
Property.PARTS.toString(), List.of(wheelProperties, doorProperties));
Property.MODEL.toString(), "300SL",
Property.PRICE.toString(), 10000L,
Property.PARTS.toString(), List.of(wheelProperties, doorProperties));

var car = new Car(carProperties);

LOGGER.info("Here is our car:");
LOGGER.info("-> model: {}", car.getModel().get());
LOGGER.info("-> price: {}", car.getPrice().get());
LOGGER.info("-> model: {}", car.getModel().orElseThrow());
LOGGER.info("-> price: {}", car.getPrice().orElseThrow());
LOGGER.info("-> parts: ");
car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}", p.getType().get(), p.getModel().get(), p.getPrice().get()));
car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}",
p.getType().orElse(null),
p.getModel().orElse(null),
p.getPrice().orElse(null))
);
}

/**
* Program entry point
* Program entry point.
*
* @param args command line args
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
import java.util.stream.Stream;

/**
* Document interface
* Document interface.
*/
public interface Document {

/**
* Puts the value related to the key
* Puts the value related to the key.
*
* @param key element key
* @param value element value
Expand All @@ -42,15 +42,15 @@ public interface Document {
Void put(String key, Object value);

/**
* Gets the value for the key
* Gets the value for the key.
*
* @param key element key
* @return value or null
*/
Object get(String key);

/**
* Gets the stream of child documents
* Gets the stream of child documents.
*
* @param key element key
* @param constructor constructor of child class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@

package com.iluwatar.abstractdocument.domain;

import java.util.Map;

import com.iluwatar.abstractdocument.AbstractDocument;
import java.util.Map;

/**
* Car entity
* Car entity.
*/
public class Car extends AbstractDocument implements HasModel, HasPrice, HasParts {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@

package com.iluwatar.abstractdocument.domain;

import java.util.Optional;

import com.iluwatar.abstractdocument.Document;
import com.iluwatar.abstractdocument.domain.enums.Property;
import java.util.Optional;

/**
* HasModel trait for static access to 'model' property
* HasModel trait for static access to 'model' property.
*/
public interface HasModel extends Document {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@

package com.iluwatar.abstractdocument.domain;

import java.util.stream.Stream;

import com.iluwatar.abstractdocument.Document;
import com.iluwatar.abstractdocument.domain.enums.Property;
import java.util.stream.Stream;

/**
* HasParts trait for static access to 'parts' property
* HasParts trait for static access to 'parts' property.
*/
public interface HasParts extends Document {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@

package com.iluwatar.abstractdocument.domain;

import java.util.Optional;

import com.iluwatar.abstractdocument.Document;
import com.iluwatar.abstractdocument.domain.enums.Property;
import java.util.Optional;

/**
* HasPrice trait for static access to 'price' property
* HasPrice trait for static access to 'price' property.
*/
public interface HasPrice extends Document {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@

package com.iluwatar.abstractdocument.domain;

import java.util.Optional;

import com.iluwatar.abstractdocument.Document;
import com.iluwatar.abstractdocument.domain.enums.Property;
import java.util.Optional;

/**
* HasType trait for static access to 'type' property
* HasType trait for static access to 'type' property.
*/
public interface HasType extends Document {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@

package com.iluwatar.abstractdocument.domain;

import java.util.Map;

import com.iluwatar.abstractdocument.AbstractDocument;
import java.util.Map;

/**
* Part entity
* Part entity.
*/
public class Part extends AbstractDocument implements HasType, HasModel, HasPrice {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.abstractdocument.domain.enums;

/**
*
* Enum To Describe Property type
*
* Enum To Describe Property type.
*/
public enum Property {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@

package com.iluwatar.abstractdocument;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

/**
* AbstractDocument test class
Expand Down Expand Up @@ -61,22 +61,22 @@ public void shouldRetrieveChildren() {

document.put(KEY, children);

Stream<DocumentImplementation> childrenStream = document.children(KEY, DocumentImplementation::new);
var childrenStream = document.children(KEY, DocumentImplementation::new);
assertNotNull(children);
assertEquals(2, childrenStream.count());
}

@Test
public void shouldRetrieveEmptyStreamForNonExistingChildren() {
Stream<DocumentImplementation> children = document.children(KEY, DocumentImplementation::new);
var children = document.children(KEY, DocumentImplementation::new);
assertNotNull(children);
assertEquals(0, children.count());
}

@Test
public void shouldIncludePropsInToString() {
Map<String, Object> props = Map.of(KEY, VALUE);
DocumentImplementation document = new DocumentImplementation(props);
var props = Map.of(KEY, (Object) VALUE);
var document = new DocumentImplementation(props);
assertTrue(document.toString().contains(KEY));
assertTrue(document.toString().contains(VALUE));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@

package com.iluwatar.abstractdocument;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.iluwatar.abstractdocument.domain.Car;
import com.iluwatar.abstractdocument.domain.Part;
import com.iluwatar.abstractdocument.domain.enums.Property;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

/**
* Test for Part and Car
Expand All @@ -47,27 +46,27 @@ public class DomainTest {

@Test
public void shouldConstructPart() {
Map<String, Object> partProperties = Map.of(
Property.TYPE.toString(), TEST_PART_TYPE,
Property.MODEL.toString(), TEST_PART_MODEL,
Property.PRICE.toString(), TEST_PART_PRICE);
Part part = new Part(partProperties);

assertEquals(TEST_PART_TYPE, part.getType().get());
assertEquals(TEST_PART_MODEL, part.getModel().get());
assertEquals(TEST_PART_PRICE, part.getPrice().get());
var partProperties = Map.of(
Property.TYPE.toString(), TEST_PART_TYPE,
Property.MODEL.toString(), TEST_PART_MODEL,
Property.PRICE.toString(), (Object) TEST_PART_PRICE
);
var part = new Part(partProperties);
assertEquals(TEST_PART_TYPE, part.getType().orElseThrow());
assertEquals(TEST_PART_MODEL, part.getModel().orElseThrow());
assertEquals(TEST_PART_PRICE, part.getPrice().orElseThrow());
}

@Test
public void shouldConstructCar() {
Map<String, Object> carProperties = Map.of(
Property.MODEL.toString(), TEST_CAR_MODEL,
Property.PRICE.toString(), TEST_CAR_PRICE,
Property.PARTS.toString(), List.of(Map.of(), Map.of()));
Car car = new Car(carProperties);

assertEquals(TEST_CAR_MODEL, car.getModel().get());
assertEquals(TEST_CAR_PRICE, car.getPrice().get());
var carProperties = Map.of(
Property.MODEL.toString(), TEST_CAR_MODEL,
Property.PRICE.toString(), TEST_CAR_PRICE,
Property.PARTS.toString(), List.of(Map.of(), Map.of())
);
var car = new Car(carProperties);
assertEquals(TEST_CAR_MODEL, car.getModel().orElseThrow());
assertEquals(TEST_CAR_PRICE, car.getPrice().orElseThrow());
assertEquals(2, car.getParts().count());
}

Expand Down
Loading