Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Fix invalid comparators #3251
Many of the table column comparators (as well as PaymentMethod.compareTo) are invalid, since they immediately return 0 upon encountering a null/missing field. This violates the Comparator/Comparable contract, which expects a total order on the objects and, in particular, an equivalence relation given by the result of "compare(x, y) == 0".
This PR attemps to fix all the invalid Comparator / Comparable instances (which are almost all table column comparators), by ordering objects with a null/missing (sub)field ahead of the populated ones. In most cases this will probably have no effect, as nulls won't be encountered for that column in practice. However, the current flawed comparators are at least breaking row sorting in the Portfolio/History table, as seen in the following two screenshots:
The ascending/descending sorting of the "Amount in BTC" field does not make sense, due to a broken comparator passed to the underlying FX library insertion sort. After the fix, the rows appear as follows:
The missing cells are deemed smaller than all the others in the column, which is consistent with TableColumnBase.DEFAULT_COMPARATOR from the openjfx library.
Don't return 0 in the case of a null 'this.id' field, as that violates the Comparable contract and leads to inconsistent NPE throwing when the arguments are swapped. Instead, just assume (reasonably) that the ID field will never be null.
Remove the flawed pattern of returning 0 in a comparator when the sub- field of one of the two inputs being compared is null or absent. This violates the Comparator contract, since returning 0 or otherwise should define an equivalence relation. Use Comparator.nullsFirst(..) in the table column comparators instead, to ensure consistent ordering when a cell is missing a value. This fixes ill-defined and erratic behaviour in the underlying merge/insertion sort of the table rows done by the FX library.