public Query<?> toPreviousQuery(State state) {
String sortFieldName = getSortFieldName();
Query<?> query = toQuery().and(sortFieldName + " <= ? and _id != ?", state.get(sortFieldName), state.getId());
for (ListIterator<Sorter> i = query.getSorters().listIterator(); i.hasNext();) {
Sorter sorter = i.next();
if (Sorter.ASCENDING_OPERATOR.equals(sorter.getOperator())) {
List<Object> options = sorter.getOptions();
if (options != null &&
options.size() > 0 &&
sortFieldName.equals(options.get(0))) {
i.set(new Sorter(Sorter.DESCENDING_OPERATOR, options));
}
}
}
return query;