📍Use lombok, reformat, and optimize the code (#1560)
* Use lombok, reformat, and optimize the code * Fix merge conflicts and some sonar issues Co-authored-by: va1m <va1m@email.com>
This commit is contained in:
@ -24,8 +24,7 @@
|
||||
package com.iluwatar.converter;
|
||||
|
||||
import java.util.List;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* The Converter pattern is a behavioral design pattern which allows a common way of bidirectional
|
||||
@ -33,10 +32,9 @@ import org.slf4j.LoggerFactory;
|
||||
* isomorphic types). Moreover, the pattern introduces a common way of converting a collection of
|
||||
* objects between types.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
@ -47,7 +45,7 @@ public class App {
|
||||
|
||||
UserDto dtoUser = new UserDto("John", "Doe", true, "whatever[at]wherever.com");
|
||||
User user = userConverter.convertFromDto(dtoUser);
|
||||
LOGGER.info("Entity converted from DTO:" + user);
|
||||
LOGGER.info("Entity converted from DTO: {}", user);
|
||||
|
||||
var users = List.of(
|
||||
new User("Camile", "Tough", false, "124sad"),
|
||||
|
@ -27,6 +27,7 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* Generic converter, thanks to Java8 features not only provides a way of generic bidirectional
|
||||
@ -36,22 +37,12 @@ import java.util.stream.Collectors;
|
||||
* @param <T> DTO representation's type
|
||||
* @param <U> Domain representation's type
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class Converter<T, U> {
|
||||
|
||||
private final Function<T, U> fromDto;
|
||||
private final Function<U, T> fromEntity;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts DTO to Entity.
|
||||
*
|
||||
|
@ -23,69 +23,21 @@
|
||||
|
||||
package com.iluwatar.converter;
|
||||
|
||||
import java.util.Objects;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* User class.
|
||||
*/
|
||||
@ToString
|
||||
@EqualsAndHashCode
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class User {
|
||||
private final String firstName;
|
||||
private final String lastName;
|
||||
private final boolean isActive;
|
||||
private final boolean active;
|
||||
private final String userId;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
var 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 + '\'' + '}';
|
||||
}
|
||||
}
|
||||
|
@ -23,70 +23,23 @@
|
||||
|
||||
package com.iluwatar.converter;
|
||||
|
||||
import java.util.Objects;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* User DTO class.
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
@EqualsAndHashCode
|
||||
@ToString
|
||||
public class UserDto {
|
||||
|
||||
private final String firstName;
|
||||
private final String lastName;
|
||||
private final boolean isActive;
|
||||
private final boolean active;
|
||||
private final String email;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
var 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 + '\'' + '}';
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ import org.junit.jupiter.api.Test;
|
||||
/**
|
||||
* Tests for {@link Converter}
|
||||
*/
|
||||
public class ConverterTest {
|
||||
class ConverterTest {
|
||||
|
||||
private final UserConverter userConverter = new UserConverter();
|
||||
|
||||
@ -40,7 +40,7 @@ public class ConverterTest {
|
||||
* Tests whether a converter created of opposite functions holds equality as a bijection.
|
||||
*/
|
||||
@Test
|
||||
public void testConversionsStartingFromDomain() {
|
||||
void testConversionsStartingFromDomain() {
|
||||
var u1 = new User("Tom", "Hanks", true, "tom@hanks.com");
|
||||
var u2 = userConverter.convertFromDto(userConverter.convertFromEntity(u1));
|
||||
assertEquals(u1, u2);
|
||||
@ -50,7 +50,7 @@ public class ConverterTest {
|
||||
* Tests whether a converter created of opposite functions holds equality as a bijection.
|
||||
*/
|
||||
@Test
|
||||
public void testConversionsStartingFromDto() {
|
||||
void testConversionsStartingFromDto() {
|
||||
var u1 = new UserDto("Tom", "Hanks", true, "tom@hanks.com");
|
||||
var u2 = userConverter.convertFromEntity(userConverter.convertFromDto(u1));
|
||||
assertEquals(u1, u2);
|
||||
@ -61,7 +61,7 @@ public class ConverterTest {
|
||||
* instantiated allowing various different conversion strategies to be implemented.
|
||||
*/
|
||||
@Test
|
||||
public void testCustomConverter() {
|
||||
void testCustomConverter() {
|
||||
var converter = new Converter<UserDto, User>(
|
||||
userDto -> new User(
|
||||
userDto.getFirstName(),
|
||||
@ -85,7 +85,7 @@ public class ConverterTest {
|
||||
* domain users returns an equal collection.
|
||||
*/
|
||||
@Test
|
||||
public void testCollectionConversion() {
|
||||
void testCollectionConversion() {
|
||||
var users = List.of(
|
||||
new User("Camile", "Tough", false, "124sad"),
|
||||
new User("Marti", "Luther", true, "42309fd"),
|
||||
|
Reference in New Issue
Block a user