Skip to content

Commit

Permalink
Split casting and sorting methods for readability
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis committed Jul 17, 2024
1 parent 877591a commit 5199329
Showing 1 changed file with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
import org.opensearch.search.pipeline.SearchResponseProcessor;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* Processor that sorts an array of items.
Expand Down Expand Up @@ -166,24 +165,34 @@ public SearchResponse processResponse(SearchRequest request, SearchResponse resp
return response;
}

private <T extends Comparable<T>> List<Object> getSortedValues(List<Object> values) {
List<T> comparableValues = new ArrayList<>(values.size());
private List<Object> getSortedValues(List<Object> values) {
// Downcast list elements to comparable (or throw exception) so we can sort
List<? extends Comparable<Object>> comparableValues = getComparableValues(values);
if (sortOrder.equals(SortOrder.ASCENDING)) {
Collections.sort(comparableValues);
} else {
Collections.sort(comparableValues, Collections.reverseOrder());
}
// Upcast list elements back to Object
return new ArrayList<>(comparableValues);
}

private List<? extends Comparable<Object>> getComparableValues(List<Object> values) {
List<Comparable<Object>> comparableValues = new ArrayList<>(values.size());
for (Object obj : values) {
if (obj == null) {
throw new IllegalArgumentException("field [" + sortField + "] contains a null value.]");
} else if (Comparable.class.isAssignableFrom(obj.getClass())) {
@SuppressWarnings("unchecked")
T comp = (T) obj;
Comparable<Object> comp = (Comparable<Object>) obj;
comparableValues.add(comp);
} else {
throw new IllegalArgumentException(
"field [" + sortField + "] of type [" + obj.getClass().getName() + "] is not comparable.]"
);
}
}
return comparableValues.stream()
.sorted(sortOrder.equals(SortOrder.ASCENDING) ? Comparator.naturalOrder() : Comparator.reverseOrder())
.collect(Collectors.toList());
return comparableValues;
}

static class Factory implements Processor.Factory<SearchResponseProcessor> {
Expand Down

0 comments on commit 5199329

Please sign in to comment.