SortUtil.java

1
package com.reallifedeveloper.tools.test.database.inmemory;
2
3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.Comparator;
6
import java.util.List;
7
8
import org.checkerframework.checker.nullness.qual.Nullable;
9
import org.springframework.data.domain.Sort;
10
import org.springframework.data.domain.Sort.NullHandling;
11
import org.springframework.data.domain.Sort.Order;
12
13
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14
import lombok.AllArgsConstructor;
15
16
import com.reallifedeveloper.tools.test.TestUtil;
17
18
/**
19
 * A helper class to assist with sorting lists in accordance with {@code org.springframework.data.domain.Sort} instances.
20
 *
21
 * @author RealLifeDeveloper
22
 */
23
public final class SortUtil {
24
25
    /**
26
     * Since this is a utility class that should not be instantiated, we hide the only constructor.
27
     */
28
    private SortUtil() {
29
    }
30
31
    /**
32
     * Sorts the given list according to the sort definition provided.
33
     *
34
     * @param <T>   the type of elements in the list
35
     * @param items the list of items to sort
36
     * @param sort  the {@code org.springframework.data.domain.Sort} instance defining how the list should be sorted
37
     *
38
     * @return a copy of {@code items}, sorted according to {@code sort}
39
     */
40
    public static <T> List<T> sort(List<T> items, Sort sort) {
41
        List<T> sortedItems = new ArrayList<>(items);
42
        // We reverse the Order instances so that we sort by the least important first and the most important last.
43
        List<Order> orders = reverseOrders(sort);
44
        for (Order order : orders) {
45 1 1. sort : removed call to com/reallifedeveloper/tools/test/database/inmemory/SortUtil::sort → KILLED
            sort(sortedItems, order);
46
        }
47 1 1. sort : replaced return value with Collections.emptyList for com/reallifedeveloper/tools/test/database/inmemory/SortUtil::sort → KILLED
        return sortedItems;
48
    }
49
50
    private static List<Order> reverseOrders(Sort sort) {
51
        List<Order> orders = new ArrayList<>(sort.toList());
52 1 1. reverseOrders : removed call to java/util/Collections::reverse → KILLED
        Collections.reverse(orders);
53 1 1. reverseOrders : replaced return value with Collections.emptyList for com/reallifedeveloper/tools/test/database/inmemory/SortUtil::reverseOrders → KILLED
        return orders;
54
    }
55
56
    private static <T> void sort(List<T> items, Order order) {
57 1 1. sort : removed call to java/util/Collections::sort → RUN_ERROR
        Collections.sort(items, new FieldComparator<>(order));
58
    }
59
60
    @AllArgsConstructor
61
    @SuppressFBWarnings(value = "SE_COMPARATOR_SHOULD_BE_SERIALIZABLE", justification = "This class is only used interally when sorting")
62
    private static final class FieldComparator<T> implements Comparator<T> {
63
64
        private Order order;
65
66
        @Override
67
        @SuppressWarnings("unchecked")
68
        public int compare(T o1, T o2) {
69
            T fieldValue1 = (T) TestUtil.getFieldValue(o1, order.getProperty());
70
            T fieldValue2 = (T) TestUtil.getFieldValue(o2, order.getProperty());
71 1 1. compare : replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::compare → KILLED
            return nullSafeCompare(fieldValue1, fieldValue2);
72
        }
73
74
        @SuppressWarnings("unchecked")
75
        private int nullSafeCompare(@Nullable T fieldValue1, @Nullable T fieldValue2) {
76 1 1. nullSafeCompare : negated conditional → KILLED
            if (fieldValue1 == null) {
77 1 1. nullSafeCompare : negated conditional → NO_COVERAGE
                if (fieldValue2 == null) {
78
                    return 0;
79 1 1. nullSafeCompare : negated conditional → NO_COVERAGE
                } else if (order.getNullHandling() == NullHandling.NULLS_FIRST) {
80 1 1. nullSafeCompare : replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::nullSafeCompare → NO_COVERAGE
                    return -1;
81
                } else {
82 1 1. nullSafeCompare : replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::nullSafeCompare → NO_COVERAGE
                    return 1;
83
                }
84
            }
85 1 1. nullSafeCompare : negated conditional → KILLED
            if (fieldValue2 == null) {
86 1 1. nullSafeCompare : negated conditional → NO_COVERAGE
                if (order.getNullHandling() == NullHandling.NULLS_FIRST) {
87 1 1. nullSafeCompare : replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::nullSafeCompare → NO_COVERAGE
                    return 1;
88
                } else {
89 1 1. nullSafeCompare : replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::nullSafeCompare → NO_COVERAGE
                    return -1;
90
                }
91
            }
92
            // We simply try to cast the field value to Comparable, it it fails we get a ClassCastException:
93
            int compareResult = ((Comparable<T>) fieldValue1).compareTo(fieldValue2);
94 1 1. nullSafeCompare : negated conditional → KILLED
            if (order.isAscending()) {
95 1 1. nullSafeCompare : replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::nullSafeCompare → KILLED
                return compareResult;
96
            } else {
97 1 1. nullSafeCompare : replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::nullSafeCompare → KILLED
                return invert(compareResult);
98
            }
99
        }
100
101
        private static int invert(int c) {
102 2 1. invert : changed conditional boundary → SURVIVED
2. invert : negated conditional → KILLED
            if (c < 0) {
103 1 1. invert : replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::invert → SURVIVED
                return 1;
104 2 1. invert : negated conditional → KILLED
2. invert : changed conditional boundary → KILLED
            } else if (c > 0) {
105 1 1. invert : replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::invert → RUN_ERROR
                return -1;
106
            } else {
107
                return 0;
108
            }
109
        }
110
    }
111
}

Mutations

45

1.1
Location : sort
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:findAllSort()]
removed call to com/reallifedeveloper/tools/test/database/inmemory/SortUtil::sort → KILLED

47

1.1
Location : sort
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:findAllSort()]
replaced return value with Collections.emptyList for com/reallifedeveloper/tools/test/database/inmemory/SortUtil::sort → KILLED

52

1.1
Location : reverseOrders
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:findAllSort()]
removed call to java/util/Collections::reverse → KILLED

53

1.1
Location : reverseOrders
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:findAllSort()]
replaced return value with Collections.emptyList for com/reallifedeveloper/tools/test/database/inmemory/SortUtil::reverseOrders → KILLED

57

1.1
Location : sort
Killed by : none
removed call to java/util/Collections::sort → RUN_ERROR

71

1.1
Location : compare
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:findAllSort()]
replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::compare → KILLED

76

1.1
Location : nullSafeCompare
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:findAllSort()]
negated conditional → KILLED

77

1.1
Location : nullSafeCompare
Killed by : none
negated conditional → NO_COVERAGE

79

1.1
Location : nullSafeCompare
Killed by : none
negated conditional → NO_COVERAGE

80

1.1
Location : nullSafeCompare
Killed by : none
replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::nullSafeCompare → NO_COVERAGE

82

1.1
Location : nullSafeCompare
Killed by : none
replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::nullSafeCompare → NO_COVERAGE

85

1.1
Location : nullSafeCompare
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:findAllSort()]
negated conditional → KILLED

86

1.1
Location : nullSafeCompare
Killed by : none
negated conditional → NO_COVERAGE

87

1.1
Location : nullSafeCompare
Killed by : none
replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::nullSafeCompare → NO_COVERAGE

89

1.1
Location : nullSafeCompare
Killed by : none
replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::nullSafeCompare → NO_COVERAGE

94

1.1
Location : nullSafeCompare
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:findAllSort()]
negated conditional → KILLED

95

1.1
Location : nullSafeCompare
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:findAllPageableWithSorting()]
replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::nullSafeCompare → KILLED

97

1.1
Location : nullSafeCompare
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:findAllSort()]
replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::nullSafeCompare → KILLED

102

1.1
Location : invert
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:findAllSort()]
negated conditional → KILLED

2.2
Location : invert
Killed by : none
changed conditional boundary → SURVIVED
Covering tests

103

1.1
Location : invert
Killed by : none
replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::invert → SURVIVED
Covering tests

104

1.1
Location : invert
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:findAllSort()]
negated conditional → KILLED

2.2
Location : invert
Killed by : com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepositoryTest]/[method:findAllSort()]
changed conditional boundary → KILLED

105

1.1
Location : invert
Killed by : none
replaced int return with 0 for com/reallifedeveloper/tools/test/database/inmemory/SortUtil$FieldComparator::invert → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.20.2