Skip to content

Commit

Permalink
refactor: refactor DTOMapper so it does not use static attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
mastastny committed Aug 8, 2023
1 parent b4ef31a commit 216b3c0
Showing 1 changed file with 12 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,39 @@
import io.patriotframework.virtualsmarthomeplus.house.devices.finalDevices.Thermometer;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeMap;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.stream.Collectors;

/**
* Class responsible for mapping model objects to DTOs
*/
@Component
@Service
public class DTOMapper {
private static final HashMap<Class<? extends Device>, Class<? extends DeviceDTO>> CLASS_TO_DTO;
private static final HashMap<Class<? extends DeviceDTO>, Class<? extends Device>> DTO_TO_CLASS;
private final HashMap<Class<? extends Device>, Class<? extends DeviceDTO>> CLASS_TO_DTO = new HashMap<>();
private final HashMap<Class<? extends DeviceDTO>, Class<? extends Device>> DTO_TO_CLASS = new HashMap<>();

static {
CLASS_TO_DTO = new HashMap<>();
private final ModelMapper modelMapper;

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

static {
DTO_TO_CLASS = new HashMap<>();
DTO_TO_CLASS.put(DeviceDTO.class, Device.class);
DTO_TO_CLASS.put(FireplaceDTO.class, Fireplace.class);
DTO_TO_CLASS.put(DoorDTO.class, Door.class);
DTO_TO_CLASS.put(ThermometerDTO.class, Thermometer.class);
DTO_TO_CLASS.put(RGBLightDTO.class, RGBLight.class);

}

private final ModelMapper modelMapper;

/**
* Constructs DTOMapper for all instances of the House interface
*
* @param modelMapper instance of ModelMapper
*/
public DTOMapper(ModelMapper modelMapper) {
this.modelMapper = modelMapper;

final TypeMap<FireplaceDTO, Fireplace> fireplaceTypeMap = this.modelMapper
Expand Down

0 comments on commit 216b3c0

Please sign in to comment.