public class GridPkgFactory
extends java.lang.Object
GridPkgFactory
class provides a set of static methods
for constructing grids and the objects they contain, and for keeping
track of what types of grids or grid objects are available to a
specific application.
The GridPkgFactory
class is based on the
College Board's MBSFactory
class,
as allowed by the GNU General Public License.
Modifier and Type | Class and Description |
---|---|
protected static class |
GridPkgFactory.ClassCategory |
Constructor and Description |
---|
GridPkgFactory() |
Modifier and Type | Method and Description |
---|---|
static void |
addBoundedGridClassNames(java.lang.String[] classNames)
Adds bounded grid classes to the set maintained by the factory.
|
protected static void |
addClassesToFactory(java.lang.String[] classNames,
GridPkgFactory.ClassCategory whichCategory)
Helper to add new classes to the factory.
|
protected static void |
addClassesToFactory(java.lang.String[] classNames,
java.lang.String whichCategory)
Helper to add new classes to the factory.
|
static void |
addGridObjClassNames(java.lang.String[] classNames)
Adds grid object classes to the set maintained by the factory.
|
static void |
addUnboundedGridClassNames(java.lang.String[] classNames)
Adds unbounded grid classes to the set maintained by the factory.
|
static java.util.Set<java.lang.Class> |
boundedGridClasses()
Returns the set of bounded grid classes known to the factory.
|
static Grid |
constructGrid(java.lang.Class cls)
Creates an instance of a Grid using the default constructor
of the given class.
|
static Grid |
constructGrid(java.lang.Class cls,
int numRows,
int numCols)
Creates an instance of a Grid using the 2-argument constructor
of the given class.
|
static java.lang.Object |
constructGridObject(java.lang.Class cls,
Grid grid,
Location loc)
Creates an instance of the given grid object class using either a
two-argument constructor that takes a grid and a location or a
default constructor followed by a call to add the object to
the given grid at the given location.
|
static java.lang.Object |
constructGridObject(java.lang.Class cls,
Grid grid,
Location loc,
Direction dir)
Creates an instance of the given grid object class using a
three-argument constructor that takes a grid, a location,
and a direction.
|
static java.lang.Object |
constructGridObject(java.lang.Class cls,
Grid grid,
Location loc,
Direction dir,
java.awt.Color color)
Creates an instance of the given grid object class using a
four-argument constructor that takes a grid, a location,
a direction, and a color.
|
static java.lang.Object |
constructObject(java.lang.Class<?> cls,
java.lang.Class<?>[] parameterTypes,
java.lang.Object[] parameters)
Creates an object of the given class.
|
static java.lang.Class<? extends Grid> |
getDefaultBoundedGridClass()
Returns the class that serves as the default bounded grid class.
|
static java.lang.Class<? extends Grid> |
getDefaultUnboundedGridClass()
Returns the class that serves as the default bounded grid class.
|
static java.util.Set<java.lang.Class> |
gridObjectClasses()
Returns the set of grid object classes known to the factory.
|
protected static boolean |
hasCorrectCtor(java.lang.Class<?> cls,
java.lang.Class<?> requiredCls,
java.lang.Class<?>[] ctorParameters)
Verifies that a class has the required constructor and
is properly assignable.
|
static boolean |
hasFourArgCtor(java.lang.Class<?> cls)
Reports wehther a given grid object class has an accessible
four-arg constructor that takes Grid, Location, Direction,
and Color.
|
static boolean |
isValidGridClass(java.lang.Class<?> cls,
java.lang.Class<?>[] ctorParameters)
Verifies that a class has the required constructor and
is properly assignable to Grid.
|
static boolean |
isValidGridObjectClass(java.lang.Class<?> cls,
java.lang.Class<?>[] ctorParameters)
Verifies that a class has the required constructor and
is properly assignable to GridObject.
|
static void |
setDefaultBoundedGridClass(java.lang.Class<? extends Grid> cls)
Sets the class that serves as the default bounded grid class to
the given parameter.
|
static void |
setDefaultUnboundedGridClass(java.lang.Class<? extends Grid> cls)
Sets the class that serves as the default bounded grid class to
the given parameter.
|
static java.util.Set<java.lang.Class> |
unboundedGridClasses()
Returns the set of unbounded grid classes known to the factory.
|
public static java.lang.Class<? extends Grid> getDefaultBoundedGridClass()
BoundedGrid
unless it has been set
to something else by the setDefaultBoundedGrid
method.public static void setDefaultBoundedGridClass(java.lang.Class<? extends Grid> cls)
cls
must have a constructor that
takes two int
parameters representing the number of
rows and number of columns in a new grid.)cls
- public static java.lang.Class<? extends Grid> getDefaultUnboundedGridClass()
BoundedGrid
unless it has been set
to something else by the setDefaultBoundedGrid
method.public static void setDefaultUnboundedGridClass(java.lang.Class<? extends Grid> cls)
cls
must have a constructor that
takes two int
parameters representing the number of
rows and number of columns in a new grid.)cls
- public static java.lang.Object constructObject(java.lang.Class<?> cls, java.lang.Class<?>[] parameterTypes, java.lang.Object[] parameters)
cls
- class of new objectparameterTypes
- parameter types expected by constructor
for classparameters
- actual parameters to pass to constructorjava.lang.RuntimeException
- if an object of the specified class cannot
be constructed with the specified
parameterspublic static java.lang.Object constructGridObject(java.lang.Class cls, Grid grid, Location loc)
cls
- class of new grid objectgrid
- grid in which this object will resideloc
- location the object will occupyjava.lang.RuntimeException
- if a grid object of the specified class
cannot be constructed with the specified
parameterspublic static java.lang.Object constructGridObject(java.lang.Class cls, Grid grid, Location loc, Direction dir)
cls
- class of new grid objectgrid
- grid in which this object will actloc
- location the object will occupydir
- direction the object will facejava.lang.RuntimeException
- if an object of the specified class cannot
be constructed with the specified
parameterspublic static java.lang.Object constructGridObject(java.lang.Class cls, Grid grid, Location loc, Direction dir, java.awt.Color color)
cls
- class of new grid objectgrid
- grid in which this object will actloc
- location the object will occupydir
- direction the object will facecolor
- color of the objectjava.lang.RuntimeException
- if an object of the specified class cannot
be constructed with the specified
parameterspublic static Grid constructGrid(java.lang.Class cls)
cls
- class of new gridjava.lang.RuntimeException
- if an object of the specified class cannot
be constructed with no parameterspublic static Grid constructGrid(java.lang.Class cls, int numRows, int numCols)
cls
- class of new gridjava.lang.RuntimeException
- if an object of the specified class cannot
be constructed with the specified
number of rows and columnspublic static java.util.Set<java.lang.Class> gridObjectClasses()
addGridObjClassNames
method.public static java.util.Set<java.lang.Class> boundedGridClasses()
addBoundedClassNames
method.public static java.util.Set<java.lang.Class> unboundedGridClasses()
addUnboundedClassNames
method.protected static void addClassesToFactory(java.lang.String[] classNames, java.lang.String whichCategory)
classNames
- an array of class nameswhichCategory
- descriptive name for category: "bounded grid"
for any bounded grid class or "unbounded grid"
for any unbounded grid class; any other string
will be taken to indicate a grid object classprotected static void addClassesToFactory(java.lang.String[] classNames, GridPkgFactory.ClassCategory whichCategory)
classNames
- an array of class nameswhichCategory
- specifies whether the classes being added are
for bounded grids, unbounded grids, or grid
objectspublic static void addGridObjClassNames(java.lang.String[] classNames)
classNames
- an array of grid object class namespublic static void addBoundedGridClassNames(java.lang.String[] classNames)
classNames
- an array of bounded grid class namespublic static void addUnboundedGridClassNames(java.lang.String[] classNames)
classNames
- an array of unbounded grid class namespublic static boolean isValidGridClass(java.lang.Class<?> cls, java.lang.Class<?>[] ctorParameters) throws java.lang.NoSuchMethodException
java.lang.NoSuchMethodException
public static boolean isValidGridObjectClass(java.lang.Class<?> cls, java.lang.Class<?>[] ctorParameters) throws java.lang.NoSuchMethodException
java.lang.NoSuchMethodException
protected static boolean hasCorrectCtor(java.lang.Class<?> cls, java.lang.Class<?> requiredCls, java.lang.Class<?>[] ctorParameters) throws java.lang.NoSuchMethodException
java.lang.NoSuchMethodException
public static boolean hasFourArgCtor(java.lang.Class<?> cls)