[#1710] Add <E> Map<Record, List<E>> ResultQuery.fetchGroups(Field<?>[],

Class<E>) - Aligned API with other fetchGroups() methods
This commit is contained in:
Lukas Eder 2012-10-12 14:03:15 +02:00
parent 70c4c704c6
commit cdccbceb61
6 changed files with 31 additions and 29 deletions

View File

@ -1803,40 +1803,40 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
// keys -> POJO
// Grouping by BOOK.AUTHOR_ID, BOOK.LANGUAGE_ID
Map<List<?>, List<Object>> map3 = create().selectFrom(TBook()).orderBy(TBook_ID())
Map<Record, List<Object>> map3 = create().selectFrom(TBook()).orderBy(TBook_ID())
.fetchGroups(new Field<?>[] { TBook_AUTHOR_ID(), TBook_LANGUAGE_ID() }, TBookPojo());
Iterator<Entry<List<?>, List<Object>>> iterator = map3.entrySet().iterator();
Entry<List<?>, List<Object>> entry1_en = iterator.next();
Iterator<Entry<Record, List<Object>>> iterator = map3.entrySet().iterator();
Entry<Record, List<Object>> entry1_en = iterator.next();
assertEquals(2, entry1_en.getValue().size());
assertEquals(entry1_en.getKey().get(0), on(entry1_en.getValue().get(0)).call("getAuthorId").get());
assertEquals(entry1_en.getKey().get(0), on(entry1_en.getValue().get(1)).call("getAuthorId").get());
assertEquals(entry1_en.getKey().get(1), on(entry1_en.getValue().get(0)).call("getLanguageId").get());
assertEquals(entry1_en.getKey().get(1), on(entry1_en.getValue().get(1)).call("getLanguageId").get());
assertEquals(entry1_en.getKey().getValue(0), on(entry1_en.getValue().get(0)).call("getAuthorId").get());
assertEquals(entry1_en.getKey().getValue(0), on(entry1_en.getValue().get(1)).call("getAuthorId").get());
assertEquals(entry1_en.getKey().getValue(1), on(entry1_en.getValue().get(0)).call("getLanguageId").get());
assertEquals(entry1_en.getKey().getValue(1), on(entry1_en.getValue().get(1)).call("getLanguageId").get());
Entry<List<?>, List<Object>> entry2_pt = iterator.next();
Entry<Record, List<Object>> entry2_pt = iterator.next();
assertEquals(1, entry2_pt.getValue().size());
assertEquals(entry2_pt.getKey().get(0), on(entry2_pt.getValue().get(0)).call("getAuthorId").get());
assertEquals(entry2_pt.getKey().get(1), on(entry2_pt.getValue().get(0)).call("getLanguageId").get());
assertEquals(entry2_pt.getKey().getValue(0), on(entry2_pt.getValue().get(0)).call("getAuthorId").get());
assertEquals(entry2_pt.getKey().getValue(1), on(entry2_pt.getValue().get(0)).call("getLanguageId").get());
Entry<List<?>, List<Object>> entry2_de = iterator.next();
Entry<Record, List<Object>> entry2_de = iterator.next();
assertEquals(1, entry2_de.getValue().size());
assertEquals(entry2_de.getKey().get(0), on(entry2_de.getValue().get(0)).call("getAuthorId").get());
assertEquals(entry2_de.getKey().get(1), on(entry2_de.getValue().get(0)).call("getLanguageId").get());
assertEquals(entry2_de.getKey().getValue(0), on(entry2_de.getValue().get(0)).call("getAuthorId").get());
assertEquals(entry2_de.getKey().getValue(1), on(entry2_de.getValue().get(0)).call("getLanguageId").get());
assertFalse(iterator.hasNext());
// Grouping by BOOK.AUTHOR_ID, BOOK.LANGUAGE_ID, BOOK.TITLE
Map<List<?>, List<Object>> map4 = create().selectFrom(TBook()).orderBy(TBook_ID())
Map<Record, List<Object>> map4 = create().selectFrom(TBook()).orderBy(TBook_ID())
.fetchGroups(new Field<?>[] { TBook_ID(), TBook_LANGUAGE_ID(), TBook_TITLE() }, TBookPojo());
assertEquals(4, map4.size());
for (List<?> keyList : map4.keySet()) {
for (Record keyList : map4.keySet()) {
List<Object> result = map4.get(keyList);
assertEquals(1, result.size());
assertEquals(keyList.get(0), on(result.get(0)).call("getId").get());
assertEquals(keyList.get(1), on(result.get(0)).call("getLanguageId").get());
assertEquals(keyList.get(2), on(result.get(0)).call("getTitle").get());
assertEquals(keyList.getValue(0), on(result.get(0)).call("getId").get());
assertEquals(keyList.getValue(1), on(result.get(0)).call("getLanguageId").get());
assertEquals(keyList.getValue(2), on(result.get(0)).call("getTitle").get());
}
}

View File

@ -1955,7 +1955,7 @@ public interface Result<R extends Record> extends FieldProvider, List<R>, Attach
* @throws MappingException wrapping any reflection or data type conversion
* exception that might have occurred while mapping records
*/
<E> Map<List<?>, List<E>> intoGroups(Field<?>[] keys, Class<? extends E> type) throws MappingException;
<E> Map<Record, List<E>> intoGroups(Field<?>[] keys, Class<? extends E> type) throws MappingException;
/**

View File

@ -623,7 +623,7 @@ public interface ResultQuery<R extends Record> extends Query {
* exception that might have occurred while mapping records
* @see Result#intoGroups(Field[], Class)
*/
<E> Map<List<?>, List<E>> fetchGroups(Field<?>[] keys, Class<? extends E> type) throws MappingException;
<E> Map<Record, List<E>> fetchGroups(Field<?>[] keys, Class<? extends E> type) throws MappingException;
/**
* Return a {@link Map} with results grouped by the given key and mapped

View File

@ -285,7 +285,7 @@ abstract class AbstractDelegatingSelect<R extends Record>
}
@Override
public final <E> Map<List<?>, List<E>> fetchGroups(Field<?>[] keys, Class<? extends E> type) {
public final <E> Map<Record, List<E>> fetchGroups(Field<?>[] keys, Class<? extends E> type) {
return getDelegate().fetchGroups(keys, type);
}

View File

@ -481,7 +481,7 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
}
@Override
public final <E> Map<List<?>, List<E>> fetchGroups(Field<?>[] keys, Class<? extends E> type) {
public final <E> Map<Record, List<E>> fetchGroups(Field<?>[] keys, Class<? extends E> type) {
return fetch().intoGroups(keys, type);
}

View File

@ -1538,23 +1538,25 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
}
@Override
public final <E> Map<List<?>, List<E>> intoGroups(Field<?>[] keys, Class<? extends E> type) {
public final <E> Map<Record, List<E>> intoGroups(Field<?>[] keys, Class<? extends E> type) {
if (keys == null) {
keys = new Field[0];
}
Map<List<?>, List<E>> map = new LinkedHashMap<List<?>, List<E>>();
Map<Record, List<E>> map = new LinkedHashMap<Record, List<E>>();
FieldList keyList = new FieldList(keys);
for (R record : this) {
List<Object> keyValueList = new ArrayList<Object>();
for (Field<?> key : keys) {
keyValueList.add(record.getValue(key));
Record key = new RecordImpl(keyList);
for (Field<?> field : keys) {
Util.setValue(key, field, record, field);
}
List<E> list = map.get(keyValueList);
List<E> list = map.get(key);
if (list == null) {
list = new ArrayList<E>();
map.put(keyValueList, list);
map.put(key, list);
}
list.add(record.into(type));