if (orderBy == null) {
comparator = null;
} else if (orderBy.size() == 1) {
Map.Entry<String, SortOrder> entry = orderBy.entrySet().iterator().next();
Comparator comp = new ReflectionExtractor(entry.getKey());
comparator = entry.getValue() == SortOrder.ASCENDING ? comp : new InverseComparator(comp);
} else {
ArrayList<Comparator> comparators = new ArrayList<Comparator>(orderBy.size());
for (Map.Entry<String, SortOrder> entry : orderBy.entrySet()) {
Comparator comp = new ReflectionExtractor(entry.getKey());
comparators.add(entry.getValue() == SortOrder.ASCENDING ? comp : new InverseComparator(comp));
}
comparator = new ChainedComparator(comparators.toArray(new Comparator[comparators.size()]));
}
if (offset > 0 && limit >= 0) {
long bestSize = -1;