Skip to content

Commit 98524c7

Browse files
committed
update sort parameter
1 parent 9fae6b9 commit 98524c7

1 file changed

Lines changed: 33 additions & 16 deletions

File tree

src/main/java/it/aboutbits/springboot/toolbox/parameter/SortParameter.java

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package it.aboutbits.springboot.toolbox.parameter;
22

3+
import lombok.EqualsAndHashCode;
4+
import lombok.Getter;
35
import lombok.NonNull;
6+
import lombok.experimental.Accessors;
47
import org.springframework.data.domain.Sort;
58

6-
import java.util.Collections;
9+
import java.util.ArrayList;
710
import java.util.List;
811
import java.util.Map;
912
import java.util.stream.Collectors;
@@ -15,18 +18,33 @@
1518
* based on enum constants and associated sort properties. Sorting criteria can be defined
1619
* with various configurations, including direction and null-handling behavior.
1720
*/
18-
public record SortParameter<T extends Enum<?> & SortParameter.Definition>(List<SortField> sortFields) {
21+
@EqualsAndHashCode
22+
public final class SortParameter<T extends Enum<?> & SortParameter.Definition> {
1923
private static final String DEFAULT_SORT_PROPERTY = "id";
2024
private static final Sort.Direction DEFAULT_SORT_DIRETION = Sort.Direction.ASC;
2125

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+
2240
/**
2341
* Creates a {@link SortParameter} that represents an unsorted state.
2442
*
2543
* @param <T> a type that extends both {@link Enum} and {@link Definition}.
2644
* @return an instance of {@link SortParameter} configured with no sorting fields.
2745
*/
2846
public static <T extends Enum<?> & Definition> SortParameter<T> unsorted() {
29-
return new SortParameter<>(Collections.emptyList());
47+
return new SortParameter<>();
3048
}
3149

3250
/**
@@ -192,7 +210,7 @@ public SortParameter<T> and(
192210
* or the provided fallback if it does not.
193211
*/
194212
public SortParameter<T> or(@NonNull SortParameter<T> fallback) {
195-
return sortFields == null || sortFields.isEmpty() ? fallback : this;
213+
return sortFields.isEmpty() ? fallback : this;
196214
}
197215

198216
/**
@@ -235,23 +253,22 @@ public Sort buildSort(@NonNull Map<T, String> mapping) {
235253
return buildSort(stringMapping, true);
236254
}
237255

238-
// SonarLint: Replace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()' and ensure that the list is unmodified.
239-
@SuppressWarnings("java:S6204")
240256
private Sort buildSort(@NonNull Map<String, String> mapping, boolean withDefault) {
241-
if (sortFields == null || sortFields.isEmpty()) {
257+
if (sortFields.isEmpty()) {
242258
return withDefault ? getMappedDefaultSort(mapping) : Sort.unsorted();
243259
}
244260

245261
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+
)
255272
);
256273

257274
if (withDefault) {

0 commit comments

Comments
 (0)