GroupingExpression extends EvaluationExpression { @Override public IJsonNode evaluate(final IJsonNode node) { this.result.clear(); if (((IArrayNode<?>) node).size() == 0) return this.result; this.fillGroups((IArrayNode<?>) node); final ExpressionCache<EvaluationExpression> resultExpressions = this.resultExpressions; int index = 0; for (FastMap.Entry<IJsonNode, CachingArrayNode<IJsonNode>> e = this.groups.head(), end = this.groups.tail(); (e = e.getNext()) != end;) { final CachingArrayNode<IJsonNode> group = e.getValue(); if (!group.isEmpty()) this.result.add(resultExpressions.get(index++).evaluate(group)); } this.emptyGroups(); return this.result; } GroupingExpression(final EvaluationExpression groupingExpression, final EvaluationExpression resultExpression);   GroupingExpression(); @Override void appendAsString(final Appendable appendable); @Override boolean equals(final Object obj); @Override IJsonNode evaluate(final IJsonNode node); @Override int hashCode(); @Override ChildIterator iterator();  }
@Test public void shouldReuseTarget() { final ArrayNode<IJsonNode> input = new ArrayNode<IJsonNode>(); input.add(createObjectNode("key", 1, "value", 11)); input.add(createObjectNode("key", 2, "value", 24)); input.add(createObjectNode("key", 3, "value", 33)); input.add(createObjectNode("key", 2, "value", 25)); input.add(createObjectNode("key", 1, "value", 12)); final GroupingExpression aggExpression = new GroupingExpression(new ObjectAccess("key"), createFunctionCall(CoreFunctions.SUM, new ArrayProjection(new ObjectAccess("value")))); final IJsonNode result1 = aggExpression.evaluate(input); final IArrayNode<?> expected = JsonUtil.createArrayNode(23, 49, 33); Assert.assertEquals(expected, result1); final IJsonNode result2 = aggExpression.evaluate(input); Assert.assertSame(result1, result2); Assert.assertEquals(expected, result2); }
@Test public void testAggregation() { final ArrayNode<IJsonNode> input = new ArrayNode<IJsonNode>(); input.add(createObjectNode("key", 1, "value", 11)); input.add(createObjectNode("key", 2, "value", 24)); input.add(createObjectNode("key", 3, "value", 33)); input.add(createObjectNode("key", 2, "value", 25)); input.add(createObjectNode("key", 1, "value", 12)); final GroupingExpression aggExpression = new GroupingExpression(new ObjectAccess("key"), createFunctionCall(CoreFunctions.SUM, new ArrayProjection(new ObjectAccess("value")))); final IJsonNode result = aggExpression.evaluate(input); final IArrayNode<?> expected = JsonUtil.createArrayNode(23, 49, 33); Assert.assertEquals(expected, result); }