Package org.moeaframework.util.format
Class TabularData<T>
java.lang.Object
org.moeaframework.util.format.TabularData<T>
- Type Parameters:
T- the type of records (rows)
- All Implemented Interfaces:
Displayable
Formats and displays tabular data.
-
Constructor Summary
ConstructorsConstructorDescriptionTabularData(Iterable<T> dataSource) Creates a new tabular data object using the given data source. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a column to this data.voidaddFormatter(Formatter<?> formatter) Adds a formatter.voiddisplay(PrintStream out) Displays the contents of this object to the given output stream.voiddisplay(TableFormat tableFormat) Displays the data in the given format to the terminal.voiddisplay(TableFormat tableFormat, PrintStream out) Displays the data in the given format.voiddisplay(TableFormat tableFormat, PrintWriter writer) Displays the data in the given format.protected StringReads the value of the specified column and formats it as a string.protected StringformatValue(Object value) Formats the value using the default formatter, if available, or withObject.toString().static <K,V> TabularData<org.apache.commons.lang3.tuple.Pair<K, V>> Constructs a table of the given pairs.static <K,V> TabularData<org.apache.commons.lang3.tuple.Pair<K, V>> Constructs a table of the given pairs.voidRemoves any existing formatters.voidsave(TableFormat tableFormat, File file) Saves the data to a file in the requested format.voidsave(TableFormat tableFormat, OutputStream out) Saves the data to an output stream in the requested format.voidsave(TableFormat tableFormat, Writer writer) Saves the data to a writer in the requested format.protected voidtoARFF(PrintWriter out) Writes the data formatted as an ARFF file.protected voidtoCSV(PrintWriter out) Writes the data formatted as CSV.protected voidtoJson(PrintWriter out) Writes the data formatted as a Json object.protected voidtoLatex(PrintWriter out) Writes the data formatted as a Latex table.protected voidtoMarkdown(PrintWriter out) Writes the data formatted as a Markdown table.protected voidtoPlaintext(PrintWriter out) Writes the data in the default, plaintext format.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.moeaframework.util.format.Displayable
display
-
Constructor Details
-
TabularData
Creates a new tabular data object using the given data source.- Parameters:
dataSource- the source of data
-
-
Method Details
-
addFormatter
Adds a formatter. This will be used to format any values matching the formatter's type (seeFormatter.getType().- Parameters:
formatter- the default formatter
-
removeAllFormatters
public void removeAllFormatters()Removes any existing formatters. -
addColumn
Adds a column to this data. The columns will be displayed in the order they are added.- Parameters:
column- the column
-
format
Reads the value of the specified column and formats it as a string.- Parameters:
row- the record (row) to read fromcolumn- the column to read from- Returns:
- the formatted value
-
formatValue
Formats the value using the default formatter, if available, or withObject.toString().- Parameters:
value- the value to format- Returns:
- the formatted value
-
display
Description copied from interface:DisplayableDisplays the contents of this object to the given output stream. This method does not close the underlying stream; the caller is responsible for disposing it.- Specified by:
displayin interfaceDisplayable- Parameters:
out- the output stream
-
display
Displays the data in the given format to the terminal.- Parameters:
tableFormat- the table format
-
display
Displays the data in the given format.- Parameters:
tableFormat- the table formatout- the output stream
-
display
Displays the data in the given format.- Parameters:
tableFormat- the table formatwriter- the output writer
-
save
Saves the data to a file in the requested format.- Parameters:
tableFormat- the resulting table formatfile- the resulting file- Throws:
IOException- if an I/O error occurred
-
save
Saves the data to an output stream in the requested format.- Parameters:
tableFormat- the resulting table formatout- the output stream- Throws:
IOException- if an I/O error occurred
-
save
Saves the data to a writer in the requested format.- Parameters:
tableFormat- the resulting table formatwriter- the writer- Throws:
IOException- if an I/O error occurred
-
toPlaintext
Writes the data in the default, plaintext format.- Parameters:
out- the output stream
-
toCSV
Writes the data formatted as CSV.- Parameters:
out- the output stream
-
toMarkdown
Writes the data formatted as a Markdown table.- Parameters:
out- the output stream
-
toLatex
Writes the data formatted as a Latex table.- Parameters:
out- the output stream
-
toJson
Writes the data formatted as a Json object. This format is designed to be compatible with Pandas, matching the output produced byorient='record'.import pandas as pd df = pd.DataFrame(...) df.to_json("data.json", orient='record') df = pd.read_json("data.json")- Parameters:
out- the output stream
-
toARFF
Writes the data formatted as an ARFF file. This file format is a standard used by data mining applications.- Parameters:
out- the output stream- See Also:
-
of
public static <K,V> TabularData<org.apache.commons.lang3.tuple.Pair<K,V>> of(Iterable<org.apache.commons.lang3.tuple.Pair<K, V>> data) Constructs a table of the given pairs.- Type Parameters:
K- the type of each key in the pairV- the type of each value in the pair- Parameters:
data- the table data- Returns:
- the table
-
of
public static <K,V> TabularData<org.apache.commons.lang3.tuple.Pair<K,V>> of(Iterable<org.apache.commons.lang3.tuple.Pair<K, V>> data, String keyName, String valueName) Constructs a table of the given pairs.- Type Parameters:
K- the type of each key in the pairV- the type of each value in the pair- Parameters:
data- the table datakeyName- the column header for the keyvalueName- the column header for the value- Returns:
- the table
-