Class Rows

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<Rows.Row>, java.util.Collection<Rows.Row>, java.util.List<Rows.Row>, java.util.RandomAccess

    public class Rows
    extends java.util.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:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Rows.Row
      Represents a single row in a database result set where values can be accessed by a zero based integer index or by a case insensitive key/column name.
      protected static class  Rows.RowKeys
      An ordered list of case insensitive key/column names shared by all Row instances in a Rows.
    • Field Summary

      • Fields inherited from class java.util.AbstractList

        modCount
    • Constructor Summary

      Constructors 
      Constructor Description
      Rows()
      Creates an empty Rows with no keys/columns.
      Rows​(java.lang.String[] keys)
      Creates a Rows with keys/columns equal to keys
      Rows​(java.util.List<java.lang.String> keys)
      Creates a Rows with keys/columns equal to keys
      Rows​(java.util.Map row)
      Creates a Rows with a single Row with keys/columns equal to row.getKeySet()
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(Rows.Row row)
      Adds the key/values from row as a new Row.
      boolean addAll​(int index, java.util.Collection<? extends Rows.Row> rows)
      Calls #addRow(int, Map) for each Row in rows
      boolean addAll​(java.util.Collection<? extends Rows.Row> rows)
      Calls #addRow(Map) for each Row in rows
      int addKey​(java.lang.String key)
      Adds a key/column for each Row at the end of the iteration order.
      Rows.Row addRow()
      Adds a new empty Row to the end of the list.
      Rows.Row addRow​(int index, java.lang.Object[] values)
      Adds values as the new indexth Row.
      Rows.Row addRow​(int index, java.util.List values)
      Adds values as the new indexth Row.
      Rows.Row addRow​(int index, java.util.Map map)
      Insert key/values from map as the new indexth Row.
      Rows.Row addRow​(java.lang.Object[] values)
      Adds valuesas a new Row to the end of the list.
      Rows.Row addRow​(java.util.List values)
      Adds valuesas a new Row to the end of the list.
      Rows.Row addRow​(java.util.Map map)
      Adds key/values from map to a new Row.
      java.util.List<java.lang.String> keyList()  
      java.util.Set<java.lang.String> keySet()  
      void put​(java.lang.Object value)
      Adds value to the end of lastRow.
      void put​(java.lang.String key, java.lang.Object value)
      Sets key/value on lastRow.
      • Methods inherited from class java.util.ArrayList

        add, clear, clone, contains, ensureCapacity, equals, forEach, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeIf, removeRange, replaceAll, retainAll, set, size, sort, spliterator, subList, toArray, toArray, trimToSize
      • Methods inherited from class java.util.AbstractCollection

        containsAll, toString
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, stream, toArray
      • Methods inherited from interface java.util.List

        containsAll
    • Constructor Detail

      • Rows

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

        public Rows​(java.util.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​(java.lang.String[] keys)
        Creates a Rows with keys/columns equal to keys
        Parameters:
        keys - the column names
      • Rows

        public Rows​(java.util.List<java.lang.String> keys)
        Creates a Rows with keys/columns equal to keys
        Parameters:
        keys - the column keys
    • Method Detail

      • keyList

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

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

        public int addKey​(java.lang.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​(java.util.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()
      • addRow

        public Rows.Row addRow​(int index,
                               java.util.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(int, Object[])
      • addRow

        public Rows.Row addRow​(java.util.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(int, Object[])
      • addRow

        public Rows.Row addRow​(int index,
                               java.util.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(int, Object[])
      • addRow

        public Rows.Row addRow​(java.lang.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(int, Object[])
      • addRow

        public Rows.Row addRow​(int index,
                               java.lang.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:
        addRow(int, Object[])
      • put

        public void put​(java.lang.String key,
                        java.lang.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​(java.lang.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 java.util.Collection<Rows.Row>
        Specified by:
        add in interface java.util.List<Rows.Row>
        Overrides:
        add in class java.util.ArrayList<Rows.Row>
        Parameters:
        row - a map containing the key/values to add
        See Also:
        addRow(Map)
      • addAll

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

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