}
}
if (comparators.size() == 1) {
comparator = comparators.get(0);
} else {
comparator = new ChainedComparator(comparators.toArray(new Comparator[comparators.size()]));
}
}
} else {
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;
int bestExtra = Integer.MAX_VALUE;
for (long pageSize = limit; pageSize < 2 * limit || bestSize < 0; ++pageSize) {