Class TabularData<T>

java.lang.Object
org.moeaframework.util.format.TabularData<T>
Type Parameters:
T - the type of records (rows)
All Implemented Interfaces:
Displayable

public class TabularData<T> extends Object implements Displayable
Formats and displays tabular data.
  • Constructor Details

    • TabularData

      public TabularData(Iterable<T> dataSource)
      Creates a new tabular data object using the given data source.
      Parameters:
      dataSource - the source of data
  • Method Details

    • addFormatter

      public void addFormatter(Formatter<?> formatter)
      Adds a formatter. This will be used to format any values matching the formatter's type (see Formatter.getType().
      Parameters:
      formatter - the default formatter
    • removeAllFormatters

      public void removeAllFormatters()
      Removes any existing formatters.
    • addColumn

      public void addColumn(Column<T,?> column)
      Adds a column to this data. The columns will be displayed in the order they are added.
      Parameters:
      column - the column
    • format

      protected String format(T row, Column<T,?> column)
      Reads the value of the specified column and formats it as a string.
      Parameters:
      row - the record (row) to read from
      column - the column to read from
      Returns:
      the formatted value
    • formatValue

      protected String formatValue(Object value)
      Formats the value using the default formatter, if available, or with Object.toString().
      Parameters:
      value - the value to format
      Returns:
      the formatted value
    • display

      public void display(PrintStream out)
      Description copied from interface: Displayable
      Displays 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:
      display in interface Displayable
      Parameters:
      out - the output stream
    • display

      public void display(TableFormat tableFormat)
      Displays the data in the given format to the terminal.
      Parameters:
      tableFormat - the table format
    • display

      public void display(TableFormat tableFormat, PrintStream out)
      Displays the data in the given format.
      Parameters:
      tableFormat - the table format
      out - the output stream
    • display

      public void display(TableFormat tableFormat, PrintWriter writer)
      Displays the data in the given format.
      Parameters:
      tableFormat - the table format
      writer - the output writer
    • save

      public void save(TableFormat tableFormat, File file) throws IOException
      Saves the data to a file in the requested format.
      Parameters:
      tableFormat - the resulting table format
      file - the resulting file
      Throws:
      IOException - if an I/O error occurred
    • save

      public void save(TableFormat tableFormat, OutputStream out) throws IOException
      Saves the data to an output stream in the requested format.
      Parameters:
      tableFormat - the resulting table format
      out - the output stream
      Throws:
      IOException - if an I/O error occurred
    • save

      public void save(TableFormat tableFormat, Writer writer) throws IOException
      Saves the data to a writer in the requested format.
      Parameters:
      tableFormat - the resulting table format
      writer - the writer
      Throws:
      IOException - if an I/O error occurred
    • toPlaintext

      protected void toPlaintext(PrintWriter out)
      Writes the data in the default, plaintext format.
      Parameters:
      out - the output stream
    • toCSV

      protected void toCSV(PrintWriter out)
      Writes the data formatted as CSV.
      Parameters:
      out - the output stream
    • toMarkdown

      protected void toMarkdown(PrintWriter out)
      Writes the data formatted as a Markdown table.
      Parameters:
      out - the output stream
    • toLatex

      protected void toLatex(PrintWriter out)
      Writes the data formatted as a Latex table.
      Parameters:
      out - the output stream
    • toJson

      protected void toJson(PrintWriter out)
      Writes the data formatted as a Json object. This format is designed to be compatible with Pandas, matching the output produced by orient='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

      protected void toARFF(PrintWriter out)
      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 pair
      V - 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 pair
      V - the type of each value in the pair
      Parameters:
      data - the table data
      keyName - the column header for the key
      valueName - the column header for the value
      Returns:
      the table