mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
small renamings
This commit is contained in:
commit
215c9935e5
@ -1,6 +1,19 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query;
|
||||
|
||||
|
||||
/**
|
||||
* Executes query on a Projectable and transforms results into T. This can be used for example
|
||||
* to group projected columns or to filter out duplicate results.
|
||||
*
|
||||
* @see com.mysema.query.support.GroupBy
|
||||
* @author sasa
|
||||
*
|
||||
* @param <T> Transformations target type
|
||||
*/
|
||||
public interface ResultTransformer<T> {
|
||||
|
||||
public T transform(Projectable projectable);
|
||||
|
||||
@ -9,6 +9,7 @@ import java.util.List;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.mysema.query.Tuple;
|
||||
import com.mysema.query.types.Expression;
|
||||
|
||||
/**
|
||||
@ -62,6 +63,16 @@ public interface Group {
|
||||
<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);
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
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;
|
||||
|
||||
/**
|
||||
@ -112,10 +113,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;
|
||||
@ -26,6 +28,17 @@ public class GroupByTest {
|
||||
|
||||
private final NumberExpression<Integer> commentId = new NumberPath<Integer>(Integer.class, "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,9 @@ public class GroupByTest {
|
||||
return row;
|
||||
}
|
||||
|
||||
private static <T> CloseableIterator<T> iterator(T... rows) {
|
||||
return new IteratorAdapter<T>(Arrays.asList(rows).iterator());
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> CloseableIterator<T> iterator(Object[]... rows) {
|
||||
return new IteratorAdapter(Arrays.asList(rows).iterator());
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user