Class TabularFile

  • Direct Known Subclasses:
    TabularInputFile, TabularOutputFile

    public abstract class TabularFile
    extends java.lang.Object
    An abstraction for holding tabular data in a single file. That is, a list of columns with specified data types and rows containing the values of every column stored within rows within a file. The order of the columns is important. (first column, second column, etc.). The order of the rows is relevant (first row, second row, etc.). There are only two types: numeric and text. The numeric type should be used for numeric data (float, double, long, int, etc.). In addition, use the numeric type for boolean values, which are stored 1.0 = true, 0.0 = false). The text type should be used for strings and date/time data. Date/time data is saved as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS"). If you need more type complexity, you should use a database.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static com.google.common.collect.ImmutableBiMap<DataType,​org.jooq.DataType> JOOQ_TYPE  
      protected java.util.List<java.lang.String> myColumnNames  
      protected java.util.LinkedHashMap<java.lang.String,​DataType> myColumnTypes  
      protected java.util.List<DataType> myDataTypes  
      protected com.google.common.collect.BiMap<java.lang.String,​java.lang.Integer> myNameAndIndex  
      protected com.google.common.collect.BiMap<java.lang.Integer,​java.lang.Integer> myNumericIndices  
      protected java.nio.file.Path myPath  
      protected com.google.common.collect.BiMap<java.lang.Integer,​java.lang.Integer> myTextIndices  
    • Constructor Summary

      Constructors 
      Constructor Description
      TabularFile​(java.util.LinkedHashMap<java.lang.String,​DataType> columnTypes, java.nio.file.Path path)  
    • Field Detail

      • JOOQ_TYPE

        public static final com.google.common.collect.ImmutableBiMap<DataType,​org.jooq.DataType> JOOQ_TYPE
      • myColumnTypes

        protected final java.util.LinkedHashMap<java.lang.String,​DataType> myColumnTypes
      • myNameAndIndex

        protected final com.google.common.collect.BiMap<java.lang.String,​java.lang.Integer> myNameAndIndex
      • myNumericIndices

        protected final com.google.common.collect.BiMap<java.lang.Integer,​java.lang.Integer> myNumericIndices
      • myTextIndices

        protected final com.google.common.collect.BiMap<java.lang.Integer,​java.lang.Integer> myTextIndices
      • myColumnNames

        protected final java.util.List<java.lang.String> myColumnNames
      • myDataTypes

        protected final java.util.List<DataType> myDataTypes
      • myPath

        protected final java.nio.file.Path myPath
    • Constructor Detail

      • TabularFile

        public TabularFile​(java.util.LinkedHashMap<java.lang.String,​DataType> columnTypes,
                           java.nio.file.Path path)
    • Method Detail

      • numericColumn

        public static ColumnType numericColumn​(java.lang.String name)
        Creates a double column
        Parameters:
        name - the name of the column, must not be null
        Returns:
        the created column
      • textColumn

        public static ColumnType textColumn​(java.lang.String name)
        Creates a text column
        Parameters:
        name - the name of the column, must not be null
        Returns:
        the created column
      • column

        public static ColumnType column​(java.lang.String name,
                                        DataType dataType)
        Creates a column with the given data type
        Parameters:
        name - the name of the column, must not be null
        dataType - the type of the column, must not be null
        Returns:
        the created column
      • columnNames

        public static java.util.List<java.lang.String> columnNames​(int number)
        Makes a list of strings containing, C1, C2, ..., CN, where N = number
        Parameters:
        number - the number of names, must be 1 or more
        Returns:
        the list of names
      • columnNames

        public static java.util.List<java.lang.String> columnNames​(java.lang.String prefix,
                                                                   int number)
        Makes a list of strings containing, prefix1, prefix2,..., prefixN, where N = number
        Parameters:
        prefix - the prefix for each name, must not be null
        number - the number of names, must be 1 or more
        Returns:
        the list of names
      • columns

        public static java.util.LinkedHashMap<java.lang.String,​DataType> columns​(java.util.List<java.lang.String> names,
                                                                                       DataType dataType)
        Creates names.size() columns with the provided names and data type
        Parameters:
        names - the names for the columns, must not be null or empty
        dataType - the data type to associated with each column
        Returns:
        a map with the column names all assigned the same data type
      • columns

        public static java.util.LinkedHashMap<java.lang.String,​DataType> columns​(int numColumns,
                                                                                       DataType dataType)
        Creates n = numColumns of columns all with the same data type, with names C1, C2, ..., Cn
        Parameters:
        numColumns - the number of columns to make, must be greater than 0
        dataType - the type of all of the columns
        Returns:
        a map with the column names all assigned the same data type
      • isNumeric

        public static boolean isNumeric​(java.lang.Object element)
        Test if the object is any of {Double, Long, Integer, Boolean, Float, Short, Byte}
        Parameters:
        element - the element to test
        Returns:
        true if it is numeric
      • asDouble

        public static double asDouble​(java.lang.Object element)
        Parameters:
        element - the element to convert
        Returns:
        the element as a double, the element must be numeric
      • createAllNumeric

        public static TabularOutputFile createAllNumeric​(java.util.List<java.lang.String> columnNames,
                                                         java.nio.file.Path pathToFile)
        Creates an all numeric output file
        Parameters:
        columnNames - the names of the columns
        pathToFile - the path to the file
        Returns:
        the output file
      • createAllNumeric

        public static TabularOutputFile createAllNumeric​(int numColumns,
                                                         java.nio.file.Path pathToFile)
        Creates an all numeric output file with columns C1, C2, etc
        Parameters:
        numColumns - the number of columns
        pathToFile - path to file
        Returns:
        the tabular output file
      • createAllNumeric

        public static TabularOutputFile createAllNumeric​(int numColumns,
                                                         java.lang.String fileName)
        Creates an all numeric output file with columns C1, C2, .. in JSL.getInstance().getOutDir()
        Parameters:
        numColumns - number of columns in the file
        fileName - the name of the file, must not be null
        Returns:
        the tabular output file
      • getPath

        public final java.nio.file.Path getPath()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getColumnName

        public final java.lang.String getColumnName​(int colNum)
        Parameters:
        colNum - 0 based indexing
        Returns:
        the name of the column at the index
      • getDataType

        public final DataType getDataType​(int colNum)
        Parameters:
        colNum - 0 based indexing
        Returns:
        the data type of the column at the index
      • getNumberColumns

        public final int getNumberColumns()
        Returns:
        the number of columns of tabular data
      • getColumn

        public final int getColumn​(java.lang.String name)
        Parameters:
        name - the name of the column
        Returns:
        the index or -1 if not found
      • isNumeric

        public final boolean isNumeric​(int colNum)
        Parameters:
        colNum - the index into the row (0 based)
        Returns:
        true if the cell at location i is NUMERIC
      • isText

        public final boolean isText​(int colNum)
        Parameters:
        colNum - the index into the row (0 based)
        Returns:
        true if the cell at location i is TEXT
      • getNumNumericColumns

        public final int getNumNumericColumns()
        Returns:
        the total number of numeric columns
      • getNumTextColumns

        public final int getNumTextColumns()
        Returns:
        the total number of text columns
      • getColumnTypes

        public final java.util.LinkedHashMap<java.lang.String,​DataType> getColumnTypes()
        Returns:
        the map of columns associated with this tabular file
      • getColumnNames

        public final java.util.List<java.lang.String> getColumnNames()
        Returns:
        an ordered list of the column names
      • getNumericColumnNames

        public final java.util.List<java.lang.String> getNumericColumnNames()
        Returns:
        A list of all the numeric column names
      • getTextColumnNames

        public final java.util.List<java.lang.String> getTextColumnNames()
        Returns:
        A list of all the text column names
      • getDataTypes

        public final java.util.List<DataType> getDataTypes()
        Returns:
        an ordered list of the column data types
      • isAllNumeric

        public final boolean isAllNumeric()
        Returns:
        true if all columns have type NUMERIC
      • isAllText

        public final boolean isAllText()
        Returns:
        true if all columns have type TEXT
      • checkTypes

        public final boolean checkTypes​(java.lang.Object[] elements)
        Parameters:
        elements - the elements to check
        Returns:
        true if all elements match the correct types