-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHospitalDtoMapper.java
More file actions
80 lines (74 loc) · 3.43 KB
/
HospitalDtoMapper.java
File metadata and controls
80 lines (74 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.swyth.hospitalservice.dto;
import com.swyth.hospitalservice.entity.Hospital;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Utility class for mapping Hospital entities to their corresponding HospitalDTO representations.
*
* This class provides static methods for converting Hospital entities and collections of Hospital
* entities to data transfer objects (DTOs), making it easier to transfer data between application
* layers while decoupling the internal entity model from external representations.
*
* Features:
* - Converts a single Hospital entity to a HospitalDTO.
* - Converts a collection of Hospital entities (Set) to a list of HospitalDTOs, sorted by hospital ID.
*
* Methods:
* - The conversion process includes mapping hospital attributes (ID, name, address, etc.) and
* available bed information for each medical specialization into the DTO.
* - Ensures the output DTOs are sorted by unique identifiers for consistency in representation.
*
* Annotations:
* - The class is utilitarian in purpose and is marked with a private constructor to prevent
* instantiation.
*
* Usage:
* - The methods are designed to be used statically in services or business logic handling hospital
* data transformation.
*
* Purpose:
* - Centralizes and standardizes the entity-to-DTO conversion logic, reducing code duplication
* and ensuring consistency across the application.
*/
public class HospitalDtoMapper {
private HospitalDtoMapper() {
}
/**
* Converts a {@link Hospital} entity to its corresponding {@link HospitalDTO}.
*
* The conversion involves mapping the hospital's attributes, including basic details
* such as ID, name, address, and location (latitude and longitude), as well as
* available beds for each specialization, into a data transfer object (DTO).
* Specializations are sorted by their IDs, with nulls handled appropriately.
*
* @param hospital the {@link Hospital} entity to be converted
* @return a {@link HospitalDTO} containing the mapped attributes from the given hospital entity
*/
public static HospitalDTO convertToDTO(Hospital hospital) {
return new HospitalDTO(
hospital.getId(),
hospital.getName(),
hospital.getAddress(),
hospital.getPostCode(),
hospital.getCity(),
hospital.getLatitude(),
hospital.getLongitude(),
hospital.getHospitalBedAvailabilities().stream()
.map(hospitalBedAvailability -> new Hospital.SpecializationAvailability(
hospitalBedAvailability.getSpecialization().getId(),
hospitalBedAvailability.getSpecialization().getName(),
hospitalBedAvailability.getAvailableBeds()
))
.sorted(Comparator.comparing(Hospital.SpecializationAvailability::getId, Comparator.nullsLast(Comparator.naturalOrder())))
.toList()
);
}
public static List<HospitalDTO> convertToDTO(Set<Hospital> hospitals) {
return hospitals.stream()
.sorted(Comparator.comparing(Hospital::getId, Comparator.nullsLast(Comparator.naturalOrder())))
.map(HospitalDtoMapper::convertToDTO)
.collect(Collectors.toList());
}
}