|
16 | 16 | package org.springframework.samples.petclinic.api.dto;
|
17 | 17 |
|
18 | 18 | import com.fasterxml.jackson.annotation.JsonIgnore;
|
19 |
| -import lombok.Data; |
20 | 19 |
|
21 |
| -import java.util.ArrayList; |
22 | 20 | import java.util.List;
|
23 | 21 |
|
24 |
| -import static java.util.stream.Collectors.toList; |
25 |
| - |
26 | 22 | /**
|
27 | 23 | * @author Maciej Szarlinski
|
28 | 24 | */
|
29 |
| -@Data |
30 |
| -public class OwnerDetails { |
| 25 | +public record OwnerDetails( |
| 26 | + int id, |
| 27 | + String firstName, |
| 28 | + String lastName, |
| 29 | + String address, |
| 30 | + String city, |
| 31 | + String telephone, |
| 32 | + List<PetDetails> pets) { |
31 | 33 |
|
32 |
| - private int id; |
| 34 | + @JsonIgnore |
| 35 | + public List<Integer> getPetIds() { |
| 36 | + return pets.stream() |
| 37 | + .map(PetDetails::id) |
| 38 | + .toList(); |
| 39 | + } |
33 | 40 |
|
34 |
| - private String firstName; |
35 | 41 |
|
36 |
| - private String lastName; |
| 42 | + public static final class OwnerDetailsBuilder { |
| 43 | + private int id; |
| 44 | + private String firstName; |
| 45 | + private String lastName; |
| 46 | + private String address; |
| 47 | + private String city; |
| 48 | + private String telephone; |
| 49 | + private List<PetDetails> pets; |
37 | 50 |
|
38 |
| - private String address; |
| 51 | + private OwnerDetailsBuilder() { |
| 52 | + } |
39 | 53 |
|
40 |
| - private String city; |
| 54 | + public static OwnerDetailsBuilder anOwnerDetails() { |
| 55 | + return new OwnerDetailsBuilder(); |
| 56 | + } |
41 | 57 |
|
42 |
| - private String telephone; |
| 58 | + public OwnerDetailsBuilder id(int id) { |
| 59 | + this.id = id; |
| 60 | + return this; |
| 61 | + } |
43 | 62 |
|
44 |
| - private final List<PetDetails> pets = new ArrayList<>(); |
| 63 | + public OwnerDetailsBuilder firstName(String firstName) { |
| 64 | + this.firstName = firstName; |
| 65 | + return this; |
| 66 | + } |
45 | 67 |
|
46 |
| - @JsonIgnore |
47 |
| - public List<Integer> getPetIds() { |
48 |
| - return pets.stream() |
49 |
| - .map(PetDetails::getId) |
50 |
| - .collect(toList()); |
| 68 | + public OwnerDetailsBuilder lastName(String lastName) { |
| 69 | + this.lastName = lastName; |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + public OwnerDetailsBuilder address(String address) { |
| 74 | + this.address = address; |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + public OwnerDetailsBuilder city(String city) { |
| 79 | + this.city = city; |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + public OwnerDetailsBuilder telephone(String telephone) { |
| 84 | + this.telephone = telephone; |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + public OwnerDetailsBuilder pets(List<PetDetails> pets) { |
| 89 | + this.pets = pets; |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + public OwnerDetails build() { |
| 94 | + return new OwnerDetails(id, firstName, lastName, address, city, telephone, pets); |
| 95 | + } |
51 | 96 | }
|
52 | 97 | }
|
0 commit comments