com.partnersoft.data
Class GridContainer<T>

java.lang.Object
  extended by com.partnersoft.data.GridContainer<T>
All Implemented Interfaces:
Coggable

public class GridContainer<T>
extends java.lang.Object
implements Coggable

Grid based container. Contents are stored in Lists of Lists.

Gets and Sets for columns,rows, or cells should be constant time.

Copyright 2009 Partner Software, Inc.

Version:
$Id$
Author:
Rich Stepanski

Constructor Summary
GridContainer(Cog node)
          Creates a new GridContainer matching contents of passed Cog.
GridContainer(GridContainer<T> nother)
          Copy constructor for GridContainer.
GridContainer(int width, int height)
          Creates a new GridContainer of the specified size.
 
Method Summary
 void clearContentsInColumn(int column)
          Deletes contents of an entire column.
 void clearContentsInRow(int row)
          Deletes contents of an entire row.
 boolean contains(T item)
          Searches all columns for parameter.
 T getContentsAt(int column, int row)
          Gets content from single cell.
 java.util.List<T> getContentsInColumn(int col)
          Returns a List of elements in a given column.
 java.util.List<T> getContentsInRow(int row)
          Returns a List of elements in a given row.
 int getHeight()
          Returns the number of cells high this container will allow.
 int getWidth()
          Returns the number of cells wide this container will allow.
 void insertColumnAt(int column)
          Inserts a column at given index.
 void insertRowAt(int row)
          Inserts a row at given index.
 java.util.List<T> listContents()
          Returns a list of all items contained in this grid.
 T remove(T item)
          Searches all columns for parameter.
 java.util.List<T> removeColumnAt(int column)
          Removes column and column contents at specified row.
 T removeContentsAt(int column, int row)
          Removes and returns single cell contents.
 java.util.List<T> removeRowAt(int row)
          Removes row and row contents at specified row.
 void setContentsAt(T storage, int column, int row)
          Sets content in cell.
 void setContentsInColumn(java.util.List<T> entireColumnInOrder, int column)
          Sets contents for a given column.
 void setContentsInRow(java.util.List<T> entireRowInOrder, int row)
          Sets contents for a given row.
 void setHeight(int height)
          Sets the number of cells high this container should allow.
 void setWidth(int width)
          Sets the number of cells wide this container should allow.
 Cog toCog()
          Returns the complete internal state of this object in the form of a Cog.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GridContainer

public GridContainer(int width,
                     int height)
Creates a new GridContainer of the specified size.


GridContainer

public GridContainer(Cog node)
Creates a new GridContainer matching contents of passed Cog.

Parameters:
node -

GridContainer

public GridContainer(GridContainer<T> nother)
Copy constructor for GridContainer. Creates a new GridContainer from argument. Does not copy contained objects, only lists.

Method Detail

getContentsAt

public T getContentsAt(int column,
                       int row)
Gets content from single cell. Returns null if nothing is found or request column,row is beyond size of container.

Parameters:
column - - column to pull from. column 0 is first column.
row - - row to pull from. row 0 is first row.
Throws:
java.lang.IllegalStateException - if column or row is < 0 or >= width,height.

setContentsAt

public void setContentsAt(T storage,
                          int column,
                          int row)
Sets content in cell. Inflates grid size to fit column and row if needed.

Parameters:
storage - - Item to store.
column - - column to store in.
row - - row to store in.
Throws:
java.lang.IllegalStateException - if column or row is < 0 or >= width,height.

removeContentsAt

public T removeContentsAt(int column,
                          int row)
Removes and returns single cell contents.

Throws:
java.lang.IllegalStateException - if column or row is < 0 or >= width,height.

getContentsInRow

public java.util.List<T> getContentsInRow(int row)
Returns a List of elements in a given row.

Returns:
List of elements in requested row. Empty cells are filled with null.
Throws:
java.lang.IllegalStateException - if row is < 0 or >= width.

getContentsInColumn

public java.util.List<T> getContentsInColumn(int col)
Returns a List of elements in a given column.

Returns:
List of elements in requested column. Empty cells are filled with null.
Throws:
java.lang.IllegalStateException - if column is < 0 or >= height.

contains

public boolean contains(T item)
Searches all columns for parameter. Returns true if it is found, false otherwise. Uses T.equals() for comparison.

Parameters:
item - to search for.
Returns:
true if item's .equals() returns true on any contents. False otherwise.

listContents

public java.util.List<T> listContents()
Returns a list of all items contained in this grid. Nulls are not included. List is each column's contents from index 0 to height from column 0 to column width.

Returns:
- List containing all items, nulls are not included.

remove

public T remove(T item)
Searches all columns for parameter. Sets location to null and returns item. Returns null if item is not found. Uses T.equals() for comparison.

Parameters:
item - to remove.
Returns:
Passed item if found. Null otherwise.

setContentsInRow

public void setContentsInRow(java.util.List<T> entireRowInOrder,
                             int row)
Sets contents for a given row.

Parameters:
entireRowInOrder - - list of items to set for a given row.
row - - index of row to set.
Throws:
java.lang.IllegalStateException - if row is < 0 or >= height.

setContentsInColumn

public void setContentsInColumn(java.util.List<T> entireColumnInOrder,
                                int column)
Sets contents for a given column.

Parameters:
entireColumnInOrder - - list of items to set for a given column.
column - - index of column to set.
Throws:
java.lang.IllegalStateException - if column is < 0 or >= width.

clearContentsInRow

public void clearContentsInRow(int row)
Deletes contents of an entire row. Does not delete the row, only the contents.

Parameters:
row - - index of row to clear.
Throws:
java.lang.IllegalStateException - if row is < 0 or >= height.

clearContentsInColumn

public void clearContentsInColumn(int column)
Deletes contents of an entire column. Ddoes not delete the column, only contents.

Parameters:
column - - index of column to clear.
Throws:
java.lang.IllegalStateException - if column is < 0 or >= width.

removeColumnAt

public java.util.List<T> removeColumnAt(int column)
Removes column and column contents at specified row. This actually removes the column from the data structure. Effectively shifting all remaining higher columns lower.

Parameters:
column - - index of column to remove.
Throws:
java.lang.IllegalStateException - if column < 0 or >= width

removeRowAt

public java.util.List<T> removeRowAt(int row)
Removes row and row contents at specified row. This actually removes the row from the data structure. Effectively shifting all remaining higher rows lower.

Parameters:
row - - index of row to remove
Throws:
java.lang.IllegalStateException - if row < 0 or row >= height.

insertRowAt

public void insertRowAt(int row)
Inserts a row at given index. Increases height by 1.

Parameters:
row -

insertColumnAt

public void insertColumnAt(int column)
Inserts a column at given index. Increases width by 1.

Parameters:
column -

setWidth

public void setWidth(int width)
Sets the number of cells wide this container should allow. Adds or removes columns as needed. Note, this could delete contents in fringe columns.

Parameters:
width - for this container.

setHeight

public void setHeight(int height)
Sets the number of cells high this container should allow. Adds or removes rows to match height. Note, this could remove data in fringe rows.

Parameters:
height - for container.

getHeight

public int getHeight()
Returns the number of cells high this container will allow.

Returns:

getWidth

public int getWidth()
Returns the number of cells wide this container will allow.

Returns:

toCog

public Cog toCog()
Description copied from interface: Coggable
Returns the complete internal state of this object in the form of a Cog.

Specified by:
toCog in interface Coggable
Returns:
Cog representing the internal state of this object