Skip to content

Commit 8632baf

Browse files
committed
comments, tests and description
1 parent e8b634c commit 8632baf

File tree

10 files changed

+219
-51
lines changed

10 files changed

+219
-51
lines changed

api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* An adapter to communicate with the Image microservice
3636
*/
3737
@Component
38-
public class ImageClientImpl implements ImageClient{
38+
public class ImageClientImpl implements ImageClient {
3939
/**
4040
* Makes a simple HTTP Get request to the Image microservice
4141
* @return The path to the image

api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* An adapter to communicate with the Price microservice
3636
*/
3737
@Component
38-
public class PriceClientImpl implements PriceClient{
38+
public class PriceClientImpl implements PriceClient {
3939
/**
4040
* Makes a simple HTTP Get request to the Price microservice
4141
* @return The price of the product

converter/README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ tags:
1010
---
1111

1212
## Intent
13-
TODO
13+
The purpose of the Converter Pattern is to provide a generic, common way of bidirectional
14+
conversion between corresponding types, allowing a clean implementation in which the types do not
15+
need to be aware of each other. Moreover, the Converter Pattern introduces bidirectional collection
16+
mapping, reducing a boilerplate code to minimum.
1417

15-
![alt text](./etc/converter.png "TODO")
18+
![alt text](./etc/converter.png "Converter Pattern")
1619

1720
## Applicability
18-
TODO
21+
Use the Converter Pattern in the following situations:
1922

20-
* TODO 1
21-
* TODO 2
23+
* When you have types that logically correspond which other and you need to convert entities between them
24+
* When you want to provide different ways of types conversions depending on a context
25+
* Whenever you introduce a DTO (Data transfer object), you will probably need to convert it into the domain equivalence
2226

2327
## Credits
2428

25-
* [Converter](http://todo.com)
29+
* [Converter](http://www.xsolve.pl/blog/converter-pattern-in-java-8/)

converter/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
<artifactId>junit</artifactId>
1515
<scope>test</scope>
1616
</dependency>
17+
<dependency>
18+
<groupId>com.google.guava</groupId>
19+
<artifactId>guava</artifactId>
20+
</dependency>
1721
</dependencies>
1822
<artifactId>converter</artifactId>
1923

converter/src/main/java/com/iluwatar/converter/App.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@
2222
*/
2323
package com.iluwatar.converter;
2424

25+
26+
import com.google.common.collect.Lists;
27+
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
31+
/**
32+
* The Converter pattern is a behavioral design pattern which allows a common way of bidirectional
33+
* conversion between corresponding types (e.g. DTO and domain representations of the logically
34+
* isomorphic types). Moreover, the pattern introduces a common way of converting a collection of
35+
* objects between types.
36+
*/
2537
public class App {
2638
/**
2739
* Program entry point
@@ -30,11 +42,22 @@ public class App {
3042
*/
3143
public static void main(String[] args) {
3244
Converter<UserDto, User> userConverter = new Converter<>(
33-
userDto -> new User(userDto.getFirstName(), userDto.getLastName(), userDto.isActive()),
34-
user -> new UserDto(user.getFirstName(), user.getLastName(), user.isActive()));
35-
UserDto dtoUser = new UserDto("John", "Doe", true);
45+
userDto -> new User(userDto.getFirstName(), userDto.getLastName(), userDto.isActive(),
46+
userDto.getEmail()),
47+
user -> new UserDto(user.getFirstName(), user.getLastName(), user.isActive(), user.getUserId()));
48+
49+
UserDto dtoUser = new UserDto("John", "Doe", true, "whatever[at]wherever.com");
3650
User user = userConverter.convertFromDto(dtoUser);
3751
UserDto dtoUserCopy = userConverter.convertFromEntity(user);
3852

53+
ArrayList<User> users = Lists.newArrayList(new User("Camile", "Tough", false, "124sad"),
54+
new User("Marti", "Luther", true, "42309fd"), new User("Kate", "Smith", true, "if0243"));
55+
System.out.println("Domain entities:");
56+
users.forEach(System.out::println);
57+
58+
System.out.println("DTO entities converted from domain:");
59+
List<UserDto> dtoEntities = userConverter.createFromEntities(users);
60+
dtoEntities.forEach(System.out::println);
61+
3962
}
4063
}

converter/src/main/java/com/iluwatar/converter/Converter.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
import java.util.stream.Collectors;
3030

3131
/**
32+
* Generic converter, thanks to Java8 features not only provides a way of generic bidirectional
33+
* conversion between coresponding types, but also a common way of converting a collection of objects
34+
* of the same type, reducing boilerplate code to the absolute minimum.
3235
* @param <T> DTO representation's type
3336
* @param <U> Domain representation's type
3437
*/
@@ -47,37 +50,37 @@ public Converter(final Function<T, U> fromDto, final Function<U, T> fromEntity)
4750
}
4851

4952
/**
50-
* @param arg DTO entity
53+
* @param userDto DTO entity
5154
* @return The domain representation - the result of the converting function application on dto entity.
5255
*/
53-
public U convertFromDto(final T arg) {
54-
return fromDto.apply(arg);
56+
public final U convertFromDto(final T userDto) {
57+
return fromDto.apply(userDto);
5558
}
5659

5760
/**
58-
* @param arg domain entity
61+
* @param user domain entity
5962
* @return The DTO representation - the result of the converting function application on domain entity.
6063
*/
61-
public T convertFromEntity(final U arg) {
62-
return fromEntity.apply(arg);
64+
public final T convertFromEntity(final U user) {
65+
return fromEntity.apply(user);
6366
}
6467

6568
/**
66-
* @param arg collection of DTO entities
69+
* @param dtoUsers collection of DTO entities
6770
* @return List of domain representation of provided entities retrieved by
6871
* mapping each of them with the convertion function
6972
*/
70-
public List<U> createFromDtos(final Collection<T> arg) {
71-
return arg.stream().map(this::convertFromDto).collect(Collectors.toList());
73+
public final List<U> createFromDtos(final Collection<T> dtoUsers) {
74+
return dtoUsers.stream().map(this::convertFromDto).collect(Collectors.toList());
7275
}
7376

7477
/**
75-
* @param arg collection of domain entities
78+
* @param users collection of domain entities
7679
* @return List of domain representation of provided entities retrieved by
7780
* mapping each of them with the convertion function
7881
*/
79-
public List<T> createFromEntities(final Collection<U> arg) {
80-
return arg.stream().map(this::convertFromEntity).collect(Collectors.toList());
82+
public final List<T> createFromEntities(final Collection<U> users) {
83+
return users.stream().map(this::convertFromEntity).collect(Collectors.toList());
8184
}
8285

8386
}

converter/src/main/java/com/iluwatar/converter/User.java

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

2424
package com.iluwatar.converter;
2525

26+
import java.util.Objects;
27+
2628
public class User {
2729
private String firstName;
2830
private String lastName;
2931
private boolean isActive;
32+
private String userId;
3033

3134
/**
32-
*
3335
* @param firstName user's first name
34-
* @param lastName user's last name
35-
* @param isActive flag indicating whether the user is active
36+
* @param lastName user's last name
37+
* @param isActive flag indicating whether the user is active
38+
* @param userId user's identificator
3639
*/
37-
public User(String firstName, String lastName, boolean isActive) {
40+
public User(String firstName, String lastName, boolean isActive, String userId) {
3841
this.firstName = firstName;
3942
this.lastName = lastName;
4043
this.isActive = isActive;
44+
this.userId = userId;
4145
}
4246

4347
public String getFirstName() {
4448
return firstName;
4549
}
4650

47-
public void setFirstName(String firstName) {
48-
this.firstName = firstName;
49-
}
50-
5151
public String getLastName() {
5252
return lastName;
5353
}
5454

55-
public void setLastName(String lastName) {
56-
this.lastName = lastName;
57-
}
58-
5955
public boolean isActive() {
6056
return isActive;
6157
}
6258

63-
public void setActive(boolean active) {
64-
isActive = active;
59+
public String getUserId() {
60+
return userId;
61+
}
62+
63+
@Override public boolean equals(Object o) {
64+
if (this == o) {
65+
return true;
66+
}
67+
if (o == null || getClass() != o.getClass()) {
68+
return false;
69+
}
70+
User user = (User) o;
71+
return isActive == user.isActive && Objects.equals(firstName, user.firstName) && Objects
72+
.equals(lastName, user.lastName) && Objects.equals(userId, user.userId);
73+
}
74+
75+
@Override public int hashCode() {
76+
return Objects.hash(firstName, lastName, isActive, userId);
77+
}
78+
79+
@Override public String toString() {
80+
return "User{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\''
81+
+ ", isActive=" + isActive + ", userId='" + userId + '\'' + '}';
6582
}
6683
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.converter;
25+
26+
/**
27+
* Example implementation of the simple User converter.
28+
*/
29+
public class UserConverter extends Converter<UserDto, User> {
30+
31+
/**
32+
* Constructor.
33+
*/
34+
public UserConverter() {
35+
super(userDto -> new User(userDto.getFirstName(), userDto.getLastName(), userDto.isActive(),
36+
userDto.getEmail()),
37+
user -> new UserDto(user.getFirstName(), user.getLastName(), user.isActive(),
38+
user.getUserId()));
39+
}
40+
}

converter/src/main/java/com/iluwatar/converter/UserDto.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,44 +24,62 @@
2424
package com.iluwatar.converter;
2525

2626

27+
import java.util.Objects;
28+
2729
public class UserDto {
30+
2831
private String firstName;
2932
private String lastName;
3033
private boolean isActive;
34+
private String email;
3135

3236
/**
33-
*
3437
* @param firstName user's first name
35-
* @param lastName user's last name
36-
* @param isActive flag indicating whether the user is active
38+
* @param lastName user's last name
39+
* @param isActive flag indicating whether the user is active
40+
* @param email user's email address
3741
*/
38-
public UserDto(String firstName, String lastName, boolean isActive) {
42+
public UserDto(String firstName, String lastName, boolean isActive, String email) {
3943
this.firstName = firstName;
4044
this.lastName = lastName;
4145
this.isActive = isActive;
46+
this.email = email;
4247
}
4348

4449
public String getFirstName() {
4550
return firstName;
4651
}
4752

48-
public void setFirstName(String firstName) {
49-
this.firstName = firstName;
50-
}
51-
5253
public String getLastName() {
5354
return lastName;
5455
}
5556

56-
public void setLastName(String lastName) {
57-
this.lastName = lastName;
58-
}
59-
6057
public boolean isActive() {
6158
return isActive;
6259
}
6360

64-
public void setActive(boolean active) {
65-
isActive = active;
61+
public String getEmail() {
62+
return email;
63+
}
64+
65+
@Override public boolean equals(Object o) {
66+
if (this == o) {
67+
return true;
68+
}
69+
if (o == null || getClass() != o.getClass()) {
70+
return false;
71+
}
72+
UserDto userDto = (UserDto) o;
73+
return isActive == userDto.isActive && Objects.equals(firstName, userDto.firstName) && Objects
74+
.equals(lastName, userDto.lastName) && Objects.equals(email, userDto.email);
75+
}
76+
77+
@Override public int hashCode() {
78+
return Objects.hash(firstName, lastName, isActive, email);
79+
}
80+
81+
@Override public String toString() {
82+
return "UserDto{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\''
83+
+ ", isActive=" + isActive + ", email='" + email + '\'' + '}';
6684
}
6785
}

0 commit comments

Comments
 (0)