NaiveSchemaFactory implements SchemaFactory { @Override public Schema create(final Iterable<EvaluationExpression> keyExpressions) { final List<ObjectAccess> objectAccesses = new ArrayList<ObjectAccess>(); final List<ArrayAccess> arrayAccesses = new ArrayList<ArrayAccess>(); final List<EvaluationExpression> mappings = new ArrayList<EvaluationExpression>(); for (final EvaluationExpression evaluationExpression : keyExpressions) { mappings.add(evaluationExpression); if (evaluationExpression instanceof ObjectAccess) objectAccesses.add((ObjectAccess) evaluationExpression); if (evaluationExpression instanceof ArrayAccess) arrayAccesses.add((ArrayAccess) evaluationExpression); } if (mappings.isEmpty()) return new DirectSchema(); if (objectAccesses.size() == mappings.size()) return new ObjectSchema(objectAccesses); else if (arrayAccesses.size() == mappings.size()) { final int startIndex = arrayAccesses.get(0).getStartIndex(); final int endIndex = arrayAccesses.get(arrayAccesses.size() - 1).getEndIndex(); if (startIndex == 0) return new HeadArraySchema(endIndex + 1); return new TailArraySchema(endIndex - startIndex + 1); } return new GeneralSchema(mappings); }  @Override Schema create(final Iterable<EvaluationExpression> keyExpressions); @Override void toString(StringBuilder builder);  }
@Test public void shouldCreateDirectSchema() { final List<EvaluationExpression> expressions = new ArrayList<EvaluationExpression>(); final Schema schema = this.factory.create(expressions); Assert.assertTrue(schema instanceof DirectSchema); }
@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); }
@Test public void shouldCreateHeadArraySchema() { final List<EvaluationExpression> accesses = new ArrayList<EvaluationExpression>(); accesses.add(new ArrayAccess(0)); accesses.add(new ArrayAccess(1)); accesses.add(new ArrayAccess(2)); accesses.add(new ArrayAccess(3)); final Schema schema = this.factory.create(accesses); Assert.assertTrue(schema instanceof HeadArraySchema); Assert.assertEquals(4, ((HeadArraySchema) schema).getHeadSize()); }
@Test public void shouldCreateObjectSchema() { final List<EvaluationExpression> accesses = new ArrayList<EvaluationExpression>(); accesses.add(new ObjectAccess("firstname")); accesses.add(new ObjectAccess("lastname")); accesses.add(new ObjectAccess("age")); final Schema schema = this.factory.create(accesses); Assert.assertTrue(schema instanceof ObjectSchema); }
@Test public void shouldCreateTailArraySchema() { final List<EvaluationExpression> accesses = new ArrayList<EvaluationExpression>(); accesses.add(new ArrayAccess(7)); accesses.add(new ArrayAccess(8)); accesses.add(new ArrayAccess(9)); accesses.add(new ArrayAccess(10)); final Schema schema = this.factory.create(accesses); Assert.assertTrue(schema instanceof TailArraySchema); Assert.assertEquals(4, ((TailArraySchema) schema).getTailSize()); }