Skip to content

Commit

Permalink
Java 11 migrate c-d (remaining) (#1111)
Browse files Browse the repository at this point in the history
* Moves converter pattern to Java 11

* Moves cqrs pattern to Java 11

* Moves dao pattern to Java 11

* Moves data-bus pattern to Java 11

* Moves data-locality pattern to Java 11

* Moves data-mapper pattern to Java 11

* Moves data-transfer-object pattern to Java 11

* Moves decorator pattern to Java 11

* Moves delegation pattern to Java 11

* Moves dependency-injection to Java 11

* Moves dirty-flag to Java 11

* Moves double-buffer to Java 11

* Moves double-checked-locking to Java 11

* Moves double-dispatch to Java 11

* Corrects with changes thats breaking test cases
  • Loading branch information
anuragagarwal561994 authored and iluwatar committed Dec 14, 2019
1 parent 5681684 commit ea57934
Show file tree
Hide file tree
Showing 75 changed files with 573 additions and 710 deletions.
20 changes: 13 additions & 7 deletions converter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,26 @@ The specialized converters inherit from this base class as follows.
public class UserConverter extends Converter<UserDto, User> {

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()));
super(UserConverter::convertToEntity, UserConverter::convertToDto);
}

private static UserDto convertToDto(User user) {
return new UserDto(user.getFirstName(), user.getLastName(), user.isActive(), user.getUserId());
}

private static User convertToEntity(UserDto dto) {
return new User(dto.getFirstName(), dto.getLastName(), dto.isActive(), dto.getEmail());
}

}
```

Now mapping between User and UserDto becomes trivial.

```java
Converter<UserDto, User> userConverter = new UserConverter();
UserDto dtoUser = new UserDto("John", "Doe", true, "whatever[at]wherever.com");
User user = userConverter.convertFromDto(dtoUser);
var userConverter = new UserConverter();
var dtoUser = new UserDto("John", "Doe", true, "whatever[at]wherever.com");
var user = userConverter.convertFromDto(dtoUser);
```

## Class diagram
Expand Down
7 changes: 5 additions & 2 deletions converter/src/main/java/com/iluwatar/converter/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ public static void main(String[] args) {
User user = userConverter.convertFromDto(dtoUser);
LOGGER.info("Entity converted from DTO:" + user);

var users = List.of(new User("Camile", "Tough", false, "124sad"),
new User("Marti", "Luther", true, "42309fd"), new User("Kate", "Smith", true, "if0243"));
var users = List.of(
new User("Camile", "Tough", false, "124sad"),
new User("Marti", "Luther", true, "42309fd"),
new User("Kate", "Smith", true, "if0243")
);
LOGGER.info("Domain entities:");
users.stream().map(User::toString).forEach(LOGGER::info);

Expand Down
2 changes: 1 addition & 1 deletion converter/src/main/java/com/iluwatar/converter/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
User user = (User) o;
var user = (User) o;
return isActive == user.isActive && Objects.equals(firstName, user.firstName) && Objects
.equals(lastName, user.lastName) && Objects.equals(userId, user.userId);
}
Expand Down
17 changes: 10 additions & 7 deletions converter/src/main/java/com/iluwatar/converter/UserConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
*/
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()));
super(UserConverter::convertToEntity, UserConverter::convertToDto);
}

private static UserDto convertToDto(User user) {
return new UserDto(user.getFirstName(), user.getLastName(), user.isActive(), user.getUserId());
}

private static User convertToEntity(UserDto dto) {
return new User(dto.getFirstName(), dto.getLastName(), dto.isActive(), dto.getEmail());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
UserDto userDto = (UserDto) o;
var userDto = (UserDto) o;
return isActive == userDto.isActive && Objects.equals(firstName, userDto.firstName) && Objects
.equals(lastName, userDto.lastName) && Objects.equals(email, userDto.email);
}
Expand Down
3 changes: 1 addition & 2 deletions converter/src/test/java/com/iluwatar/converter/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public class AppTest {

@Test
public void testMain() {
String[] args = {};
App.main(args);
App.main(new String[]{});
}

}
53 changes: 31 additions & 22 deletions converter/src/test/java/com/iluwatar/converter/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@

package com.iluwatar.converter;

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

import java.util.List;
import java.util.Random;

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

/**
* Tests for {@link Converter}
Expand All @@ -42,8 +41,8 @@ public class ConverterTest {
*/
@Test
public void testConversionsStartingFromDomain() {
User u1 = new User("Tom", "Hanks", true, "tom@hanks.com");
User u2 = userConverter.convertFromDto(userConverter.convertFromEntity(u1));
var u1 = new User("Tom", "Hanks", true, "tom@hanks.com");
var u2 = userConverter.convertFromDto(userConverter.convertFromEntity(u1));
assertEquals(u1, u2);
}

Expand All @@ -52,37 +51,47 @@ public void testConversionsStartingFromDomain() {
*/
@Test
public void testConversionsStartingFromDto() {
UserDto u1 = new UserDto("Tom", "Hanks", true, "tom@hanks.com");
UserDto u2 = userConverter.convertFromEntity(userConverter.convertFromDto(u1));
var u1 = new UserDto("Tom", "Hanks", true, "tom@hanks.com");
var 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.
* 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);
var converter = new Converter<UserDto, User>(
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")
);
var u1 = new User("John", "Doe", false, "12324");
var userDto = converter.convertFromEntity(u1);
assertEquals("johndoe@whatever.com", userDto.getEmail());
}

/**
* Test whether converting a collection of Users to DTO Users and then converting them back to domain
* users returns an equal collection.
* 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() {
List<User> users = List.of(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));
var users = List.of(
new User("Camile", "Tough", false, "124sad"),
new User("Marti", "Luther", true, "42309fd"),
new User("Kate", "Smith", true, "if0243")
);
var fromDtos = userConverter.createFromDtos(userConverter.createFromEntities(users));
assertEquals(users, fromDtos);
}
}
22 changes: 8 additions & 14 deletions cqrs/src/main/java/com/iluwatar/cqrs/app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,9 @@
package com.iluwatar.cqrs.app;

import com.iluwatar.cqrs.commandes.CommandServiceImpl;
import com.iluwatar.cqrs.commandes.ICommandService;
import com.iluwatar.cqrs.constants.AppConstants;
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;
import java.math.BigInteger;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -56,7 +50,7 @@ public class App {
* @param args command line args
*/
public static void main(String[] args) {
ICommandService commands = new CommandServiceImpl();
var commands = new CommandServiceImpl();

// Create Authors and Books using CommandService
commands.authorCreated(AppConstants.E_EVANS, "Eric Evans", "evans@email.com");
Expand All @@ -72,15 +66,15 @@ public static void main(String[] args) {
commands.bookAddedToAuthor("Domain Specific Languages", 48.89, AppConstants.M_FOWLER);
commands.authorNameUpdated(AppConstants.E_EVANS, "Eric J. Evans");

IQueryService queries = new QueryServiceImpl();
var queries = new QueryServiceImpl();

// Query the database using QueryService
Author nullAuthor = queries.getAuthorByUsername("username");
Author evans = queries.getAuthorByUsername(AppConstants.E_EVANS);
BigInteger blochBooksCount = queries.getAuthorBooksCount(AppConstants.J_BLOCH);
BigInteger authorsCount = queries.getAuthorsCount();
Book dddBook = queries.getBook("Domain-Driven Design");
List<Book> blochBooks = queries.getAuthorBooks(AppConstants.J_BLOCH);
var nullAuthor = queries.getAuthorByUsername("username");
var evans = queries.getAuthorByUsername(AppConstants.E_EVANS);
var blochBooksCount = queries.getAuthorBooksCount(AppConstants.J_BLOCH);
var authorsCount = queries.getAuthorsCount();
var dddBook = queries.getBook("Domain-Driven Design");
var blochBooks = queries.getAuthorBooks(AppConstants.J_BLOCH);

LOGGER.info("Author username : {}", nullAuthor);
LOGGER.info("Author evans : {}", evans);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import com.iluwatar.cqrs.domain.model.Author;
import com.iluwatar.cqrs.domain.model.Book;
import com.iluwatar.cqrs.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
Expand All @@ -39,9 +37,9 @@ 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");
Author author;
try (var session = sessionFactory.openSession()) {
var query = session.createQuery("from Author where username=:username");
query.setParameter("username", username);
author = (Author) query.uniqueResult();
}
Expand All @@ -53,9 +51,9 @@ private Author getAuthorByUsername(String username) {
}

private Book getBookByTitle(String title) {
Book book = null;
try (Session session = sessionFactory.openSession()) {
Query query = session.createQuery("from Book where title=:title");
Book book;
try (var session = sessionFactory.openSession()) {
var query = session.createQuery("from Book where title=:title");
query.setParameter("title", title);
book = (Book) query.uniqueResult();
}
Expand All @@ -68,8 +66,8 @@ private Book getBookByTitle(String title) {

@Override
public void authorCreated(String username, String name, String email) {
Author author = new Author(username, name, email);
try (Session session = sessionFactory.openSession()) {
var author = new Author(username, name, email);
try (var session = sessionFactory.openSession()) {
session.beginTransaction();
session.save(author);
session.getTransaction().commit();
Expand All @@ -78,9 +76,9 @@ public void authorCreated(String username, String name, String email) {

@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()) {
var author = getAuthorByUsername(username);
var book = new Book(title, price, author);
try (var session = sessionFactory.openSession()) {
session.beginTransaction();
session.save(book);
session.getTransaction().commit();
Expand All @@ -89,9 +87,9 @@ public void bookAddedToAuthor(String title, double price, String username) {

@Override
public void authorNameUpdated(String username, String name) {
Author author = getAuthorByUsername(username);
var author = getAuthorByUsername(username);
author.setName(name);
try (Session session = sessionFactory.openSession()) {
try (var session = sessionFactory.openSession()) {
session.beginTransaction();
session.update(author);
session.getTransaction().commit();
Expand All @@ -100,9 +98,9 @@ public void authorNameUpdated(String username, String name) {

@Override
public void authorUsernameUpdated(String oldUsername, String newUsername) {
Author author = getAuthorByUsername(oldUsername);
var author = getAuthorByUsername(oldUsername);
author.setUsername(newUsername);
try (Session session = sessionFactory.openSession()) {
try (var session = sessionFactory.openSession()) {
session.beginTransaction();
session.update(author);
session.getTransaction().commit();
Expand All @@ -111,9 +109,9 @@ public void authorUsernameUpdated(String oldUsername, String newUsername) {

@Override
public void authorEmailUpdated(String username, String email) {
Author author = getAuthorByUsername(username);
var author = getAuthorByUsername(username);
author.setEmail(email);
try (Session session = sessionFactory.openSession()) {
try (var session = sessionFactory.openSession()) {
session.beginTransaction();
session.update(author);
session.getTransaction().commit();
Expand All @@ -122,9 +120,9 @@ public void authorEmailUpdated(String username, String email) {

@Override
public void bookTitleUpdated(String oldTitle, String newTitle) {
Book book = getBookByTitle(oldTitle);
var book = getBookByTitle(oldTitle);
book.setTitle(newTitle);
try (Session session = sessionFactory.openSession()) {
try (var session = sessionFactory.openSession()) {
session.beginTransaction();
session.update(book);
session.getTransaction().commit();
Expand All @@ -133,9 +131,9 @@ public void bookTitleUpdated(String oldTitle, String newTitle) {

@Override
public void bookPriceUpdated(String title, double price) {
Book book = getBookByTitle(title);
var book = getBookByTitle(title);
book.setPrice(price);
try (Session session = sessionFactory.openSession()) {
try (var session = sessionFactory.openSession()) {
session.beginTransaction();
session.update(book);
session.getTransaction().commit();
Expand Down
2 changes: 1 addition & 1 deletion cqrs/src/main/java/com/iluwatar/cqrs/dto/Author.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public boolean equals(Object obj) {
if (!(obj instanceof Author)) {
return false;
}
Author other = (Author) obj;
var other = (Author) obj;
return username.equals(other.getUsername()) && email.equals(other.getEmail()) && name
.equals(other.getName());

Expand Down
2 changes: 1 addition & 1 deletion cqrs/src/main/java/com/iluwatar/cqrs/dto/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public boolean equals(Object obj) {
if (!(obj instanceof Book)) {
return false;
}
Book book = (Book) obj;
var book = (Book) obj;
return title.equals(book.getTitle()) && price == book.getPrice();
}

Expand Down

0 comments on commit ea57934

Please sign in to comment.