|
1 | 1 | package it.aboutbits.springboot.toolbox.parameter; |
2 | 2 |
|
| 3 | +import lombok.EqualsAndHashCode; |
| 4 | +import lombok.Getter; |
3 | 5 | import lombok.NonNull; |
| 6 | +import lombok.experimental.Accessors; |
4 | 7 | import org.springframework.data.domain.Sort; |
5 | 8 |
|
6 | | -import java.util.Collections; |
| 9 | +import java.util.ArrayList; |
7 | 10 | import java.util.List; |
8 | 11 | import java.util.Map; |
9 | 12 | import java.util.stream.Collectors; |
|
15 | 18 | * based on enum constants and associated sort properties. Sorting criteria can be defined |
16 | 19 | * with various configurations, including direction and null-handling behavior. |
17 | 20 | */ |
18 | | -public record SortParameter<T extends Enum<?> & SortParameter.Definition>(List<SortField> sortFields) { |
| 21 | +@EqualsAndHashCode |
| 22 | +public final class SortParameter<T extends Enum<?> & SortParameter.Definition> { |
19 | 23 | private static final String DEFAULT_SORT_PROPERTY = "id"; |
20 | 24 | private static final Sort.Direction DEFAULT_SORT_DIRETION = Sort.Direction.ASC; |
21 | 25 |
|
| 26 | + @Accessors(fluent = true) |
| 27 | + @Getter |
| 28 | + private final List<SortField> sortFields = new ArrayList<>(); |
| 29 | + |
| 30 | + public SortParameter(List<SortField> sortFields) { |
| 31 | + if (sortFields == null) { |
| 32 | + return; |
| 33 | + } |
| 34 | + this.sortFields.addAll(sortFields); |
| 35 | + } |
| 36 | + |
| 37 | + private SortParameter() { |
| 38 | + } |
| 39 | + |
22 | 40 | /** |
23 | 41 | * Creates a {@link SortParameter} that represents an unsorted state. |
24 | 42 | * |
25 | 43 | * @param <T> a type that extends both {@link Enum} and {@link Definition}. |
26 | 44 | * @return an instance of {@link SortParameter} configured with no sorting fields. |
27 | 45 | */ |
28 | 46 | public static <T extends Enum<?> & Definition> SortParameter<T> unsorted() { |
29 | | - return new SortParameter<>(Collections.emptyList()); |
| 47 | + return new SortParameter<>(); |
30 | 48 | } |
31 | 49 |
|
32 | 50 | /** |
@@ -192,7 +210,7 @@ public SortParameter<T> and( |
192 | 210 | * or the provided fallback if it does not. |
193 | 211 | */ |
194 | 212 | public SortParameter<T> or(@NonNull SortParameter<T> fallback) { |
195 | | - return sortFields == null || sortFields.isEmpty() ? fallback : this; |
| 213 | + return sortFields.isEmpty() ? fallback : this; |
196 | 214 | } |
197 | 215 |
|
198 | 216 | /** |
@@ -235,23 +253,22 @@ public Sort buildSort(@NonNull Map<T, String> mapping) { |
235 | 253 | return buildSort(stringMapping, true); |
236 | 254 | } |
237 | 255 |
|
238 | | - // SonarLint: Replace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()' and ensure that the list is unmodified. |
239 | | - @SuppressWarnings("java:S6204") |
240 | 256 | private Sort buildSort(@NonNull Map<String, String> mapping, boolean withDefault) { |
241 | | - if (sortFields == null || sortFields.isEmpty()) { |
| 257 | + if (sortFields.isEmpty()) { |
242 | 258 | return withDefault ? getMappedDefaultSort(mapping) : Sort.unsorted(); |
243 | 259 | } |
244 | 260 |
|
245 | 261 | var additionalSort = Sort.by( |
246 | | - sortFields.stream() |
247 | | - .filter(sortField -> mapping.containsKey(sortField.property())) |
248 | | - .map(sortField -> new Sort.Order( |
249 | | - sortField.direction(), |
250 | | - mapping.get(sortField.property()), |
251 | | - sortField.nullHandling() |
252 | | - )) |
253 | | - // We do not use .toList() here as we potentially want to modify the sort list later in the StoreImpl |
254 | | - .collect(Collectors.toList()) |
| 262 | + new ArrayList<>( |
| 263 | + sortFields.stream() |
| 264 | + .filter(sortField -> mapping.containsKey(sortField.property())) |
| 265 | + .map(sortField -> new Sort.Order( |
| 266 | + sortField.direction(), |
| 267 | + mapping.get(sortField.property()), |
| 268 | + sortField.nullHandling() |
| 269 | + )) |
| 270 | + .toList() |
| 271 | + ) |
255 | 272 | ); |
256 | 273 |
|
257 | 274 | if (withDefault) { |
|
0 commit comments