com.jakewharton.fliptables.FlipTableConverters Maven / Gradle / Ivy
package com.jakewharton.fliptables;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** Helper methods for creating {@link FlipTable tables} from various different data sets. */
public final class FlipTableConverters {
private static final Pattern METHOD = Pattern.compile("^(?:get|is|has)([A-Z][a-zA-Z0-9]*)+$");
private static final Comparator METHOD_COMPARATOR = new Comparator() {
@Override public int compare(Method o1, Method o2) {
return o1.getName().compareTo(o2.getName());
}
};
/** Create a table from an array of objects using {@link String#valueOf}. */
public static String fromObjects(String[] headers, Object[][] data) {
if (headers == null) throw new NullPointerException("headers == null");
if (data == null) throw new NullPointerException("data == null");
List stringData = new ArrayList<>();
for (Object[] row : data) {
String[] stringRow = new String[row.length];
for (int column = 0; column < row.length; column++) {
stringRow[column] = String.valueOf(row[column]);
}
stringData.add(stringRow);
}
String[][] dataArray = stringData.toArray(new String[stringData.size()][]);
return FlipTable.of(headers, dataArray);
}
/**
* Create a table from a group of objects. Public accessor methods on the class type with no
* arguments will be used as the columns.
*/
public static String fromIterable(Iterable rows, Class rowType) {
if (rows == null) throw new NullPointerException("rows == null");
if (rowType == null) throw new NullPointerException("rowType == null");
Method[] declaredMethods = rowType.getDeclaredMethods();
Arrays.sort(declaredMethods, METHOD_COMPARATOR);
List methods = new ArrayList<>();
List headers = new ArrayList<>();
for (Method declaratedMethod : declaredMethods) {
if (declaratedMethod.getParameterTypes().length > 0) continue;
if (declaratedMethod.getReturnType() == void.class) continue;
Matcher matcher = METHOD.matcher(declaratedMethod.getName());
if (!matcher.matches()) continue;
declaratedMethod.setAccessible(true);
methods.add(declaratedMethod);
headers.add(matcher.group(1));
}
int columnCount = methods.size();
List data = new ArrayList<>();
for (T row : rows) {
String[] rowData = new String[columnCount];
for (int column = 0; column < columnCount; column++) {
try {
rowData[column] = String.valueOf(methods.get(column).invoke(row));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
data.add(rowData);
}
String[] headerArray = headers.toArray(new String[headers.size()]);
String[][] dataArray = data.toArray(new String[data.size()][]);
return FlipTable.of(headerArray, dataArray);
}
/** Create a table from a {@link ResultSet}. */
static String fromResultSet(ResultSet resultSet) throws SQLException {
if (resultSet == null) throw new NullPointerException("resultSet == null");
if (!resultSet.isBeforeFirst()) throw new IllegalStateException("Result set not at first.");
List headers = new ArrayList<>();
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int columnCount = resultSetMetaData.getColumnCount();
for (int column = 0; column < columnCount; column++) {
headers.add(resultSetMetaData.getColumnName(column + 1));
}
List data = new ArrayList<>();
while (resultSet.next()) {
String[] rowData = new String[columnCount];
for (int column = 0; column < columnCount; column++) {
rowData[column] = resultSet.getString(column + 1);
}
data.add(rowData);
}
String[] headerArray = headers.toArray(new String[headers.size()]);
String[][] dataArray = data.toArray(new String[data.size()][]);
return FlipTable.of(headerArray, dataArray);
}
private FlipTableConverters() {
throw new AssertionError("No instances.");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy