/** Mirrors the values of all columns in {@code otherRow} */ public RowBuilder putAll(Row otherRow) { return putAll(otherRow, otherRow.getColumnNames()); }
/** * Gets the (raw) Json representation of the object stored in the row * * @param columnName The column to fetch * @return The {@link JsonNode} object that represents the stored object * @throws NoSuchElementException if this column does not exist */ public JsonNode get(String columnName) { if (!_data.has(columnName)) { throw new NoSuchElementException(missingColumnErrorMessage(columnName, getColumnNames())); } return _data.get(columnName); }
/** * Adds a new row to data rows * * @param row The row to add */ public @Nonnull TableAnswerElement addRow(Row row) { checkArgument( row.getColumnNames().equals(_tableMetadata.toColumnMap().keySet()), "Row columns %s do not match metadata columns metadata %s", row.getColumnNames(), _tableMetadata.toColumnMap().keySet()); _rows.add(row); _rowsList.add(row); return this; }
/** * Gets the value of specified column * * @param columnName The column to fetch * @return The result * @throws NoSuchElementException if this column is not present * @throws ClassCastException if the recovered data cannot be cast to the expected object */ public Object get(String columnName, Schema columnSchema) { if (!_data.has(columnName)) { throw new NoSuchElementException(missingColumnErrorMessage(columnName, getColumnNames())); } if (_data.get(columnName).isNull()) { return null; } return SchemaUtils.convertType(_data.get(columnName), columnSchema); }
/** * Adds a new row to excluded data rows * * @param row The row to add */ public @Nonnull TableAnswerElement addExcludedRow(Row row, String exclusionName) { checkArgument( row.getColumnNames().equals(_tableMetadata.toColumnMap().keySet()), "Row columns %s do not match metadata columns metadata %s", row.getColumnNames(), row); for (ExcludedRows exRows : _excludedRows) { if (exRows.getExclusionName().equals(exclusionName)) { exRows.addRow(row); return this; } } // no matching exclusionName found; create a new one ExcludedRows rows = new ExcludedRows(exclusionName); rows.addRow(row); _excludedRows.add(rows); return this; }
private static Row toSearchFiltersRow( @Nullable AclLineMatchExpr description, Row row, boolean generateExplanations) { RowBuilder rowBuilder = Row.builder().putAll(row, row.getColumnNames()); if (generateExplanations) { /* * Sending the explanation to the client as a JSON blob. TODO: do something better. */ String jsonDescription; try { jsonDescription = BatfishObjectMapper.writeString(description); } catch (JsonProcessingException e) { e.printStackTrace(); jsonDescription = e.getMessage(); } rowBuilder.put(COL_HEADER_SPACE, jsonDescription); } return rowBuilder.build(); }
@Test public void fillInEmptyColumnsOnBuild() { Row row = Row.builder(ImmutableMap.of("col", new ColumnMetadata("col", Schema.INTEGER, "desc"))) .build(); assertThat(row.getColumnNames(), contains("col")); assertThat(row.hasNonNull("col"), equalTo(false)); } }
@Override protected boolean matchesSafely(Row item, Description mismatchDescription) { Collection<String> columnNames = item.getColumnNames(); Optional<String> columnNameOption = columnNames.stream().filter(_keyMatcher::matches).findFirst(); if (!columnNameOption.isPresent()) { mismatchDescription.appendText("No column of row matched: "); hasItem(_keyMatcher).describeMismatch(columnNames, mismatchDescription); return false; } String columnName = columnNameOption.get(); Object value; try { value = item.get(columnName, _schema); } catch (Exception e) { mismatchDescription.appendText( String.format( "Value for column '%s' could not be converted to a '%s': %s", columnName, _schema, Throwables.getStackTraceAsString(e))); return false; } if (!_valueMatcher.matches(value)) { mismatchDescription.appendText( String.format("Value for column '%s' did not match: ", columnName)); _valueMatcher.describeMismatch(value, mismatchDescription); return false; } return true; } }
Row.RowBuilder builder = Row.builder(); oldRow .getColumnNames() .forEach( column -> {
@Test public void testAddEmptyRow() { TableAnswerElement table = new TableAnswerElement(oneKeyTableMetadata); Row row = Row.builder().build(); _thrown.expect(IllegalArgumentException.class); _thrown.expectMessage( String.format( "Row columns %s do not match metadata columns metadata %s", row.getColumnNames(), oneKeyTableMetadata.toColumnMap().keySet())); table.addRow(row); }
@Test public void testAddRowMissingColumn() { TableAnswerElement table = new TableAnswerElement(twoKeyTableMetadata); Row row = Row.builder().put("key1", null).build(); _thrown.expect(IllegalArgumentException.class); _thrown.expectMessage( String.format( "Row columns %s do not match metadata columns metadata %s", row.getColumnNames(), twoKeyTableMetadata.toColumnMap().keySet())); table.addRow(row); }
@Test public void testAddRowExtraColumn() { TableAnswerElement table = new TableAnswerElement(twoKeyTableMetadata); Row row = Row.builder().put("key1", null).put("key2", null).put("key3", null).build(); _thrown.expect(IllegalArgumentException.class); _thrown.expectMessage( String.format( "Row columns %s do not match metadata columns metadata %s", row.getColumnNames(), twoKeyTableMetadata.toColumnMap().keySet())); table.addRow(row); } }