org.knowceans.util
Class KeyTableList

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.ArrayList<TableList.Fields>
              extended by org.knowceans.util.TableList
                  extended by org.knowceans.util.KeyTableList
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<TableList.Fields>, java.util.Collection<TableList.Fields>, java.util.List<TableList.Fields>, java.util.RandomAccess

public class KeyTableList
extends TableList

KeyTableList is a table list that allows to define indices on fields called keys. Use this implementation if frequent lookups are necessary without reordering the list and using binarySearch. This implementation is thought to provide quick access to larger table lists. It also allows to search for elements using wildcards and regular expressions.

Important note: The class is designed to grow the list, add key maps and then possibly add elements and find elements, but manipulation of the elements via the get method result in undefined behaviour because the key maps become inconsistent. Use the set method and replace complete list elements (fields). Further, removal and manipulation via set are expensive because of the reverse lookup in the key lists.

TODO: Check why iteration is actually faster than map.

TODO: To retain consistent behaviour, implement a transaction that stores the old values to look them up later in the maps and replace them with new values.

This class re-enacts much of a table in a relational (or object-relational) database.

TODO: merge with JoSQL to increase scalability.

Author:
gregor heinrich
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class org.knowceans.util.TableList
TableList.FieldBetween, TableList.FieldComparator, TableList.FieldEquals, TableList.FieldGreaterThan, TableList.FieldLessThan, TableList.FieldRegexFind, TableList.Fields, TableList.Filter, TableList.SingleFieldFilter
 
Constructor Summary
KeyTableList()
           
KeyTableList(int initialCapacity)
           
KeyTableList(java.util.List<java.lang.String> fields)
           
KeyTableList(java.util.List<TableList.Fields> list, java.util.List<java.lang.String> fields)
           
KeyTableList(TableList list)
           
 
Method Summary
 void add(int index, TableList.Fields element)
           
 boolean add(TableList.Fields f)
           
 boolean addAll(java.util.Collection<? extends TableList.Fields> c)
           
 boolean addAll(int index, java.util.Collection<? extends TableList.Fields> c)
           
 boolean contains(java.lang.String field, java.lang.Object elem)
          Checks whether the element is contained in this list, based on the equals method.
 boolean containsAll(java.lang.String field, java.util.Collection<?> c)
          Checks whether all of the elements of c are contained in the field within the list.
 int indexOf(java.lang.String field, java.lang.Object elem)
          Finds the first index of the list element with elem as key field.
 int[] indicesOf(java.lang.String field, java.lang.Object elem)
          Finds all indices of the list elements with elem as key field.
 TableList indicesOfRegex(java.lang.String keyfield, java.lang.String regex)
          Find all rows that match the string field as a regular expression.
 int lastIndexOf(java.lang.String field, java.lang.Object elem)
          Finds the last index of the list element with elem as key field.
static void main(java.lang.String[] args)
           
 TableList.Fields remove(int index)
           
 boolean removeAll(java.util.Collection<?> c)
           
 TableList.Fields set(int index, TableList.Fields element)
          Set the fields at the index and updates the keys.
 void set(java.lang.String field, int index, java.lang.Object value)
          Set the field at the index with the value, updating the corresponding key if necessary.
 void setAsKey(java.lang.String field)
          Adds an existing field to the table list.
 
Methods inherited from class org.knowceans.util.TableList
addIndexList, addIndexList, addList, addMap, binarySearch, binarySearch, filter, get, get, getField, getField, getFields, getList, getList, getMap, getSubList, indexOf, indicesOf, lastIndexOf, load, removeList, save, set, sort, sort, split, toArray
 
Methods inherited from class java.util.ArrayList
clear, clone, contains, ensureCapacity, get, indexOf, isEmpty, lastIndexOf, remove, size, toArray, toArray, trimToSize
 
Methods inherited from class java.util.AbstractList
equals, hashCode, iterator, listIterator, listIterator, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, retainAll, toString
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, equals, hashCode, iterator, listIterator, listIterator, retainAll, subList
 

Constructor Detail

KeyTableList

public KeyTableList()

KeyTableList

public KeyTableList(int initialCapacity)
Parameters:
initialCapacity -

KeyTableList

public KeyTableList(java.util.List<TableList.Fields> list,
                    java.util.List<java.lang.String> fields)
Parameters:
list -
fields -

KeyTableList

public KeyTableList(java.util.List<java.lang.String> fields)
Parameters:
fields -

KeyTableList

public KeyTableList(TableList list)
Parameters:
list -
Method Detail

main

public static void main(java.lang.String[] args)

setAsKey

public void setAsKey(java.lang.String field)
Adds an existing field to the table list. By this, a hash multi map is created that maps the field values to the Fields objects that the actual list consists of.

Parameters:
field -

add

public boolean add(TableList.Fields f)
Specified by:
add in interface java.util.Collection<TableList.Fields>
Specified by:
add in interface java.util.List<TableList.Fields>
Overrides:
add in class java.util.ArrayList<TableList.Fields>

add

public void add(int index,
                TableList.Fields element)
Specified by:
add in interface java.util.List<TableList.Fields>
Overrides:
add in class java.util.ArrayList<TableList.Fields>

addAll

public boolean addAll(java.util.Collection<? extends TableList.Fields> c)
Specified by:
addAll in interface java.util.Collection<TableList.Fields>
Specified by:
addAll in interface java.util.List<TableList.Fields>
Overrides:
addAll in class java.util.ArrayList<TableList.Fields>

addAll

public boolean addAll(int index,
                      java.util.Collection<? extends TableList.Fields> c)
Specified by:
addAll in interface java.util.List<TableList.Fields>
Overrides:
addAll in class java.util.ArrayList<TableList.Fields>

contains

public boolean contains(java.lang.String field,
                        java.lang.Object elem)
Checks whether the element is contained in this list, based on the equals method. This implementation uses the key maps where possible, if no key is available, the list is iterated. Therefore use binarySearch or a key field for efficiency.

Parameters:
field -
elem -
Returns:
true if a field with the value elem is contained in the list.

containsAll

public boolean containsAll(java.lang.String field,
                           java.util.Collection<?> c)
Checks whether all of the elements of c are contained in the field within the list.

Parameters:
field -
c -
Returns:

indexOf

public int indexOf(java.lang.String field,
                   java.lang.Object elem)
Finds the first index of the list element with elem as key field. This implementation uses the key maps where possible, if no key is available, the list is iterated. Therefore use binarySearch or a key field for efficiency.

Overrides:
indexOf in class TableList
Parameters:
field -
elem -
Returns:

lastIndexOf

public int lastIndexOf(java.lang.String field,
                       java.lang.Object elem)
Finds the last index of the list element with elem as key field. This implementation uses the key maps where possible, if no key is available, the list is iterated. Therefore use binarySearch or a key field for efficiency.

Overrides:
lastIndexOf in class TableList
Parameters:
field -
elem -
Returns:

indicesOf

public int[] indicesOf(java.lang.String field,
                       java.lang.Object elem)
Finds all indices of the list elements with elem as key field. This implementation uses the key maps where possible, if no key is available, the list is iterated. Therefore use binarySearch or a key field for efficiency.

Overrides:
indicesOf in class TableList
Parameters:
field -
elem -
Returns:
an array of indices.

indicesOfRegex

public TableList indicesOfRegex(java.lang.String keyfield,
                                java.lang.String regex)
Find all rows that match the string field as a regular expression. This method only applies to fields that are indexed using a key and whose type is String. This method is expensive because it iterates through the complete key map. (This could be done on any fields if implemented like in HashMultiMap)

Parameters:
keyfield -
regex -
Returns:
a table list with the matching entries (no key maps contained).

remove

public TableList.Fields remove(int index)
Specified by:
remove in interface java.util.List<TableList.Fields>
Overrides:
remove in class java.util.ArrayList<TableList.Fields>

removeAll

public boolean removeAll(java.util.Collection<?> c)
Specified by:
removeAll in interface java.util.Collection<TableList.Fields>
Specified by:
removeAll in interface java.util.List<TableList.Fields>
Overrides:
removeAll in class java.util.AbstractCollection<TableList.Fields>

set

public TableList.Fields set(int index,
                            TableList.Fields element)
Set the fields at the index and updates the keys.

Specified by:
set in interface java.util.List<TableList.Fields>
Overrides:
set in class java.util.ArrayList<TableList.Fields>
Parameters:
index -
element -
Returns:

set

public void set(java.lang.String field,
                int index,
                java.lang.Object value)
Set the field at the index with the value, updating the corresponding key if necessary.

Overrides:
set in class TableList
Parameters:
field -
index -
value -