org.knowceans.util
Class TableList

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
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
Direct Known Subclasses:
KeyTableList

public class TableList
extends java.util.ArrayList<TableList.Fields>

TableList handles parallel lists whose elements with same index can be accessed, sorted, filtered etc. simultaneously. Internally, each element of the list is a list on its own, representing the fields of the list. The contract is that these element lists are of equal size when manipulating single elements. Filtering operations are provided via the filter method and Filter interface as well as the indices() methods.

This class is optimised for coding rather than runtime efficiency. Particularly, manipulating the structure of the fields (columns) is expensive as it iterates through all rows. Sorting, shuffling etc. are provided by the static Collections methods. To find rows of large lists, first sort and then do binary search via the collections interface.

Author:
gregor
See Also:
Serialized Form

Nested Class Summary
 class TableList.FieldBetween
          FieldBetween checks if the field is between low and high value.
 class TableList.FieldComparator
          FieldSorter sorts fields according to a numeric field.
 class TableList.FieldEquals
          FieldEquals is an equals condition
 class TableList.FieldGreaterThan
          FieldLargerThan checks if field larger than value.
 class TableList.FieldLessThan
          FieldLessThan checks if field less than.
 class TableList.FieldRegexFind
          FieldRegexFind matches field with the regular expression.
 class TableList.Fields
          Fields extends an array list by a comparison capability over the map list.
static interface TableList.Filter
          Filter allows to filter entries by calling filter with an implementation of this interface.
 class TableList.SingleFieldFilter
          SingleFieldFilter represents the common case of filtering according to the value of one field.
 
Constructor Summary
TableList()
           
TableList(int initialCapacity)
           
TableList(java.util.List<TableList.Fields> list, java.util.List<java.lang.String> fields)
          Initialise the parallel list with an existing list.
TableList(TableList list)
          Copy constructor.
 
Method Summary
 void addIndexList(java.lang.String field)
          Add an index plus to the list.
 void addIndexList(java.lang.String field, int offset)
          Adds an index plus an offset to the list.
 void addList(java.lang.String field, java.util.List<? extends java.lang.Object> a)
          Add a list to the internal maps.
 void addMap(java.lang.String keyfield, java.lang.String valfield, java.util.Map<? extends java.lang.Object,? extends java.lang.Object> map)
          Add the keys and values of the map to this table list, in the order that the map iterator provides.
 int binarySearch(java.lang.String field, java.lang.Object key)
          Perform a binary search on the field.
 int binarySearch(java.lang.String field, java.lang.Object key, TableList.FieldComparator comp)
          Perform a binary search, specifying the condition with a Comparator.
 TableList filter(TableList.Filter filt)
          Get a sublist of this list according to the filter criterion.
 java.lang.Object get(int field, int index)
          Get one element of the list with the specified key.
 java.lang.Object get(java.lang.String field, int index)
          Get one element of the list with the specified key.
 java.lang.String getField(int field)
          Get key of field index.
 int getField(java.lang.String field)
          Get field index of key.
 java.util.List<java.lang.String> getFields()
          Get the field names of the table list.
 java.util.ArrayList<?> getList(int index)
          Get the list with the specified key.
 java.util.ArrayList<?> getList(java.lang.String field)
          Get the list with the specified key.
 void getMap(java.lang.String keyfield, java.lang.String valfield, java.util.Map map)
          Get a map representation of the keys and values fields.
 TableList getSubList(int fromIndex, int toIndex)
          Get a sublist of this list according to the indices of the current sorting, as a copy.
 int indexOf(java.lang.String field, java.lang.Object key)
          Find the first index the field matches with key.
 int indexOf(TableList.Filter filt)
          Find the first index valid for the filter.
 int[] indicesOf(java.lang.String field, java.lang.Object key)
          Find all indices that the field matches with key.
 int[] indicesOf(TableList.Filter filt)
          Find all indices that are valid for the filter.
 int lastIndexOf(java.lang.String field, java.lang.Object key)
          Find the last index the field matches with key.
 int lastIndexOf(TableList.Filter filt)
          Find the last index that is valid for the filter.
static TableList load(java.lang.String file)
          Initialise the table list from the file.
static void main(java.lang.String[] args)
           
 void removeList(java.lang.String field)
          Remove the list with key from the internal maps.
 void save(java.lang.String file)
          Save the table list to a file.
 void set(int field, int index, java.lang.Object value)
          Set the field at the index with the value.
 void set(java.lang.String field, int index, java.lang.Object value)
          Set the field at the index with the value.
 TableList sort(java.util.Comparator<TableList.Fields> comp)
          Sort the table with the specific comparator given.
 TableList sort(java.lang.String field, boolean reverse)
          Sort the table list by the specified field.
 TableList[] split(TableList.Filter filt)
          Get two sublists of this list according to the filter criterion.
 java.lang.Object[] toArray(java.lang.String field)
          Get an object array of the field.
 
Methods inherited from class java.util.ArrayList
add, add, addAll, addAll, clear, clone, contains, ensureCapacity, get, indexOf, isEmpty, lastIndexOf, remove, remove, set, 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, removeAll, 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, removeAll, retainAll, subList
 

Constructor Detail

TableList

public TableList()

TableList

public TableList(int initialCapacity)
Parameters:
initialCapacity -

TableList

public TableList(java.util.List<TableList.Fields> list,
                 java.util.List<java.lang.String> fields)
Initialise the parallel list with an existing list. The sorting key is set to 0 -- the first field.

Parameters:
list -
field -

TableList

public TableList(TableList list)
Copy constructor.

Parameters:
list -
Method Detail

main

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

load

public static TableList load(java.lang.String file)
Initialise the table list from the file.

Parameters:
file -
Throws:
java.lang.ClassNotFoundException
java.io.IOException

save

public void save(java.lang.String file)
Save the table list to a file.

Parameters:
file -

addList

public void addList(java.lang.String field,
                    java.util.List<? extends java.lang.Object> a)
Add a list to the internal maps.

Parameters:
a -

addIndexList

public void addIndexList(java.lang.String field,
                         int offset)
Adds an index plus an offset to the list. After sorting, this way the original sorting order can be tracked.

Parameters:
field -

addIndexList

public void addIndexList(java.lang.String field)
Add an index plus to the list. After sorting, this way the original sorting order can be tracked.

Parameters:
field -

addMap

public void addMap(java.lang.String keyfield,
                   java.lang.String valfield,
                   java.util.Map<? extends java.lang.Object,? extends java.lang.Object> map)
Add the keys and values of the map to this table list, in the order that the map iterator provides. This method can only be used if the list is empty or if the size of the map is exactly the size of the list or the key and value field are exactly the same as the two only fields in the map, in which case the map is added to the end of the list.

Parameters:
keyfield - name of the field for the keys
valfield - name of the field for the values.
map -

getMap

public void getMap(java.lang.String keyfield,
                   java.lang.String valfield,
                   java.util.Map map)
Get a map representation of the keys and values fields. Depending on the runtime type of the map, certain requirements need to be observed. For instance, the bijective map contract requires that keys and values are both unique. Uniqueness in maps is automatically ensured by overwriting exsting values along the iteration. Use a multi map to allow non-unique map keys.

Parameters:
keyfield -
valfield -

removeList

public void removeList(java.lang.String field)
Remove the list with key from the internal maps.

Parameters:
field -

getList

public java.util.ArrayList<?> getList(java.lang.String field)
Get the list with the specified key.

Parameters:
index -

getList

public java.util.ArrayList<?> getList(int index)
Get the list with the specified key.

Parameters:
index -

get

public java.lang.Object get(int field,
                            int index)
Get one element of the list with the specified key.

Parameters:
field -
index -
Returns:

get

public java.lang.Object get(java.lang.String field,
                            int index)
Get one element of the list with the specified key.

Parameters:
field -
index -
Returns:

set

public void set(int field,
                int index,
                java.lang.Object value)
Set the field at the index with the value.

Parameters:
field -
index -
value -

set

public void set(java.lang.String field,
                int index,
                java.lang.Object value)
Set the field at the index with the value.

Parameters:
field -
index -
value -

getSubList

public TableList getSubList(int fromIndex,
                            int toIndex)
Get a sublist of this list according to the indices of the current sorting, as a copy. The actual values are referenced, field names are copied.

Parameters:
filt -
Returns:

toArray

public java.lang.Object[] toArray(java.lang.String field)
Get an object array of the field.

Parameters:
field -
Returns:

filter

public TableList filter(TableList.Filter filt)
Get a sublist of this list according to the filter criterion. The actual values and field names are referenced.

Parameters:
filt -
Returns:

split

public TableList[] split(TableList.Filter filt)
Get two sublists of this list according to the filter criterion. The actual values and field names are referenced. The result is a 2-array with the elements that satisfy the filter condition in the 0-element, and those that don't in the 1-element.

Parameters:
filt -
Returns:

indicesOf

public int[] indicesOf(java.lang.String field,
                       java.lang.Object key)
Find all indices that the field matches with key.

Parameters:
field -
key -
Returns:

indicesOf

public int[] indicesOf(TableList.Filter filt)
Find all indices that are valid for the filter.

Parameters:
filt -
Returns:

indexOf

public int indexOf(java.lang.String field,
                   java.lang.Object key)
Find the first index the field matches with key.

Parameters:
field -
key -
Returns:

indexOf

public int indexOf(TableList.Filter filt)
Find the first index valid for the filter.

Parameters:
filt -
Returns:

lastIndexOf

public int lastIndexOf(java.lang.String field,
                       java.lang.Object key)
Find the last index the field matches with key.

Parameters:
field -
key -
Returns:

lastIndexOf

public int lastIndexOf(TableList.Filter filt)
Find the last index that is valid for the filter.

Parameters:
key -
filt -
Returns:

binarySearch

public int binarySearch(java.lang.String field,
                        java.lang.Object key)
Perform a binary search on the field.

Parameters:
field -
key -
Returns:

binarySearch

public int binarySearch(java.lang.String field,
                        java.lang.Object key,
                        TableList.FieldComparator comp)
Perform a binary search, specifying the condition with a Comparator.

Parameters:
comparator -
Returns:

sort

public TableList sort(java.lang.String field,
                      boolean reverse)
Sort the table list by the specified field. Use the Collections.sort() or sort(Comparator) method for other comparators.

Parameters:
field -
reverse -
Returns:
this

sort

public TableList sort(java.util.Comparator<TableList.Fields> comp)
Sort the table with the specific comparator given. Alternative to Collections.sort().

Parameters:
comp -
Returns:
this

getField

public int getField(java.lang.String field)
Get field index of key.

Parameters:
field -
Returns:

getField

public java.lang.String getField(int field)
Get key of field index.

Parameters:
field -
Returns:

getFields

public java.util.List<java.lang.String> getFields()
Get the field names of the table list.

Returns: