org.moyoman.module
Class ModuleConfig

java.lang.Object
  |
  +--org.moyoman.module.ModuleConfig

public class ModuleConfig
extends Object

Read and write configuration information related to modules. This class is responsible for the following types of information:

  1. The order of module types.
  2. The module names and their order for a module type and mode.
  3. The location and structure of the permenant storage of this information.


Field Summary
private  HashMap allModules
           
private  HashMap hashmaps
          The key is the mode name.
private static HelperConfigTool hct
          The HelperConfigTool object.
private static ModuleConfig mc
          Only one ModuleConfig object is created.
private  ModuleType[] orderedModuleTypes
          The ModuleType objects are ordered from first to last.
private static ServerConfig sc
          The ServerConfig object.
private static String topDir
          The top level directory of the moyoman distribution.
 
Constructor Summary
protected ModuleConfig()
          Create the ModuleConfig object.
 
Method Summary
protected  void addMode(Mode newMode, Mode oldMode)
          Add a new mode which has the same ordering of module names as the existing mode.
protected  void addModuleName(ModuleName mn)
          Add the module name at the end of the list for the given module type.
protected  void addModuleType(ModuleType mt, int index)
          Insert module type into the ordered list of modules.
protected  void cancel()
          Undo all changes made since the last call to save() or cancel().
 boolean checkModuleTypeOrder(ModuleType earlier, ModuleType later)
          Check the relationship in question.
protected  void deleteMode(String name)
          Delete the mode.
protected  void deleteModuleName(ModuleName mn)
          Delete the ModuleName from the list for the given module type.
protected  void deleteModuleType(ModuleType mt)
          Delete the ModuleType object from the list.
protected  ModuleName[] getAllModuleNames()
          Get an array of all module names, in no particular order.
protected  ModuleType[] getAllModuleTypes()
          Get an array of all module types in ascending order.
 ModuleName getFirstModuleName(Mode mode, ModuleType type)
          Get the most highly ranked ModuleName for the given mode and module type.
static ModuleConfig getModuleConfig()
          Get the singleton ModuleConfig object.
 ModuleName[] getModuleNames(Mode mode, ModuleType type)
          Get the module names for the mode and module type.
private  ModuleName[] getModuleNamesForTypeAndMode(Element elem, ModuleType mt, Properties props)
          Get the ModuleName objects in order for the module type and mode.
private  void indentLine(BufferedWriter bw, int level)
          Convenience method to write out the appropriate number of spaces.
private  void processNodes(NodeList nl, ModuleType mt, Properties props)
          This method processes the module.xml entry for a single module type.
protected  void save()
          Save the information in this object to permanent storage.
 void setModuleNameOrder(Mode mode, ModuleType type, ModuleName[] names)
          Set the module name order for a given module type and mode.
 void setModuleTypeOrder(ModuleType[] types)
          Set the mode type order.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

hashmaps

private HashMap hashmaps
The key is the mode name. The value is a HashMap object, in which the key is the ModuleType object, and the value is an array of ModuleName objects ordered from most to least preferred.


orderedModuleTypes

private ModuleType[] orderedModuleTypes
The ModuleType objects are ordered from first to last.


allModules

private HashMap allModules

topDir

private static String topDir
The top level directory of the moyoman distribution.


sc

private static ServerConfig sc
The ServerConfig object.


mc

private static ModuleConfig mc
Only one ModuleConfig object is created.


hct

private static HelperConfigTool hct
The HelperConfigTool object.

Constructor Detail

ModuleConfig

protected ModuleConfig()
                throws InternalErrorException
Create the ModuleConfig object. Only one ModuleConfig object is ever created.

Throws:
InternalErrorException - Thrown is the operation fails for any reason.
Method Detail

processNodes

private void processNodes(NodeList nl,
                          ModuleType mt,
                          Properties props)
                   throws InconsistentStateException
This method processes the module.xml entry for a single module type.

Parameters:
nl - A NodeList object obtained by the XML parser.
mt - The ModuleType being parsed.
props - A Properties file containing module name information.
InconsistentStateException

getModuleConfig

public static ModuleConfig getModuleConfig()
Get the singleton ModuleConfig object.

Returns:
The ModuleConfig object.

getAllModuleTypes

protected ModuleType[] getAllModuleTypes()
Get an array of all module types in ascending order.

Returns:
An array of ModuleType objects.

getAllModuleNames

protected ModuleName[] getAllModuleNames()
Get an array of all module names, in no particular order.

Returns:
An array of ModuleName objects.

getModuleNamesForTypeAndMode

private ModuleName[] getModuleNamesForTypeAndMode(Element elem,
                                                  ModuleType mt,
                                                  Properties props)
                                           throws InconsistentStateException
Get the ModuleName objects in order for the module type and mode. The first entry is the most preferred, and the last entry the least preferred.

Parameters:
elem - The Element corresponding to the Mode entry
mt - The ModuleType being processed.
props - The properties object with the module name information.
Throws:
InconsistentStateException - - Thrown if the ranking information does not consist of one entry for each integer between 1 and the number of ModuleName objects.

checkModuleTypeOrder

public boolean checkModuleTypeOrder(ModuleType earlier,
                                    ModuleType later)
Check the relationship in question. Return true if the index in moduleList for earlier is less than that for later. If earlier and later are the same, return false, as a module may not use another module of the same type.

Parameters:
earlier - A module type to be used by later.
later - A module type that wishes to use earlier.
Returns:
true if the dependency is legal, or false.

getFirstModuleName

public ModuleName getFirstModuleName(Mode mode,
                                     ModuleType type)
                              throws NoSuchDataException
Get the most highly ranked ModuleName for the given mode and module type.

Parameters:
mode - The specific mode.
type - The specific module type.
Returns:
A ModuleName object.
Throws:
NoSuchDataException - Thrown if there is no ModuleName specified for the parameters.

getModuleNames

public ModuleName[] getModuleNames(Mode mode,
                                   ModuleType type)
Get the module names for the mode and module type.

Parameters:
mode - The mode of the game, such as BEST.
type - The module type.

addModuleType

protected void addModuleType(ModuleType mt,
                             int index)
                      throws IllegalArgumentException,
                             DataAlreadyExistsException
Insert module type into the ordered list of modules.

Parameters:
mt - The ModuleType object to be added.
index - The position to insert the module type at; first position is 0.
IllegalArgumentException
DataAlreadyExistsException

deleteModuleType

protected void deleteModuleType(ModuleType mt)
                         throws PermissionsException,
                                NoSuchDataException
Delete the ModuleType object from the list. If it has any associated ModuleName objects, throw an exception.

Parameters:
mt - The ModuleType object to delete.
Throws:
PermissionsException - Throw an exception if there are any ModuleName objects associated with this ModuleType object.
NoSuchDataException - Thrown if the module type is not in the list.

addModuleName

protected void addModuleName(ModuleName mn)
Add the module name at the end of the list for the given module type.

Parameters:
mn - The ModuleName object.

deleteModuleName

protected void deleteModuleName(ModuleName mn)
Delete the ModuleName from the list for the given module type.

Parameters:
mn - The ModuleName object to be deleted.

addMode

protected void addMode(Mode newMode,
                       Mode oldMode)
                throws DataAlreadyExistsException
Add a new mode which has the same ordering of module names as the existing mode.

Parameters:
newMode - The mode to be added.
oldMode - The mode for which the ordering of module names is to be copied from.
Throws:
DataAlreadyExistsException - Thrown if the new mode already exists.

deleteMode

protected void deleteMode(String name)
                   throws PermissionsException,
                          NoSuchDataException
Delete the mode.

Parameters:
name - The name of the mode.
Throws:
PermissionsException - Thrown if the mode is a standard mode.
NoSuchDataException - Thrown if the mode does not exist.

cancel

protected void cancel()
               throws InternalErrorException
Undo all changes made since the last call to save() or cancel().

Throws:
InternalErrorException - Thrown if the operation fails for any reason.

save

protected void save()
             throws InternalErrorException
Save the information in this object to permanent storage. This method is called after the setModuleNameOrder() and setModuleTypeOrder() methods have been called.

InternalErrorException

indentLine

private void indentLine(BufferedWriter bw,
                        int level)
                 throws InternalErrorException
Convenience method to write out the appropriate number of spaces. This has no effect on parsing, and is just to make the file easier to read.

Parameters:
bw - The BufferedWriter to write the spaces to.
level - The number of levels to indent.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

setModuleNameOrder

public void setModuleNameOrder(Mode mode,
                               ModuleType type,
                               ModuleName[] names)
Set the module name order for a given module type and mode. Note that calling this method has no effect on permanent storage until save() is called.

Parameters:
mode - The mode of the game, such as FAST.
type - The module type.
names - The module names in order from most preferred to least preferred.

setModuleTypeOrder

public void setModuleTypeOrder(ModuleType[] types)
Set the mode type order. Note that calling this method has no effect on permanent storage until save() is called.

Parameters:
types - An array of module types, from first to last.