ImmutableTableData implements TableData { @Override public Iterable<Optional<String>> getValuesOfField(final String fieldName) { final FieldType fieldType = new NamedFieldTypeReference(fieldName).resolve(tableType); if (fieldType == null) { throw new NoSuchDaleqFieldException( "Table " + tableType.getName() + " does not contain the Field " + fieldName); } return Iterables.transform(rows, new Function<RowData, Optional<String>>() { @Override public Optional<String> apply(@Nullable final RowData row) { if (row == null) { return null; } return row.getFieldBy(fieldName).getValue(); } }); } ImmutableTableData(final TableType tableType, final List<RowData> rows); @Override TableType getTableType(); @Override String getName(); @Override List<RowData> getRows(); @Override Iterable<Optional<String>> getValuesOfField(final String fieldName); @Override boolean equals(final Object obj); @Override int hashCode(); @Override String toString();  }
@Test public void getValuesOfField_ifFieldExists_should_returnThoseFields() { final TableData table = Daleq.aTable(TheTable.class).withRowsBetween(0, 4).build(context()); final List<Optional<String>> expected = Lists.transform( Lists.newArrayList("0", "1", "2", "3", "4"), new Function<String, Optional<String>>() { @Override public Optional<String> apply(@Nullable final String input) { if (input == null) { return Optional.absent(); } return Optional.of(input); } } ); assertThat(Lists.newArrayList(table.getValuesOfField("ID")), Matchers.is(expected)); }
@Test(expected = NoSuchDaleqFieldException.class) public void getValuesOfField_ifFieldDoesNotExist_should_fail() { final TableData table = Daleq.aTable(TheTable.class).withRowsBetween(0, 5).build(ContextFactory.context()); final Iterable<Optional<String>> result = table.getValuesOfField("DOES_NOT_EXIST"); assertThat(result, Matchers.nullValue()); }