[#1712] Add <T extends Comparable<? super T>> Result.sortAsc,
.sortDesc(Field<T>) [#1713] Add <T> Result.sortAsc, .sortDesc(Field<T>, Comparator<? super T>)
This commit is contained in:
parent
32cd3d9393
commit
d5bf83cfe8
107
jOOQ-test/src/org/jooq/test/_/testcases/ResultTests.java
Normal file
107
jOOQ-test/src/org/jooq/test/_/testcases/ResultTests.java
Normal file
@ -0,0 +1,107 @@
|
||||
/**
|
||||
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed to you under the Apache License, Version 2.0
|
||||
* (the "License"); You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* . Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* . Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* . Neither the name "jOOQ" nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.jooq.test._.testcases;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.jooq.Result;
|
||||
import org.jooq.TableRecord;
|
||||
import org.jooq.UpdatableRecord;
|
||||
import org.jooq.test.BaseTest;
|
||||
import org.jooq.test.jOOQAbstractTest;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ResultTests<
|
||||
A extends UpdatableRecord<A>,
|
||||
AP,
|
||||
B extends UpdatableRecord<B>,
|
||||
S extends UpdatableRecord<S>,
|
||||
B2S extends UpdatableRecord<B2S>,
|
||||
BS extends UpdatableRecord<BS>,
|
||||
L extends TableRecord<L>,
|
||||
X extends TableRecord<X>,
|
||||
DATE extends UpdatableRecord<DATE>,
|
||||
BOOL extends UpdatableRecord<BOOL>,
|
||||
D extends UpdatableRecord<D>,
|
||||
T extends UpdatableRecord<T>,
|
||||
U extends TableRecord<U>,
|
||||
I extends TableRecord<I>,
|
||||
IPK extends UpdatableRecord<IPK>,
|
||||
T658 extends TableRecord<T658>,
|
||||
T725 extends UpdatableRecord<T725>,
|
||||
T639 extends UpdatableRecord<T639>,
|
||||
T785 extends TableRecord<T785>>
|
||||
extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658, T725, T639, T785> {
|
||||
|
||||
public ResultTests(jOOQAbstractTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658, T725, T639, T785> delegate) {
|
||||
super(delegate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResultSort() throws Exception {
|
||||
Result<B> result = create().fetch(TBook());
|
||||
|
||||
class C implements Comparator<Integer> {
|
||||
|
||||
@Override
|
||||
public int compare(Integer o1, Integer o2) {
|
||||
|
||||
// Put 1 at the end of everything
|
||||
if (o1 == 1 && o2 != 1) return 1;
|
||||
if (o2 == 1 && o1 != 1) return -1;
|
||||
|
||||
return o1.compareTo(o2);
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(result == result.sortAsc(TBook_ID()));
|
||||
assertEquals(asList(1, 2, 3, 4), result.getValues(TBook_ID()));
|
||||
|
||||
assertTrue(result == result.sortDesc(TBook_ID()));
|
||||
assertEquals(asList(4, 3, 2, 1), result.getValues(TBook_ID()));
|
||||
|
||||
assertTrue(result == result.sortAsc(TBook_ID(), new C()));
|
||||
assertEquals(asList(2, 3, 4, 1), result.getValues(TBook_ID()));
|
||||
|
||||
assertTrue(result == result.sortDesc(TBook_ID(), new C()));
|
||||
assertEquals(asList(1, 4, 3, 2), result.getValues(TBook_ID()));
|
||||
}
|
||||
}
|
||||
@ -109,6 +109,7 @@ import org.jooq.test._.testcases.OrderByTests;
|
||||
import org.jooq.test._.testcases.PlainSQLTests;
|
||||
import org.jooq.test._.testcases.PredicateTests;
|
||||
import org.jooq.test._.testcases.RenderAndBindTests;
|
||||
import org.jooq.test._.testcases.ResultTests;
|
||||
import org.jooq.test._.testcases.RoutineAndUDTTests;
|
||||
import org.jooq.test._.testcases.SchemaAndMappingTests;
|
||||
import org.jooq.test._.testcases.SelectTests;
|
||||
@ -964,6 +965,11 @@ public abstract class jOOQAbstractTest<
|
||||
new SelectTests(this).testDistinctQuery();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResultSort() throws Exception {
|
||||
new ResultTests(this).testResultSort();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFetch() throws Exception {
|
||||
new FetchTests(this).testFetch();
|
||||
|
||||
@ -1875,4 +1875,47 @@ public interface Result<R extends Record> extends FieldProvider, List<R>, Attach
|
||||
*/
|
||||
ResultSet intoResultSet();
|
||||
|
||||
/**
|
||||
* Sort this result by one of its contained fields.
|
||||
* <p>
|
||||
* <code>nulls</code> are sorted last by this method.
|
||||
*
|
||||
* @param field The sort field
|
||||
* @return The result itself
|
||||
*/
|
||||
<T extends Comparable<? super T>> Result<R> sortAsc(Field<T> field);
|
||||
|
||||
/**
|
||||
* Reverse-sort this result by one of its contained fields.
|
||||
* <p>
|
||||
* <code>nulls</code> are sorted last by this method.
|
||||
*
|
||||
* @param field The sort field
|
||||
* @return The result itself
|
||||
*/
|
||||
<T extends Comparable<? super T>> Result<R> sortDesc(Field<T> field);
|
||||
|
||||
/**
|
||||
* 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 field The sort field
|
||||
* @param comparator The comparator used to sort this result.
|
||||
* @return The result itself
|
||||
*/
|
||||
<T> Result<R> sortAsc(Field<T> field, java.util.Comparator<? super T> comparator);
|
||||
|
||||
/**
|
||||
* 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 field The sort field
|
||||
* @param comparator The comparator used to sort this result.
|
||||
* @return The result itself
|
||||
*/
|
||||
<T> Result<R> sortDesc(Field<T> field, java.util.Comparator<? super T> comparator);
|
||||
}
|
||||
|
||||
@ -51,6 +51,8 @@ import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
@ -1321,6 +1323,67 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
|
||||
return new ResultSetImpl(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final <T extends Comparable<? super T>> Result<R> sortAsc(Field<T> field) {
|
||||
return sortAsc(field, new NaturalComparator<T>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public final <T> Result<R> sortAsc(Field<T> field, Comparator<? super T> comparator) {
|
||||
Collections.sort(this, new RecordComparator<T>(getIndex(field), comparator));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final <T extends Comparable<? super T>> Result<R> sortDesc(Field<T> field) {
|
||||
return sortAsc(field, Collections.reverseOrder(new NaturalComparator<T>()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final <T> Result<R> sortDesc(Field<T> field, Comparator<? super T> comparator) {
|
||||
return sortAsc(field, Collections.reverseOrder(comparator));
|
||||
}
|
||||
|
||||
/**
|
||||
* A comparator for records, wrapping another comparator for <T>
|
||||
*/
|
||||
private class RecordComparator<T> implements Comparator<R> {
|
||||
|
||||
private final Comparator<? super T> comparator;
|
||||
private final int fieldIndex;
|
||||
|
||||
RecordComparator(int fieldIndex, Comparator<? super T> comparator) {
|
||||
this.fieldIndex = fieldIndex;
|
||||
this.comparator = comparator;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public int compare(R record1, R record2) {
|
||||
return comparator.compare((T) record1.getValue(fieldIndex), (T) record2.getValue(fieldIndex));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A natural comparator
|
||||
*/
|
||||
private class NaturalComparator<T extends Comparable<? super T>> implements Comparator<T> {
|
||||
|
||||
@Override
|
||||
public int compare(T o1, T o2) {
|
||||
if (o1 == null && o2 == null) {
|
||||
return 0;
|
||||
}
|
||||
else if (o1 == null) {
|
||||
return -1;
|
||||
}
|
||||
else if (o2 == null) {
|
||||
return 1;
|
||||
}
|
||||
return o1.compareTo(o2);
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX Object API
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
Loading…
Reference in New Issue
Block a user