com.devnullsoftware.objlist
Class Objlist

java.lang.Object
  |
  +--com.devnullsoftware.objlist.Objlist

public class Objlist
extends java.lang.Object
implements java.io.Serializable

A growable List class that stores Objects and allows searches on both primary and/or secondary indices.

See Also:
Serialized Form

Field Summary
static boolean debug
          Debug flag.
(package private) static long serialVersionUID
           
 
Constructor Summary
Objlist()
          Creates a new Objlist with a default size of 100.
Objlist(java.lang.Object[] from)
          Creates a new Objlist copied from the array passed in.
Objlist(Objlist from)
          Creates a new Objlist copied from the Objlist passed in.
 
Method Summary
 java.lang.Object add(java.lang.Object obj)
          Adds a new Object to the list.
 void delete(java.lang.String id)
          Deletes an Object from the list.
protected  void deleteCurrentIndex()
          Deletes the object that is the current index.
 boolean deleteObj(java.lang.Object obj)
          Deletes an Object from the list.
 java.lang.Object find(java.lang.String id)
          Finds the Object identified by the string passed in.
 java.lang.Object find(java.lang.String key, java.lang.String indexName)
          Finds the object using the secondary index identified by the string passed in.
 int findIndex(java.lang.Object obj)
          Finds the Index number associated with a particular object.
 java.lang.Object first()
          Retrieves the first Object from the Objlist.
 int firstIndex()
          Gives the first index number in the list that contains a valid Object.
 int getIndex()
          Gets the current Index number.
 int getNumItems()
          Gets the number of items currently stored in the list.
 java.lang.Object index(int Index)
          Retrieves Object at the index given.
 java.lang.Object last()
          Retrieves the last Object stored in the Objlist.
 int lastIndex()
          Gives the last index number in the list that stores a valid Object.
 java.lang.Object next()
          Retrieves the next Object from the list.
 java.lang.Object next(java.lang.Object obj)
          Given an object, finds it in the list and returns the Object that comes after it.
 java.lang.String[] orderedStringArray()
          Converts the Objects in the list to Strings and puts them in an array of strings.
 java.lang.Object overwrite(java.lang.Object obj)
          Adds a new Object to the list.
 java.lang.Object prev()
          Retrieves the previous Object from the list.
 java.lang.Object prev(java.lang.Object obj)
          Given an object, finds it in the list and returns the Object that comes before it.
protected  void setIndex(int newIndex)
          Sets the index to the value given.
 java.lang.String[] stringArray()
          Converts the Objects in the list to Strings and puts them in an array of strings.
 java.lang.String[] stringArrayKeys()
          Converts the Objects in the list to Strings and puts them in an array of strings.
 void updateAllIndices(java.lang.Object obj)
          updates all secondary indices for an Object.
 void updateIndex(java.lang.Object obj, java.lang.String indexName, java.lang.String oldKey, java.lang.String newKey)
          updates secondary index for an Object.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

static final long serialVersionUID

debug

public static boolean debug
Debug flag. If set to true, debugging statements will be output.
Constructor Detail

Objlist

public Objlist()
Creates a new Objlist with a default size of 100.

Objlist

public Objlist(Objlist from)
Creates a new Objlist copied from the Objlist passed in. Useful for creating list iterators.
Parameters:
from - Objlist to be copied.

Objlist

public Objlist(java.lang.Object[] from)
Creates a new Objlist copied from the array passed in.
Parameters:
from - array to be copied.
Method Detail

getIndex

public int getIndex()
Gets the current Index number.

getNumItems

public int getNumItems()
Gets the number of items currently stored in the list.

setIndex

protected void setIndex(int newIndex)
Sets the index to the value given. Dangerous to rely on this since other internal routines in this class will change the index without warning.
Parameters:
newIndex - the new Index # to set the list to.

stringArray

public java.lang.String[] stringArray()
Converts the Objects in the list to Strings and puts them in an array of strings. Only the "id" of the Objects are returned (i.e. the output of the Object.toString() function). Not guaranteed to come out in any particular order.

stringArrayKeys

public java.lang.String[] stringArrayKeys()
Converts the Objects in the list to Strings and puts them in an array of strings. Only the "id" of the Objects are returned (i.e. the output of the Object.toString() function). Not guaranteed to come out in any particular order.

orderedStringArray

public java.lang.String[] orderedStringArray()
Converts the Objects in the list to Strings and puts them in an array of strings. Only the "id" of the Objects are returned (i.e. the output of the Object.toString() function). The order of the array will be the order in which the objects were put in.

add

public java.lang.Object add(java.lang.Object obj)
                     throws java.lang.NullPointerException,
                            java.lang.NoSuchFieldException,
                            java.lang.IllegalAccessException
Adds a new Object to the list. If the Object implements the IndexedData interface, it will poll the object for it's secondary indexes and add them as well.
Parameters:
obj - Object to be put on the list.
Returns:
same Object that was passed in.
Throws:
java.lang.NullPointerException - if obj is null.

overwrite

public java.lang.Object overwrite(java.lang.Object obj)
Adds a new Object to the list. If an object that is similar to the added object is already in the list, it will be overwritten by the new data.
Parameters:
obj - Object to be put on the list.
Returns:
same Object that was passed in.

updateIndex

public void updateIndex(java.lang.Object obj,
                        java.lang.String indexName,
                        java.lang.String oldKey,
                        java.lang.String newKey)
                 throws IndexesNotSupportedException,
                        java.lang.NullPointerException,
                        java.lang.IllegalArgumentException,
                        java.lang.NoSuchFieldException,
                        java.lang.IllegalAccessException
updates secondary index for an Object. This is necessary if the objects value which is used as a secondary index lookups changes.
Parameters:
obj - Object which lists need to be updated for.
indexName - index which is being updated.
String - oldKey the old value of the field which was used as a key
String - newKey the new value of the field which will be used as a key
Throws:
IndexesNotSupportedException - if the object does not implement the IndexedData interface
java.lang.NullPointerException - if the object passed in is null
java.lang.IllegalArgumentException - if the object is not already in the list

updateAllIndices

public void updateAllIndices(java.lang.Object obj)
                      throws IndexesNotSupportedException,
                             java.lang.NullPointerException,
                             java.lang.IllegalArgumentException,
                             java.lang.IllegalAccessException
updates all secondary indices for an Object. This is necessary if the objects values which are used as secondary index lookups change.
Parameters:
obj - Object which lists need to be updated for
Throws:
IndexesNotSupportedException - if the object does not implement the IndexedData interface
java.lang.NullPointerException - if the object passed in is null
java.lang.IllegalArgumentException - if the object is not already in the list

delete

public void delete(java.lang.String id)
Deletes an Object from the list.
Parameters:
id - The id of the Object to be deleted. In the case of Objects that are not strings, the id is the result of the Object.toString() method.

deleteCurrentIndex

protected void deleteCurrentIndex()
Deletes the object that is the current index. Very dangerous to use externally since the index is rarely guaranteed to be set to a specific value and can change internally without notice.

deleteObj

public boolean deleteObj(java.lang.Object obj)
Deletes an Object from the list.
Parameters:
obj - The Object to be deleted from the list. Note that this does not destroy the Object, just the list's reference to it.

index

public java.lang.Object index(int Index)
Retrieves Object at the index given.
Parameters:
Index - the index number to retrieve the Object from
Returns:
the Object asked for, or null if no Object stored at that index location.

first

public java.lang.Object first()
Retrieves the first Object from the Objlist.
Returns:
Object or null if list is empty.

firstIndex

public int firstIndex()
Gives the first index number in the list that contains a valid Object.
Returns:
number or -1 if list is empty.

last

public java.lang.Object last()
Retrieves the last Object stored in the Objlist.
Returns:
Object or null if list is empty.

lastIndex

public int lastIndex()
Gives the last index number in the list that stores a valid Object.
Returns:
number or -1 if list is empty.

next

public java.lang.Object next()
Retrieves the next Object from the list. Uses the index number set by routines in this class and is not necessarily guaranteed to be what the user thinks. Used mainly for iterators. Also increments the index itself in preparation for the next call to this routine.
Returns:
Object or null if there is no object at or past current index.

next

public java.lang.Object next(java.lang.Object obj)
Given an object, finds it in the list and returns the Object that comes after it.
Parameters:
obj - the Object to find in the list.
Returns:
Object or null if the Object given was the last in the list or not found.

prev

public java.lang.Object prev()
Retrieves the previous Object from the list. Uses the index number set by routines in this class and is not necessarily guaranteed to be what the user thinks. Used mainly for iterators. Also decrements the index itself in preparation for the next call to this routine.
Returns:
Object or null if there is no object previous to the current index.

prev

public java.lang.Object prev(java.lang.Object obj)
Given an object, finds it in the list and returns the Object that comes before it.
Parameters:
obj - the Object to find in the list.
Returns:
Object or null if the Object given was the first in the list or not found.

find

public java.lang.Object find(java.lang.String id)
Finds the Object identified by the string passed in.
Parameters:
id - The id of the Object to be deleted. In the case of Objects that are not strings, the id is the result of the Object.toString() method.
Returns:
Object or null if the Object is not found in the list.

find

public java.lang.Object find(java.lang.String key,
                             java.lang.String indexName)
                      throws java.lang.NullPointerException,
                             java.lang.IllegalArgumentException
Finds the object using the secondary index identified by the string passed in.
Parameters:
key - the secondary key used to look up the item.
indexName - the name of the secondary index to use for looking up the item.
Returns:
Object or null if the Object was not found in the list.
Throws:
java.lang.NullPointerException - if key or indexName is null.
java.lang.IllegalArgumentException - if the Objects in this Objlist do not implement the IndexedData interface.

findIndex

public int findIndex(java.lang.Object obj)
Finds the Index number associated with a particular object.
Parameters:
obj - The Object to search for.
Returns:
number or -1 if the Object is not found in the list.