/** * Returns a new {@link Row} with the given entries. * * <p>{@code objects} should be an even number of parameters, where the 0th and every even * parameter is a {@link String} representing the name of a column. */ public static Row of(Object... objects) { return builder().rowOf(objects); }
public Flow getFlow(String column) { return (Flow) get(column, Schema.FLOW); }
/** * 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); }
@Test public void testGetTypeReference() { String column = "col"; List<String> stringList = ImmutableList.of("a", "b"); Row row = Row.builder().put(column, stringList).build(); List<String> extracted = row.get(column, new TypeReference<List<String>>() {}); assertThat(stringList, equalTo(extracted)); }
Row applyRowIssuesConfiguration( Row oldRow, Set<String> issueColumns, Map<String, MajorIssueConfig> issueConfigs) { Row.RowBuilder builder = Row.builder(); oldRow .getColumnNames() .forEach( column -> { if (!issueColumns.contains(column)) { builder.put(column, oldRow.get(column)); return; Issue oldIssue = oldRow.getIssue(column); MajorIssueConfig config = issueConfigs.get(oldIssue.getType().getMajor()); Issue newIssue;
private static Row toRow(BidirectionalTrace trace) { return Row.of( COL_FORWARD_FLOW, trace.getForwardFlow(), COL_FORWARD_TRACE, trace.getForwardTrace(), COL_REVERSE_FLOW, trace.getReverseFlow(), COL_REVERSE_TRACE, trace.getReverseTrace()); } }
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)); } }
while (baseRows.hasNext()) { Row baseRow = baseRows.next(); Object baseKey = baseRow.getKey(inputMetadata.getColumnMetadata()); baseKeys.add(baseKey); List<Row> deltaRows = deltaMap.get(baseKey); if (deltaRows == null) { // no matching keys in delta table if (includeOneTableKeys) { RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(baseRow, keyColumns); diffRowValues(diffRowBuilder, baseRow, null, inputMetadata); diffTable.addRow(diffRowBuilder.build()); for (Row deltaRow : deltaRows) { if (!baseRow.getValue(valueColumns).equals(deltaRow.getValue(valueColumns))) { RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(baseRow, keyColumns); diffRowValues(diffRowBuilder, baseRow, deltaRow, inputMetadata); diffTable.addRow(diffRowBuilder.build()); while (deltaRows.hasNext()) { Row deltaRow = deltaRows.next(); Object deltaKey = deltaRow.getKey(inputMetadata.getColumnMetadata()); if (baseKeys.contains(deltaKey)) { continue; RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(deltaRow, keyColumns); diffRowValues(diffRowBuilder, null, deltaRow, inputMetadata); diffTable.addRow(diffRowBuilder.build());
@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; } }
/** Mirrors the values of all columns in {@code otherRow} */ public RowBuilder putAll(Row otherRow) { return putAll(otherRow, otherRow.getColumnNames()); }
@VisibleForTesting static @Nonnull Map<String, MajorIssueConfig> computeMajorIssueConfigs(TableAnswerElement table) { Map<String, ImmutableList.Builder<MinorIssueConfig>> majorIssueConfigs = new HashMap<>(); // For every issue column of every row, extract the issue and use it to update the map table.getMetadata().getColumnMetadata().stream() .filter(c -> c.getSchema().equals(Schema.ISSUE)) .map(ColumnMetadata::getName) .flatMap( column -> table.getRowsList().stream() .filter(row -> row.hasNonNull(column)) .map(row -> row.getIssue(column))) .forEach( issue -> majorIssueConfigs .computeIfAbsent(issue.getType().getMajor(), m -> ImmutableList.builder()) .add( new MinorIssueConfig( issue.getType().getMinor(), issue.getSeverity(), issue.getUrl()))); return CommonUtil.toImmutableMap( majorIssueConfigs, Entry::getKey, // major issue type e -> new MajorIssueConfig(e.getKey(), e.getValue().build())); }
Function<Row, Integer> rowToInteger; if (schema.equals(Schema.INTEGER)) { rowToInteger = r -> r.getInteger(column); } else if (schema.equals(Schema.ISSUE)) { rowToInteger = r -> r.getIssue(column).getSeverity(); } else {
/** This used to be the old signature, changed now to {@link #getKey(List)} */ @Deprecated public List<Object> getKey(TableMetadata metadata) { return getKey(metadata.getColumnMetadata()); }
/** This used to be the old signature, changed now to {@link #getValue(List)} */ @Deprecated public List<Object> getValue(TableMetadata metadata) { return getValue(metadata.getColumnMetadata()); }
@VisibleForTesting static Set<String> computeEmptyColumns(TableAnswerElement table) { return table.getMetadata().toColumnMap().keySet().stream() .filter(column -> table.getRowsList().stream().allMatch(row -> !row.hasNonNull(column))) .collect(ImmutableSet.toImmutableSet()); }
@VisibleForTesting static TableAnswerElement resolveLocation(SpecifiersQuestion question, SpecifierContext context) { List<ColumnMetadata> columns = ImmutableList.of(new ColumnMetadata(COL_LOCATION, Schema.STRING, "Location", false, false)); TableAnswerElement table = new TableAnswerElement(new TableMetadata(columns)); Map<String, ColumnMetadata> columnMap = table.getMetadata().toColumnMap(); Set<Location> locations = question.getLocationSpecifier().resolve(context); for (Location location : locations) { table.addRow(Row.of(columnMap, COL_LOCATION, location.toString())); } return table; }
@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); }