[#1975] Add Result.sort{Asc | Desc}(int) and (String) to order by field

index / name Edit
This commit is contained in:
Lukas Eder 2012-11-23 12:20:15 +01:00
parent 4bcc9f5524
commit e1ca385651
2 changed files with 152 additions and 1 deletions

View File

@ -839,6 +839,54 @@ public interface Result<R extends Record> extends FieldProvider, List<R>, Attach
*/
<T extends Comparable<? super T>> Result<R> sortDesc(Field<T> field) throws IllegalArgumentException;
/**
* Sort this result by one of its contained fields.
* <p>
* <code>nulls</code> are sorted last by this method.
*
* @param fieldIndex The sort field index
* @return The result itself
* @throws IllegalArgumentException If the argument field is not contained
* in {@link #getFields()}
*/
Result<R> sortAsc(int fieldIndex) throws IllegalArgumentException;
/**
* Reverse-sort this result by one of its contained fields.
* <p>
* <code>nulls</code> are sorted last by this method.
*
* @param fieldIndex The sort field index
* @return The result itself
* @throws IllegalArgumentException If the argument field is not contained
* in {@link #getFields()}
*/
Result<R> sortDesc(int fieldIndex) throws IllegalArgumentException;
/**
* Sort this result by one of its contained fields.
* <p>
* <code>nulls</code> are sorted last by this method.
*
* @param fieldName The sort field name
* @return The result itself
* @throws IllegalArgumentException If the argument field is not contained
* in {@link #getFields()}
*/
Result<R> sortAsc(String fieldName) throws IllegalArgumentException;
/**
* Reverse-sort this result by one of its contained fields.
* <p>
* <code>nulls</code> are sorted last by this method.
*
* @param fieldName The sort field name
* @return The result itself
* @throws IllegalArgumentException If the argument field is not contained
* in {@link #getFields()}
*/
Result<R> sortDesc(String fieldName) throws IllegalArgumentException;
/**
* Sort this result by one of its contained fields using a comparator.
* <p>
@ -868,6 +916,64 @@ public interface Result<R extends Record> extends FieldProvider, List<R>, Attach
*/
<T> Result<R> sortDesc(Field<T> field, java.util.Comparator<? super T> comparator) throws IllegalArgumentException;
/**
* Sort this result by one of its contained fields using a comparator.
* <p>
* <code>null</code> sorting must be handled by the supplied
* <code>comparator</code>.
*
* @param fieldIndex The sort field index
* @param comparator The comparator used to sort this result.
* @return The result itself
* @throws IllegalArgumentException If the argument field is not contained
* in {@link #getFields()}
*/
Result<R> sortAsc(int fieldIndex, java.util.Comparator<?> comparator) throws IllegalArgumentException;
/**
* Reverse-sort this result by one of its contained fields using a
* comparator.
* <p>
* <code>null</code> sorting must be handled by the supplied
* <code>comparator</code>.
*
* @param fieldIndex The sort field index
* @param comparator The comparator used to sort this result.
* @return The result itself
* @throws IllegalArgumentException If the argument field is not contained
* in {@link #getFields()}
*/
Result<R> sortDesc(int fieldIndex, java.util.Comparator<?> comparator) throws IllegalArgumentException;
/**
* Sort this result by one of its contained fields using a comparator.
* <p>
* <code>null</code> sorting must be handled by the supplied
* <code>comparator</code>.
*
* @param fieldName The sort field name
* @param comparator The comparator used to sort this result.
* @return The result itself
* @throws IllegalArgumentException If the argument field is not contained
* in {@link #getFields()}
*/
Result<R> sortAsc(String fieldName, java.util.Comparator<?> comparator) throws IllegalArgumentException;
/**
* Reverse-sort this result by one of its contained fields using a
* comparator.
* <p>
* <code>null</code> sorting must be handled by the supplied
* <code>comparator</code>.
*
* @param fieldName The sort field name
* @param comparator The comparator used to sort this result.
* @return The result itself
* @throws IllegalArgumentException If the argument field is not contained
* in {@link #getFields()}
*/
Result<R> sortDesc(String fieldName, java.util.Comparator<?> comparator) throws IllegalArgumentException;
/**
* Sort this result using a comparator that can compare records.
*

View File

@ -1046,9 +1046,32 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
return sortAsc(field, new NaturalComparator<T>());
}
@SuppressWarnings("rawtypes")
@Override
public final Result<R> sortAsc(int fieldIndex) {
return sortAsc(fieldIndex, new NaturalComparator());
}
@SuppressWarnings("rawtypes")
@Override
public final Result<R> sortAsc(String fieldName) {
return sortAsc(fieldName, new NaturalComparator());
}
@Override
public final <T> Result<R> sortAsc(Field<T> field, Comparator<? super T> comparator) {
return sortAsc(new RecordComparator<T, R>(getIndex(field), comparator));
return sortAsc(getIndex(field), comparator);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public final Result<R> sortAsc(int fieldIndex, Comparator<?> comparator) {
return sortAsc(new RecordComparator(fieldIndex, comparator));
}
@Override
public final Result<R> sortAsc(String fieldName, Comparator<?> comparator) {
return sortAsc(getIndex(getField(fieldName)), comparator);
}
@Override
@ -1062,11 +1085,33 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
return sortAsc(field, Collections.reverseOrder(new NaturalComparator<T>()));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public final Result<R> sortDesc(int fieldIndex) {
return sortAsc(fieldIndex, Collections.reverseOrder(new NaturalComparator()));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public final Result<R> sortDesc(String fieldName) {
return sortAsc(fieldName, Collections.reverseOrder(new NaturalComparator()));
}
@Override
public final <T> Result<R> sortDesc(Field<T> field, Comparator<? super T> comparator) {
return sortAsc(field, Collections.reverseOrder(comparator));
}
@Override
public final Result<R> sortDesc(int fieldIndex, Comparator<?> comparator) {
return sortAsc(fieldIndex, Collections.reverseOrder(comparator));
}
@Override
public final Result<R> sortDesc(String fieldName, Comparator<?> comparator) {
return sortAsc(fieldName, Collections.reverseOrder(comparator));
}
@Override
public final Result<R> sortDesc(Comparator<? super R> comparator) {
return sortAsc(Collections.reverseOrder(comparator));