Class Rows

All Implemented Interfaces:
Serializable, Cloneable, Iterable<Rows.Row>, Collection<Rows.Row>, List<Rows.Row>, RandomAccess

public class Rows extends ArrayList<Rows.Row>
An utility abstraction of a database result set where all child Row objects are themselves maps that share the same case insensitive key set.

The idea is to be a little more memory efficient, offer zero base integer index or case insensitive key/column name access, and have key/column order match on all rows.

This was initially developed so that a JDBC ResultSet could be loaded into a list of maps without having to replicate the keys for every row.

Implementation Notes:

While Row implements Map, it actually uses a List to maintain its values.

A instance of the RowKeys, which maintains a Map from case insensitive string keys their index position in each Row's list, is shared by all Row instances.

See Also:
  • Constructor Details

    • Rows

      public Rows()
      Creates an empty Rows with no keys/columns.
    • Rows

      public Rows(Map row)
      Creates a Rows with a single Row with keys/columns equal to row.getKeySet()
      Parameters:
      row - key pars to add
    • Rows

      public Rows(String[] keys)
      Creates a Rows with keys/columns equal to keys
      Parameters:
      keys - the column names
    • Rows

      public Rows(List<String> keys)
      Creates a Rows with keys/columns equal to keys
      Parameters:
      keys - the column keys
  • Method Details

    • keyList

      public List<String> keyList()
      Returns:
      the ordered key/column names
    • keySet

      public Set<String> keySet()
      Returns:
      key/column names as a set that preserves iteration order
    • addKey

      public int addKey(String key)
      Adds a key/column for each Row at the end of the iteration order.
      Parameters:
      key - the new key to add
      Returns:
      the integer index of key/column which will be keys.size() -1 if the key is new or the existing index if a case insensitive match of key already exited
    • addRow

      public Rows.Row addRow()
      Adds a new empty Row to the end of the list.
      Returns:
      the new last/current row
    • addRow

      public Rows.Row addRow(Map map)
      Adds key/values from map to a new Row.
      Parameters:
      map - the key/values to add to the new Row
      Returns:
      the new last/current Row
      See Also:
    • addRow

      public Rows.Row addRow(int index, Map map)
      Insert key/values from map as the new indexth Row.

      If RowKeys has not been initialized, it will be initialized with map.keySet().

      If RowKeys has been initialized, only keys/values with a case insensitive matching key in RowKeys will be copied into the new Row.

      Parameters:
      index - the position to insert the new Row or -1 to indicate the 'at the end'
      map - the key/values to add to the new Row
      Returns:
      the new last/current Row
      See Also:
    • addRow

      public Rows.Row addRow(List values)
      Adds valuesas a new Row to the end of the list.
      Parameters:
      values - the values to add to the new Row
      Returns:
      the new last/current Row
      See Also:
    • addRow

      public Rows.Row addRow(int index, List values)
      Adds values as the new indexth Row.
      Parameters:
      index - the position to insert the new Row or -1 to indicate the 'at the end'
      values - the values to add to the new Row
      Returns:
      the new last/current Row
      See Also:
    • addRow

      public Rows.Row addRow(Object[] values)
      Adds valuesas a new Row to the end of the list.
      Parameters:
      values - the values to add to the new Row
      Returns:
      the new last/current Row
      See Also:
    • addRow

      public Rows.Row addRow(int index, Object[] values)
      Adds values as the new indexth Row.

      The returned Row becomes lastRow

      Parameters:
      index - the position to insert the new Row or -1 to indicate the 'at the end'
      values - the values to add to the new Row
      Returns:
      the new last/current Row
      See Also:
    • put

      public void put(String key, Object value)
      Sets key/value on lastRow.

      If RowKeys does not have a case insensitive match for key then key automatically becomes the new last column for all rows.

      Parameters:
      key - the row key
      value - the value to store
    • put

      public void put(Object value)
      Adds value to the end of lastRow.
      Parameters:
      value - the value to store
    • add

      public boolean add(Rows.Row row)
      Adds the key/values from row as a new Row.

      The actual Row object is not added to the Rows list because its RowKeys object will not be the same. Instead all key/values are copied into a new Row.

      Specified by:
      add in interface Collection<Rows.Row>
      Specified by:
      add in interface List<Rows.Row>
      Overrides:
      add in class ArrayList<Rows.Row>
      Parameters:
      row - a map containing the key/values to add
      See Also:
    • addAll

      public boolean addAll(Collection<? extends Rows.Row> rows)
      Calls #addRow(Map) for each Row in rows
      Specified by:
      addAll in interface Collection<Rows.Row>
      Specified by:
      addAll in interface List<Rows.Row>
      Overrides:
      addAll in class ArrayList<Rows.Row>
    • addAll

      public boolean addAll(int index, Collection<? extends Rows.Row> rows)
      Calls #addRow(int, Map) for each Row in rows
      Specified by:
      addAll in interface List<Rows.Row>
      Overrides:
      addAll in class ArrayList<Rows.Row>