Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DTOMapper so it does not use static attributes #60

Open
wants to merge 1 commit into
base: new_implementation
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,33 @@
import io.patriotframework.virtualsmarthomeplus.house.devices.finalDevices.Fireplace;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeMap;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
* Class responsible for mapping model objects to DTOs
*/
@Component
@Service
public class DTOMapper {
private final ModelMapper modelMapper;

private static final HashMap<Class<?extends Device>, Class<?extends DeviceDTO>>classToDto;
static {
classToDto = new HashMap<>();
classToDto.put(Device.class, DeviceDTO.class);
classToDto.put(Fireplace.class, FireplaceDTO.class);
classToDto.put(Door.class, DoorDTO.class);
}
private final HashMap<Class<? extends Device>, Class<? extends DeviceDTO>> classToDtoMap = new HashMap<>();
private final HashMap<Class<? extends DeviceDTO>, Class<? extends Device>> dtoToClassMap = new HashMap<>();

private static final HashMap<Class<?extends DeviceDTO>, Class<?extends Device>> dtoToClass;
static {
dtoToClass = new HashMap<>();
dtoToClass.put(DeviceDTO.class, Device.class);
dtoToClass.put(FireplaceDTO.class, Fireplace.class);
dtoToClass.put(DoorDTO.class, Door.class);
}
private final ModelMapper modelMapper;

/**
* Constructs DTOMapper for all instances of the House interface
* @param modelMapper instance of ModelMapper
*/
public DTOMapper(ModelMapper modelMapper) {
classToDtoMap.put(Device.class, DeviceDTO.class);
classToDtoMap.put(Fireplace.class, FireplaceDTO.class);
classToDtoMap.put(Door.class, DoorDTO.class);

dtoToClassMap.put(DeviceDTO.class, Device.class);
dtoToClassMap.put(FireplaceDTO.class, Fireplace.class);
dtoToClassMap.put(DoorDTO.class, Door.class);

this.modelMapper = modelMapper;

TypeMap<FireplaceDTO, Fireplace> fireplaceTypeMap = this.modelMapper
Expand Down Expand Up @@ -76,11 +71,11 @@ public HouseDTO map (House house) {
* @throws DeviceMappingNotSupportedException if class of DTO is unknown for the mapper
*/
public DeviceDTO map(Device device) throws DeviceMappingNotSupportedException {
if(!classToDto.containsKey(device.getClass())) {
if (!classToDtoMap.containsKey(device.getClass())) {
throw new DeviceMappingNotSupportedException(
String.format("Device %s is not supported by DeviceMapper", device.getClass()));
}
return modelMapper.map(device, classToDto.get(device.getClass()));
return modelMapper.map(device, classToDtoMap.get(device.getClass()));
}

/**
Expand All @@ -90,11 +85,11 @@ public DeviceDTO map(Device device) throws DeviceMappingNotSupportedException {
* @throws DeviceMappingNotSupportedException if class of DTO is unknown for the mapper
*/
public Device map(DeviceDTO dto) throws DeviceMappingNotSupportedException {
if(!dtoToClass.containsKey(dto.getClass())) {
if (!this.dtoToClassMap.containsKey(dto.getClass())) {
throw new DeviceMappingNotSupportedException(
String.format("DeviceDTO %s is not supported by DeviceMapper", dto.getClass()));
}
return modelMapper.map(dto, dtoToClass.get(dto.getClass()));
return modelMapper.map(dto, this.dtoToClassMap.get(dto.getClass()));
}

/**
Expand All @@ -105,8 +100,9 @@ public Device map(DeviceDTO dto) throws DeviceMappingNotSupportedException {
*/
public Class<?extends Device> mapDtoClassType(Class<? extends DeviceDTO> dto)
throws DeviceMappingNotSupportedException {
Class<?extends Device> result = dtoToClass.get(dto);
if(result == null) {

final Class<? extends Device> result = this.dtoToClassMap.get(dto);
if (result == null) {
throw new DeviceMappingNotSupportedException(
String.format("DeviceDTO %s is not supported by DeviceMapper", dto));
}
Expand All @@ -119,9 +115,9 @@ public Class<?extends Device> mapDtoClassType(Class<? extends DeviceDTO> dto)
* @return class of the corresponding DTO
* @throws DeviceMappingNotSupportedException if class of device is unknown for the mapper
*/
public Class<?extends DeviceDTO> mapDeviceClassType(Class<? extends Device> device) {
Class<?extends DeviceDTO> result = classToDto.get(device);
if(result == null) {
public Class<? extends DeviceDTO> mapDeviceClassType(Class<? extends Device> device) {
final Class<? extends DeviceDTO> result = classToDtoMap.get(device);
if (result == null) {
throw new DeviceMappingNotSupportedException(
String.format("Device %s is not supported by DeviceMapper", device));
}
Expand Down