From e1ca385651c5e3c8dce378daca0b1d590095ef04 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 23 Nov 2012 12:20:15 +0100 Subject: [PATCH] [#1975] Add Result.sort{Asc | Desc}(int) and (String) to order by field index / name Edit --- jOOQ/src/main/java/org/jooq/Result.java | 106 ++++++++++++++++++ .../main/java/org/jooq/impl/ResultImpl.java | 47 +++++++- 2 files changed, 152 insertions(+), 1 deletion(-) diff --git a/jOOQ/src/main/java/org/jooq/Result.java b/jOOQ/src/main/java/org/jooq/Result.java index 220dda13d8..05f7588d8b 100644 --- a/jOOQ/src/main/java/org/jooq/Result.java +++ b/jOOQ/src/main/java/org/jooq/Result.java @@ -839,6 +839,54 @@ public interface Result extends FieldProvider, List, Attach */ > Result sortDesc(Field field) throws IllegalArgumentException; + /** + * Sort this result by one of its contained fields. + *

+ * nulls 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 sortAsc(int fieldIndex) throws IllegalArgumentException; + + /** + * Reverse-sort this result by one of its contained fields. + *

+ * nulls 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 sortDesc(int fieldIndex) throws IllegalArgumentException; + + /** + * Sort this result by one of its contained fields. + *

+ * nulls 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 sortAsc(String fieldName) throws IllegalArgumentException; + + /** + * Reverse-sort this result by one of its contained fields. + *

+ * nulls 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 sortDesc(String fieldName) throws IllegalArgumentException; + /** * Sort this result by one of its contained fields using a comparator. *

@@ -868,6 +916,64 @@ public interface Result extends FieldProvider, List, Attach */ Result sortDesc(Field field, java.util.Comparator comparator) throws IllegalArgumentException; + /** + * Sort this result by one of its contained fields using a comparator. + *

+ * null sorting must be handled by the supplied + * comparator. + * + * @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 sortAsc(int fieldIndex, java.util.Comparator comparator) throws IllegalArgumentException; + + /** + * Reverse-sort this result by one of its contained fields using a + * comparator. + *

+ * null sorting must be handled by the supplied + * comparator. + * + * @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 sortDesc(int fieldIndex, java.util.Comparator comparator) throws IllegalArgumentException; + + /** + * Sort this result by one of its contained fields using a comparator. + *

+ * null sorting must be handled by the supplied + * comparator. + * + * @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 sortAsc(String fieldName, java.util.Comparator comparator) throws IllegalArgumentException; + + /** + * Reverse-sort this result by one of its contained fields using a + * comparator. + *

+ * null sorting must be handled by the supplied + * comparator. + * + * @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 sortDesc(String fieldName, java.util.Comparator comparator) throws IllegalArgumentException; + /** * Sort this result using a comparator that can compare records. * diff --git a/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java b/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java index f66977bf6b..ef5d862522 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java @@ -1046,9 +1046,32 @@ class ResultImpl implements Result, AttachableInternal { return sortAsc(field, new NaturalComparator()); } + @SuppressWarnings("rawtypes") + @Override + public final Result sortAsc(int fieldIndex) { + return sortAsc(fieldIndex, new NaturalComparator()); + } + + @SuppressWarnings("rawtypes") + @Override + public final Result sortAsc(String fieldName) { + return sortAsc(fieldName, new NaturalComparator()); + } + @Override public final Result sortAsc(Field field, Comparator comparator) { - return sortAsc(new RecordComparator(getIndex(field), comparator)); + return sortAsc(getIndex(field), comparator); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Override + public final Result sortAsc(int fieldIndex, Comparator comparator) { + return sortAsc(new RecordComparator(fieldIndex, comparator)); + } + + @Override + public final Result sortAsc(String fieldName, Comparator comparator) { + return sortAsc(getIndex(getField(fieldName)), comparator); } @Override @@ -1062,11 +1085,33 @@ class ResultImpl implements Result, AttachableInternal { return sortAsc(field, Collections.reverseOrder(new NaturalComparator())); } + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Override + public final Result sortDesc(int fieldIndex) { + return sortAsc(fieldIndex, Collections.reverseOrder(new NaturalComparator())); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Override + public final Result sortDesc(String fieldName) { + return sortAsc(fieldName, Collections.reverseOrder(new NaturalComparator())); + } + @Override public final Result sortDesc(Field field, Comparator comparator) { return sortAsc(field, Collections.reverseOrder(comparator)); } + @Override + public final Result sortDesc(int fieldIndex, Comparator comparator) { + return sortAsc(fieldIndex, Collections.reverseOrder(comparator)); + } + + @Override + public final Result sortDesc(String fieldName, Comparator comparator) { + return sortAsc(fieldName, Collections.reverseOrder(comparator)); + } + @Override public final Result sortDesc(Comparator comparator) { return sortAsc(Collections.reverseOrder(comparator));