/** * Create a new aggregating map expression using a backing LinkedHashMap * * @param key key for the map entries * @param value value for the map entries * @return wrapper expression */ public static <K, V, U> AbstractGroupExpression<Pair<K, V>, Map<K, U>> map(Expression<K> key, GroupExpression<V, U> value) { return map(new GOne<K>(key), value); }
/** * Create a new aggregating map expression using a backing LinkedHashMap * * @param key key for the map entries * @param value value for the map entries * @return wrapper expression */ public static <K, V, T> AbstractGroupExpression<Pair<K, V>, Map<T, V>> map(GroupExpression<K, T> key, Expression<V> value) { return map(key, new GOne<V>(value)); }
@Test public void map2() { Map<Integer, Map<Integer, String>> results = MAP2_RESULTS.transform( groupBy(postId).as(map(commentId, commentText))); Map<Integer, String> comments = results.get(1); assertEquals(3, comments.size()); assertEquals("comment 2", comments.get(2)); }
@Test public void map2() { List<Map<Integer, String>> results = MAP2_RESULTS.transform( groupBy(postId).list(map(commentId, commentText))); assertEquals(4, results.size()); Map<Integer, String> comments = results.get(1); assertEquals(3, comments.size()); assertEquals("comment 2", comments.get(2)); }
@Test public void map1() { Map<Integer, Group> results = MAP_RESULTS.transform( groupBy(postId).as(postName, map(commentId, commentText))); Group group = results.get(1); Map<Integer, String> comments = group.getMap(commentId, commentText); assertEquals(3, comments.size()); assertEquals("comment 2", comments.get(2)); }
@Test public void map4() { Map<Integer, Map<Map<Integer, String>, String>> actual = MAP4_RESULTS.transform( groupBy(postId).as(map(map(postId, commentText), postName))); Map<Integer, Map<Map<Integer, String>, String>> expected = new LinkedHashMap<Integer, Map<Map<Integer, String>, String>>(); for (Iterator<Tuple> iterator = MAP4_RESULTS.iterate(); iterator.hasNext();) { Tuple tuple = iterator.next(); Object[] array = tuple.toArray(); Map<Map<Integer, String>, String> comments = expected.get(array[0]); if (comments == null) { comments = new LinkedHashMap<Map<Integer, String>, String>(); expected.put((Integer) array[0], comments); } @SuppressWarnings("unchecked") Pair<Pair<Integer, String>, String> pair = (Pair<Pair<Integer, String>, String>) array[1]; Pair<Integer, String> first = pair.getFirst(); Map<Integer, String> posts = Collections.singletonMap(first.getFirst(), first.getSecond()); comments.put(posts, pair.getSecond()); } assertEquals(expected.toString(), actual.toString()); }
@Test public void map4() { CloseableIterator<Map<Map<Integer, String>, String>> results = MAP4_RESULTS.transform( groupBy(postId).iterate(map(map(postId, commentText), postName))); List<Map<Map<Integer, String>, String>> actual = IteratorAdapter.asList(results); Object commentId = null; Map<Map<Integer, String>, String> comments = null; List<Map<Map<Integer, String>, String>> expected = new LinkedList<Map<Map<Integer, String>, String>>(); for (Iterator<Tuple> iterator = MAP4_RESULTS.iterate(); iterator.hasNext();) { Tuple tuple = iterator.next(); Object[] array = tuple.toArray(); if (comments == null || !(commentId == array[0] || commentId != null && commentId.equals(array[0]))) { comments = new LinkedHashMap<Map<Integer, String>, String>(); expected.add(comments); } commentId = array[0]; @SuppressWarnings("unchecked") Pair<Pair<Integer, String>, String> pair = (Pair<Pair<Integer, String>, String>) array[1]; Pair<Integer, String> first = pair.getFirst(); Map<Integer, String> posts = Collections.singletonMap(first.getFirst(), first.getSecond()); comments.put(posts, pair.getSecond()); } assertEquals(expected.toString(), actual.toString()); }
@Test public void map3() { Map<Integer, Map<Integer, Map<Integer, String>>> actual = MAP3_RESULTS.transform( groupBy(postId).as(map(postId, map(commentId, commentText)))); Map<Integer, Map<Integer, Map<Integer, String>>> expected = new LinkedHashMap<Integer, Map<Integer, Map<Integer, String>>>(); for (Iterator<Tuple> iterator = MAP3_RESULTS.iterate(); iterator.hasNext();) { Tuple tuple = iterator.next(); Object[] array = tuple.toArray(); Map<Integer, Map<Integer, String>> posts = expected.get(array[0]); if (posts == null) { posts = new LinkedHashMap<Integer, Map<Integer,String>>(); expected.put((Integer) array[0], posts); } @SuppressWarnings("unchecked") Pair<Integer, Pair<Integer, String>> pair = (Pair<Integer, Pair<Integer, String>>) array[1]; Integer first = pair.getFirst(); Map<Integer, String> comments = posts.get(first); if (comments == null) { comments = new LinkedHashMap<Integer, String>(); posts.put(first, comments); } Pair<Integer, String> second = pair.getSecond(); comments.put(second.getFirst(), second.getSecond()); } assertEquals(expected.toString(), actual.toString()); }
@Test public void map2() { CloseableIterator<Map<Integer, String>> resultsIt = MAP2_RESULTS.transform( groupBy(postId).iterate(map(commentId, commentText))); List<Map<Integer, String>> results = IteratorAdapter.asList(resultsIt); assertEquals(4, results.size()); Map<Integer, String> comments = results.get(1); assertEquals(3, comments.size()); assertEquals("comment 2", comments.get(2)); }
@Test public void map1() { List<Group> results = MAP_RESULTS.transform( groupBy(postId).list(postName, map(commentId, commentText))); assertEquals(4, results.size()); Group group = results.get(1); Map<Integer, String> comments = group.getMap(commentId, commentText); assertEquals(3, comments.size()); assertEquals("comment 2", comments.get(2)); }
@Test public void map22() { CloseableIterator<Map<Integer, String>> results = MAP2_RESULTS.transform( groupBy(postId).iterate(map(commentId, commentText))); List<Map<Integer, String>> actual = IteratorAdapter.asList(results); Object commentId = null; Map<Integer, String> comments = null; List<Map<Integer, String>> expected = new LinkedList<Map<Integer, String>>(); for (Iterator<Tuple> iterator = MAP2_RESULTS.iterate(); iterator.hasNext();) { Tuple tuple = iterator.next(); Object[] array = tuple.toArray(); if (comments == null || !(commentId == array[0] || commentId != null && commentId.equals(array[0]))) { comments = new LinkedHashMap<Integer,String>(); expected.add(comments); } commentId = array[0]; @SuppressWarnings("unchecked") Pair<Integer, String> pair = (Pair<Integer, String>) array[1]; comments.put(pair.getFirst(), pair.getSecond()); } assertEquals(expected.toString(), actual.toString()); }
@Test public void map3() { List<Map<Integer, Map<Integer, String>>> actual = MAP3_RESULTS.transform( groupBy(postId).list(map(postId, map(commentId, commentText)))); Object postId = null; Map<Integer, Map<Integer, String>> posts = null; List<Map<Integer, Map<Integer, String>>> expected = new LinkedList<Map<Integer, Map<Integer, String>>>(); for (Iterator<Tuple> iterator = MAP3_RESULTS.iterate(); iterator.hasNext();) { Tuple tuple = iterator.next(); Object[] array = tuple.toArray(); if (posts == null || !(postId == array[0] || postId != null && postId.equals(array[0]))) { posts = new LinkedHashMap<Integer, Map<Integer,String>>(); expected.add(posts); } postId = array[0]; @SuppressWarnings("unchecked") Pair<Integer, Pair<Integer, String>> pair = (Pair<Integer, Pair<Integer, String>>) array[1]; Integer first = pair.getFirst(); Map<Integer, String> comments = posts.get(first); if (comments == null) { comments = new LinkedHashMap<Integer, String>(); posts.put(first, comments); } Pair<Integer, String> second = pair.getSecond(); comments.put(second.getFirst(), second.getSecond()); } assertEquals(expected.toString(), actual.toString()); }
@Test public void map4() { CloseableIterator<Map<Map<Integer, String>, String>> results = MAP4_RESULTS.transform( groupBy(postId).iterate(map(map(postId, commentText), postName))); List<Map<Map<Integer, String>, String>> actual = IteratorAdapter.asList(results); Object commentId = null; Map<Map<Integer, String>, String> comments = null; List<Map<Map<Integer, String>, String>> expected = new LinkedList<Map<Map<Integer, String>, String>>(); for (Iterator<Tuple> iterator = MAP4_RESULTS.iterate(); iterator.hasNext();) { Tuple tuple = iterator.next(); Object[] array = tuple.toArray(); if (comments == null || !(commentId == array[0] || commentId != null && commentId.equals(array[0]))) { comments = new LinkedHashMap<Map<Integer, String>, String>(); expected.add(comments); } commentId = array[0]; @SuppressWarnings("unchecked") Pair<Pair<Integer, String>, String> pair = (Pair<Pair<Integer, String>, String>) array[1]; Pair<Integer, String> first = pair.getFirst(); Map<Integer, String> posts = Collections.singletonMap(first.getFirst(), first.getSecond()); comments.put(posts, pair.getSecond()); } assertEquals(expected.toString(), actual.toString()); }
@Test public void map1() { CloseableIterator<Group> resultsIt = MAP_RESULTS.transform( groupBy(postId).iterate(postName, map(commentId, commentText))); List<Group> results = IteratorAdapter.asList(resultsIt); assertEquals(4, results.size()); Group group = results.get(1); Map<Integer, String> comments = group.getMap(commentId, commentText); assertEquals(3, comments.size()); assertEquals("comment 2", comments.get(2)); }
@Test public void map3() { CloseableIterator<Map<Integer, Map<Integer, String>>> results = MAP3_RESULTS.transform( groupBy(postId).iterate(map(postId, map(commentId, commentText)))); List<Map<Integer, Map<Integer, String>>> actual = IteratorAdapter.asList(results); Object postId = null; Map<Integer, Map<Integer, String>> posts = null; List<Map<Integer, Map<Integer, String>>> expected = new LinkedList<Map<Integer, Map<Integer, String>>>(); for (Iterator<Tuple> iterator = MAP3_RESULTS.iterate(); iterator.hasNext();) { Tuple tuple = iterator.next(); Object[] array = tuple.toArray(); if (posts == null || !(postId == array[0] || postId != null && postId.equals(array[0]))) { posts = new LinkedHashMap<Integer, Map<Integer,String>>(); expected.add(posts); } postId = array[0]; @SuppressWarnings("unchecked") Pair<Integer, Pair<Integer, String>> pair = (Pair<Integer, Pair<Integer, String>>) array[1]; Integer first = pair.getFirst(); Map<Integer, String> comments = posts.get(first); if (comments == null) { comments = new LinkedHashMap<Integer, String>(); posts.put(first, comments); } Pair<Integer, String> second = pair.getSecond(); comments.put(second.getFirst(), second.getSecond()); } assertEquals(expected.toString(), actual.toString()); }
@Test public void signature() { StringPath str = Expressions.stringPath("str"); NumberPath<BigDecimal> bigd = Expressions.numberPath(BigDecimal.class, "bigd"); ResultTransformer<Map<String, SortedMap<BigDecimal, SortedMap<BigDecimal, Map<String, String>>>>> resultTransformer = GroupBy.groupBy(str).as( GroupBy.sortedMap(bigd, GroupBy.sortedMap(bigd, GroupBy.map(str, str), Ordering.natural().nullsLast()), Ordering.natural().nullsFirst())); assertNotNull(resultTransformer); }
/** * Create a new aggregating map expression using a backing LinkedHashMap * * @param key key for the map entries * @param value value for the map entries * @return wrapper expression */ public static <K, V, U> AbstractGroupExpression<Pair<K, V>, Map<K, U>> map(Expression<K> key, GroupExpression<V, U> value) { return map(new GOne<K>(key), value); }
/** * Create a new aggregating map expression using a backing LinkedHashMap * * @param key key for the map entries * @param value value for the map entries * @return wrapper expression */ public static <K, V, T> AbstractGroupExpression<Pair<K, V>, Map<T, V>> map(GroupExpression<K, T> key, Expression<V> value) { return map(key, new GOne<V>(value)); }
@Test public void transform_groupBy() { QEmployee employee = new QEmployee("employee"); QEmployee employee2 = new QEmployee("employee2"); Map<Integer, Map<Integer, Employee>> results = query().from(employee, employee2) .transform(GroupBy.groupBy(employee.id).as(GroupBy.map(employee2.id, employee2))); int count = (int) query().from(employee).fetchCount(); assertEquals(count, results.size()); for (Map.Entry<Integer, Map<Integer, Employee>> entry : results.entrySet()) { Map<Integer, Employee> employees = entry.getValue(); assertEquals(count, employees.size()); } }
@Test public void groupBy_superior() { SQLQuery<?> qry = query() .from(employee) .innerJoin(employee._superiorIdKey, employee2); QTuple subordinates = Projections.tuple(employee2.id, employee2.firstname, employee2.lastname); Map<Integer, Group> results = qry.transform( GroupBy.groupBy(employee.id).as(employee.firstname, employee.lastname, GroupBy.map(employee2.id, subordinates))); assertEquals(2, results.size()); // Mike Smith Group group = results.get(1); assertEquals("Mike", group.getOne(employee.firstname)); assertEquals("Smith", group.getOne(employee.lastname)); Map<Integer, Tuple> emps = group.getMap(employee2.id, subordinates); assertEquals(4, emps.size()); assertEquals("Steve", emps.get(12).get(employee2.firstname)); // Mary Smith group = results.get(2); assertEquals("Mary", group.getOne(employee.firstname)); assertEquals("Smith", group.getOne(employee.lastname)); emps = group.getMap(employee2.id, subordinates); assertEquals(4, emps.size()); assertEquals("Mason", emps.get(21).get(employee2.lastname)); }