ComparatorColumn.java

1
/*******************************************************************************
2
 * Copyhacked (H) 2012-2025.
3
 * This program and the accompanying materials
4
 * are made available under no term at all, use it like
5
 * you want, but share and discuss it
6
 * every time possible with every body.
7
 * 
8
 * Contributors:
9
 *      ron190 at ymail dot com - initial implementation
10
 ******************************************************************************/
11
package com.jsql.view.swing.table;
12
13
import java.util.Comparator;
14
15
/**
16
 * Comparator for table column values ; column with only int data is sorted like 3 lt 20 lt 100,
17
 * column with string will sort like 100 gt 20 gt 3 gt abc.
18
 */
19
public class ComparatorColumn<T> implements Comparator<T> {
20
    
21
    /**
22
     * Custom compare to sort numbers as numbers.
23
     * Strings as strings, with numbers ordered before strings.
24
     */
25
    @Override
26
    public int compare(T cellLeft, T cellRight) {
27
        var isFirstNumber = true;
28
        var isSecondNumber = true;
29
        
30
        String valueCellLeft = cellLeft.toString().trim();
31
        String valueCellRight = cellRight.toString().trim();
32
33
        try {
34
            Long.parseLong(valueCellLeft);
35
        } catch (NumberFormatException e) {
36
            isFirstNumber = false;
37
        }
38
        try {
39
            Long.parseLong(valueCellRight);
40
        } catch (NumberFormatException e) {
41
            isSecondNumber = false;
42
        }
43
        
44
        int sortOrder;
45 2 1. compare : negated conditional → NO_COVERAGE
2. compare : negated conditional → NO_COVERAGE
        if (isFirstNumber && isSecondNumber) {
46
            sortOrder = Long.valueOf(valueCellLeft).compareTo(Long.valueOf(valueCellRight));  // or Sort by Number
47 1 1. compare : negated conditional → NO_COVERAGE
        } else if (isFirstNumber) {
48
            sortOrder = -1;  // or Sort by Number first
49 1 1. compare : negated conditional → NO_COVERAGE
        } else if (isSecondNumber) {
50
            sortOrder = 1;  // or Sort by Letter first
51
        } else {
52
            sortOrder = valueCellLeft.compareToIgnoreCase(valueCellRight);  // Sort by Letter
53
        }
54 1 1. compare : replaced int return with 0 for com/jsql/view/swing/table/ComparatorColumn::compare → NO_COVERAGE
        return sortOrder;
55
    }
56
}

Mutations

45

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

2.2
Location : compare
Killed by : none
negated conditional → NO_COVERAGE

47

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

49

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

54

1.1
Location : compare
Killed by : none
replaced int return with 0 for com/jsql/view/swing/table/ComparatorColumn::compare → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.18.2