Таблица
public class CustomerTable {
public enum Column {
FIRST_NAME, LAST_NAME, ADDRESS_LINE1, CITY, STATE_PROVINCE_CD, POSTAL_CODE;
}
private Table<Integer, Column, String> table = HashBasedTable.create();
@Override
public String toString() {
return table.toString();
}
public void createRow(String[] values) {
if (Column.values().length != values.length) {
throw new IllegalArgumentException();
}
Integer rowNum = table.rowKeySet().size() + 1;
for(int i=0; i < values.length; i ++) {
table.put(rowNum, Column.values()[i], values[i]);
}
}
public Table<Integer, Column, String> query(Predicate<Map<Column, String>> query) {
return query(query, allOf(Column.class));
}
public Table<Integer, Column, String> query(Predicate<Map<Column, String>> query, EnumSet<Column> columns) {
Map<Integer, Map<Column, String>> filtered = Maps.filterValues(table.rowMap(), query);
return createResultTable(filtered, columns);
}
private Table<Integer, Column, String> createResultTable(Map<Integer, Map<Column, String>> resultMap, final EnumSet<Column> columns) {
int i = 0;
Table<Integer, Column, String> result = HashBasedTable.create();
for (Map<Column, String> row : resultMap.values()) {
i++;
for (Column column : row.keySet()){
if (columns.contains(column)) {
result.put(i, column, row.get(column));
}
}
}
return result;
}
предиката
class LikePredicate implements Predicate<Map<CustomerTable.Column, String>> {
private Column column;
private String value;
public LikePredicate(Column column, String value) {
this.column = column;
this.value = value;
}
@Override
public boolean apply(Map<Column, String> input) {
return input.get(column) != null && input.get(column).contains(value);
}
public static LikePredicate like(Column column, String value) {
return new LikePredicate(column, value);
}
}
Пример использования
public static void main(String[] args) {
CustomerTable customerTable = new CustomerTable();
customerTable.createRow(new String[]{"Ben", "Miller", "101 Candy Rd.", "Redmond", "WA", "98052"});
customerTable.createRow(new String[]{"Garret", "Vargas", "10203 Acorn Avenue", "Calgary", "AB", "T2P 2G8"});
//Create other rows or read rows from a file
Table<Integer, Column, String> result;
/*
SELECT FirstName, LastName, City
FROM Customers
WHERE FirstName LIKE '%ab%'
OR LastName LIKE '%an%'
*/
result = customerTable.query(or(like(Column.FIRST_NAME, "ab"), like(Column.LAST_NAME, "an")),
EnumSet.of(Column.FIRST_NAME, Column.LAST_NAME, Column.CITY));
System.out.println(result);
}
Наконец-то я реализовал основную структуру TABLE в соответствии с ответом Евгения (http://stackoverflow.com/a/36494261/4419474) и FluentIterable в соответствии с ответом mfulton26 (http://stackoverflow.com/a/36500647/4419474) – Chebyr