net.sf.freecol.common.model
Class FreeColGameObject

java.lang.Object
  extended by net.sf.freecol.common.model.FreeColGameObject
Direct Known Subclasses:
Building, ColonyTile, Europe, Game, GoodsContainer, Map, Market, Monarch, Player, Settlement, Tile, TradeRoute, Unit, UnitContainer

public abstract class FreeColGameObject
extends java.lang.Object

The superclass of all game objects in FreeCol.


Field Summary
static java.lang.String COPYRIGHT
           
static java.lang.String LICENSE
           
static java.lang.String REVISION
           
 
Constructor Summary
protected FreeColGameObject()
           
  FreeColGameObject(Game game)
          Creates a new FreeColGameObject with an automatically assigned ID and registers this object at the specified Game.
  FreeColGameObject(Game game, org.w3c.dom.Element e)
          Initiates a new FreeColGameObject from an Element.
  FreeColGameObject(Game game, java.lang.String id)
          Initiates a new FreeColGameObject with the given ID.
  FreeColGameObject(Game game, javax.xml.stream.XMLStreamReader in)
          Initiates a new FreeColGameObject from an Element.
 
Method Summary
protected  void addModelMessage(FreeColGameObject source, java.lang.String messageID, java.lang.String[][] data, int type)
          Creates a ModelMessage and uses getGame().addModelMessage(modelMessage) to register it.
protected  void addModelMessage(FreeColGameObject source, java.lang.String messageID, java.lang.String[][] data, int type, java.lang.Object display)
          Creates a ModelMessage and uses getGame().addModelMessage(modelMessage) to register it.
 void dispose()
          Removes all references to this object.
 boolean equals(FreeColGameObject o)
          Checks if the given FreeColGameObject equals this object.
 boolean equals(java.lang.Object o)
          Checks if the given FreeColGameObject equals this object.
 Game getGame()
          Gets the game object this FreeColGameObject belongs to.
 GameOptions getGameOptions()
          Gets the GameOptions that is associated with the Game owning this FreeColGameObject.
 java.lang.String getID()
          Gets the unique ID of this object.
 java.lang.Integer getIntegerID()
          Gets the ID's integer part of this object.
static java.lang.String getXMLElementTagName()
          Gets the tag name of the root element representing this object.
 int hashCode()
           
 boolean hasID(java.lang.String id)
          Checks if this object has the specified ID.
 boolean isDisposed()
          Checks if this object has been disposed.
 boolean isUninitialized()
          Checks if this FreeColGameObject is uninitialized.
protected  boolean[][] readFromArrayElement(java.lang.String tagName, javax.xml.stream.XMLStreamReader in, boolean[][] arrayType)
          Reads an XML-representation of an array.
protected static int[] readFromArrayElement(java.lang.String tagName, javax.xml.stream.XMLStreamReader in, int[] arrayType)
          Reads an XML-representation of an array.
protected  int[][] readFromArrayElement(java.lang.String tagName, javax.xml.stream.XMLStreamReader in, int[][] arrayType)
          Reads an XML-representation of an array.
protected  java.lang.String[] readFromArrayElement(java.lang.String tagName, javax.xml.stream.XMLStreamReader in, java.lang.String[] arrayType)
          Reads an XML-representation of an array.
 void readFromXML(javax.xml.stream.XMLStreamReader in)
          Initialize this object from an XML-representation of this object.
 void readFromXMLElement(org.w3c.dom.Element element)
          Initialize this object from an XML-representation of this object.
protected abstract  void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
          Initialize this object from an XML-representation of this object.
 void setFakeID(java.lang.String newID)
          Sets the ID of this object for temporary use with toXMLElement.
 void setGame(Game game)
          Sets the game object this FreeColGameObject belongs to.
 void setID(java.lang.String newID)
          Sets the unique ID of this object.
protected  void toArrayElement(java.lang.String tagName, boolean[][] array, javax.xml.stream.XMLStreamWriter out)
          Creates an XML-representation of an array.
protected  void toArrayElement(java.lang.String tagName, int[][] array, javax.xml.stream.XMLStreamWriter out)
          Creates an XML-representation of an array.
protected static void toArrayElement(java.lang.String tagName, int[] array, javax.xml.stream.XMLStreamWriter out)
          Creates an XML-representation of an array.
protected  void toArrayElement(java.lang.String tagName, java.lang.String[] array, javax.xml.stream.XMLStreamWriter out)
          Creates an XML-representation of an array.
 void toSavedXML(javax.xml.stream.XMLStreamWriter out)
          This method writes an XML-representation of this object to the given stream for the purpose of storing this object as a part of a saved game.
 java.lang.String toString()
          Returns a string representation of the object.
 void toXML(javax.xml.stream.XMLStreamWriter out)
          This method writes an XML-representation of this object to the given stream.
 void toXML(javax.xml.stream.XMLStreamWriter out, Player player)
          This method writes an XML-representation of this object to the given stream.
 void toXML(javax.xml.stream.XMLStreamWriter out, Player player, boolean showAll, boolean toSavedGame)
          This method writes an XML-representation of this object to the given stream.
 org.w3c.dom.Element toXMLElement(Player player, org.w3c.dom.Document document)
          This method writes an XML-representation of this object to the given stream.
 org.w3c.dom.Element toXMLElement(Player player, org.w3c.dom.Document document, boolean showAll, boolean toSavedGame)
          This method writes an XML-representation of this object to the given stream.
protected abstract  void toXMLImpl(javax.xml.stream.XMLStreamWriter out, Player player, boolean showAll, boolean toSavedGame)
          This method writes an XML-representation of this object to the given stream.
 void updateID()
          Updates the id.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

COPYRIGHT

public static final java.lang.String COPYRIGHT
See Also:
Constant Field Values

LICENSE

public static final java.lang.String LICENSE
See Also:
Constant Field Values

REVISION

public static final java.lang.String REVISION
See Also:
Constant Field Values
Constructor Detail

FreeColGameObject

protected FreeColGameObject()

FreeColGameObject

public FreeColGameObject(Game game)
Creates a new FreeColGameObject with an automatically assigned ID and registers this object at the specified Game.

Parameters:
game - The Game in which this object belong.

FreeColGameObject

public FreeColGameObject(Game game,
                         javax.xml.stream.XMLStreamReader in)
                  throws javax.xml.stream.XMLStreamException
Initiates a new FreeColGameObject from an Element.

Parameters:
game - The Game in which this object belong.
in - The input stream containing the XML.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

FreeColGameObject

public FreeColGameObject(Game game,
                         org.w3c.dom.Element e)
Initiates a new FreeColGameObject from an Element.

Parameters:
game - The Game in which this object belong.
e - An XML-element that will be used to initialize this object.

FreeColGameObject

public FreeColGameObject(Game game,
                         java.lang.String id)
Initiates a new FreeColGameObject with the given ID. The object should later be initialized by calling either readFromXML(XMLStreamReader) or readFromXMLElement(Element).

Parameters:
game - The Game in which this object belong.
id - The unique identifier for this object.
Method Detail

getGame

public Game getGame()
Gets the game object this FreeColGameObject belongs to.

Returns:
The game.

getGameOptions

public GameOptions getGameOptions()
Gets the GameOptions that is associated with the Game owning this FreeColGameObject.

Returns:
The same GameOptions-object as returned by getGame().getGameOptions().

setGame

public void setGame(Game game)
Sets the game object this FreeColGameObject belongs to.

Parameters:
game - The game.

dispose

public void dispose()
Removes all references to this object.


isDisposed

public boolean isDisposed()
Checks if this object has been disposed.

Returns:
true if this object has been disposed.
See Also:
dispose()

isUninitialized

public boolean isUninitialized()
Checks if this FreeColGameObject is uninitialized. That is: it has been referenced by another object, but has not yet been updated with readFromXML(javax.xml.stream.XMLStreamReader).

Returns:
true if this object is not initialized.

updateID

public void updateID()
Updates the id. This method should be overwritten by server model objects.


toXML

public void toXML(javax.xml.stream.XMLStreamWriter out,
                  Player player)
           throws javax.xml.stream.XMLStreamException
This method writes an XML-representation of this object to the given stream. Only attributes visible to the given Player will be added to that representation.

Parameters:
out - The target stream.
player - The Player this XML-representation is made for.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.
See Also:
toXML(XMLStreamWriter, Player, boolean, boolean)

toXML

public void toXML(javax.xml.stream.XMLStreamWriter out)
           throws javax.xml.stream.XMLStreamException
This method writes an XML-representation of this object to the given stream. All attributes will be made visable.

Parameters:
out - The target stream.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.
See Also:
toXML(XMLStreamWriter, Player, boolean, boolean)

toSavedXML

public void toSavedXML(javax.xml.stream.XMLStreamWriter out)
                throws javax.xml.stream.XMLStreamException
This method writes an XML-representation of this object to the given stream for the purpose of storing this object as a part of a saved game.

Parameters:
out - The target stream.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.
See Also:
toXML(XMLStreamWriter, Player, boolean, boolean)

toXMLElement

public org.w3c.dom.Element toXMLElement(Player player,
                                        org.w3c.dom.Document document)
This method writes an XML-representation of this object to the given stream.

Only attributes visible to the given Player will be added to that representation if showAll is set to false.

Parameters:
player - The Player this XML-representation should be made for, or null if showAll == true.
document - The Document.
Returns:
An XML-representation of this object.

toXMLElement

public org.w3c.dom.Element toXMLElement(Player player,
                                        org.w3c.dom.Document document,
                                        boolean showAll,
                                        boolean toSavedGame)
This method writes an XML-representation of this object to the given stream.

Only attributes visible to the given Player will be added to that representation if showAll is set to false.

Parameters:
player - The Player this XML-representation should be made for, or null if showAll == true.
document - The Document.
showAll - Only attributes visible to player will be added to the representation if showAll is set to false.
toSavedGame - If true then information that is only needed when saving a game is added.
Returns:
An XML-representation of this object.

readFromXMLElement

public void readFromXMLElement(org.w3c.dom.Element element)
Initialize this object from an XML-representation of this object.

Parameters:
element - An XML-element that will be used to initialize this object.

toXMLImpl

protected abstract void toXMLImpl(javax.xml.stream.XMLStreamWriter out,
                                  Player player,
                                  boolean showAll,
                                  boolean toSavedGame)
                           throws javax.xml.stream.XMLStreamException
This method writes an XML-representation of this object to the given stream.

Only attributes visible to the given Player will be added to that representation if showAll is set to false.

Parameters:
out - The target stream.
player - The Player this XML-representation should be made for, or null if showAll == true.
showAll - Only attributes visible to player will be added to the representation if showAll is set to false.
toSavedGame - If true then information that is only needed when saving a game is added.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

readFromXMLImpl

protected abstract void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
                                 throws javax.xml.stream.XMLStreamException
Initialize this object from an XML-representation of this object.

Parameters:
in - The input stream containing the XML.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

toXML

public final void toXML(javax.xml.stream.XMLStreamWriter out,
                        Player player,
                        boolean showAll,
                        boolean toSavedGame)
                 throws javax.xml.stream.XMLStreamException
This method writes an XML-representation of this object to the given stream.

Only attributes visible to the given Player will be added to that representation if showAll is set to false.

Parameters:
out - The target stream.
player - The Player this XML-representation should be made for, or null if showAll == true.
showAll - Only attributes visible to player will be added to the representation if showAll is set to false.
toSavedGame - If true then information that is only needed when saving a game is added.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

readFromXML

public final void readFromXML(javax.xml.stream.XMLStreamReader in)
                       throws javax.xml.stream.XMLStreamException
Initialize this object from an XML-representation of this object.

Parameters:
in - The input stream containing the XML.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

getXMLElementTagName

public static java.lang.String getXMLElementTagName()
Gets the tag name of the root element representing this object. This method should be overwritten by any sub-class, preferably with the name of the class with the first letter in lower case.

Returns:
"unknown".

getID

public java.lang.String getID()
Gets the unique ID of this object.

Returns:
The unique ID of this object.

getIntegerID

public java.lang.Integer getIntegerID()
Gets the ID's integer part of this object.

Returns:
The unique ID of this object.

setID

public void setID(java.lang.String newID)
Sets the unique ID of this object. When setting a new ID to this object, it it automatically registered at the corresponding Game with the new ID.

Parameters:
newID - the unique ID of this object,

setFakeID

public void setFakeID(java.lang.String newID)
Sets the ID of this object for temporary use with toXMLElement. This method does not register the object.

Parameters:
newID - the unique ID of this object,

addModelMessage

protected void addModelMessage(FreeColGameObject source,
                               java.lang.String messageID,
                               java.lang.String[][] data,
                               int type)
Creates a ModelMessage and uses getGame().addModelMessage(modelMessage) to register it.


Example:

Using addModelMessage(this, "messageID", new String[][] {{"%test1%", "ok1"}, {"%test2%", "ok2"}) with the entry "messageID=This is %test1% and %test2%" in Messages, would give the following message: "This is ok1 and ok2".

Parameters:
source - The source of the message. This is what the message should be associated with. In addition, the owner of the source is the player getting the message.
messageID - The ID of the message to display. See: Messages.
data - Contains the data to be displayed in the message or null.
type - The type of message.
See Also:
Canvas, Game.addModelMessage(ModelMessage), ModelMessage

addModelMessage

protected void addModelMessage(FreeColGameObject source,
                               java.lang.String messageID,
                               java.lang.String[][] data,
                               int type,
                               java.lang.Object display)
Creates a ModelMessage and uses getGame().addModelMessage(modelMessage) to register it.


Example:

Using addModelMessage(this, "messageID", new String[][] {{"%test1%", "ok1"}, {"%test2%", "ok2"}) with the entry "messageID=This is %test1% and %test2%" in Messages, would give the following message: "This is ok1 and ok2".

Parameters:
source - The source of the message. This is what the message should be associated with. In addition, the owner of the source is the player getting the message.
messageID - The ID of the message to display. See: Messages.
data - Contains the data to be displayed in the message or null.
type - The type of message.
display - The Object to display.
See Also:
Canvas, Game.addModelMessage(ModelMessage), ModelMessage

hasID

public boolean hasID(java.lang.String id)
Checks if this object has the specified ID.

Parameters:
id - The ID to check against.
Returns:
true if the specified ID match the ID of this object and false otherwise.

equals

public boolean equals(FreeColGameObject o)
Checks if the given FreeColGameObject equals this object.

Parameters:
o - The FreeColGameObject to compare against this object.
Returns:
true if the two FreeColGameObject are equal and false otherwise.

equals

public boolean equals(java.lang.Object o)
Checks if the given FreeColGameObject equals this object.

Overrides:
equals in class java.lang.Object
Parameters:
o - The FreeColGameObject to compare against this object.
Returns:
true if the two FreeColGameObject are equal and false otherwise.

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Returns a string representation of the object.

Overrides:
toString in class java.lang.Object
Returns:
The String

toArrayElement

protected void toArrayElement(java.lang.String tagName,
                              int[][] array,
                              javax.xml.stream.XMLStreamWriter out)
                       throws javax.xml.stream.XMLStreamException
Creates an XML-representation of an array.

Parameters:
tagName - The tagname for the Element representing the array.
array - The array to represent.
out - The target stream.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

readFromArrayElement

protected int[][] readFromArrayElement(java.lang.String tagName,
                                       javax.xml.stream.XMLStreamReader in,
                                       int[][] arrayType)
                                throws javax.xml.stream.XMLStreamException
Reads an XML-representation of an array.

Parameters:
tagName - The tagname for the Element representing the array.
in - The input stream with the XML.
arrayType - The type of array to be read.
Returns:
The array.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

toArrayElement

protected static void toArrayElement(java.lang.String tagName,
                                     int[] array,
                                     javax.xml.stream.XMLStreamWriter out)
                              throws javax.xml.stream.XMLStreamException
Creates an XML-representation of an array.

Parameters:
tagName - The tagname for the Element representing the array.
array - The array to represent.
out - The target stream.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

readFromArrayElement

protected static int[] readFromArrayElement(java.lang.String tagName,
                                            javax.xml.stream.XMLStreamReader in,
                                            int[] arrayType)
                                     throws javax.xml.stream.XMLStreamException
Reads an XML-representation of an array.

Parameters:
tagName - The tagname for the Element representing the array.
in - The input stream with the XML.
arrayType - The type of array to be read.
Returns:
The array.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

toArrayElement

protected void toArrayElement(java.lang.String tagName,
                              boolean[][] array,
                              javax.xml.stream.XMLStreamWriter out)
                       throws javax.xml.stream.XMLStreamException
Creates an XML-representation of an array.

Parameters:
tagName - The tagname for the Element representing the array.
array - The array to represent.
out - The target stream.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

readFromArrayElement

protected boolean[][] readFromArrayElement(java.lang.String tagName,
                                           javax.xml.stream.XMLStreamReader in,
                                           boolean[][] arrayType)
                                    throws javax.xml.stream.XMLStreamException
Reads an XML-representation of an array.

Parameters:
tagName - The tagname for the Element representing the array.
in - The input stream with the XML.
arrayType - The type of array to be read.
Returns:
The array.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

toArrayElement

protected void toArrayElement(java.lang.String tagName,
                              java.lang.String[] array,
                              javax.xml.stream.XMLStreamWriter out)
                       throws javax.xml.stream.XMLStreamException
Creates an XML-representation of an array.

Parameters:
tagName - The tagname for the Element representing the array.
array - The array to represent.
out - The target stream.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

readFromArrayElement

protected java.lang.String[] readFromArrayElement(java.lang.String tagName,
                                                  javax.xml.stream.XMLStreamReader in,
                                                  java.lang.String[] arrayType)
                                           throws javax.xml.stream.XMLStreamException
Reads an XML-representation of an array.

Parameters:
tagName - The tagname for the Element representing the array.
in - The input stream with the XML.
arrayType - The type of array to be read.
Returns:
The array.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.