use of org.revapi.CorrespondenceComparatorDeducer in project revapi by revapi.
the class JavaApiAnalyzer method getCorrespondenceDeducer.
@Override
@Nonnull
public CorrespondenceComparatorDeducer getCorrespondenceDeducer() {
return (l1, l2) -> {
if (l1.isEmpty() || l2.isEmpty()) {
return Comparator.naturalOrder();
}
// quickly peek inside to see if there even can be methods in the lists - all of the elements in either list
// will have a common parent and parents of both lists will have the same type or be both null.
Element parent = l1.get(0).getParent();
if (!(parent instanceof TypeElement)) {
return Comparator.naturalOrder();
}
IdentityHashMap<MethodElement, Integer> c1MethodOrder = new IdentityHashMap<>(l1.size());
IdentityHashMap<MethodElement, Integer> c2MethodOrder = new IdentityHashMap<>(l2.size());
// this will reorder the methods in the lists and will also fill in the method order indices in the maps
// so that they can be used for comparisons below
determineOrder(l1, l2, c1MethodOrder, c2MethodOrder);
// and return a comparator
return (e1, e2) -> {
int ret = JavaElementFactory.compareByType(e1, e2);
if (ret != 0) {
return ret;
}
// let's just look that up.
if (e1 instanceof MethodElement && e2 instanceof MethodElement) {
MethodElement m1 = (MethodElement) e1;
MethodElement m2 = (MethodElement) e2;
return c1MethodOrder.get(m1) - c2MethodOrder.get(m2);
} else {
return e1.compareTo(e2);
}
};
};
}
Aggregations