mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
Added getRow(int) to Group
This commit is contained in:
parent
ebe6903e45
commit
99c422d2cf
@ -9,6 +9,7 @@ import java.util.List;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.mysema.query.Tuple;
|
||||
import com.mysema.query.types.Expression;
|
||||
|
||||
/**
|
||||
@ -61,6 +62,15 @@ public interface Group {
|
||||
@Nullable
|
||||
<T> List<T> getList(Expression<T> expr);
|
||||
|
||||
/**
|
||||
* Returns the i'th row of this group.
|
||||
*
|
||||
* @param i 0 <= i < size()
|
||||
* @throws IndexOutOfBoundsException if i < 0 or size() <= i
|
||||
* @return i'th row as a tuple of this group
|
||||
*/
|
||||
Tuple getRow(int i);
|
||||
|
||||
int size();
|
||||
|
||||
}
|
||||
|
||||
@ -13,6 +13,7 @@ import java.util.List;
|
||||
import com.mysema.commons.lang.CloseableIterator;
|
||||
import com.mysema.query.Projectable;
|
||||
import com.mysema.query.ResultTransformer;
|
||||
import com.mysema.query.Tuple;
|
||||
import com.mysema.query.types.Expression;
|
||||
|
||||
/**
|
||||
@ -114,10 +115,40 @@ public class GroupBy implements ResultTransformer<Collection<Group>> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple getRow(int i) {
|
||||
return new TupleImpl(values.get(i));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return values.size();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private class TupleImpl implements Tuple {
|
||||
|
||||
final Object[] row;
|
||||
|
||||
public TupleImpl(Object[] row) {
|
||||
this.row = row;
|
||||
}
|
||||
@Override
|
||||
public <T> T get(int index, Class<T> type) {
|
||||
return (T) row[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T get(Expression<T> expr) {
|
||||
int index = indexOf(expr);
|
||||
return index != -1 ? (T) row[index] : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return row;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -11,7 +11,9 @@ import java.util.List;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.commons.lang.CloseableIterator;
|
||||
import com.mysema.commons.lang.EmptyCloseableIterator;
|
||||
import com.mysema.commons.lang.IteratorAdapter;
|
||||
import com.mysema.query.Tuple;
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.expr.NumberExpression;
|
||||
import com.mysema.query.types.expr.StringExpression;
|
||||
@ -20,12 +22,23 @@ import com.mysema.query.types.path.StringPath;
|
||||
|
||||
public class GroupByTest {
|
||||
|
||||
private final NumberExpression<Integer> postId = new NumberPath(Integer.class, null, "postId");
|
||||
private final NumberExpression<Integer> postId = new NumberPath<Integer>(Integer.class, null, "postId");
|
||||
|
||||
private final StringExpression postName = new StringPath(null, "postName");
|
||||
|
||||
private final NumberExpression<Integer> commentId = new NumberPath(Integer.class, null, "commentId");
|
||||
private final NumberExpression<Integer> commentId = new NumberPath<Integer>(Integer.class, null, "commentId");
|
||||
|
||||
@Test
|
||||
public void Expression_Order() {
|
||||
new GroupBy(postId, postName, commentId).transform(new AbstractProjectable(){
|
||||
public CloseableIterator<Object[]> iterate(Expression<?>[] args) {
|
||||
assertEquals(postId, args[0]);
|
||||
assertEquals(postName, args[1]);
|
||||
assertEquals(commentId, args[2]);
|
||||
return new EmptyCloseableIterator<Object[]>();
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* <ol>
|
||||
* <li>Order of groups by first row of a group
|
||||
@ -34,13 +47,9 @@ public class GroupByTest {
|
||||
* </ol>
|
||||
*/
|
||||
@Test
|
||||
public void Group_By() {
|
||||
public void Multiple_Groups() {
|
||||
Collection<Group> results = new GroupBy(postId, postName, commentId).transform(new AbstractProjectable(){
|
||||
public CloseableIterator<Object[]> iterate(Expression<?>[] args) {
|
||||
assertEquals(postId, args[0]);
|
||||
assertEquals(postName, args[1]);
|
||||
assertEquals(commentId, args[2]);
|
||||
|
||||
return iterator(
|
||||
row(1, "post 1", 1),
|
||||
row(2, "post 2", 4),
|
||||
@ -90,6 +99,40 @@ public class GroupByTest {
|
||||
assertEquals(toInt(8), comments.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Get_Row() {
|
||||
Collection<Group> results = new GroupBy(postId, postName, commentId).transform(new AbstractProjectable(){
|
||||
public CloseableIterator<Object[]> iterate(Expression<?>[] args) {
|
||||
return iterator(
|
||||
row(1, "post 1", 1),
|
||||
row(null, "null post", 2)
|
||||
);
|
||||
}
|
||||
});
|
||||
assertEquals(2, results.size());
|
||||
Iterator<Group> iter = results.iterator();
|
||||
|
||||
Group group = iter.next();
|
||||
Tuple row = group.getRow(0);
|
||||
assertEquals(toInt(1), row.get(postId));
|
||||
assertEquals("post 1", row.get(postName));
|
||||
assertEquals(toInt(1), row.get(commentId));
|
||||
|
||||
group = iter.next();
|
||||
row = group.getRow(0);
|
||||
assertEquals(null, row.get(postId));
|
||||
}
|
||||
|
||||
@Test(expected=IndexOutOfBoundsException.class)
|
||||
public void Row_Out_of_Bounds() {
|
||||
Collection<Group> results = new GroupBy(postId, postName, commentId).transform(new AbstractProjectable(){
|
||||
public CloseableIterator<Object[]> iterate(Expression<?>[] args) {
|
||||
return iterator(row(1, "post 1", 1));
|
||||
}
|
||||
});
|
||||
results.iterator().next().getRow(1);
|
||||
}
|
||||
|
||||
private Integer toInt(int i) {
|
||||
return Integer.valueOf(i);
|
||||
}
|
||||
@ -98,7 +141,7 @@ public class GroupByTest {
|
||||
return row;
|
||||
}
|
||||
|
||||
private static <T> CloseableIterator<T> iterator(Object[]... rows) {
|
||||
return new IteratorAdapter(Arrays.asList(rows).iterator());
|
||||
private static CloseableIterator<Object[]> iterator(Object[]... rows) {
|
||||
return new IteratorAdapter<Object[]>(Arrays.asList(rows).iterator());
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user