createFromEntities(final Collection users) {
+ return users.stream().map(this::convertFromEntity).collect(Collectors.toList());
}
}
diff --git a/converter/src/main/java/com/iluwatar/converter/User.java b/converter/src/main/java/com/iluwatar/converter/User.java
index 8a4e9186c..f40c01e79 100644
--- a/converter/src/main/java/com/iluwatar/converter/User.java
+++ b/converter/src/main/java/com/iluwatar/converter/User.java
@@ -23,44 +23,61 @@
package com.iluwatar.converter;
+import java.util.Objects;
+
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 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) {
+ 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 void setFirstName(String firstName) {
- this.firstName = firstName;
- }
-
public String getLastName() {
return lastName;
}
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
-
public boolean isActive() {
return isActive;
}
- public void setActive(boolean active) {
- isActive = active;
+ 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 + '\'' + '}';
}
}
diff --git a/converter/src/main/java/com/iluwatar/converter/UserConverter.java b/converter/src/main/java/com/iluwatar/converter/UserConverter.java
new file mode 100644
index 000000000..9ef1d03c2
--- /dev/null
+++ b/converter/src/main/java/com/iluwatar/converter/UserConverter.java
@@ -0,0 +1,40 @@
+/**
+ * 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;
+
+/**
+ * Example implementation of the simple User converter.
+ */
+public class UserConverter extends Converter {
+
+ /**
+ * 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()));
+ }
+}
diff --git a/converter/src/main/java/com/iluwatar/converter/UserDto.java b/converter/src/main/java/com/iluwatar/converter/UserDto.java
index bdadf6e39..8f55bbe0e 100644
--- a/converter/src/main/java/com/iluwatar/converter/UserDto.java
+++ b/converter/src/main/java/com/iluwatar/converter/UserDto.java
@@ -24,44 +24,62 @@
package com.iluwatar.converter;
+import java.util.Objects;
+
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 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) {
+ 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 void setFirstName(String firstName) {
- this.firstName = firstName;
- }
-
public String getLastName() {
return lastName;
}
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
-
public boolean isActive() {
return isActive;
}
- public void setActive(boolean active) {
- isActive = active;
+ 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 + '\'' + '}';
}
}
diff --git a/converter/src/test/java/com/iluwatar/converter/ConverterTest.java b/converter/src/test/java/com/iluwatar/converter/ConverterTest.java
new file mode 100644
index 000000000..45cebc4e6
--- /dev/null
+++ b/converter/src/test/java/com/iluwatar/converter/ConverterTest.java
@@ -0,0 +1,59 @@
+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;
+
+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 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 users = Lists.newArrayList(new User("Camile", "Tough", false, "124sad"),
+ new User("Marti", "Luther", true, "42309fd"), new User("Kate", "Smith", true, "if0243"));
+ List fromDtos = userConverter.createFromDtos(userConverter.createFromEntities(users));
+ assertEquals(fromDtos, users);
+ }
+}