ComparatorCombinatorsInspection: support chain comparisons, provide fix

This commit is contained in:
Roman
2017-10-02 18:43:25 +07:00
parent 2c033cb321
commit 19551bd6af
16 changed files with 679 additions and 55 deletions
@@ -0,0 +1,18 @@
// "Replace with Comparator chain" "true"
import java.util.Comparator;
import java.util.List;
public class Main {
static class Part implements Comparable<Part> {
}
static class Obj {
double first;
Part second;
}
void sort(List<Obj> objs) {
objs.sort(Comparator.comparingDouble((Obj o) -> o.first).thenComparing(o -> o.second));
}
}
@@ -0,0 +1,20 @@
// "Replace with Comparator chain" "true"
import java.util.Comparator;
import java.util.List;
public class Main {
static class Part implements Comparable<Part> {
}
static class Obj {
double first;
long second;
char third(){};
Part fourth;
}
void sort(List<Obj> objs) {
objs.sort(Comparator.comparingDouble((Obj o) -> o.first).thenComparingLong(o -> o.second).thenComparingInt(Obj::third).thenComparing(o -> o.fourth));
}
}
@@ -0,0 +1,19 @@
// "Replace with Comparator chain" "true"
import java.util.Comparator;
import java.util.List;
public class Main {
static class Part implements Comparable<Part> {
}
static class Obj {
Part first;
Part second;
int third;
}
void sort(List<Obj> objs) {
objs.sort(Comparator.comparing((Obj o) -> o.first).thenComparing(o -> o.second).thenComparingInt(o -> o.third));
}
}
@@ -0,0 +1,18 @@
// "Replace with Comparator chain" "true"
import java.util.Comparator;
import java.util.List;
public class Main {
static class Part implements Comparable<Part> {
}
static class Obj {
Part first;
Part second;
}
void sort(List<Obj> objs) {
objs.sort(Comparator.comparing((Obj o) -> o.first).thenComparing(o -> o.second));
}
}
@@ -0,0 +1,18 @@
// "Replace with Comparator chain" "true"
import java.util.Comparator;
import java.util.List;
public class Main {
static class Part implements Comparable<Part> {
}
static class Obj {
Part first;
Part second;
}
void sort(List<Obj> objs) {
objs.sort(Comparator.comparing((Obj o) -> o.first).thenComparing(o -> o.second));
}
}
@@ -0,0 +1,19 @@
// "Replace with Comparator chain" "true"
import java.util.Comparator;
import java.util.List;
public class Main {
static class Part implements Comparable<Part> {
}
static class Obj {
Part first;
Part second;
Part third;
}
void sort(List<Obj> objs) {
objs.sort(Comparator.comparing((Obj o) -> o.first).thenComparing(o -> o.second).thenComparing(o -> o.third));
}
}
@@ -0,0 +1,21 @@
// "Replace with Comparator chain" "true"
import java.util.List;
public class Main {
static class Part implements Comparable<Part> {
}
static class Obj {
double first;
Part second;
}
void sort(List<Obj> objs) {
objs.sort((o1, o2) -> {<caret>
int res = Double.compare(o1.first, o2.first);
if(res == 0) res = o1.second.compareTo(o2.second);
return res;
});
}
}
@@ -0,0 +1,25 @@
// "Replace with Comparator chain" "true"
import java.util.List;
public class Main {
static class Part implements Comparable<Part> {
}
static class Obj {
double first;
long second;
char third(){};
Part fourth;
}
void sort(List<Obj> objs) {
objs.sort((o1, o2) -> {<caret>
int res = Double.compare(o1.first, o2.first);
if(res == 0) res = Long.compare(o1.second, o2.second);
if(res == 0) res = o1.third() - o2.third();
if(res == 0) res = o1.fourth.compareTo(o2.fourth);
return res;
});
}
}
@@ -0,0 +1,23 @@
// "Replace with Comparator chain" "true"
import java.util.List;
public class Main {
static class Part implements Comparable<Part> {
}
static class Obj {
Part first;
Part second;
int third;
}
void sort(List<Obj> objs) {
objs.sort((o1, o2) -> {<caret>
int res = o1.first.compareTo(o2.first);
if(res == 0) res = o1.second.compareTo(o2.second);
if(res == 0) res = o1.third - o2.third;
return res;
});
}
}
@@ -0,0 +1,21 @@
// "Replace with Comparator chain" "true"
import java.util.List;
public class Main {
static class Part implements Comparable<Part> {
}
static class Obj {
Part first;
Part second;
}
void sort(List<Obj> objs) {
objs.sort((o1, o2) -> {<caret>
int res = o1.first.compareTo(o2.first);
if(res == 0) res = o1.second.compareTo(o2.second);
return res;
});
}
}
@@ -0,0 +1,20 @@
// "Replace with Comparator chain" "true"
import java.util.List;
public class Main {
static class Part implements Comparable<Part> {
}
static class Obj {
Part first;
Part second;
}
void sort(List<Obj> objs) {
objs.sort((o1, o2) -> {<caret>
int res = o1.first.compareTo(o2.first);
return res == 0 ? o1.second.compareTo(o2.second) : res;
});
}
}
@@ -0,0 +1,23 @@
// "Replace with Comparator chain" "true"
import java.util.List;
public class Main {
static class Part implements Comparable<Part> {
}
static class Obj {
Part first;
Part second;
Part third;
}
void sort(List<Obj> objs) {
objs.sort((o1, o2) -> {<caret>
int res = o1.first.compareTo(o2.first);
if(res != 0) return res;
res = o1.second.compareTo(o2.second);
return res == 0 ? o1.third.compareTo(o2.third) : res;
});
}
}