fi.pelam.csv.table

Table

final case class Table[RT, CT, M <: TableMetadata](cells: IndexedSeq[IndexedSeq[Cell]], rowTypes: SortedBiMap[RowKey, RT], colTypes: SortedBiMap[ColKey, CT], metadata: M) extends Product with Serializable

This class is an immutable container for Cells with optional row and column types. The ideas in the API roughly follow popular spread sheet programs.

The cells are stored in rows which are numbered conceptually from top to bottom and then in columns which are numbered from left to right.

The row and column types are an additional abstraction with the purpose of simplifying machine reading of complex spread sheets.

This class is part of the the higher level API for reading, writing and processing CSV data.

The simpler stream based API is enough for many scenarios, but if several different sets of data will be pulled from the same CSV file and the structure of the CSV file is not rigid, this API may be a better fit.

Several methods are provided for getting cells based on the row and column types. For example

Example

This example constructs a table directly, although usually it is done via a TableReader. In this example, simple String values are used for row and column types, although usually an enumeration or case object type solution is cleaner and safer.

val table = Table(
  List(StringCell(CellKey(0, 0), "name"),
    StringCell(CellKey(0, 1), "value"),
    StringCell(CellKey(1, 0), "foo"),
    IntegerCell(CellKey(1, 1), 1),
    StringCell(CellKey(2, 0), "bar"),
    IntegerCell(CellKey(2, 1), 2)
  ),

  SortedBiMap(RowKey(0) -> "header",
    RowKey(1) -> "data",
    RowKey(2) -> "data"),

  SortedBiMap(ColKey(0) -> "name",
    ColKey(1) -> "number")
 )

table.getSingleCol("name", "data").map(_.value).toList
// Will give List("foo","bar")

table.getSingleCol("number", "data").map(_.value).toList)
// Will give List(1,2)

Note on row and column numbers

Internally rows and columns have zero based index numbers, but in some cases like in toString methods of Cell and CellKey the index numbers are represented similarly to popular spread sheet programs. In that csae row numbers are one based and column numbers are alphabetic.

RT

The client specific row type.

CT

The client specific column type.

M

The type of the metadata parameter. Must be a sub type of TableMetadata. This specifies the character set and separator to use when reading the CSV data from the input stream.

cells

All cells in a structure of nested IndexedSeqs. The order is first rows, then columns.

rowTypes

A bidirectional map mapping rows to their row types and vice versa. Multiple rows can have the same type.

colTypes

A bidirectional map mapping columns to their column types and vice versa. Multiple columns can have the same type.

metadata

User extensible metadata that is piggybacked in the Table instance.

Source
Table.scala
Linear Supertypes
Serializable, Serializable, Product, Equals, AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Table
  2. Serializable
  3. Serializable
  4. Product
  5. Equals
  6. AnyRef
  7. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Table(cells: IndexedSeq[IndexedSeq[Cell]], rowTypes: SortedBiMap[RowKey, RT], colTypes: SortedBiMap[ColKey, CT], metadata: M)

    The actual constructor is on the companion object.

    The actual constructor is on the companion object.

    cells

    All cells in a structure of nested IndexedSeqs. The order is first rows, then columns.

    rowTypes

    A bidirectional map mapping rows to their row types and vice versa. Multiple rows can have the same type.

    colTypes

    A bidirectional map mapping columns to their column types and vice versa. Multiple columns can have the same type.

    metadata

    User extensible metadata that is piggybacked in the Table instance.

Type Members

  1. type TableType = Table[RT, CT, M]

Value Members

  1. final def !=(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  5. val cells: IndexedSeq[IndexedSeq[Cell]]

    All cells in a structure of nested IndexedSeqs.

    All cells in a structure of nested IndexedSeqs. The order is first rows, then columns.

  6. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  7. val colCount: Int

    The horizontal size of this table.

    The horizontal size of this table. The table has strict rectangular form. Unused cells simply contain empty StringCells.

  8. val colKeys: SortedSet[ColKey]

  9. val colTypes: SortedBiMap[ColKey, CT]

    A bidirectional map mapping columns to their column types and vice versa.

    A bidirectional map mapping columns to their column types and vice versa. Multiple columns can have the same type.

  10. def colsByType: SortedMap[CT, IndexedSeq[ColKey]]

    This method is a shorthand for colTypes.reverse.

    This method is a shorthand for colTypes.reverse.

    returns

    a map from column types to matching ColKeys (column indices)

  11. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  12. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  13. def getCell(key: CellKey): Cell

    Get cell at specific row, column address.

  14. def getCellKeys(colKey: ColKey): IndexedSeq[CellKey]

    Get cell keys corresponding to all cells on the specified column.

  15. def getCellKeys(rowKey: RowKey): IndexedSeq[CellKey]

    Get cell keys corresponding to all cells on the specified row.

  16. def getCells(colKey: ColKey): IndexedSeq[Cell]

    Get all cells from the specified column

  17. def getCells(rowKey: RowKey): IndexedSeq[Cell]

    Get all cells from the specified row.

  18. def getCells(): IndexedSeq[Cell]

    Get all cells in a single sequence.

    Get all cells in a single sequence. One way to think about this is that the rows are catenated one after the other.

  19. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  20. def getRow(cellKey: CellKey): IndexedSeq[Cell]

  21. def getRows(rowType: RT): IndexedSeq[IndexedSeq[Cell]]

    Get a full rows from table defined by rowType.

  22. def getSingleCell(rowKey: RowKey, colType: CT): Cell

    Get a cell from specified row matching the specified column type.

    Get a cell from specified row matching the specified column type. This method throws an error if more than 1 or zero cells match the criteria. Example:

    // Get the user name cell from the 11th row.
    table.getSingleCell(RowKey(10), ColumnTypeUserName)
    rowKey

    identifies the row to target.

    colType

    identifies the column type which must correspond to exactly 1 column.

    returns

    the matching cell object.

  23. def getSingleCol(rowTypeMatcher: (RT) ⇒ Boolean, colKey: ColKey): IndexedSeq[Cell]

    Otherwise same as getSingleCol(rowTypeMatcher, CT), but the column is addressed directly with colKey (column index).

  24. def getSingleCol(rowType: RT, colKey: ColKey): IndexedSeq[Cell]

    Otherwise same as getSingleCol(rowTypeMatcher, CT), but the column is addressed directly with colKey (column index) and a single bare row type is specified instead of the more general predicate form.

  25. def getSingleCol(rowType: RT, colType: CT): IndexedSeq[Cell]

    Otherwise same as getSingleCol(rowTypeMatcher, CT), but a single bare row type is specified instead of the more general predicate form.

  26. def getSingleCol(rowTypeMatcher: (RT) ⇒ Boolean, colType: CT): IndexedSeq[Cell]

    Gets a selected set of cells from a particular column.

    Gets a selected set of cells from a particular column. The cells will be picked from rows having a row type (RT) for which rowTypeMatcher returns true. Throws an exception if CT fits more than one or zero columns. Here is an imaginary example:

    // Gets the street address for two types of addresses
    table.getSingleCol(Set(HomeAddressRow, BillingAddressRow), StreetAddressColumn)
  27. def getSingleColKeyByType(colType: CT): ColKey

    Find a single column with given type.

    Find a single column with given type. Throws if the number of columns with given type is not 1

  28. def getSingleRow(rowKey: RowKey, colTypeMatcher: (CT) ⇒ Boolean): IndexedSeq[Cell]

    Otherwise same as getSingleRow(RT, colTypeMatcher), but row is addressed directly with RowKey (row index).

  29. def getSingleRow(rowKey: RowKey, colType: CT): IndexedSeq[Cell]

    Otherwise same as getSingleRow(RT, colTypeMatcher), but row is addressed directly with RowKey (row index) and a single bare column type is specified instead of the more general predicate form.

  30. def getSingleRow(rowType: RT, colType: CT): IndexedSeq[Cell]

    Otherwise same as getSingleRow(RT, colTypeMatcher), but a single bare column type is specified instead of the more general predicate form.

  31. def getSingleRow(rowType: RT, colTypeMatcher: (CT) ⇒ Boolean): IndexedSeq[Cell]

    Gets a selected set of cells from a particular row.

    Gets a selected set of cells from a particular row. The cells will be picked from columns having a column type (CT) for which colTypeMatcher returns true. Throws an exception if RT fits more than one or zero rows. Here is an imaginary example:

    // Gets the extra notes on projects and clients
    table.getSingleRow(ExtraNotesRow, Set(ProjectColumn, ClientColumn))
  32. def getSingleRowKeyByType(rowType: RT): RowKey

    Find a single row with given type.

    Find a single row with given type. Throws if the number of rows with given type is not 1

  33. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  34. val metadata: M

    User extensible metadata that is piggybacked in the Table instance.

  35. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  36. final def notify(): Unit

    Definition Classes
    AnyRef
  37. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  38. def projection: TableProjection[RT, CT, M]

    Table can be "projected" ie.

    Table can be "projected" ie. select some rows and columns and create a new table.

    See TableProjection for an example.

  39. def projectionFull: TableProjection[RT, CT, M]

    Same as projection, but start with all rows and columns which can then be removed.

    Same as projection, but start with all rows and columns which can then be removed.

    See TableProjection for more details.

  40. def resized(resizeCellKey: CellKey, rowsResize: Int, colsResize: Int, fillerGenerator: CellGenerator): TableType

    Returns a copy of the table with specified amounts of rows and columns added or removed.

    Returns a copy of the table with specified amounts of rows and columns added or removed.

    Basically the same as first doing resizedRows and then resizedCols.

  41. def resized(targetRegion: (CellKey, CellKey), resizedRegion: (CellKey, CellKey), fillerGenerator: CellGenerator): TableType

    Return a new table with different dimensions.

    Return a new table with different dimensions.

    The resizing is based on two rectangular regions targetRegion and resizedRegion.

    The idea is that this method can grow or shrink the region specified by targetRegion as needed to match the resizedRegion.

    targetRegion

    defines a rectangular region of cells to be resized. The idea is that targetRegion will be resized to match the resizedRegion.

    resizedRegion

    is a rectangular region to define the new size for the targetRegion.

    The idea is that resizedRegion can define (span) a different rectangular region than the targetRegion.

    If there are extra cells in the targetRegion, rows and columns are deleted from the "ends" of the targetRegion.

    If the resizedRegion doesn't fit in targetRegion, the targetRegion is expanded to contain it. New rows and columns are generated with fillerGenerator as needed.

    There is one limitation however. The top left corner of resizedRegion must be equal to or towards down and right with respect to the top left corner of targetRegion.

    fillerGenerator

    When new cells need to be created, this is used. The cell does not need to have correct cellKey, a copy with the correct cellKey will be made if necessary.

  42. def resizedCols(colKey: ColKey, value: Int, fillerGenerator: CellGenerator = emptyStringCell, updateSide: HorizontalDirection = LeftColumn): TableType

    Returns a modified a table with the specified amount of columns added or removed.

    Returns a modified a table with the specified amount of columns added or removed.

    colKey

    where new columns are added or old ones deleted If columns are deleted this will be the first column to go and further columns will be the ones before this. If columns are added, they are added before this column with the same type as this column.

    value

    for negative values, rows left of colKey are deleted. For positive values columns at colKey are added. For zero, this table is returned.

    updateSide

    Allows adding the new colums _after_ the column indicated by colKey.

    returns

    a modified copy of the table

  43. def resizedRows(rowKey: RowKey, value: Int, fillerGenerator: CellGenerator = emptyStringCell): TableType

    Returns a modified a table with the specified amount of rows added or removed.

    Returns a modified a table with the specified amount of rows added or removed.

    rowKey

    where new rows are added or old rows deleted. If rows are deleted this will be the first row to go and further rows will be the ones before this. If rows are added, they are added after this row with the same type as this row.

    value

    for negative values, rows above rowKey are deleted For positive values rows at rowKey are added. For zero, this table is returned.

    returns

    a modified copy of the table

  44. val rowCount: Int

    The vertical size of this table.

    The vertical size of this table. The table has strict rectangular form. Unused cells simply contain empty StringCells.

  45. val rowKeys: SortedSet[RowKey]

  46. val rowTypes: SortedBiMap[RowKey, RT]

    A bidirectional map mapping rows to their row types and vice versa.

    A bidirectional map mapping rows to their row types and vice versa. Multiple rows can have the same type.

  47. def rowsByType: SortedMap[RT, IndexedSeq[RowKey]]

    This method is a shorthand for rowTypes.reverse.

    This method is a shorthand for rowTypes.reverse.

    returns

    a map from row types to matching RowKeys (row indices)

  48. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  49. def toString(): String

    Definition Classes
    Table → AnyRef → Any
  50. def updatedCell(cell: Cell): TableType

    Return a new table with given cell replacing the previous cell in the location cell.cellKey.

  51. def updatedCells(cells: Cell*): TableType

    A convenient form for returning a new table with specified cells updated.

    A convenient form for returning a new table with specified cells updated.

    val updated = table.updatedCells(StringCell(CellKey(0, 0), "foo"), StringCell(CellKey(1, 0), "bar"),
  52. def updatedCells(newCells: TraversableOnce[Cell]): TableType

    Return a new table with given cells replacing the previous cells at each the location cell.cellKey.

  53. def updatedCols(targetRegion: Region, replacementCells: TraversableOnce[Cell], fillerGenerator: CellGenerator = emptyStringCell): TableType

    Return a new table with replacementCells inserted into the table as columns with the height of the targetRegion.

    Return a new table with replacementCells inserted into the table as columns with the height of the targetRegion.

    This method kind of squeezes given cells into the given region column by column.

    If the given replacementCells don't fit snugly into the region, the region is resized horizontally as needed.

    Another way to define this method, is that it is same as updatedRegion, but replacementCells are renumbered from top to bottom and left to right into the targetRegion.

  54. def updatedRegion(targetRegion: Region, replacementCells: TraversableOnce[Cell], fillerGenerator: CellGenerator = emptyStringCell): TableType

    targetRegion

    defines a rectangular region of cells to be replaced. Region spanned by replacementCells does not need to fit targetRegion. Table will be resized to match. See below for more details.

    replacementCells

    a rectangular region of cells to replace targetRegion. Gaps are ok. The replacementCells can define (span) a different rectangular region than the targetRegion.

    See resized for details on how the resizing works.

    fillerGenerator

    When new cells need to be created, this is used.

    returns

    a new table with the replaced cells. Original table is not modified.

  55. def updatedRows(targetRegion: Region, replacementCells: TraversableOnce[Cell], fillerGenerator: CellGenerator = emptyStringCell): TableType

    Return a new table with replacementCells inserted into the table as rows with the width of the targetRegion.

    Return a new table with replacementCells inserted into the table as rows with the width of the targetRegion.

    This method kind of squeezes given cells into the given region row by row.

    If the given replacementCells don't fit snugly into the region, the region is resized vertically as needed.

    Another way to define this method, is that it is same as updatedRegion, but replacementCells are renumbered from left to right and top to bottom into the targetRegion.

  56. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  57. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  58. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from Serializable

Inherited from Serializable

Inherited from Product

Inherited from Equals

Inherited from AnyRef

Inherited from Any

Ungrouped