@Test public void shouldCreateArrayWithListAsParam() { final List<EvaluationExpression> list = new ArrayList<EvaluationExpression>(); list.add(new ConstantExpression(IntNode.valueOf(0))); list.add(EvaluationExpression.VALUE); final IJsonNode result = new ArrayCreation(list).evaluate(IntNode.valueOf(42)); Assert.assertEquals(createArrayNode(IntNode.valueOf(0), IntNode.valueOf(42)), result); }
@Test public void shouldReuseTarget() { final ArrayCreation arrayCreation = new ArrayCreation(new ConstantExpression(IntNode.valueOf(42))); final IJsonNode result1 = arrayCreation.evaluate(IntNode.valueOf(42)); final IJsonNode result2 = arrayCreation.evaluate(IntNode.valueOf(42)); Assert.assertEquals(new ArrayNode<IJsonNode>(IntNode.valueOf(42)), result1); Assert.assertSame(result1, result2); Assert.assertEquals(new ArrayNode<IJsonNode>(IntNode.valueOf(42)), result2); }
@Override protected void map(final IJsonNode value, JsonCollector<IJsonNode> out) { final IJsonNode target = this.arrayPath.evaluate(value); if (!(target instanceof IArrayNode<?>)) throw new EvaluationException("Cannot split non-array"); final IArrayNode<?> array = (IArrayNode<?>) target; int index = 0; IntNode indexNode = IntNode.valueOf(0); IArrayNode<IJsonNode> contextNode = JsonUtil.asArray(NullNode.getInstance(), indexNode, array, value); for (IJsonNode element : array) { contextNode.set(0, element); indexNode.setValue(index); out.collect(this.splitProjection.evaluate(contextNode)); index++; } }
@Test public void shouldCreateGeneralSchema() { final List<EvaluationExpression> expressions = new ArrayList<EvaluationExpression>(); expressions.add(new ConstantExpression(IntNode.valueOf(42))); expressions.add( new ArithmeticExpression( new ArrayAccess(0), ArithmeticOperator.ADDITION, new ArrayAccess(1))); final Schema schema = this.factory.create(expressions); Assert.assertTrue(schema instanceof GeneralSchema); }
/** * Reuses a {@link IJsonNode} by simply copying the value from the source to the target node. The * value will not be copied if the source and target are no {@link IPrimitiveNode}s * * @param source the IJsonNode that should be used as the source * @param target the IJsonNode that should be used as the target * @return the reused IJsonNode */ public static IJsonNode reusePrimitive(final IJsonNode source, final IJsonNode target) { final Class<? extends IJsonNode> sourceClass = source.getClass(); if (sourceClass != target.getClass() || sourceClass.equals(BooleanNode.class) || source.isNull()) return source; if (!(source instanceof IPrimitiveNode)) return source; if (sourceClass.equals(IntNode.class)) ((IntNode) target).setValue(((IntNode) source).getIntValue()); else if (sourceClass.equals(DoubleNode.class)) ((DoubleNode) target).setValue(((DoubleNode) source).getDoubleValue()); else if (sourceClass.equals(LongNode.class)) ((LongNode) target).setValue(((LongNode) source).getLongValue()); else if (sourceClass.equals(DecimalNode.class)) ((DecimalNode) target).setValue(((DecimalNode) source).getDecimalValue()); else if (sourceClass.equals(BigIntegerNode.class)) ((BigIntegerNode) target).setValue(((BigIntegerNode) source).getBigIntegerValue()); return target; }
@Override protected ArrayCreation createDefaultInstance(final int index) { return new ArrayCreation(new ConstantExpression(IntNode.valueOf(index))); }