public Double getDouble(String column) { return (Double) get(column, Schema.DOUBLE); }
public NodeInterfacePair getInterface(String column) { return (NodeInterfacePair) get(column, Schema.INTERFACE); }
public String getString(String column) { return (String) get(column, Schema.STRING); }
public FileLines getFileLines(String column) { return (FileLines) get(column, Schema.FILE_LINES); }
public Ip getIp(String column) { return (Ip) get(column, Schema.IP); }
public @Nullable AclTrace getAclTrace(String column) { return (AclTrace) get(column, Schema.ACL_TRACE); }
public @Nullable Long getLong(String column) { return (Long) get(column, Schema.LONG); }
public @Nullable FlowTrace getFlowTrace(String column) { return (FlowTrace) get(column, Schema.FLOW_TRACE); }
public boolean matches(@Nonnull Row row) { return row.get(_column).toString().toLowerCase().contains(_filterText.toLowerCase()); }
/** * Mirrors the values of {@code columns} in {@code otherRow} * * @throws NoSuchElementException if one of the columns is not present in {@code otherRow}. */ public RowBuilder putAll(Row otherRow, Collection<String> columns) { columns.forEach(col -> put(col, otherRow.get(col))); return this; } }
/** Get the value of specified column safely cast to type specifed via {@code typeReference}. */ public <T> T get(String columnName, TypeReference<T> typeReference) { ObjectMapper mapper = BatfishObjectMapper.mapper(); JsonNode node = get(columnName); try { return mapper.readValue(mapper.treeAsTokens(node), typeReference); } catch (IOException e) { throw new ClassCastException( String.format( "Cannot cast row element in column '%s' given the provided TypeReference: %s", columnName, Throwables.getStackTraceAsString(e))); } }
/** * Returns the list of values in all columns declared as key in the metadata. * * @param metadata Provides information on which columns are key and their {@link Schema} * @return The list */ public List<Object> getKey(List<ColumnMetadata> metadata) { List<Object> keyList = new LinkedList<>(); for (ColumnMetadata column : metadata) { if (column.getIsKey()) { keyList.add(get(column.getName(), column.getSchema())); } } return keyList; }
@SuppressWarnings({"rawtypes", "unchecked"}) @VisibleForTesting @Nonnull Comparator<Row> columnComparator(ColumnMetadata columnMetadata) { Schema schema = columnMetadata.getSchema(); Comparator schemaComparator = schemaComparator(schema); Comparator comparator = comparing((Row r) -> r.get(columnMetadata.getName(), schema), nullsFirst(schemaComparator)); return comparator; }
@VisibleForTesting static void fillProperty( String columnName, Object propertyValue, RowBuilder row, PropertyDescriptor<?> propertyDescriptor) { row.put(columnName, propertyValue); // if this barfs, the value cannot be converted to expected Schema row.build().get(columnName, propertyDescriptor.getSchema()); }
static boolean matchesQuestionFilters( Row row, Set<String> nodes, Set<String> remoteNodes, BgpSessionQuestion question) { if (!matchesNodesAndType(row, nodes, remoteNodes, question)) { return false; } // Check session status String statusName = (String) row.get(COL_ESTABLISHED_STATUS, Schema.STRING); SessionStatus status = statusName == null ? null : SessionStatus.valueOf(statusName); if (!question.matchesStatus(status)) { return false; } return true; } }
private Object getActualValue(String property, Schema schema) { return getRows(property).iterator().next().get(property, schema); }
@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)); }
@Test public void putNullValue() { TypedRowBuilder builder = Row.builder(ImmutableMap.of("col", new ColumnMetadata("col", Schema.INTEGER, "desc"))); // we should be able to put null Row row = builder.put("col", null).build(); assertThat(row.get("col", Schema.INTEGER), Matchers.is(nullValue())); }
@Test public void putBaseType() { TypedRowBuilder builder = Row.builder(ImmutableMap.of("col", new ColumnMetadata("col", Schema.INTEGER, "desc"))); // we should be able to put an Integer but not a string Row row = builder.put("col", 2).build(); assertThat(row.get("col", Schema.INTEGER), Matchers.equalTo(2)); _thrown.expect(IllegalArgumentException.class); _thrown.expectMessage("Cannot convert"); builder.put("col", "string"); }
@Test public void testGetTypeReferenceInvalid() { String column = "col"; Row row = Row.builder().put(column, "text").build(); _thrown.expect(ClassCastException.class); row.get(column, new TypeReference<Integer>() {}); }