Class NamedValuesFormatter

  • Direct Known Subclasses:
    NamedValuesColumnFormatter, NamedValuesRowFormatter

    public abstract class NamedValuesFormatter
    extends Object
    NamedValuesFormatter is a base class for the classes which provide support for printing the values in a NamedValues instance.

    NamedValues is a class for holding lists of String values where each list is associated with a name. NamedValuesFormatter can print the name/values in a NamedValues as a table or as a list of rows.

    See NamedValuesColumnFormatter for help with formatting a NamedValues as a table (each name and all its values in one column). See NamedValuesRowFormatter for help printing as a list of row oriented data (each name and all its values on one row).

    For example, to create a table style output formatter, assign it data, and print the data:
                            NamedValuesFormatter formatter =
                                    NamedValuesFormatter.create( NamedValuesFormatter.STYLE_COLUMNS );
                            formatter.setWhetherDisplayLabels( true );
                            formatter.assignNamedValues( namedValues );  // A named values system created elsewhere
                            formatter.display( System.out );
     
    Author:
    11.11.11 Greg White (greg@slac.stanford.edu) and Chris Larrieu.
    See Also:
    NamedValuesColumnFormatter, NamedValuesRowFormatter
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected String[][] cells  
      protected String[] labels  
      protected int numCols  
      protected int numRows  
      static int STYLE_COLUMNS
      To create a NamedValuesFormatter that prints NamedValues as a table, 1 name and its values in a column.
      static int STYLE_ROWS
      To create a NamedValuesFormatter that prints NamedValues as 1 name and its values on 1 row.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void assignNamedValues​(NamedValues namedValues)
      Tells the NamedValuesFormatter which NamedValues system it should format.
      protected static String centerText​(String text, String space)
      Returns the given "text" overlaid in the center of the space string.
      static NamedValuesFormatter create​(int style)
      Creates an NamedValuesFormatter that will print a NamedValues object in either columns or rows orientation, depending on the argument.
      void display​(PrintStream out)
      Prints the formatted NamedValues as a table or list of rows, according to the style used to create the formatter.
      abstract String getCell​(int row, int col)
      Thinking of the namedValues system as a matrix of strings, return a given cell's string data
      protected int getColumnWidth​(int col)
      Finds the widest cell of the given column and returns its width.
      abstract String getFormattedCell​(int row, int col, String space)
      Thinking of the namedValues system as a matrix of strings, return a given cell's string data formatted in a string according to whether the system as a whole would be printed in column or row formatting.
      boolean getWhetherDisplayLabels()
      Returns boolean indicating if the printed output will include headings.
      abstract boolean isLabelCell​(int row, int col)
      Thinking of the namedValues system as a matrix of strings, return whether the given cell is a label cell or not.
      protected static String leftJustifyText​(String text, String space)
      Returns the given "text" overlaid and to the left of the space string.
      protected static String rightJustifyText​(String text, String space)
      Returns the given "text" overlaid and to the right of the space string.
      void setWhetherDisplayLabels​(boolean yesno)
      Sets whether or not headings will be printed with the data.
      protected abstract void updateDimensions()
      Thinking of the namedValues system as a matrix of strings, reset the internal row and column count based on things like whether labels would be printed (if not, that's one less row).
    • Field Detail

      • STYLE_ROWS

        public static final int STYLE_ROWS
        To create a NamedValuesFormatter that prints NamedValues as 1 name and its values on 1 row.
        See Also:
        Constant Field Values
      • STYLE_COLUMNS

        public static final int STYLE_COLUMNS
        To create a NamedValuesFormatter that prints NamedValues as a table, 1 name and its values in a column.
        See Also:
        Constant Field Values
      • labels

        protected String[] labels
      • cells

        protected String[][] cells
      • numRows

        protected int numRows
      • numCols

        protected int numCols
    • Constructor Detail

      • NamedValuesFormatter

        public NamedValuesFormatter()
    • Method Detail

      • create

        public static NamedValuesFormatter create​(int style)
        Creates an NamedValuesFormatter that will print a NamedValues object in either columns or rows orientation, depending on the argument.
        Parameters:
        style - If style is given and is valued NamedValuesFormatter.STYLE_ROWS (1) then a row formatter is created, otherwise a column formatter is created.
        Returns:
        An instance of a NamedValuesFormatter of the given style.
        See Also:
        STYLE_ROWS, STYLE_COLUMNS
      • assignNamedValues

        public void assignNamedValues​(NamedValues namedValues)
        Tells the NamedValuesFormatter which NamedValues system it should format.
        Parameters:
        namedValues - The system of named values that should be formatted by this NamedValuesFormatter.
      • getColumnWidth

        protected int getColumnWidth​(int col)
        Finds the widest cell of the given column and returns its width. This is used internally to assess the character width that should be used to print a column.
        Parameters:
        col - the column
        Returns:
        int width of the widest cell in the given column.
      • display

        public void display​(PrintStream out)
        Prints the formatted NamedValues as a table or list of rows, according to the style used to create the formatter.
        Parameters:
        out - The stream to which to print the output.
      • getWhetherDisplayLabels

        public boolean getWhetherDisplayLabels()
        Returns boolean indicating if the printed output will include headings. If so, the names in the names/values system will be used as the heading labels.
        Returns:
        true if the formatter will print the labels, and false otherwise. See setWhetherDisplayLabels to change the value of the print labels setting.
        See Also:
        setWhetherDisplayLabels(boolean)
      • setWhetherDisplayLabels

        public void setWhetherDisplayLabels​(boolean yesno)
        Sets whether or not headings will be printed with the data.
        Parameters:
        yesno - If false the formatter will not print the labels. If true, or this method is not used, the headings will be printed.
      • centerText

        protected static String centerText​(String text,
                                           String space)
        Returns the given "text" overlaid in the center of the space string.
        Parameters:
        text - The string of data. This would be one cell's value data.
        space - A string into which to put the text. Usually space would be a String of spaces.
        Returns:
        space, same width as given, with text in the middle.
      • leftJustifyText

        protected static String leftJustifyText​(String text,
                                                String space)
        Returns the given "text" overlaid and to the left of the space string.
        Parameters:
        text - The string of data. This would be one cell's value data.
        space - A string into which to put the text. Usually space would be a String of spaces.
        Returns:
        space, same width as given, with text in it and left justified.
      • rightJustifyText

        protected static String rightJustifyText​(String text,
                                                 String space)
        Returns the given "text" overlaid and to the right of the space string.
        Parameters:
        text - The string of data. This would be one cell's value data.
        space - A string into which to put the text. Usually space would be a String of spaces.
        Returns:
        space, same width as given, with text in it and right justified.
      • isLabelCell

        public abstract boolean isLabelCell​(int row,
                                            int col)
        Thinking of the namedValues system as a matrix of strings, return whether the given cell is a label cell or not. Basically, if the system is column oriented, is it the first row.
        Parameters:
        row - the row of the cell
        col - the column of the cell
        Returns:
        true if the cell value should be treated as a label.
      • getCell

        public abstract String getCell​(int row,
                                       int col)
        Thinking of the namedValues system as a matrix of strings, return a given cell's string data
        Parameters:
        row - Specifies the index of the vector of values that were added to the NamedValues system, starting from 0 being the first name and value vector added.
        col - Specifies the index of a column, where 0 indicates the name, and >0 indicates one of the values associated with that name.
        Returns:
        The cell's string data.
      • getFormattedCell

        public abstract String getFormattedCell​(int row,
                                                int col,
                                                String space)
        Thinking of the namedValues system as a matrix of strings, return a given cell's string data formatted in a string according to whether the system as a whole would be printed in column or row formatting.
        Parameters:
        row - Specifies the index of the vector of values that were added to the NamedValues system, starting from 0 being the first name and value vector added.
        col - Specifies the index of a column, where 0 indicates the name, and >0 indicates one of the values associated with that name.
        space - A string into which to put the text. Usually space would be a String of spaces.
        Returns:
        The cell's string data, formatted appropriately as being printed for the system as a whole.
      • updateDimensions

        protected abstract void updateDimensions()
        Thinking of the namedValues system as a matrix of strings, reset the internal row and column count based on things like whether labels would be printed (if not, that's one less row).