Compare commits
210 Commits
1.14.0
...
explanatio
Author | SHA1 | Date | |
---|---|---|---|
600553ec0f | |||
54d8ec94be | |||
ffbf56f01a | |||
83fcbe0713 | |||
0982f00a61 | |||
871df4f918 | |||
7ae9e3ee22 | |||
c572fe885e | |||
a221245909 | |||
8bf4497879 | |||
8073b93182 | |||
9b1a9aafc1 | |||
c6b6c8123f | |||
87c10faaac | |||
d1b11539ec | |||
c744bf418a | |||
5873aeeb69 | |||
d8919d88f0 | |||
b67719ab32 | |||
a8f50297eb | |||
3128d3fb40 | |||
a2dba5bf6d | |||
40c00ca2af | |||
8e25ec55bf | |||
8881950e6d | |||
51bcee5d7d | |||
15a25f0ef3 | |||
f3902ffc16 | |||
8208f6252b | |||
ca73621f4d | |||
4e9988877a | |||
ac721c695c | |||
d793160bce | |||
b65b89baaf | |||
edea7d2220 | |||
bf8bc4df4b | |||
d13635533a | |||
37cd4cbb3b | |||
3d29755842 | |||
5b47d48fc2 | |||
73934e25e5 | |||
41d487d32a | |||
a0916aab6c | |||
863589ed29 | |||
85acb1cf6c | |||
09fb79099f | |||
22a7c15acc | |||
0182b840af | |||
f87249e03b | |||
dd0ca2d16e | |||
24f258848c | |||
f2e35ec03d | |||
ae1d9cf7a8 | |||
1bbc597671 | |||
4d6467e435 | |||
17039dc5e9 | |||
30dcbee2cc | |||
08901f3c26 | |||
c746004f73 | |||
09aa44ddcb | |||
992e76ac61 | |||
167a43f72e | |||
fe1e45bd69 | |||
4b32fb65da | |||
0546223bba | |||
857902ab95 | |||
0b57edd211 | |||
fb26d42b51 | |||
6857486f27 | |||
1abd96a9c8 | |||
08c4202852 | |||
6ecf994258 | |||
0687a3f9f8 | |||
64337dff06 | |||
34b09c75ec | |||
66c6f30c1c | |||
e413b116ac | |||
6e0b3e37ea | |||
be3f4dce50 | |||
6d87f63ed5 | |||
c92a8daeda | |||
20062faae6 | |||
678524704c | |||
2b229d8ea1 | |||
03aa99c55f | |||
20a4c054a7 | |||
3ed6cc19d2 | |||
87a9387e62 | |||
8530d01e10 | |||
a34e7be9c2 | |||
ee3744cb0a | |||
2830a407ba | |||
ccfb6709c7 | |||
a1c5131304 | |||
44401988d1 | |||
6e8eaf7593 | |||
645e91ed23 | |||
9e7e8a64f6 | |||
152b2762c3 | |||
82b9f4fea7 | |||
ff8d854a8d | |||
31f4b15e86 | |||
cd54cf5512 | |||
798aee47b3 | |||
dce767c1c5 | |||
60ebcc56f8 | |||
2643dfa0b8 | |||
c96ebcb197 | |||
b72d545349 | |||
311bb79870 | |||
f495a88e91 | |||
8b0c14cae0 | |||
b7a6a018e0 | |||
46e0fa4825 | |||
86009f2261 | |||
146f367188 | |||
bc4d029a87 | |||
960eee3d43 | |||
30315e788f | |||
073d06c0ae | |||
8fea969912 | |||
139876f96a | |||
f3c4640d12 | |||
c1c4411957 | |||
3f272bf291 | |||
2921448f8b | |||
952c207a62 | |||
6ba4d7be98 | |||
854101bb49 | |||
a5ab5c7d8e | |||
286d6c3a4c | |||
c2a7b902a9 | |||
09585c3874 | |||
f84c4c1611 | |||
2c2d874ac8 | |||
72a0765ec6 | |||
cca4d5a670 | |||
bb4a1bdc05 | |||
1aed5905d7 | |||
175e9f58c1 | |||
77a534385c | |||
2ed7acbc31 | |||
3a243eee6e | |||
8632bafcd7 | |||
449aed1a53 | |||
a20abae21c | |||
7423f47daa | |||
f09578c091 | |||
29715028d1 | |||
e5034c6ae9 | |||
a09866d35b | |||
1824b4138b | |||
764ff4bf53 | |||
e8b634c33e | |||
8871f788d2 | |||
e9c54011e9 | |||
6b795e52c3 | |||
b5bdf2d7d7 | |||
3fd6887975 | |||
eecffb0ea5 | |||
fd7107694a | |||
cca4760f69 | |||
a1ff55b462 | |||
ddac9dc6cb | |||
2bbf84233e | |||
82f8460243 | |||
59ea20745f | |||
3d3dd58501 | |||
3324e1bc43 | |||
6202f3ab44 | |||
453862cfc2 | |||
a8e2c157de | |||
080965fb17 | |||
7200329a6b | |||
f170aaa42b | |||
c598748549 | |||
ed11c4c4f9 | |||
283f198ba8 | |||
15913d6382 | |||
65e047974c | |||
c529e35791 | |||
3342851005 | |||
c167f87ce5 | |||
e8fc3427c6 | |||
26b79a5382 | |||
9ec0935a1c | |||
c6d0d28557 | |||
e26215578c | |||
d6fc28e120 | |||
115a85301c | |||
29c5b80f19 | |||
0c8bb1c22e | |||
6292690250 | |||
20b1c2bd49 | |||
e210e4ed62 | |||
fb98da86c4 | |||
c8b3c773c7 | |||
61b1356976 | |||
487f9ddc80 | |||
9b7ce556e3 | |||
e17cf27e5a | |||
937a1e269d | |||
803a97237c | |||
e64f7faa3f | |||
15d660b117 | |||
544f7fb6d1 | |||
c4eb198a8d | |||
7067d1ae56 | |||
d80edd1ed3 | |||
aeefc11f25 |
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>abstract-document</artifactId>
|
||||
<dependencies>
|
||||
|
@ -27,6 +27,22 @@ Use the Abstract Factory pattern when
|
||||
* a system should be configured with one of multiple families of products
|
||||
* a family of related product objects is designed to be used together, and you need to enforce this constraint
|
||||
* you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations
|
||||
* the lifetime of the dependency is conceptually shorter than the lifetime of the consumer.
|
||||
* you need a run-time value to construct a particular dependency
|
||||
* you want to decide which product to call from a family at runtime.
|
||||
* you need to supply one or more parameters only known at run-time before you can resolve a dependency.
|
||||
|
||||
## Use Cases:
|
||||
|
||||
* Selecting to call the appropriate implementation of FileSystemAcmeService or DatabaseAcmeService or NetworkAcmeService at runtime.
|
||||
* Unit test case writing becomes much easier
|
||||
|
||||
## Consequences:
|
||||
|
||||
* Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.
|
||||
|
||||
|
||||
|
||||
|
||||
## Real world examples
|
||||
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>abstract-factory</artifactId>
|
||||
<dependencies>
|
||||
|
@ -28,6 +28,9 @@ import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test for abstract factory
|
||||
*/
|
||||
public class AbstractFactoryTest {
|
||||
|
||||
private App app = new App();
|
||||
|
@ -21,6 +21,9 @@ incompatible interfaces.
|
||||
|
||||

|
||||
|
||||
## General usage of Adapter Pattern:
|
||||
+ Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
|
||||
|
||||
## Applicability
|
||||
Use the Adapter pattern when
|
||||
|
||||
@ -28,6 +31,19 @@ Use the Adapter pattern when
|
||||
* you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces
|
||||
* you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
|
||||
|
||||
## Consequences:
|
||||
Class and object adapters have different trade-offs. A class adapter
|
||||
|
||||
* adapts Adaptee to Target by committing to a concrete Adaptee class. As a consequence, a class adapter won’t work when we want to adapt a class and all its subclasses.
|
||||
* let’s Adapter override some of Adaptee’s behavior, since Adapter is a subclass of Adaptee.
|
||||
* introduces only one object, and no additional pointer indirection is needed to get to the adaptee.
|
||||
|
||||
An object adapter
|
||||
|
||||
* let’s a single Adapter work with many Adaptees—that is, the Adaptee itself and all of its subclasses (if any). The Adapter can also add functionality to all Adaptees at once.
|
||||
* makes it harder to override Adaptee behavior. It will require subclassing Adaptee and making Adapter refer to the subclass rather than the Adaptee itself.
|
||||
|
||||
|
||||
## Real world examples
|
||||
|
||||
* [java.util.Arrays#asList()](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList%28T...%29)
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>adapter</artifactId>
|
||||
<dependencies>
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>aggregator-microservices</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -25,6 +25,9 @@ package com.iluwatar.aggregator.microservices;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Spring Boot EntryPoint Class
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
|
||||
|
@ -22,15 +22,18 @@
|
||||
*/
|
||||
package com.iluwatar.aggregator.microservices;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test Aggregation of domain objects
|
||||
*/
|
||||
public class AggregatorTest {
|
||||
|
||||
@InjectMocks
|
||||
@ -64,4 +67,4 @@ public class AggregatorTest {
|
||||
assertEquals(inventories, testProduct.getProductInventories());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>aggregator-microservices</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -26,6 +26,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Controller providing endpoints to retrieve information about products
|
||||
*/
|
||||
@RestController
|
||||
public class InformationController {
|
||||
|
||||
|
@ -25,6 +25,9 @@ package com.iluwatar.information.microservice;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test for Information Rest Controller
|
||||
*/
|
||||
public class InformationControllerTest {
|
||||
|
||||
@Test
|
||||
@ -36,4 +39,4 @@ public class InformationControllerTest {
|
||||
Assert.assertEquals("The Product Title.", title);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>aggregator-microservices</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -26,6 +26,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Controller providing endpoints to retrieve product inventories
|
||||
*/
|
||||
@RestController
|
||||
public class InventoryController {
|
||||
|
||||
|
@ -25,8 +25,10 @@ package com.iluwatar.inventory.microservice;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test Inventory Rest Controller
|
||||
*/
|
||||
public class InventoryControllerTest {
|
||||
|
||||
@Test
|
||||
public void testGetProductInventories() throws Exception {
|
||||
InventoryController inventoryController = new InventoryController();
|
||||
@ -35,4 +37,4 @@ public class InventoryControllerTest {
|
||||
|
||||
Assert.assertEquals(5, numberOfInventories);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>aggregator-microservices</artifactId>
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>api-gateway</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>api-gateway-service</artifactId>
|
||||
|
@ -35,7 +35,7 @@ import java.io.IOException;
|
||||
* An adapter to communicate with the Image microservice
|
||||
*/
|
||||
@Component
|
||||
public class ImageClientImpl implements ImageClient{
|
||||
public class ImageClientImpl implements ImageClient {
|
||||
/**
|
||||
* Makes a simple HTTP Get request to the Image microservice
|
||||
* @return The path to the image
|
||||
|
@ -35,7 +35,7 @@ import java.io.IOException;
|
||||
* An adapter to communicate with the Price microservice
|
||||
*/
|
||||
@Component
|
||||
public class PriceClientImpl implements PriceClient{
|
||||
public class PriceClientImpl implements PriceClient {
|
||||
/**
|
||||
* Makes a simple HTTP Get request to the Price microservice
|
||||
* @return The price of the product
|
||||
|
@ -22,15 +22,18 @@
|
||||
*/
|
||||
package com.iluwatar.api.gateway;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test API Gateway Pattern
|
||||
*/
|
||||
public class ApiGatewayTest {
|
||||
|
||||
@InjectMocks
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>api-gateway</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
@ -25,6 +25,9 @@ package com.iluwatar.image.microservice;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test for Image Rest Controller
|
||||
*/
|
||||
public class ImageControllerTest {
|
||||
@Test
|
||||
public void testGetImagePath() {
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>api-gateway</artifactId>
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>api-gateway</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
@ -25,6 +25,10 @@ package com.iluwatar.price.microservice;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
/**
|
||||
* Test for Price Rest Controller
|
||||
*/
|
||||
public class PriceControllerTest {
|
||||
@Test
|
||||
public void testgetPrice() {
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>async-method-invocation</artifactId>
|
||||
<dependencies>
|
||||
|
@ -25,8 +25,8 @@ package com.iluwatar.async.method.invocation;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
*
|
||||
* AsyncResult interface
|
||||
* @param <T> parameter returned when getValue is invoked
|
||||
*/
|
||||
public interface AsyncResult<T> {
|
||||
|
||||
|
27
balking/README.md
Normal file
27
balking/README.md
Normal file
@ -0,0 +1,27 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Balking
|
||||
folder: balking
|
||||
permalink: /patterns/balking/
|
||||
categories: Concurrency
|
||||
tags:
|
||||
- Java
|
||||
- Difficulty-Beginner
|
||||
---
|
||||
|
||||
## Intent
|
||||
Balking Pattern is used to prevent an object from executing certain code if it is an
|
||||
incomplete or inappropriate state
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
Use the Balking pattern when
|
||||
|
||||
*you want to invoke an action on an object only when it is in a particular state
|
||||
*objects are generally only in a state that is prone to balking temporarily
|
||||
but for an unknown amount of time
|
||||
|
||||
## Related patterns
|
||||
* Guarded Suspendion Pattern
|
||||
* Double Checked Locking Pattern
|
BIN
balking/etc/balking.png
Normal file
BIN
balking/etc/balking.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 21 KiB |
46
balking/etc/balking.ucls
Normal file
46
balking/etc/balking.ucls
Normal file
@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.13" icons="true" automaticImage="PNG" always-add-relationships="false"
|
||||
generalizations="true" realizations="true" associations="true" dependencies="false" nesting-relationships="true"
|
||||
router="FAN">
|
||||
<class id="1" language="java" name="com.iluwatar.balking.App" project="balking"
|
||||
file="/balking/src/main/java/com/iluwatar/balking/App.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="113" width="114" x="135" y="103"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="2" language="java" name="com.iluwatar.balking.WashingMachine" project="balking"
|
||||
file="/balking/src/main/java/com/iluwatar/balking/WashingMachine.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="149" width="268" x="289" y="103"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<enumeration id="3" language="java" name="com.iluwatar.balking.WashingMachineState" project="balking"
|
||||
file="/balking/src/main/java/com/iluwatar/balking/WashingMachineState.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="113" width="192" x="289" y="292"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</enumeration>
|
||||
<association id="4">
|
||||
<end type="SOURCE" refId="2" navigable="false">
|
||||
<attribute id="5" name="washingMachineState"/>
|
||||
<multiplicity id="6" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="3" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
24
balking/etc/balking.urm.puml
Normal file
24
balking/etc/balking.urm.puml
Normal file
@ -0,0 +1,24 @@
|
||||
@startuml
|
||||
package com.iluwatar.balking {
|
||||
class App {
|
||||
- LOGGER : Logger {static}
|
||||
+ App()
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
class WashingMachine {
|
||||
- LOGGER : Logger {static}
|
||||
- washingMachineState : WashingMachineState
|
||||
+ WashingMachine()
|
||||
+ endOfWashing()
|
||||
+ getWashingMachineState() : WashingMachineState
|
||||
+ wash()
|
||||
}
|
||||
enum WashingMachineState {
|
||||
+ ENABLED {static}
|
||||
+ WASHING {static}
|
||||
+ valueOf(name : String) : WashingMachineState {static}
|
||||
+ values() : WashingMachineState[] {static}
|
||||
}
|
||||
}
|
||||
WashingMachine --> "-washingMachineState" WashingMachineState
|
||||
@enduml
|
46
balking/pom.xml
Normal file
46
balking/pom.xml
Normal file
@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
|
||||
The MIT License
|
||||
Copyright (c) 2014 Ilkka Seppälä
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>balking</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
65
balking/src/main/java/com/iluwatar/balking/App.java
Normal file
65
balking/src/main/java/com/iluwatar/balking/App.java
Normal file
@ -0,0 +1,65 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.balking;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* In Balking Design Pattern if an object’s method is invoked when it is in an inappropriate state,
|
||||
* then the method will return without doing anything. Objects that use this pattern are generally only in a
|
||||
* state that is prone to balking temporarily but for an unknown amount of time
|
||||
*
|
||||
* In this example implementation WashingMachine is an object that has two states
|
||||
* in which it can be: ENABLED and WASHING. If the machine is ENABLED
|
||||
* the state is changed into WASHING that any other thread can't invoke this action on this and then do the job.
|
||||
* On the other hand if it have been already washing and any other thread execute wash()
|
||||
* it can't do that once again and returns doing nothing.
|
||||
*/
|
||||
|
||||
public class App {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* @param args the command line arguments - not used
|
||||
*/
|
||||
public static void main(String... args) {
|
||||
final WashingMachine washingMachine = new WashingMachine();
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
executorService.execute(washingMachine::wash);
|
||||
}
|
||||
executorService.shutdown();
|
||||
try {
|
||||
executorService.awaitTermination(10, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException ie) {
|
||||
LOGGER.error("ERROR: Waiting on executor service shutdown!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.balking;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Washing machine class
|
||||
*/
|
||||
public class WashingMachine {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(WashingMachine.class);
|
||||
|
||||
private WashingMachineState washingMachineState;
|
||||
|
||||
public WashingMachine() {
|
||||
washingMachineState = WashingMachineState.ENABLED;
|
||||
}
|
||||
|
||||
public WashingMachineState getWashingMachineState() {
|
||||
return washingMachineState;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method responsible for washing
|
||||
* if the object is in appropriate state
|
||||
*/
|
||||
public void wash() {
|
||||
synchronized (this) {
|
||||
LOGGER.info("{}: Actual machine state: {}", Thread.currentThread().getName(), getWashingMachineState());
|
||||
if (washingMachineState == WashingMachineState.WASHING) {
|
||||
LOGGER.error("ERROR: Cannot wash if the machine has been already washing!");
|
||||
return;
|
||||
}
|
||||
washingMachineState = WashingMachineState.WASHING;
|
||||
}
|
||||
LOGGER.info("{}: Doing the washing", Thread.currentThread().getName());
|
||||
try {
|
||||
Thread.sleep(50);
|
||||
} catch (InterruptedException ie) {
|
||||
ie.printStackTrace();
|
||||
}
|
||||
endOfWashing();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method responsible of ending the washing
|
||||
* by changing machine state
|
||||
*/
|
||||
public synchronized void endOfWashing() {
|
||||
washingMachineState = WashingMachineState.ENABLED;
|
||||
LOGGER.info("{}: Washing completed.", Thread.currentThread().getId());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.balking;
|
||||
|
||||
/**
|
||||
* WashingMachineState enum describes in which state machine is,
|
||||
* it can be enabled and ready to work as well as during washing
|
||||
*/
|
||||
|
||||
public enum WashingMachineState {
|
||||
ENABLED, WASHING
|
||||
}
|
39
balking/src/test/java/com/iluwatar/balking/AppTest.java
Normal file
39
balking/src/test/java/com/iluwatar/balking/AppTest.java
Normal file
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.balking;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void main() throws Exception {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.balking;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Tests for {@link WashingMachine}
|
||||
*/
|
||||
public class WashingMachineTest {
|
||||
|
||||
private volatile WashingMachineState machineStateGlobal;
|
||||
|
||||
@Test
|
||||
public void wash() throws Exception {
|
||||
WashingMachine washingMachine = new WashingMachine();
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||
executorService.execute(washingMachine::wash);
|
||||
executorService.execute(() -> {
|
||||
washingMachine.wash();
|
||||
machineStateGlobal = washingMachine.getWashingMachineState();
|
||||
});
|
||||
executorService.shutdown();
|
||||
try {
|
||||
executorService.awaitTermination(10, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException ie) {
|
||||
ie.printStackTrace();
|
||||
}
|
||||
assertEquals(WashingMachineState.WASHING, machineStateGlobal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void endOfWashing() throws Exception {
|
||||
WashingMachine washingMachine = new WashingMachine();
|
||||
washingMachine.wash();
|
||||
assertEquals(WashingMachineState.ENABLED, washingMachine.getWashingMachineState());
|
||||
}
|
||||
|
||||
}
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>bridge</artifactId>
|
||||
<dependencies>
|
||||
|
@ -86,15 +86,15 @@ package com.iluwatar.builder {
|
||||
+ values() : Weapon[] {static}
|
||||
}
|
||||
}
|
||||
Hero --> "-profession" Profession
|
||||
Builder ..+ Hero
|
||||
Hero --> "-profession" Profession
|
||||
Hero --> "-armor" Armor
|
||||
Builder --> "-hairColor" HairColor
|
||||
Builder --> "-weapon" Weapon
|
||||
Builder --> "-hairType" HairType
|
||||
Builder --> "-hairColor" HairColor
|
||||
Hero --> "-hairColor" HairColor
|
||||
Builder --> "-profession" Profession
|
||||
Hero --> "-weapon" Weapon
|
||||
Hero --> "-hairType" HairType
|
||||
Hero --> "-weapon" Weapon
|
||||
Builder --> "-armor" Armor
|
||||
@enduml
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>builder</artifactId>
|
||||
<dependencies>
|
||||
|
@ -46,8 +46,8 @@ package com.iluwatar.business.delegate {
|
||||
+ values() : ServiceType[] {static}
|
||||
}
|
||||
}
|
||||
BusinessDelegate --> "-serviceType" ServiceType
|
||||
BusinessLookup --> "-ejbService" EjbService
|
||||
BusinessDelegate --> "-serviceType" ServiceType
|
||||
Client --> "-businessDelegate" BusinessDelegate
|
||||
BusinessDelegate --> "-businessService" BusinessService
|
||||
BusinessDelegate --> "-lookupService" BusinessLookup
|
||||
|
@ -30,7 +30,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>business-delegate</artifactId>
|
||||
<dependencies>
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>caching</artifactId>
|
||||
<dependencies>
|
||||
|
@ -167,7 +167,7 @@ public class LruCache {
|
||||
* Returns cache data in list form.
|
||||
*/
|
||||
public List<UserAccount> getCacheDataInListForm() {
|
||||
ArrayList<UserAccount> listOfCacheData = new ArrayList<>();
|
||||
List<UserAccount> listOfCacheData = new ArrayList<>();
|
||||
Node temp = head;
|
||||
while (temp != null) {
|
||||
listOfCacheData.add(temp.userAccount);
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>callback</artifactId>
|
||||
<dependencies>
|
||||
|
@ -53,8 +53,8 @@ package com.iluwatar.chain {
|
||||
}
|
||||
}
|
||||
RequestHandler --> "-next" RequestHandler
|
||||
OrcKing --> "-chain" RequestHandler
|
||||
Request --> "-requestType" RequestType
|
||||
OrcKing --> "-chain" RequestHandler
|
||||
OrcCommander --|> RequestHandler
|
||||
OrcOfficer --|> RequestHandler
|
||||
OrcSoldier --|> RequestHandler
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>chain</artifactId>
|
||||
<dependencies>
|
||||
|
@ -32,10 +32,10 @@
|
||||
Source = https://github.com/checkstyle/checkstyle/tree/master/src/main/resources
|
||||
|
||||
Checkstyle configurartion that checks the Google coding conventions from:
|
||||
|
||||
|
||||
- Google Java Style
|
||||
https://google-styleguide.googlecode.com/svn-history/r130/trunk/javaguide.html
|
||||
|
||||
|
||||
Checkstyle is very configurable. Be sure to read the documentation at
|
||||
http://checkstyle.sf.net (or in your downloaded distribution).
|
||||
|
||||
@ -44,12 +44,12 @@
|
||||
To completely disable a check, just comment it out or delete it from the file.
|
||||
|
||||
Authors: Max Vetrenko, Ruslan Diachenko, Roman Ivanov.
|
||||
|
||||
|
||||
-->
|
||||
|
||||
<module name="Checker">
|
||||
<property name="charset" value="UTF-8"/>
|
||||
|
||||
|
||||
<property name="fileExtensions" value="java, xml, properties"/>
|
||||
|
||||
<property name="severity" value="error"/>
|
||||
@ -77,7 +77,6 @@
|
||||
<property name="max" value="120"/>
|
||||
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
|
||||
</module>
|
||||
<module name="AvoidStarImport"/>
|
||||
<module name="OneTopLevelClass"/>
|
||||
<module name="NoLineWrap"/>
|
||||
<module name="EmptyBlock">
|
||||
@ -120,7 +119,7 @@
|
||||
<property name="tokens" value="COMMA"/>
|
||||
<property name="option" value="EOL"/>
|
||||
</module>
|
||||
|
||||
|
||||
<!-- Checks for Naming Conventions. -->
|
||||
<!-- See http://checkstyle.sf.net/config_naming.html -->
|
||||
<module name="ConstantName"/>
|
||||
@ -185,6 +184,10 @@
|
||||
<property name="allowedAnnotations" value="Override, Test, Before, After, Parameters, Given, When, BeforeClass, AfterClass, Parameterized"/>
|
||||
<property name="allowThrowsTagsForSubclasses" value="true"/>
|
||||
</module>
|
||||
<module name="JavadocType">
|
||||
<property name="scope" value="public"/>
|
||||
<property name="allowUnknownTags" value="true"/>
|
||||
</module>
|
||||
<module name="MethodName">
|
||||
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9_]*$"/>
|
||||
<message key="name.invalidPattern"
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>command</artifactId>
|
||||
<dependencies>
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>composite</artifactId>
|
||||
<dependencies>
|
||||
|
29
converter/README.md
Normal file
29
converter/README.md
Normal file
@ -0,0 +1,29 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Converter
|
||||
folder: converter
|
||||
permalink: /patterns/converter/
|
||||
categories:
|
||||
tags:
|
||||
- Java
|
||||
- Difficulty-Beginner
|
||||
---
|
||||
|
||||
## Intent
|
||||
The purpose of the Converter Pattern is to provide a generic, common way of bidirectional
|
||||
conversion between corresponding types, allowing a clean implementation in which the types do not
|
||||
need to be aware of each other. Moreover, the Converter Pattern introduces bidirectional collection
|
||||
mapping, reducing a boilerplate code to minimum.
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
Use the Converter Pattern in the following situations:
|
||||
|
||||
* When you have types that logically correspond which other and you need to convert entities between them
|
||||
* When you want to provide different ways of types conversions depending on a context
|
||||
* Whenever you introduce a DTO (Data transfer object), you will probably need to convert it into the domain equivalence
|
||||
|
||||
## Credits
|
||||
|
||||
* [Converter](http://www.xsolve.pl/blog/converter-pattern-in-java-8/)
|
BIN
converter/etc/Converter.png
Normal file
BIN
converter/etc/Converter.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 19 KiB |
51
converter/etc/Converter.ucls
Normal file
51
converter/etc/Converter.ucls
Normal file
@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.14" icons="true" automaticImage="PNG" always-add-relationships="false"
|
||||
generalizations="true" realizations="true" associations="true" dependencies="false" nesting-relationships="true"
|
||||
router="FAN">
|
||||
<class id="1" language="java" name="com.iluwatar.converter.Converter" project="converter"
|
||||
file="/converter/src/main/java/com/iluwatar/converter/Converter.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="189" width="226" x="41" y="37"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="2" language="java" name="com.iluwatar.converter.UserConverter" project="converter"
|
||||
file="/converter/src/main/java/com/iluwatar/converter/UserConverter.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="81" width="107" x="41" y="356"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="3" language="java" name="com.iluwatar.converter.User" project="converter"
|
||||
file="/converter/src/main/java/com/iluwatar/converter/User.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="279" width="188" x="307" y="37"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="4" language="java" name="com.iluwatar.converter.UserDto" project="converter"
|
||||
file="/converter/src/main/java/com/iluwatar/converter/UserDto.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="279" width="204" x="535" y="37"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<generalization id="5">
|
||||
<end type="SOURCE" refId="2"/>
|
||||
<end type="TARGET" refId="1"/>
|
||||
</generalization>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
49
converter/etc/converter.urm.puml
Normal file
49
converter/etc/converter.urm.puml
Normal file
@ -0,0 +1,49 @@
|
||||
@startuml
|
||||
package com.iluwatar.converter {
|
||||
class App {
|
||||
+ App()
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
class Converter<T, U> {
|
||||
- fromDto : Function<T, U>
|
||||
- fromEntity : Function<U, T>
|
||||
+ Converter<T, U>(fromDto : Function<T, U>, fromEntity : Function<U, T>)
|
||||
+ convertFromDto(userDto : T) : U
|
||||
+ convertFromEntity(user : U) : T
|
||||
+ createFromDtos(dtoUsers : Collection<T>) : List<U>
|
||||
+ createFromEntities(users : Collection<U>) : List<T>
|
||||
}
|
||||
class User {
|
||||
- firstName : String
|
||||
- isActive : boolean
|
||||
- lastName : String
|
||||
- userId : String
|
||||
+ User(firstName : String, lastName : String, isActive : boolean, userId : String)
|
||||
+ equals(o : Object) : boolean
|
||||
+ getFirstName() : String
|
||||
+ getLastName() : String
|
||||
+ getUserId() : String
|
||||
+ hashCode() : int
|
||||
+ isActive() : boolean
|
||||
+ toString() : String
|
||||
}
|
||||
class UserConverter {
|
||||
+ UserConverter()
|
||||
}
|
||||
class UserDto {
|
||||
- email : String
|
||||
- firstName : String
|
||||
- isActive : boolean
|
||||
- lastName : String
|
||||
+ UserDto(firstName : String, lastName : String, isActive : boolean, email : String)
|
||||
+ equals(o : Object) : boolean
|
||||
+ getEmail() : String
|
||||
+ getFirstName() : String
|
||||
+ getLastName() : String
|
||||
+ hashCode() : int
|
||||
+ isActive() : boolean
|
||||
+ toString() : String
|
||||
}
|
||||
}
|
||||
UserConverter --|> Converter
|
||||
@enduml
|
49
converter/pom.xml
Normal file
49
converter/pom.xml
Normal file
@ -0,0 +1,49 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
|
||||
The MIT License
|
||||
Copyright (c) 2014 Ilkka Seppälä
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<artifactId>converter</artifactId>
|
||||
|
||||
|
||||
</project>
|
63
converter/src/main/java/com/iluwatar/converter/App.java
Normal file
63
converter/src/main/java/com/iluwatar/converter/App.java
Normal file
@ -0,0 +1,63 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.converter;
|
||||
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The Converter pattern is a behavioral design pattern which allows a common way of bidirectional
|
||||
* conversion between corresponding types (e.g. DTO and domain representations of the logically
|
||||
* isomorphic types). Moreover, the pattern introduces a common way of converting a collection of
|
||||
* objects between types.
|
||||
*/
|
||||
public class App {
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Converter<UserDto, User> userConverter = new Converter<>(
|
||||
userDto -> new User(userDto.getFirstName(), userDto.getLastName(), userDto.isActive(),
|
||||
userDto.getEmail()),
|
||||
user -> new UserDto(user.getFirstName(), user.getLastName(), user.isActive(), user.getUserId()));
|
||||
|
||||
UserDto dtoUser = new UserDto("John", "Doe", true, "whatever[at]wherever.com");
|
||||
User user = userConverter.convertFromDto(dtoUser);
|
||||
System.out.println("Entity converted from DTO:" + user);
|
||||
|
||||
ArrayList<User> users = Lists.newArrayList(new User("Camile", "Tough", false, "124sad"),
|
||||
new User("Marti", "Luther", true, "42309fd"), new User("Kate", "Smith", true, "if0243"));
|
||||
System.out.println("Domain entities:");
|
||||
users.forEach(System.out::println);
|
||||
|
||||
System.out.println("DTO entities converted from domain:");
|
||||
List<UserDto> dtoEntities = userConverter.createFromEntities(users);
|
||||
dtoEntities.forEach(System.out::println);
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.converter;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Generic converter, thanks to Java8 features not only provides a way of generic bidirectional
|
||||
* conversion between coresponding types, but also a common way of converting a collection of objects
|
||||
* of the same type, reducing boilerplate code to the absolute minimum.
|
||||
* @param <T> DTO representation's type
|
||||
* @param <U> Domain representation's type
|
||||
*/
|
||||
public class Converter<T, U> {
|
||||
|
||||
private final Function<T, U> fromDto;
|
||||
private final Function<U, T> fromEntity;
|
||||
|
||||
/**
|
||||
* @param fromDto Function that converts given dto entity into the domain entity.
|
||||
* @param fromEntity Function that converts given domain entity into the dto entity.
|
||||
*/
|
||||
public Converter(final Function<T, U> fromDto, final Function<U, T> fromEntity) {
|
||||
this.fromDto = fromDto;
|
||||
this.fromEntity = fromEntity;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param userDto DTO entity
|
||||
* @return The domain representation - the result of the converting function application on dto entity.
|
||||
*/
|
||||
public final U convertFromDto(final T userDto) {
|
||||
return fromDto.apply(userDto);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param user domain entity
|
||||
* @return The DTO representation - the result of the converting function application on domain entity.
|
||||
*/
|
||||
public final T convertFromEntity(final U user) {
|
||||
return fromEntity.apply(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param dtoUsers collection of DTO entities
|
||||
* @return List of domain representation of provided entities retrieved by
|
||||
* mapping each of them with the convertion function
|
||||
*/
|
||||
public final List<U> createFromDtos(final Collection<T> dtoUsers) {
|
||||
return dtoUsers.stream().map(this::convertFromDto).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param users collection of domain entities
|
||||
* @return List of domain representation of provided entities retrieved by
|
||||
* mapping each of them with the convertion function
|
||||
*/
|
||||
public final List<T> createFromEntities(final Collection<U> users) {
|
||||
return users.stream().map(this::convertFromEntity).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
86
converter/src/main/java/com/iluwatar/converter/User.java
Normal file
86
converter/src/main/java/com/iluwatar/converter/User.java
Normal file
@ -0,0 +1,86 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.converter;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* User class
|
||||
*/
|
||||
public class User {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private boolean isActive;
|
||||
private String userId;
|
||||
|
||||
/**
|
||||
* @param firstName user's first name
|
||||
* @param lastName user's last name
|
||||
* @param isActive flag indicating whether the user is active
|
||||
* @param userId user's identificator
|
||||
*/
|
||||
public User(String firstName, String lastName, boolean isActive, String userId) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.isActive = isActive;
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return isActive;
|
||||
}
|
||||
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
@Override public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
User user = (User) o;
|
||||
return isActive == user.isActive && Objects.equals(firstName, user.firstName) && Objects
|
||||
.equals(lastName, user.lastName) && Objects.equals(userId, user.userId);
|
||||
}
|
||||
|
||||
@Override public int hashCode() {
|
||||
return Objects.hash(firstName, lastName, isActive, userId);
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return "User{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\''
|
||||
+ ", isActive=" + isActive + ", userId='" + userId + '\'' + '}';
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.converter;
|
||||
|
||||
/**
|
||||
* Example implementation of the simple User converter.
|
||||
*/
|
||||
public class UserConverter extends Converter<UserDto, User> {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public UserConverter() {
|
||||
super(userDto -> new User(userDto.getFirstName(), userDto.getLastName(), userDto.isActive(),
|
||||
userDto.getEmail()),
|
||||
user -> new UserDto(user.getFirstName(), user.getLastName(), user.isActive(),
|
||||
user.getUserId()));
|
||||
}
|
||||
}
|
88
converter/src/main/java/com/iluwatar/converter/UserDto.java
Normal file
88
converter/src/main/java/com/iluwatar/converter/UserDto.java
Normal file
@ -0,0 +1,88 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.converter;
|
||||
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* User DTO class
|
||||
*/
|
||||
public class UserDto {
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private boolean isActive;
|
||||
private String email;
|
||||
|
||||
/**
|
||||
* @param firstName user's first name
|
||||
* @param lastName user's last name
|
||||
* @param isActive flag indicating whether the user is active
|
||||
* @param email user's email address
|
||||
*/
|
||||
public UserDto(String firstName, String lastName, boolean isActive, String email) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.isActive = isActive;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return isActive;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
@Override public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
UserDto userDto = (UserDto) o;
|
||||
return isActive == userDto.isActive && Objects.equals(firstName, userDto.firstName) && Objects
|
||||
.equals(lastName, userDto.lastName) && Objects.equals(email, userDto.email);
|
||||
}
|
||||
|
||||
@Override public int hashCode() {
|
||||
return Objects.hash(firstName, lastName, isActive, email);
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return "UserDto{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\''
|
||||
+ ", isActive=" + isActive + ", email='" + email + '\'' + '}';
|
||||
}
|
||||
}
|
38
converter/src/test/java/com/iluwatar/converter/AppTest.java
Normal file
38
converter/src/test/java/com/iluwatar/converter/AppTest.java
Normal file
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.converter;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* App running test
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void testMain() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.converter;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
/**
|
||||
* Tests for {@link Converter}
|
||||
*/
|
||||
public class ConverterTest {
|
||||
|
||||
private UserConverter userConverter = new UserConverter();
|
||||
|
||||
/**
|
||||
* Tests whether a converter created of opposite functions holds equality as a bijection.
|
||||
*/
|
||||
@Test public void testConversionsStartingFromDomain() {
|
||||
User u1 = new User("Tom", "Hanks", true, "tom@hanks.com");
|
||||
User u2 = userConverter.convertFromDto(userConverter.convertFromEntity(u1));
|
||||
assertEquals(u1, u2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether a converter created of opposite functions holds equality as a bijection.
|
||||
*/
|
||||
@Test public void testConversionsStartingFromDto() {
|
||||
UserDto u1 = new UserDto("Tom", "Hanks", true, "tom@hanks.com");
|
||||
UserDto u2 = userConverter.convertFromEntity(userConverter.convertFromDto(u1));
|
||||
assertEquals(u1, u2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the custom users converter. Thanks to Java8 lambdas, converter can be easily and
|
||||
* cleanly instantiated allowing various different conversion strategies to be implemented.
|
||||
*/
|
||||
@Test public void testCustomConverter() {
|
||||
Converter<UserDto, User> converter = new Converter<>(
|
||||
userDto -> new User(userDto.getFirstName(), userDto.getLastName(), userDto.isActive(),
|
||||
String.valueOf(new Random().nextInt())),
|
||||
user -> new UserDto(user.getFirstName(), user.getLastName(), user.isActive(),
|
||||
user.getFirstName().toLowerCase() + user.getLastName().toLowerCase() + "@whatever.com"));
|
||||
User u1 = new User("John", "Doe", false, "12324");
|
||||
UserDto userDto = converter.convertFromEntity(u1);
|
||||
assertEquals(userDto.getEmail(), "johndoe@whatever.com");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test whether converting a collection of Users to DTO Users and then converting them back to domain
|
||||
* users returns an equal collection.
|
||||
*/
|
||||
@Test public void testCollectionConversion() {
|
||||
ArrayList<User> users = Lists.newArrayList(new User("Camile", "Tough", false, "124sad"),
|
||||
new User("Marti", "Luther", true, "42309fd"), new User("Kate", "Smith", true, "if0243"));
|
||||
List<User> fromDtos = userConverter.createFromDtos(userConverter.createFromEntities(users));
|
||||
assertEquals(fromDtos, users);
|
||||
}
|
||||
}
|
29
cqrs/README.md
Normal file
29
cqrs/README.md
Normal file
@ -0,0 +1,29 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: CQRS
|
||||
folder: cqrs
|
||||
permalink: /patterns/cqrs/
|
||||
pumlid: 7SPR4a0m3030gt00pR_RH6I8QQFouFgC_TfHb6gkd5Q7FQBx363ub4rYpoMTZKuDrYXqDX37HIuuyCPfPPTDfuuHREhGqBy0NUR0GNzAMYizMtq1
|
||||
categories: Architectural
|
||||
tags:
|
||||
- Java
|
||||
- Difficulty-Intermediate
|
||||
---
|
||||
|
||||
## Intent
|
||||
CQRS Command Query Responsibility Segregation - Separate the query side from the command side.
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
Use the CQRS pattern when
|
||||
|
||||
* you want to scale the queries and commands independently.
|
||||
* you want to use different data models for queries and commands. Useful when dealing with complex domains.
|
||||
* you want to use architectures like event sourcing or task based UI.
|
||||
|
||||
## Credits
|
||||
|
||||
* [Greg Young - CQRS, Task Based UIs, Event Sourcing agh!](http://codebetter.com/gregyoung/2010/02/16/cqrs-task-based-uis-event-sourcing-agh/)
|
||||
* [Martin Fowler - CQRS](https://martinfowler.com/bliki/CQRS.html)
|
||||
* [Oliver Wolf - CQRS for Great Good](https://www.youtube.com/watch?v=Ge53swja9Dw)
|
BIN
cqrs/etc/cqrs.png
Normal file
BIN
cqrs/etc/cqrs.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 103 KiB |
115
cqrs/etc/cqrs.ucls
Normal file
115
cqrs/etc/cqrs.ucls
Normal file
@ -0,0 +1,115 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.2.0" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
|
||||
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
|
||||
<interface id="1" language="java" name="com.iluwatar.cqrs.commandes.ICommandService" project="cqrs"
|
||||
file="/cqrs/src/main/java/com/iluwatar/cqrs/commandes/ICommandService.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="291" y="-49"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<class id="2" language="java" name="com.iluwatar.cqrs.commandes.CommandServiceImpl" project="cqrs"
|
||||
file="/cqrs/src/main/java/com/iluwatar/cqrs/commandes/CommandServiceImpl.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="263" width="256" x="170" y="87"/>
|
||||
<display autosize="false" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<interface id="3" language="java" name="com.iluwatar.cqrs.queries.IQueryService" project="cqrs"
|
||||
file="/cqrs/src/main/java/com/iluwatar/cqrs/queries/IQueryService.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="182" width="248" x="176" y="428"/>
|
||||
<display autosize="false" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<class id="4" language="java" name="com.iluwatar.cqrs.queries.QueryServiceImpl" project="cqrs"
|
||||
file="/cqrs/src/main/java/com/iluwatar/cqrs/queries/QueryServiceImpl.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="258" width="253" x="169" y="665"/>
|
||||
<display autosize="false" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="5" language="java" name="com.iluwatar.cqrs.domain.model.Book" project="cqrs"
|
||||
file="/cqrs/src/main/java/com/iluwatar/cqrs/domain/model/Book.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="326" width="158" x="778" y="-93"/>
|
||||
<display autosize="false" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="6" language="java" name="com.iluwatar.cqrs.dto.Book" project="cqrs"
|
||||
file="/cqrs/src/main/java/com/iluwatar/cqrs/dto/Book.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="219" width="150" x="541" y="607"/>
|
||||
<display autosize="false" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="7" language="java" name="com.iluwatar.cqrs.domain.model.Author" project="cqrs"
|
||||
file="/cqrs/src/main/java/com/iluwatar/cqrs/domain/model/Author.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="608" y="70"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="8" language="java" name="com.iluwatar.cqrs.dto.Author" project="cqrs"
|
||||
file="/cqrs/src/main/java/com/iluwatar/cqrs/dto/Author.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="834" y="719"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="9" language="java" name="com.iluwatar.cqrs.util.HibernateUtil" project="cqrs"
|
||||
file="/cqrs/src/main/java/com/iluwatar/cqrs/util/HibernateUtil.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="662" y="412"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<dependency id="10">
|
||||
<end type="SOURCE" refId="4"/>
|
||||
<end type="TARGET" refId="9"/>
|
||||
</dependency>
|
||||
<dependency id="11">
|
||||
<end type="SOURCE" refId="2"/>
|
||||
<end type="TARGET" refId="9"/>
|
||||
</dependency>
|
||||
<realization id="12">
|
||||
<end type="SOURCE" refId="4"/>
|
||||
<end type="TARGET" refId="3"/>
|
||||
</realization>
|
||||
<association id="13">
|
||||
<end type="SOURCE" refId="5" navigable="false">
|
||||
<attribute id="14" name="author"/>
|
||||
<multiplicity id="15" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="7" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<realization id="16">
|
||||
<end type="SOURCE" refId="2"/>
|
||||
<end type="TARGET" refId="1"/>
|
||||
</realization>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
124
cqrs/etc/cqrs.urm.puml
Normal file
124
cqrs/etc/cqrs.urm.puml
Normal file
@ -0,0 +1,124 @@
|
||||
@startuml
|
||||
package com.iluwatar.cqrs.util {
|
||||
class HibernateUtil {
|
||||
- LOGGER : Logger {static}
|
||||
- SESSIONFACTORY : SessionFactory {static}
|
||||
+ HibernateUtil()
|
||||
- buildSessionFactory() : SessionFactory {static}
|
||||
+ getSessionFactory() : SessionFactory {static}
|
||||
}
|
||||
}
|
||||
package com.iluwatar.cqrs.app {
|
||||
class App {
|
||||
+ App()
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
}
|
||||
package com.iluwatar.cqrs.dto {
|
||||
class Author {
|
||||
- email : String
|
||||
- name : String
|
||||
- username : String
|
||||
+ Author()
|
||||
+ Author(name : String, email : String, username : String)
|
||||
+ equals(obj : Object) : boolean
|
||||
+ getEmail() : String
|
||||
+ getName() : String
|
||||
+ getUsername() : String
|
||||
+ hashCode() : int
|
||||
+ toString() : String
|
||||
}
|
||||
class Book {
|
||||
- price : double
|
||||
- title : String
|
||||
+ Book()
|
||||
+ Book(title : String, price : double)
|
||||
+ equals(obj : Object) : boolean
|
||||
+ getPrice() : double
|
||||
+ getTitle() : String
|
||||
+ hashCode() : int
|
||||
+ toString() : String
|
||||
}
|
||||
}
|
||||
package com.iluwatar.cqrs.commandes {
|
||||
class CommandServiceImpl {
|
||||
- sessionFactory : SessionFactory
|
||||
+ CommandServiceImpl()
|
||||
+ authorCreated(username : String, name : String, email : String)
|
||||
+ authorEmailUpdated(username : String, email : String)
|
||||
+ authorNameUpdated(username : String, name : String)
|
||||
+ authorUsernameUpdated(oldUsername : String, newUsername : String)
|
||||
+ bookAddedToAuthor(title : String, price : double, username : String)
|
||||
+ bookPriceUpdated(title : String, price : double)
|
||||
+ bookTitleUpdated(oldTitle : String, newTitle : String)
|
||||
- getAuthorByUsername(username : String) : Author
|
||||
- getBookByTitle(title : String) : Book
|
||||
}
|
||||
interface ICommandService {
|
||||
+ authorCreated(String, String, String) {abstract}
|
||||
+ authorEmailUpdated(String, String) {abstract}
|
||||
+ authorNameUpdated(String, String) {abstract}
|
||||
+ authorUsernameUpdated(String, String) {abstract}
|
||||
+ bookAddedToAuthor(String, double, String) {abstract}
|
||||
+ bookPriceUpdated(String, double) {abstract}
|
||||
+ bookTitleUpdated(String, String) {abstract}
|
||||
}
|
||||
}
|
||||
package com.iluwatar.cqrs.queries {
|
||||
interface IQueryService {
|
||||
+ getAuthorBooks(String) : List<Book> {abstract}
|
||||
+ getAuthorBooksCount(String) : BigInteger {abstract}
|
||||
+ getAuthorByUsername(String) : Author {abstract}
|
||||
+ getAuthorsCount() : BigInteger {abstract}
|
||||
+ getBook(String) : Book {abstract}
|
||||
}
|
||||
class QueryServiceImpl {
|
||||
- sessionFactory : SessionFactory
|
||||
+ QueryServiceImpl()
|
||||
+ getAuthorBooks(username : String) : List<Book>
|
||||
+ getAuthorBooksCount(username : String) : BigInteger
|
||||
+ getAuthorByUsername(username : String) : Author
|
||||
+ getAuthorsCount() : BigInteger
|
||||
+ getBook(title : String) : Book
|
||||
}
|
||||
}
|
||||
package com.iluwatar.cqrs.domain.model {
|
||||
class Author {
|
||||
- email : String
|
||||
- id : long
|
||||
- name : String
|
||||
- username : String
|
||||
# Author()
|
||||
+ Author(username : String, name : String, email : String)
|
||||
+ getEmail() : String
|
||||
+ getId() : long
|
||||
+ getName() : String
|
||||
+ getUsername() : String
|
||||
+ setEmail(email : String)
|
||||
+ setId(id : long)
|
||||
+ setName(name : String)
|
||||
+ setUsername(username : String)
|
||||
+ toString() : String
|
||||
}
|
||||
class Book {
|
||||
- author : Author
|
||||
- id : long
|
||||
- price : double
|
||||
- title : String
|
||||
# Book()
|
||||
+ Book(title : String, price : double, author : Author)
|
||||
+ getAuthor() : Author
|
||||
+ getId() : long
|
||||
+ getPrice() : double
|
||||
+ getTitle() : String
|
||||
+ setAuthor(author : Author)
|
||||
+ setId(id : long)
|
||||
+ setPrice(price : double)
|
||||
+ setTitle(title : String)
|
||||
+ toString() : String
|
||||
}
|
||||
}
|
||||
Book --> "-author" Author
|
||||
CommandServiceImpl ..|> ICommandService
|
||||
QueryServiceImpl ..|> IQueryService
|
||||
@enduml
|
42
cqrs/pom.xml
Normal file
42
cqrs/pom.xml
Normal file
@ -0,0 +1,42 @@
|
||||
<?xml version="1.0"?>
|
||||
<!-- The MIT License Copyright (c) 2014-2016 Ilkka Seppälä Permission is
|
||||
hereby granted, free of charge, to any person obtaining a copy of this software
|
||||
and associated documentation files (the "Software"), to deal in the Software
|
||||
without restriction, including without limitation the rights to use, copy,
|
||||
modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions: The above copyright notice and this
|
||||
permission notice shall be included in all copies or substantial portions
|
||||
of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
|
||||
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE. -->
|
||||
<project
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>cqrs</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
95
cqrs/src/main/java/com/iluwatar/cqrs/app/App.java
Normal file
95
cqrs/src/main/java/com/iluwatar/cqrs/app/App.java
Normal file
@ -0,0 +1,95 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.cqrs.app;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.iluwatar.cqrs.commandes.CommandServiceImpl;
|
||||
import com.iluwatar.cqrs.commandes.ICommandService;
|
||||
import com.iluwatar.cqrs.dto.Author;
|
||||
import com.iluwatar.cqrs.dto.Book;
|
||||
import com.iluwatar.cqrs.queries.IQueryService;
|
||||
import com.iluwatar.cqrs.queries.QueryServiceImpl;
|
||||
import com.iluwatar.cqrs.util.HibernateUtil;
|
||||
|
||||
/**
|
||||
* CQRS : Command Query Responsibility Segregation. A pattern used to separate query services from commands or writes
|
||||
* services. The pattern is very simple but it has many consequences. For example, it can be used to tackle down a
|
||||
* complex domain, or to use other architectures that were hard to implement with the classical way.
|
||||
*
|
||||
* This implementation is an example of managing books and authors in a library. The persistence of books and authors is
|
||||
* done according to the CQRS architecture. A command side that deals with a data model to persist(insert,update,delete)
|
||||
* objects to a database. And a query side that uses native queries to get data from the database and return objects as
|
||||
* DTOs (Data transfer Objects).
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args
|
||||
* command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
ICommandService commands = new CommandServiceImpl();
|
||||
|
||||
// Create Authors and Books using CommandService
|
||||
commands.authorCreated("eEvans", "Eric Evans", "eEvans@email.com");
|
||||
commands.authorCreated("jBloch", "Joshua Bloch", "jBloch@email.com");
|
||||
commands.authorCreated("mFowler", "Martin Fowler", "mFowler@email.com");
|
||||
|
||||
commands.bookAddedToAuthor("Domain-Driven Design", 60.08, "eEvans");
|
||||
commands.bookAddedToAuthor("Effective Java", 40.54, "jBloch");
|
||||
commands.bookAddedToAuthor("Java Puzzlers", 39.99, "jBloch");
|
||||
commands.bookAddedToAuthor("Java Concurrency in Practice", 29.40, "jBloch");
|
||||
commands.bookAddedToAuthor("Patterns of Enterprise Application Architecture", 54.01, "mFowler");
|
||||
commands.bookAddedToAuthor("Domain Specific Languages", 48.89, "mFowler");
|
||||
commands.authorNameUpdated("eEvans", "Eric J. Evans");
|
||||
|
||||
IQueryService queries = new QueryServiceImpl();
|
||||
|
||||
// Query the database using QueryService
|
||||
Author nullAuthor = queries.getAuthorByUsername("username");
|
||||
Author eEvans = queries.getAuthorByUsername("eEvans");
|
||||
BigInteger jBlochBooksCount = queries.getAuthorBooksCount("jBloch");
|
||||
BigInteger authorsCount = queries.getAuthorsCount();
|
||||
Book dddBook = queries.getBook("Domain-Driven Design");
|
||||
List<Book> jBlochBooks = queries.getAuthorBooks("jBloch");
|
||||
|
||||
LOGGER.info("Author username : {}", nullAuthor);
|
||||
LOGGER.info("Author eEvans : {}", eEvans);
|
||||
LOGGER.info("jBloch number of books : {}", jBlochBooksCount);
|
||||
LOGGER.info("Number of authors : {}", authorsCount);
|
||||
LOGGER.info("DDD book : {}", dddBook);
|
||||
LOGGER.info("jBloch books : {}", jBlochBooks);
|
||||
|
||||
HibernateUtil.getSessionFactory().close();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,145 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.cqrs.commandes;
|
||||
|
||||
import org.hibernate.Query;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
import com.iluwatar.cqrs.domain.model.Author;
|
||||
import com.iluwatar.cqrs.domain.model.Book;
|
||||
import com.iluwatar.cqrs.util.HibernateUtil;
|
||||
|
||||
/**
|
||||
* This class is an implementation of {@link ICommandService} interface. It uses Hibernate as an api for persistence.
|
||||
*
|
||||
*/
|
||||
public class CommandServiceImpl implements ICommandService {
|
||||
|
||||
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
|
||||
|
||||
private Author getAuthorByUsername(String username) {
|
||||
Author author = null;
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
Query query = session.createQuery("from Author where username=:username");
|
||||
query.setParameter("username", username);
|
||||
author = (Author) query.uniqueResult();
|
||||
}
|
||||
if (author == null) {
|
||||
HibernateUtil.getSessionFactory().close();
|
||||
throw new NullPointerException("Author " + username + " doesn't exist!");
|
||||
}
|
||||
return author;
|
||||
}
|
||||
|
||||
private Book getBookByTitle(String title) {
|
||||
Book book = null;
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
Query query = session.createQuery("from Book where title=:title");
|
||||
query.setParameter("title", title);
|
||||
book = (Book) query.uniqueResult();
|
||||
}
|
||||
if (book == null) {
|
||||
HibernateUtil.getSessionFactory().close();
|
||||
throw new NullPointerException("Book " + title + " doesn't exist!");
|
||||
}
|
||||
return book;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void authorCreated(String username, String name, String email) {
|
||||
Author author = new Author(username, name, email);
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
session.save(author);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bookAddedToAuthor(String title, double price, String username) {
|
||||
Author author = getAuthorByUsername(username);
|
||||
Book book = new Book(title, price, author);
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
session.save(book);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void authorNameUpdated(String username, String name) {
|
||||
Author author = getAuthorByUsername(username);
|
||||
author.setName(name);
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
session.update(author);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void authorUsernameUpdated(String oldUsername, String newUsername) {
|
||||
Author author = getAuthorByUsername(oldUsername);
|
||||
author.setUsername(newUsername);
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
session.update(author);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void authorEmailUpdated(String username, String email) {
|
||||
Author author = getAuthorByUsername(username);
|
||||
author.setEmail(email);
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
session.update(author);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bookTitleUpdated(String oldTitle, String newTitle) {
|
||||
Book book = getBookByTitle(oldTitle);
|
||||
book.setTitle(newTitle);
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
session.update(book);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bookPriceUpdated(String title, double price) {
|
||||
Book book = getBookByTitle(title);
|
||||
book.setPrice(price);
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
session.update(book);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.cqrs.commandes;
|
||||
|
||||
/**
|
||||
* This interface represents the commands of the CQRS pattern
|
||||
*
|
||||
*/
|
||||
public interface ICommandService {
|
||||
|
||||
void authorCreated(String username, String name, String email);
|
||||
|
||||
void bookAddedToAuthor(String title, double price, String username);
|
||||
|
||||
void authorNameUpdated(String username, String name);
|
||||
|
||||
void authorUsernameUpdated(String oldUsername, String newUsername);
|
||||
|
||||
void authorEmailUpdated(String username, String email);
|
||||
|
||||
void bookTitleUpdated(String oldTitle, String newTitle);
|
||||
|
||||
void bookPriceUpdated(String title, double price);
|
||||
|
||||
}
|
100
cqrs/src/main/java/com/iluwatar/cqrs/domain/model/Author.java
Normal file
100
cqrs/src/main/java/com/iluwatar/cqrs/domain/model/Author.java
Normal file
@ -0,0 +1,100 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.cqrs.domain.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
/**
|
||||
* This is an Author entity. It is used by Hibernate for persistence.
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
public class Author {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
private String username;
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param username
|
||||
* username of the author
|
||||
* @param name
|
||||
* name of the author
|
||||
* @param email
|
||||
* email of the author
|
||||
*/
|
||||
public Author(String username, String name, String email) {
|
||||
super();
|
||||
this.username = username;
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
protected Author() {
|
||||
super();
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Author [name=" + name + ", email=" + email + "]";
|
||||
}
|
||||
|
||||
}
|
102
cqrs/src/main/java/com/iluwatar/cqrs/domain/model/Book.java
Normal file
102
cqrs/src/main/java/com/iluwatar/cqrs/domain/model/Book.java
Normal file
@ -0,0 +1,102 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.cqrs.domain.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
/**
|
||||
* This is a Book entity. It is used by Hibernate for persistence. Many books can be written by one {@link Author}
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
public class Book {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
private String title;
|
||||
private double price;
|
||||
@ManyToOne
|
||||
private Author author;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param title
|
||||
* title of the book
|
||||
* @param price
|
||||
* price of the book
|
||||
* @param author
|
||||
* author of the book
|
||||
*/
|
||||
public Book(String title, double price, Author author) {
|
||||
super();
|
||||
this.title = title;
|
||||
this.price = price;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
protected Book() {
|
||||
super();
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Author getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(Author author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Book [title=" + title + ", price=" + price + ", author=" + author + "]";
|
||||
}
|
||||
|
||||
}
|
93
cqrs/src/main/java/com/iluwatar/cqrs/dto/Author.java
Normal file
93
cqrs/src/main/java/com/iluwatar/cqrs/dto/Author.java
Normal file
@ -0,0 +1,93 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.cqrs.dto;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
*
|
||||
* This is a DTO (Data Transfer Object) author, contains only useful information to be returned
|
||||
*
|
||||
*/
|
||||
public class Author {
|
||||
|
||||
private String name;
|
||||
private String email;
|
||||
private String username;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param name
|
||||
* name of the author
|
||||
* @param email
|
||||
* email of the author
|
||||
* @param username
|
||||
* username of the author
|
||||
*/
|
||||
public Author(String name, String email, String username) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public Author() {
|
||||
super();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AuthorDTO [name=" + name + ", email=" + email + ", username=" + username + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(username, name, email);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof Author)) {
|
||||
return false;
|
||||
}
|
||||
Author other = (Author) obj;
|
||||
return username.equals(other.getUsername()) && email.equals(other.getEmail()) && name.equals(other.getName());
|
||||
|
||||
}
|
||||
|
||||
}
|
84
cqrs/src/main/java/com/iluwatar/cqrs/dto/Book.java
Normal file
84
cqrs/src/main/java/com/iluwatar/cqrs/dto/Book.java
Normal file
@ -0,0 +1,84 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.cqrs.dto;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
*
|
||||
* This is a DTO (Data Transfer Object) book, contains only useful information to be returned
|
||||
*
|
||||
*/
|
||||
public class Book {
|
||||
|
||||
private String title;
|
||||
private double price;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param title
|
||||
* title of the book
|
||||
* @param price
|
||||
* price of the book
|
||||
*/
|
||||
public Book(String title, double price) {
|
||||
super();
|
||||
this.title = title;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Book() {
|
||||
super();
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BookDTO [title=" + title + ", price=" + price + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(title, price);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof Book)) {
|
||||
return false;
|
||||
}
|
||||
Book book = (Book) obj;
|
||||
return title.equals(book.getTitle()) && price == book.getPrice();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.cqrs.queries;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
|
||||
import com.iluwatar.cqrs.dto.Author;
|
||||
import com.iluwatar.cqrs.dto.Book;
|
||||
|
||||
/**
|
||||
*
|
||||
* This interface represents the query methods of the CQRS pattern
|
||||
*
|
||||
*/
|
||||
public interface IQueryService {
|
||||
|
||||
Author getAuthorByUsername(String username);
|
||||
|
||||
Book getBook(String title);
|
||||
|
||||
List<Book> getAuthorBooks(String username);
|
||||
|
||||
BigInteger getAuthorBooksCount(String username);
|
||||
|
||||
BigInteger getAuthorsCount();
|
||||
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.cqrs.queries;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.SQLQuery;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.transform.Transformers;
|
||||
|
||||
import com.iluwatar.cqrs.dto.Author;
|
||||
import com.iluwatar.cqrs.dto.Book;
|
||||
import com.iluwatar.cqrs.util.HibernateUtil;
|
||||
|
||||
/**
|
||||
* This class is an implementation of {@link IQueryService}. It uses Hibernate native queries to return DTOs from the
|
||||
* database.
|
||||
*
|
||||
*/
|
||||
public class QueryServiceImpl implements IQueryService {
|
||||
|
||||
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
|
||||
|
||||
@Override
|
||||
public Author getAuthorByUsername(String username) {
|
||||
Author authorDTo = null;
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
SQLQuery sqlQuery = session
|
||||
.createSQLQuery("SELECT a.username as \"username\", a.name as \"name\", a.email as \"email\""
|
||||
+ "FROM Author a where a.username=:username");
|
||||
sqlQuery.setParameter("username", username);
|
||||
authorDTo = (Author) sqlQuery.setResultTransformer(Transformers.aliasToBean(Author.class)).uniqueResult();
|
||||
}
|
||||
return authorDTo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Book getBook(String title) {
|
||||
Book bookDTo = null;
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
SQLQuery sqlQuery = session
|
||||
.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\"" + " FROM Book b where b.title=:title");
|
||||
sqlQuery.setParameter("title", title);
|
||||
bookDTo = (Book) sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).uniqueResult();
|
||||
}
|
||||
return bookDTo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Book> getAuthorBooks(String username) {
|
||||
List<Book> bookDTos = null;
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
SQLQuery sqlQuery = session.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\""
|
||||
+ " FROM Author a , Book b where b.author_id = a.id and a.username=:username");
|
||||
sqlQuery.setParameter("username", username);
|
||||
bookDTos = sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).list();
|
||||
}
|
||||
return bookDTos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BigInteger getAuthorBooksCount(String username) {
|
||||
BigInteger bookcount = null;
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
SQLQuery sqlQuery = session.createSQLQuery(
|
||||
"SELECT count(b.title)" + " FROM Book b, Author a where b.author_id = a.id and a.username=:username");
|
||||
sqlQuery.setParameter("username", username);
|
||||
bookcount = (BigInteger) sqlQuery.uniqueResult();
|
||||
}
|
||||
return bookcount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BigInteger getAuthorsCount() {
|
||||
BigInteger authorcount = null;
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
SQLQuery sqlQuery = session.createSQLQuery("SELECT count(id) from Author");
|
||||
authorcount = (BigInteger) sqlQuery.uniqueResult();
|
||||
}
|
||||
return authorcount;
|
||||
}
|
||||
|
||||
}
|
58
cqrs/src/main/java/com/iluwatar/cqrs/util/HibernateUtil.java
Normal file
58
cqrs/src/main/java/com/iluwatar/cqrs/util/HibernateUtil.java
Normal file
@ -0,0 +1,58 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.cqrs.util;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* This class simply returns one instance of {@link SessionFactory} initialized when the application is started
|
||||
*
|
||||
*/
|
||||
public class HibernateUtil {
|
||||
|
||||
private static final SessionFactory SESSIONFACTORY = buildSessionFactory();
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(HibernateUtil.class);
|
||||
|
||||
private static SessionFactory buildSessionFactory() {
|
||||
|
||||
// configures settings from hibernate.cfg.xml
|
||||
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
|
||||
try {
|
||||
return new MetadataSources(registry).buildMetadata().buildSessionFactory();
|
||||
} catch (Exception ex) {
|
||||
StandardServiceRegistryBuilder.destroy(registry);
|
||||
LOGGER.error("Initial SessionFactory creation failed." + ex);
|
||||
throw new ExceptionInInitializerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static SessionFactory getSessionFactory() {
|
||||
return SESSIONFACTORY;
|
||||
}
|
||||
|
||||
}
|
39
cqrs/src/main/resources/hibernate.cfg.xml
Normal file
39
cqrs/src/main/resources/hibernate.cfg.xml
Normal file
@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
|
||||
The MIT License
|
||||
Copyright (c) 2014 Ilkka Seppälä
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
-->
|
||||
<!DOCTYPE hibernate-configuration SYSTEM
|
||||
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
|
||||
<property name="connection.driver_class">org.h2.Driver</property>
|
||||
<property name="connection.url">jdbc:h2:mem:test</property>
|
||||
<property name="connection.username">sa</property>
|
||||
<property name="hbm2ddl.auto">create</property>
|
||||
<mapping class="com.iluwatar.cqrs.domain.model.Author" />
|
||||
<mapping class="com.iluwatar.cqrs.domain.model.Book" />
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
37
cqrs/src/main/resources/logback.xml
Normal file
37
cqrs/src/main/resources/logback.xml
Normal file
@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
|
||||
The MIT License
|
||||
Copyright (c) 2014 Ilkka Seppälä
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
-->
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
120
cqrs/src/test/java/com/iluwatar/cqrs/IntegrationTest.java
Normal file
120
cqrs/src/test/java/com/iluwatar/cqrs/IntegrationTest.java
Normal file
@ -0,0 +1,120 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.cqrs;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.cqrs.commandes.CommandServiceImpl;
|
||||
import com.iluwatar.cqrs.commandes.ICommandService;
|
||||
import com.iluwatar.cqrs.dto.Author;
|
||||
import com.iluwatar.cqrs.dto.Book;
|
||||
import com.iluwatar.cqrs.queries.IQueryService;
|
||||
import com.iluwatar.cqrs.queries.QueryServiceImpl;
|
||||
|
||||
/**
|
||||
* Integration test of IQueryService and ICommandService with h2 data
|
||||
*
|
||||
*/
|
||||
public class IntegrationTest {
|
||||
|
||||
private static IQueryService queryService;
|
||||
private static ICommandService commandService;
|
||||
|
||||
@BeforeClass
|
||||
public static void initialize() {
|
||||
commandService = new CommandServiceImpl();
|
||||
queryService = new QueryServiceImpl();
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void populateDatabase() {
|
||||
// create first author1
|
||||
commandService.authorCreated("username1", "name1", "email1");
|
||||
|
||||
// create author1 and update all its data
|
||||
commandService.authorCreated("username2", "name2", "email2");
|
||||
commandService.authorEmailUpdated("username2", "new_email2");
|
||||
commandService.authorNameUpdated("username2", "new_name2");
|
||||
commandService.authorUsernameUpdated("username2", "new_username2");
|
||||
|
||||
// add book1 to author1
|
||||
commandService.bookAddedToAuthor("title1", 10, "username1");
|
||||
|
||||
// add book2 to author1 and update all its data
|
||||
commandService.bookAddedToAuthor("title2", 20, "username1");
|
||||
commandService.bookPriceUpdated("title2", 30);
|
||||
commandService.bookTitleUpdated("title2", "new_title2");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAuthorByUsername() {
|
||||
Author author = queryService.getAuthorByUsername("username1");
|
||||
assertEquals("username1", author.getUsername());
|
||||
assertEquals("name1", author.getName());
|
||||
assertEquals("email1", author.getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUpdatedAuthorByUsername() {
|
||||
Author author = queryService.getAuthorByUsername("new_username2");
|
||||
Author expectedAuthor = new Author("new_name2", "new_email2", "new_username2");
|
||||
assertEquals(expectedAuthor, author);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBook() {
|
||||
Book book = queryService.getBook("title1");
|
||||
assertEquals("title1", book.getTitle());
|
||||
assertEquals(10, book.getPrice(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAuthorBooks() {
|
||||
List<Book> books = queryService.getAuthorBooks("username1");
|
||||
assertTrue(books.size() == 2);
|
||||
assertTrue(books.contains(new Book("title1", 10)));
|
||||
assertTrue(books.contains(new Book("new_title2", 30)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAuthorBooksCount() {
|
||||
BigInteger bookCount = queryService.getAuthorBooksCount("username1");
|
||||
assertEquals(new BigInteger("2"), bookCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAuthorsCount() {
|
||||
BigInteger authorCount = queryService.getAuthorsCount();
|
||||
assertEquals(new BigInteger("2"), authorCount);
|
||||
}
|
||||
|
||||
}
|
39
cqrs/src/test/resources/hibernate.cfg.xml
Normal file
39
cqrs/src/test/resources/hibernate.cfg.xml
Normal file
@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
|
||||
The MIT License
|
||||
Copyright (c) 2014 Ilkka Seppälä
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
-->
|
||||
<!DOCTYPE hibernate-configuration SYSTEM
|
||||
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
|
||||
<property name="connection.driver_class">org.h2.Driver</property>
|
||||
<property name="connection.url">jdbc:h2:mem:test</property>
|
||||
<property name="connection.username">sa</property>
|
||||
<property name="hbm2ddl.auto">create</property>
|
||||
<mapping class="com.iluwatar.cqrs.domain.model.Author" />
|
||||
<mapping class="com.iluwatar.cqrs.domain.model.Book" />
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
37
cqrs/src/test/resources/logback.xml
Normal file
37
cqrs/src/test/resources/logback.xml
Normal file
@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
|
||||
The MIT License
|
||||
Copyright (c) 2014 Ilkka Seppälä
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
-->
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -34,9 +34,10 @@ package com.iluwatar.dao {
|
||||
+ getById(int) : Optional<Customer> {abstract}
|
||||
+ update(Customer) : boolean {abstract}
|
||||
}
|
||||
interface CustomerSchemaSql {
|
||||
class CustomerSchemaSql {
|
||||
+ CREATE_SCHEMA_SQL : String {static}
|
||||
+ DELETE_SCHEMA_SQL : String {static}
|
||||
- CustomerSchemaSql()
|
||||
}
|
||||
class DbCustomerDao {
|
||||
- dataSource : DataSource
|
||||
|
@ -30,7 +30,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.14.0</version>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>dao</artifactId>
|
||||
|
||||
|
@ -22,10 +22,16 @@
|
||||
*/
|
||||
package com.iluwatar.dao;
|
||||
|
||||
public interface CustomerSchemaSql {
|
||||
/**
|
||||
* Customer Schema SQL Class
|
||||
*/
|
||||
public final class CustomerSchemaSql {
|
||||
|
||||
String CREATE_SCHEMA_SQL = "CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), "
|
||||
private CustomerSchemaSql() {}
|
||||
|
||||
public static final String CREATE_SCHEMA_SQL = "CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), "
|
||||
+ "LNAME VARCHAR(100))";
|
||||
|
||||
String DELETE_SCHEMA_SQL = "DROP TABLE CUSTOMERS";
|
||||
|
||||
public static final String DELETE_SCHEMA_SQL = "DROP TABLE CUSTOMERS";
|
||||
|
||||
}
|
||||
|
@ -29,6 +29,9 @@ import static org.junit.Assert.assertNotEquals;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests {@link Customer}.
|
||||
*/
|
||||
public class CustomerTest {
|
||||
|
||||
private Customer customer;
|
||||
|
1
data-bus/.gitignore
vendored
Normal file
1
data-bus/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
33
data-bus/README.md
Normal file
33
data-bus/README.md
Normal file
@ -0,0 +1,33 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Data Bus
|
||||
folder: data-bus
|
||||
permalink: /patterns/data-bus/
|
||||
|
||||
categories: Architectural
|
||||
tags:
|
||||
- Java
|
||||
- Difficulty-Intermediate
|
||||
---
|
||||
|
||||
## Intent
|
||||
|
||||
Allows send of messages/events between components of an application
|
||||
without them needing to know about each other. They only need to know
|
||||
about the type of the message/event being sent.
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
Use Data Bus pattern when
|
||||
|
||||
* you want your components to decide themselves which messages/events they want to receive
|
||||
* you want to have many-to-many communication
|
||||
* you want your components to know nothing about each other
|
||||
|
||||
## Related Patterns
|
||||
Data Bus is similar to
|
||||
|
||||
* Mediator pattern with Data Bus Members deciding for themselves if they want to accept any given message
|
||||
* Observer pattern but supporting many-to-many communication
|
||||
* Publish/Subscribe pattern with the Data Bus decoupling the publisher and the subscriber
|
BIN
data-bus/etc/data-bus.urm.png
Normal file
BIN
data-bus/etc/data-bus.urm.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 60 KiB |
77
data-bus/etc/data-bus.urm.puml
Normal file
77
data-bus/etc/data-bus.urm.puml
Normal file
@ -0,0 +1,77 @@
|
||||
@startuml
|
||||
package com.iluwatar.databus {
|
||||
class AbstractDataType {
|
||||
- dataBus : DataBus
|
||||
+ AbstractDataType()
|
||||
+ getDataBus() : DataBus
|
||||
+ setDataBus(dataBus : DataBus)
|
||||
}
|
||||
~class App {
|
||||
- log : Logger {static}
|
||||
~ App()
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
class DataBus {
|
||||
- INSTANCE : DataBus {static}
|
||||
- listeners : Set<Member>
|
||||
+ DataBus()
|
||||
+ getInstance() : DataBus {static}
|
||||
+ publish(event : DataType)
|
||||
+ subscribe(member : Member)
|
||||
+ unsubscribe(member : Member)
|
||||
}
|
||||
interface DataType {
|
||||
+ getDataBus() : DataBus {abstract}
|
||||
+ setDataBus(DataBus) {abstract}
|
||||
}
|
||||
interface Member {
|
||||
+ accept(DataType) {abstract}
|
||||
}
|
||||
}
|
||||
package com.iluwatar.databus.data {
|
||||
class MessageData {
|
||||
- message : String
|
||||
+ MessageData(message : String)
|
||||
+ getMessage() : String
|
||||
+ of(message : String) : DataType {static}
|
||||
}
|
||||
class StartingData {
|
||||
- when : LocalDateTime
|
||||
+ StartingData(when : LocalDateTime)
|
||||
+ getWhen() : LocalDateTime
|
||||
+ of(when : LocalDateTime) : DataType {static}
|
||||
}
|
||||
class StoppingData {
|
||||
- when : LocalDateTime
|
||||
+ StoppingData(when : LocalDateTime)
|
||||
+ getWhen() : LocalDateTime
|
||||
+ of(when : LocalDateTime) : DataType {static}
|
||||
}
|
||||
}
|
||||
package com.iluwatar.databus.members {
|
||||
class CounterMember {
|
||||
- log : Logger {static}
|
||||
- name : String
|
||||
+ CounterMember(name : String)
|
||||
+ accept(data : DataType)
|
||||
- handleEvent(data : MessageData)
|
||||
}
|
||||
class StatusMember {
|
||||
- id : int
|
||||
- log : Logger {static}
|
||||
+ StatusMember(id : int)
|
||||
+ accept(data : DataType)
|
||||
- handleEvent(data : StartingData)
|
||||
- handleEvent(data : StoppingData)
|
||||
}
|
||||
}
|
||||
AbstractDataType --> "-dataBus" DataBus
|
||||
DataBus --> "-INSTANCE" DataBus
|
||||
DataBus --> "-listeners" Member
|
||||
AbstractDataType ..|> DataType
|
||||
MessageData --|> AbstractDataType
|
||||
StartingData --|> AbstractDataType
|
||||
StoppingData --|> AbstractDataType
|
||||
CounterMember ..|> Member
|
||||
StatusMember ..|> Member
|
||||
@enduml
|
51
data-bus/pom.xml
Normal file
51
data-bus/pom.xml
Normal file
@ -0,0 +1,51 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
|
||||
The MIT License
|
||||
Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
-->
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<lombok.version>1.16.14</lombok.version>
|
||||
</properties>
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>data-bus</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,67 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Paul Campbell
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.databus;
|
||||
|
||||
/**
|
||||
* Base for data to send via the Data-Bus.
|
||||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
public class AbstractDataType implements DataType {
|
||||
|
||||
private DataBus dataBus;
|
||||
|
||||
@Override
|
||||
public DataBus getDataBus() {
|
||||
return dataBus;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDataBus(DataBus dataBus) {
|
||||
this.dataBus = dataBus;
|
||||
}
|
||||
}
|
79
data-bus/src/main/java/com/iluwatar/databus/App.java
Normal file
79
data-bus/src/main/java/com/iluwatar/databus/App.java
Normal file
@ -0,0 +1,79 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.databus;
|
||||
|
||||
import com.iluwatar.databus.data.MessageData;
|
||||
import com.iluwatar.databus.data.StartingData;
|
||||
import com.iluwatar.databus.data.StoppingData;
|
||||
import com.iluwatar.databus.members.MessageCollectorMember;
|
||||
import com.iluwatar.databus.members.StatusMember;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* The Data Bus pattern
|
||||
* <p>
|
||||
* <p>{@see http://wiki.c2.com/?DataBusPattern}</p>
|
||||
* <p>
|
||||
* <p>The Data-Bus pattern provides a method where different parts of an application may
|
||||
* pass messages between each other without needing to be aware of the other's existence.</p>
|
||||
* <p>Similar to the {@code ObserverPattern}, members register themselves with the {@link DataBus}
|
||||
* and may then receive each piece of data that is published to the Data-Bus. The member
|
||||
* may react to any given message or not.</p>
|
||||
* <p>It allows for Many-to-Many distribution of data, as there may be any number of
|
||||
* publishers to a Data-Bus, and any number of members receiving the data. All members
|
||||
* will receive the same data, the order each receives a given piece of data, is an
|
||||
* implementation detail.</p>
|
||||
* <p>Members may unsubscribe from the Data-Bus to stop receiving data.</p>
|
||||
* <p>This example of the pattern implements a Synchronous Data-Bus, meaning that
|
||||
* when data is published to the Data-Bus, the publish method will not return until
|
||||
* all members have received the data and returned.</p>
|
||||
* <p>The {@link DataBus} class is a Singleton.</p>
|
||||
* <p>Members of the Data-Bus must implement the {@link Member} interface.</p>
|
||||
* <p>Data to be published via the Data-Bus must implement the {@link DataType} interface.</p>
|
||||
* <p>The {@code data} package contains example {@link DataType} implementations.</p>
|
||||
* <p>The {@code members} package contains example {@link Member} implementations.</p>
|
||||
* <p>The {@link StatusMember} demonstrates using the DataBus to publish a message
|
||||
* to the Data-Bus when it receives a message.</p>
|
||||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
final DataBus bus = DataBus.getInstance();
|
||||
bus.subscribe(new StatusMember(1));
|
||||
bus.subscribe(new StatusMember(2));
|
||||
final MessageCollectorMember foo = new MessageCollectorMember("Foo");
|
||||
final MessageCollectorMember bar = new MessageCollectorMember("Bar");
|
||||
bus.subscribe(foo);
|
||||
bus.publish(StartingData.of(LocalDateTime.now()));
|
||||
bus.publish(MessageData.of("Only Foo should see this"));
|
||||
bus.subscribe(bar);
|
||||
bus.publish(MessageData.of("Foo and Bar should see this"));
|
||||
bus.unsubscribe(foo);
|
||||
bus.publish(MessageData.of("Only Bar should see this"));
|
||||
bus.publish(StoppingData.of(LocalDateTime.now()));
|
||||
}
|
||||
}
|
73
data-bus/src/main/java/com/iluwatar/databus/DataBus.java
Normal file
73
data-bus/src/main/java/com/iluwatar/databus/DataBus.java
Normal file
@ -0,0 +1,73 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.databus;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* The Data-Bus implementation.
|
||||
*
|
||||
* <p>This implementation uses a Singleton.</p>
|
||||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
public class DataBus {
|
||||
|
||||
private static final DataBus INSTANCE = new DataBus();
|
||||
|
||||
private final Set<Member> listeners = new HashSet<>();
|
||||
|
||||
public static DataBus getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a member with the data-bus to start receiving events.
|
||||
*
|
||||
* @param member The member to register
|
||||
*/
|
||||
public void subscribe(final Member member) {
|
||||
this.listeners.add(member);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deregister a member to stop receiving events.
|
||||
*
|
||||
* @param member The member to deregister
|
||||
*/
|
||||
public void unsubscribe(final Member member) {
|
||||
this.listeners.remove(member);
|
||||
}
|
||||
|
||||
/**
|
||||
* Publish and event to all members.
|
||||
*
|
||||
* @param event The event
|
||||
*/
|
||||
public void publish(final DataType event) {
|
||||
event.setDataBus(this);
|
||||
listeners.forEach(listener -> listener.accept(event));
|
||||
}
|
||||
}
|
70
data-bus/src/main/java/com/iluwatar/databus/DataType.java
Normal file
70
data-bus/src/main/java/com/iluwatar/databus/DataType.java
Normal file
@ -0,0 +1,70 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Paul Campbell
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.databus;
|
||||
|
||||
/**
|
||||
* Events are sent via the Data-Bus.
|
||||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
|
||||
public interface DataType {
|
||||
|
||||
/**
|
||||
* Returns the data-bus the event is being sent on.
|
||||
*
|
||||
* @return The data-bus
|
||||
*/
|
||||
DataBus getDataBus();
|
||||
|
||||
/**
|
||||
* Set the data-bus the event will be sent on.
|
||||
*
|
||||
* @param dataBus The data-bus
|
||||
*/
|
||||
void setDataBus(DataBus dataBus);
|
||||
}
|
59
data-bus/src/main/java/com/iluwatar/databus/Member.java
Normal file
59
data-bus/src/main/java/com/iluwatar/databus/Member.java
Normal file
@ -0,0 +1,59 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Paul Campbell
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.databus;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Members receive events from the Data-Bus.
|
||||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
public interface Member extends Consumer<DataType> {
|
||||
|
||||
void accept(DataType event);
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.databus.data;
|
||||
|
||||
import com.iluwatar.databus.AbstractDataType;
|
||||
import com.iluwatar.databus.DataType;
|
||||
|
||||
/**
|
||||
* An event raised when a string message is sent.
|
||||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
public class MessageData extends AbstractDataType {
|
||||
|
||||
private final String message;
|
||||
|
||||
public MessageData(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public static DataType of(final String message) {
|
||||
return new MessageData(message);
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.databus.data;
|
||||
|
||||
import com.iluwatar.databus.AbstractDataType;
|
||||
import com.iluwatar.databus.DataType;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* An event raised when applications starts, containing the start time of the application.
|
||||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
public class StartingData extends AbstractDataType {
|
||||
|
||||
private final LocalDateTime when;
|
||||
|
||||
public StartingData(LocalDateTime when) {
|
||||
this.when = when;
|
||||
}
|
||||
|
||||
public LocalDateTime getWhen() {
|
||||
return when;
|
||||
}
|
||||
|
||||
public static DataType of(final LocalDateTime when) {
|
||||
return new StartingData(when);
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.databus.data;
|
||||
|
||||
import com.iluwatar.databus.AbstractDataType;
|
||||
import com.iluwatar.databus.DataType;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* An event raised when applications stops, containing the stop time of the application.
|
||||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
public class StoppingData extends AbstractDataType {
|
||||
|
||||
private final LocalDateTime when;
|
||||
|
||||
public StoppingData(LocalDateTime when) {
|
||||
this.when = when;
|
||||
}
|
||||
|
||||
public LocalDateTime getWhen() {
|
||||
return when;
|
||||
}
|
||||
|
||||
public static DataType of(final LocalDateTime when) {
|
||||
return new StoppingData(when);
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.databus.members;
|
||||
|
||||
import com.iluwatar.databus.DataType;
|
||||
import com.iluwatar.databus.Member;
|
||||
import com.iluwatar.databus.data.MessageData;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Receiver of Data-Bus events that collects the messages from each {@link MessageData}.
|
||||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
public class MessageCollectorMember implements Member {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(MessageCollectorMember.class.getName());
|
||||
|
||||
private final String name;
|
||||
|
||||
private List<String> messages = new ArrayList<>();
|
||||
|
||||
public MessageCollectorMember(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(final DataType data) {
|
||||
if (data instanceof MessageData) {
|
||||
handleEvent((MessageData) data);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleEvent(MessageData data) {
|
||||
LOGGER.info(String.format("%s sees message %s", name, data.getMessage()));
|
||||
messages.add(data.getMessage());
|
||||
}
|
||||
|
||||
public List<String> getMessages() {
|
||||
return Collections.unmodifiableList(messages);
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user