I have two classes say Animal
& AnimalDto
I want to use ModelMapper
to convert Entity to DTO and vice verca.
But the classes are supposed to have different data types for a few attributes having similar name.
How do I achieve this?
Animal.java
public class Animal {
int category;
String color;
int age;
}
AnimalDto.java
public class AnimalDto {
String category;
int color;
int age;
}
currently I'm manually transforming as such:
class AnimalTransformer {
private static Category[] categories = Category.values();
private static Color[] colors = Color.values();
animalEntityToDto(Animal animal) {
AnimalDto animalDto = new AnimalDto();
animalDto.setAge(animal.getAge());
animalDto.setCategory(categories[animal.getCategory()].toString());
animalDto.setColor(Color.valueOf(animal.getColor()).ordinal());
}
animalDtoToEntity(AnimalDto animalDto) {
Animal animal = new Animal();
animal.setAge(animalDto.getAge());
animal.setCategory(Category.valueOf(animalDto.getCategory()).ordinal());
animal.setColor(colors[animalDto.getColor()].toString());
}
}