Grid

Kind of class:public class
Package:com.bourre.structures
Inherits from:none
Implements:
Author:Cédric Néhémie
Classpath:com.bourre.structures.Grid
File last modified:Monday, 24 November 2008, 11:36:47
A Grid is basically a two dimensions data structure
based on the Collection interface.

By default a Grid object is an untyped collection that
allow duplicate and null elements. You can set your own
default value instead of null by passing it to the grid
constructor.

Its also possible to restrict the type of grid elements in the constructor
as defined by the TypedContainer interface.

The Grid class don't support all the methods of the Collectioninterface. Here the list of the unsupported methods :

  • add
  • addAll
  • isEmpty

Instead of using the methods above there are several specific methods to insert data in the
grid :

  • setVal : Use it to insert value in the grid at specified coordinates
  • setContent : Use it to set the grid with the passed-in array.
  • fill : Use it to fill the grid with the same value in all cells.

See also:
  • com.bourre.collection.Collection
  • com.bourre.collection.TypedContainer

Summary


Constructor
  • Grid (size:Dimension, a:Array = null, dV:Object = null, t:Class = null)
    • Create a new grid of passed-in size.
Instance methods
  • toString : String
    • Returns the String representation of
  • contains (o:Object) : Boolean
    • Returns true if this grid contains at least
  • toArray : Array
    • Returns an array containing all the elements in this grid.
  • isEmpty : Boolean
    • A Grid object is considered as empty if and only if all its cells
  • remove (o:Object) : Boolean
    • Removes a single instance of the specified element from this
  • clear : void
    • Removes all of the elements from this collection.
  • iterator : Iterator
    • Returns an iterator over the elements in this collection.
  • removeAll (c:Collection) : Boolean
    • Removes from this grid all of its elements that are contained
  • containsAll (c:Collection) : Boolean
    • Returns true if this grid contains
  • addAll (c:Collection) : Boolean
    • The addAll method is unsupported by the Grid class.
  • addAllAt (p:Point, c:Grid) : Boolean
    • Copy the content of the passed-in grid at the specified coordinates.
  • removeAllAt (p:Point, c:Grid) : Boolean
    • Removes the content of the passed-in grid at the specified coordinates.
  • retainAll (c:Collection) : Boolean
    • Retains only the elements in this queue that are contained
  • add (o:Object) : Boolean
    • The add method is unsupported by the Grid class.
  • size : uint
    • Returns the number of elements this collection can contains.
  • matchType (o:*) : Boolean
    • Verify if the passed-in object can be inserted in the
  • isTyped : Boolean
    • Returns true if this grid perform a verification
  • getType : Class
    • Return the current type allowed in the Grid
  • isValidCollection (c:Collection) : Boolean
    • Verify that the passed-in Collection is a valid
  • isValidType (o:Object) : Boolean
    • Verify that the passed-in object type match the current
  • fill (o:Object) : void
    • Fill the current grid with the passed-in value.
  • removeAt (p:Point) : Boolean
    • Removes the value located at the passed-in coordinate.
  • isGridCoords (p:Point) : Boolean
    • Check if a Point object is a valid coordinate
  • getSize : Dimension
    • Returns the size of the grid as Dimension.
  • getCoordinates (id:uint) : Point
    • Returns a Point witch is the corresponding
  • getDefaulValue : Object
    • Returns the current default value of this grid used
  • setDefaultValue (o:Object) : Boolean
    • Defines the default value for this grid's cells content.
  • getVal (p:Point) : Object
    • Returns the element stored at the passed-in coordinate of the
  • setVal (p:Point, o:Object) : Boolean
    • Defines value of grid cell defining by passed-in Point
  • setContent (a:Array) : Boolean
    • Fill the content with an array of witch length is equal to

Constructor

Grid

public function Grid (
size:Dimension, a:Array = null, dV:Object = null, t:Class = null)

Create a new grid of passed-in size.

If a is set, and if it have the same size that the grid,
it's used to fill the collection at creation.

If dV is set, all null elements in the grid
will be replaced by dV value.

Parameters:
size:
Size of the grid.
a :
An array to fill the grid with.
dV :
The default value for null elements.
Throws:
  • {VISDOC_LINK_0}ArgumentError — Invalid size passed in Grid constructor.
  • {VISDOC_LINK_1}ClassCastException — If objects type contained in the
    passed-in array prevents them to be added into this grid
  • {VISDOC_LINK_2}IllegalArgumentException — If the passed-in array length
    doesn't match this grid size

Instance methods

add

public function add (
o:Object) : Boolean

The add method is unsupported by the Grid class.
Parameters:
o:
element whose presence in this collection is to be ensured.
#
Throws:
  • {VISDOC_LINK_0}UnsupportedOperationException — The add method of the Collection interface is unsupported by the Grid Class
Returns:
  • a if this collection changed as a result of the
    call
    #
Specified by:

addAll

public function addAll (
c:Collection) : Boolean

The addAll method is unsupported by the Grid class.
Parameters:
c:
elements to be inserted into this collection.
#
Throws:
  • {VISDOC_LINK_0}UnsupportedOperationException — The addAll method of the Collection interface is unsupported by the Grid Class
Returns:
  • ClassCastException if this collection changed as a result of the
    call
    #
Specified by:

addAllAt

public function addAllAt (
p:Point, c:Grid) : Boolean

Copy the content of the passed-in grid at the specified coordinates.
The passed-in grid is paste into this grid such the top-left coordinates
will start at the specified Point argument.
Parameters:
p:
coordinates at which copy the grid
c:
grid to copy in this grid object
Returns:
  • true if the passed-in grid have been successfully added
    at the specified coordinates in this grid
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this grid
  • {VISDOC_LINK_1}IllegalArgumentException — If the passed-in collection
    type is not the same that the current one.
  • {VISDOC_LINK_2}IndexOutOfBoundsException — If the passed-in point
    is not valid coordinates for this grid.
  • {VISDOC_LINK_3}IndexOutOfBoundsException — If the passed-in grid
    will overlap this grid when copying.

clear

public function clear (
) : void

Removes all of the elements from this collection.
This collection will not be empty after this method.

If a default value have been defined for the grid
then all cells of the grid contains that value.

Throws:
  • {VISDOC_LINK_0}Collection — if the true method is
    not supported by this collection.
    #
Specified by:

contains

public function contains (
o:Object) : Boolean

Returns true if this grid contains at least
one occurence of the specified element. Moreformally,
returns true if and only if this grid contains
at least an element e such that o === e.
Parameters:
o:
Object whose presence in this grid
is to be tested.
Returns:
  • true if this grid contains the specified
    element.
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this grid
Specified by:

containsAll

public function containsAll (
c:Collection) : Boolean

Returns true if this grid contains
all of the elements of the specified collection. If the specified
collection is also a Grid, this method returns trueif it is a subliset of this queue.

If the passed-in Collection is null the method throw a

NullPointerException error.

If the passed-in Collection type is different than the current
one the function will throw an IllegalArgumentException.
However, if the type of this grid is null,
the passed-in Collection can have any type.

The rules which govern collaboration between typed and untyped Collectionare described in the isValidCollection descrition, all rules described
there are supported by the containsAll method.

Parameters:
c:
collection to be checked for containment in this collection.
Returns:
  • true if this collection contains all of the elements
    in the specified collection
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this grid
  • {VISDOC_LINK_1}IllegalArgumentException — If the passed-in collection
    type is not the same that the current one.
Specified by:

fill

public function fill (
o:Object) : void

Fill the current grid with the passed-in value.

If the passed-in value is a "real" object (not a primitive) then
all cells contains a reference to the same object.

Parameters:
o:
Value used to fill the grid

getCoordinates

public function getCoordinates (
id:uint) : Point

Returns a Point witch is the corresponding
position of the passed-in value.
Parameters:
id:
uint to convert in a two dimension location
Returns:
  • Point corresponding location
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — the passed-in index
    is not a valid coordinates for this grid

getDefaulValue

public function getDefaulValue (
) : Object

Returns the current default value of this grid used
to replace value when removing an element.
Returns:
  • element used as default value for the grid's cells

getSize

public function getSize (

Returns the size of the grid as Dimension.

The returned Dimension is a clone of the
internal one.

Returns:
  • the dimensions of the grid as Dimension

getType

public function getType (
) : Class

Return the current type allowed in the Grid
Returns:
  • Class used to type checking.
Specified by:

getVal

public function getVal (
p:Point) : Object

Returns the element stored at the passed-in coordinate of the
grid.
Parameters:
p:
Coordinates Point in the grid
Returns:
  • Value stored at the coorespoding location or null if the passed-in coordinates is not a valid coordinates
    for this grid
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — the passed-in point
    is not a valid coordinates for this grid

isEmpty

public function isEmpty (
) : Boolean

A Grid object is considered as empty if and only if all its cells
contains null or the default value for the current Grid.
Returns:
  • true if the grid is empty, either false.
Specified by:

isGridCoords

public function isGridCoords (
p:Point) : Boolean

Check if a Point object is a valid coordinate
in the current grid.
Parameters:
p:
Point object to check
Returns:
  • true if passed-in Point is a valid
    coordinate for the current grid
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — the passed-in point
    is not a valid coordinates for this grid

isTyped

public function isTyped (
) : Boolean

Returns true if this grid perform a verification
of the type of elements.
Returns:
  • true if this grid perform a verification
    of the type of elements.
Specified by:

isValidCollection

public function isValidCollection (
c:Collection) : Boolean

Verify that the passed-in Collection is a valid
collection for use with the addAll, removeAll,

retainAll and containsAll methods.

When dealing with typed and untyped collection, the following rules apply :

  • Two typed collection, which have the same type, can collaborate each other.
  • Two untyped collection can collaborate each other.
  • An untyped collection can add, remove, retain or contains any typed collection
    of any type without throwing errors.
  • A typed collection will always fail when attempting to add, remove, retain
    or contains an untyped collection.

If the passed-in Collection is null the method throw a

NullPointerException error.

Parameters:
c:
Collection to verify
Returns:
  • boolean true if the collection is valid,
    either false
Throws:
  • {VISDOC_LINK_0}NullPointerException — If the passed-in collection
    is null
  • {VISDOC_LINK_1}IllegalArgumentException — If the passed-in collection
    type is not the same that the current one

isValidType

public function isValidType (
o:Object) : Boolean

Verify that the passed-in object type match the current

Grid element's type.

In the case that the grid is untyped the function
will always returns true.

In the case that the object's type prevents it to be added
as element for this grid the method will throw
a ClassCastException.

Parameters:
o:
Object to verify
Returns:
  • true if the object is elligible for this
    grid object, either false
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this grid

iterator

public function iterator (

Returns an iterator over the elements in this collection. Iterations
are performed in the following order : columns first, rows after.

Result for a 2x2 grid :

  • Cell 0, 0
  • Cell 1, 0
  • Cell 0, 1
  • Cell 1, 1

Returns:
  • an Iterator over the elements in this collection.
Specified by:

matchType

public function matchType (
o:*) : Boolean

Verify if the passed-in object can be inserted in the
current Grid.
Parameters:
o:
Object to verify
Returns:
  • true if the object can be inserted in
    the Grid, either false.

remove

public function remove (
o:Object) : Boolean

Removes a single instance of the specified element from this
grid, if this grid contains one or more such elements.
Returns true if this grid contained the specified
element (or equivalently, if this collection changed as a result
of the call).

In order to remove all occurences of an element you have to call
the remove method as long as the grid contains an
occurrence of the passed-in element. Typically, the construct to
remove all occurrences of an element should look like that :


while( grid.contains( element ) ) grid.remove( element );

If the current grid object is typed and if the passed-in object's
type prevents it to be added (and then removed) in this grid,
the function throws a ClassCastException.

The Grid introduce a specific behavior for its default
value, if the passed-in element is the default value for this grid
the function return null and isnt't modified as result
of the call.

Parameters:
o:
object to be removed from this grid,
if present.
Returns:
  • true if the grid contained the
    specified element.
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this grid
Specified by:

removeAll

public function removeAll (
c:Collection) : Boolean

Removes from this grid all of its elements that are contained
in the specified collection (optional operation). At the end
of the call there's no occurences of any elements contained
in the passed-in collection.

The only values which cannot be removed by a call to removeAllis the default value for this grid. It result that all cells which contained
a value also contained in the passed-in collection are filled with the grid's
default value.

The rules which govern collaboration between typed and untyped Collectionare described in the isValidCollection descrition, all rules described
there are supported by the removeAll method.

Parameters:
c:
Collection that defines which elements will be
removed from this grid.
Returns:
  • true if this grid changed as a result
    of the call.
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this grid
  • {VISDOC_LINK_1}IllegalArgumentException — If the passed-in collection
    type is not the same that the current one.
Specified by:

removeAllAt

public function removeAllAt (
p:Point, c:Grid) : Boolean

Removes the content of the passed-in grid at the specified coordinates.
The passed-in grid is removed from this grid such the top-left coordinates
will start at the specified Point argument.

The content of the passed-in grid is only removed in the bounds of the grid
in this grid space coordinates. Values which are also stored in the passed-in
grid but whose coordinates aren't in the bounds of the operation aren't remove.

Parameters:
p:
coordinates at which remove the grid
c:
grid to remove from this grid object
Returns:
  • true if the passed-in grid have been successfully removed
    at the specified coordinates in this grid
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this grid
  • {VISDOC_LINK_1}IllegalArgumentException — If the passed-in collection
    type is not the same that the current one.
  • {VISDOC_LINK_2}IndexOutOfBoundsException — If the passed-in point
    is not valid coordinates for this grid.
  • {VISDOC_LINK_3}IndexOutOfBoundsException — If the passed-in grid
    will overlap this grid when removing.

removeAt

public function removeAt (
p:Point) : Boolean

Removes the value located at the passed-in coordinate.

If the grid changed after the call the function returns

true. If the passed-in Point isn't
a valid coordinate for this grid the function failed and return

false.

If a default value is set, the cell contains that value instead
of null after the call.

Parameters:
p:
Point position of the value to remove
Returns:
  • true if the grid changed as result of the call
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — the passed-in point
    is not a valid coordinates for this grid

retainAll

public function retainAll (
c:Collection) : Boolean

Retains only the elements in this queue that are contained
in the specified collection (optional operation). In other words,
removes from this queue all of its elements that are not
contained in the specified collection.

The only values which cannot be removed by a call to retainAllis the default value for this grid. It result that all cells which contained
a value that are not contained in the passed-in collection are filled with
the grid's default value.

The rules which govern collaboration between typed and untyped Collectionare described in the isValidCollection descrition, all rules described
there are supported by the retainAll method.

Parameters:
c:
elements to be retained in this collection.
Returns:
  • true if this collection changed as a result of the
    call
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this grid
  • {VISDOC_LINK_1}IllegalArgumentException — If the passed-in collection
    type is not the same that the current one.
Specified by:

setContent

public function setContent (
a:Array) : Boolean

Fill the content with an array of witch length is equal to
the grid size().

The call return true only if the Gridchanged as results of the call.

Parameters:
a:
Array to fill the Grid
Returns:
  • true if the Grid changed as results of the call
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this grid
  • {VISDOC_LINK_1}IllegalArgumentException — If the passed-in array length
    doesn't match this grid size

setDefaultValue

public function setDefaultValue (
o:Object) : Boolean

Defines the default value for this grid's cells content.
When changing the default value of a grid, the cells which
previously contains the old default value will contains the
new one at the end of the call.
Parameters:
o:
new default value for this grid's cells
Returns:
  • true if the grid have change as result
    of the call
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this grid

setVal

public function setVal (
p:Point, o:Object) : Boolean

Defines value of grid cell defining by passed-in Point
coordinate.

The call return true only if the Gridchanged as results of the call.

Parameters:
p:
Point position of the cell
o:
value to store in the grid
Returns:
  • true if the Grid changed as results of the call
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — the passed-in point
    is not a valid coordinates for this grid
  • {VISDOC_LINK_1}ClassCastException — If the object's type
    prevents it to be added into this grid

size

public function size (
) : uint

Returns the number of elements this collection can contains.
Returns:
  • the number of elements this collection can contains.
Specified by:

toArray

public function toArray (
) : Array

Returns an array containing all the elements in this grid.
Obeys the general contract of the Collection.toArraymethod.
Returns:
  • Array containing all of the elements
    in this grid.
Specified by:

toString

public function toString (
) : String

Returns the String representation of
this object.

The function return a string like

com.bourre.structures::Grid<String>for a typed collection. The string between the <
and > is the name of the type of the collection's
elements. If the collection is an untyped collection
the function will simply return the result of the

PixlibStringifier.stringify call.

Returns:
  • String representation of
    this object.