Package net.sf.freecol.common.model
Class FreeColObject
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- All Implemented Interfaces:
java.lang.Comparable<FreeColObject>
,ObjectWithId
- Direct Known Subclasses:
AbstractGoods
,AbstractUnit
,AIMain
,AIObject
,ExportData
,FreeColGameObject
,FreeColSpecObject
,HighScore
,LastSale
,NationSummary
,Scope
,StringTemplate
public abstract class FreeColObject extends java.lang.Object implements java.lang.Comparable<FreeColObject>, ObjectWithId
The FreeCol root class. Maintains an identifier, and an optional link to the specification this object uses. All FreeColObjects are trivially sortable on the basis of their identifiers as a consequence of the Comparable implementation here. Do not override this any further, use explicit comparators if more fully featured sorting is required.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
ARRAY_SIZE_TAG
XML tag name for array elements.protected static int
DEFAULT_CLASS_INDEX
Fallback class index.static java.util.Comparator<? super FreeColObject>
fcoComparator
Comparator by FCO identifier.private java.lang.String
id
The identifier of an object.static java.lang.String
ID_ATTRIBUTE_TAG
XML tag name for identifier attribute.protected static java.util.logging.Logger
logger
static java.lang.String
PARTIAL_ATTRIBUTE_TAG
XML attribute tag to denote partial updates.private java.beans.PropertyChangeSupport
pcs
An optional property change container, allocated on demand.protected static java.lang.String
VALUE_TAG
XML tag name for value attributes, used in many places.
-
Constructor Summary
Constructors Constructor Description FreeColObject()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
addAbility(Ability ability)
Add the given ability to this object.void
addFeatures(FreeColObject fco)
Adds all the features in an object to this object.boolean
addModifier(Modifier modifier)
Add the given modifier to this object.void
addPropertyChangeListener(java.beans.PropertyChangeListener listener)
void
addPropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
float
apply(float number, Turn turn, java.lang.String id)
Applies this objects modifiers with the given identifier to the given number.float
apply(float number, Turn turn, java.lang.String id, FreeColSpecObjectType fcgot)
Applies this objects modifiers with the given identifier to the given number.static float
applyModifiers(float number, Turn turn, java.util.Collection<Modifier> mods)
Applies a collection of modifiers to the given number.static float
applyModifiers(float number, Turn turn, java.util.stream.Stream<Modifier> mods)
Applies a stream of modifiers to the given number.static java.lang.String
arrayKey(int i)
Make the standard array key.static int
compareIds(FreeColObject fco1, FreeColObject fco2)
Compare two FreeColObjects by their identifiers.int
compareTo(FreeColObject other)
Base for Comparable implementations.boolean
containsAbilityKey(java.lang.String key)
Checks if this object contains a given ability key.boolean
containsModifierKey(java.lang.String key)
Checks if this object contains a given modifier key.<T extends FreeColObject>
Tcopy(Game game)
Copy a FreeColObject.<T extends FreeColObject>
Tcopy(Game game, java.lang.Class<T> returnClass)
Copy a FreeColObject.<T extends FreeColObject>
Tcopy(Game game, java.lang.Class<T> returnClass, Player player)
Copy a FreeColObject for a target player.<T extends FreeColObject>
Tcopy(Game game, Player player)
Copy a FreeColObject for a player.<T extends FreeColObject>
booleancopyIn(T other)
Copy another FreeColObject into this one if it is compatible.protected <T extends FreeColObject,R extends FreeColObject>
RcopyInCast(T other, java.lang.Class<R> returnClass)
If another object can be copied into this one,void
dumpObject()
Debugging tool, dump object XML to System.err.boolean
equals(java.lang.Object o)
void
fireIndexedPropertyChange(java.lang.String propertyName, int index, boolean oldValue, boolean newValue)
void
fireIndexedPropertyChange(java.lang.String propertyName, int index, int oldValue, int newValue)
void
fireIndexedPropertyChange(java.lang.String propertyName, int index, java.lang.Object oldValue, java.lang.Object newValue)
void
firePropertyChange(java.beans.PropertyChangeEvent event)
void
firePropertyChange(java.lang.String propertyName, boolean oldValue, boolean newValue)
void
firePropertyChange(java.lang.String propertyName, int oldValue, int newValue)
void
firePropertyChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)
java.util.stream.Stream<Ability>
getAbilities()
Gets a copy of the abilities of this object.java.util.stream.Stream<Ability>
getAbilities(java.lang.String id)
Gets the set of abilities with the given identifier from this object.java.util.stream.Stream<Ability>
getAbilities(java.lang.String id, FreeColSpecObjectType fcgot)
Gets the set of abilities with the given identifier from this object.java.util.stream.Stream<Ability>
getAbilities(java.lang.String id, FreeColSpecObjectType fcgot, Turn turn)
Gets the set of abilities with the given identifier from this object.int
getClassIndex()
Accessor for the class index.java.util.List<Modifier>
getDefenceModifiers()
Get the defence modifiers applicable to this object.FreeColObject
getDisplayObject()
Get an object to display when showing the user messages for this object.FeatureContainer
getFeatureContainer()
Gets the feature container for this object, if any.<T extends FreeColObject>
java.lang.Class<T>getFreeColObjectClass()
Get the FreeColObject class for this object.static <T extends FreeColObject>
java.lang.Class<T>getFreeColObjectClassByName(java.lang.String name)
Get the FreeColObject class corresponding to a class name.Game
getGame()
Gets the game this object belongs to.java.lang.String
getId()
Get the object unique identifier.int
getIdNumber()
Gets the numeric part of the identifier.java.lang.String
getIdType()
Get the type part of the identifier of this object.static java.lang.String
getIdTypeByName(java.lang.String id)
Get the type part of the identifier.java.util.stream.Stream<Modifier>
getModifiers()
Gets a copy of the modifiers of this object.java.util.stream.Stream<Modifier>
getModifiers(java.lang.String id)
Gets the set of modifiers with the given identifier from this object.java.util.stream.Stream<Modifier>
getModifiers(java.lang.String id, FreeColSpecObjectType fcgot)
Gets the set of modifiers with the given identifier from this object.java.util.stream.Stream<Modifier>
getModifiers(java.lang.String id, FreeColSpecObjectType fcgot, Turn turn)
Gets the set of modifiers with the given identifier from this object.static int
getObjectClassIndex(java.lang.Object o)
Get the class index, handling null and non-FCO objects.java.beans.PropertyChangeListener[]
getPropertyChangeListeners()
java.beans.PropertyChangeListener[]
getPropertyChangeListeners(java.lang.String propertyName)
protected java.beans.PropertyChangeSupport
getPropertyChangeSupport()
java.util.List<Ability>
getSortedAbilities()
Gets a sorted copy of the abilities of this object.java.util.List<Modifier>
getSortedModifiers()
Gets a sorted copy of the modifiers of this object.Specification
getSpecification()
Get the specification.java.lang.String
getSuffix()
Gets the usual suffix of this object's identifier, that is everything after the last '.'.java.lang.String
getSuffix(java.lang.String prefix)
Gets the identifier of this object with the given prefix removed if the id of the object starts with the prefix, and the entire id otherwise.abstract java.lang.String
getXMLTagName()
Get the serialization tag for this object.boolean
hasAbility(java.lang.String id)
Is an ability present in this object?boolean
hasAbility(java.lang.String id, FreeColSpecObjectType fcgot)
Is an ability present in this object?boolean
hasAbility(java.lang.String id, FreeColSpecObjectType fcgot, Turn turn)
Is an ability present in this object?int
hashCode()
boolean
hasListeners(java.lang.String propertyName)
boolean
hasModifier(java.lang.String id)
Is an modifier present in this object?boolean
hasModifier(java.lang.String id, FreeColSpecObjectType fcgot)
Is an modifier present in this object?boolean
hasModifier(java.lang.String id, FreeColSpecObjectType fcgot, Turn turn)
Is an modifier present in this object?boolean
idEquals(FreeColObject other)
Does another FreeColObject have the same identifier?protected <T> T
invokeMethod(java.lang.String methodName, java.lang.Class<T> returnClass, T defaultValue)
Invoke a method for this object.static <T extends FreeColObject>
voidlogFreeColObjects(java.util.Collection<T> c, LogBuilder lb)
Log a collection ofFreeColObject
s.protected void
readAttributes(FreeColXMLReader xr)
Reads the attributes of this object from an XML stream.protected void
readChild(FreeColXMLReader xr)
Reads a single child object.protected void
readChildren(FreeColXMLReader xr)
Reads the children of this object from an XML stream.void
readFromXML(FreeColXMLReader xr)
Initializes this object from an XML-representation of this object, unless the PARTIAL_ATTRIBUTE tag is present which indicates a partial update of an existing object.void
readFromXMLPartial(FreeColXMLReader xr)
Updates this object from an XML-representation of this object.void
removeAbilities(java.lang.String id)
Remove all abilities with a given identifier.Ability
removeAbility(Ability ability)
Remove the given ability from this object.void
removeFeatures(FreeColObject fco)
Removes all the features in an object from this object.Modifier
removeModifier(Modifier modifier)
Remove the given modifier from this object.void
removeModifiers(java.lang.String id)
Remove all abilities with a given identifier.void
removePropertyChangeListener(java.beans.PropertyChangeListener listener)
void
removePropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
boolean
save(java.io.File file)
Writes the object to the given file.boolean
save(java.io.File file, FreeColXMLWriter.WriteScope scope)
Writes the object to the given file.boolean
save(java.io.File file, FreeColXMLWriter.WriteScope scope, boolean pretty)
Writes the object to the given file.boolean
save(java.io.OutputStream out, FreeColXMLWriter.WriteScope scope, boolean pretty)
Writes the object to the given output streamjava.lang.String
serialize()
Serialize this FreeColObject to a string.java.lang.String
serialize(FreeColXMLWriter.WriteScope scope)
Serialize this FreeColObject to a string.java.lang.String
serialize(FreeColXMLWriter.WriteScope scope, java.util.List<java.lang.String> fields)
Serialize this FreeColObject to a string, possibly partially.java.lang.String
serialize(Player player)
Serialize this FreeColObject to a string for a target player.void
setGame(Game game)
Sets the game object this object belongs to.void
setId(java.lang.String newId)
Set the object identifier.protected void
setSpecification(Specification specification)
Sets the specification for this object.java.lang.String
toString()
void
toXML(FreeColXMLWriter xw)
This method writes an XML-representation of this object to the given stream.void
toXML(FreeColXMLWriter xw, java.lang.String tag)
This method writes an XML-representation of this object with a specified tag to the given stream.void
toXMLPartial(FreeColXMLWriter xw, java.lang.String[] fields)
This method writes a partial XML-representation of this object to the given stream using only the mandatory and specified fields.void
toXMLPartial(FreeColXMLWriter xw, java.util.List<java.lang.String> fields)
Simpler version of toXMLPartial, where the fields are pre-filled with key,value pairs.protected void
writeAttributes(FreeColXMLWriter xw)
Write the attributes of this object to a stream.protected void
writeChildren(FreeColXMLWriter xw)
Write the children of this object to a stream.
-
-
-
Field Detail
-
logger
protected static final java.util.logging.Logger logger
-
fcoComparator
public static final java.util.Comparator<? super FreeColObject> fcoComparator
Comparator by FCO identifier.
-
DEFAULT_CLASS_INDEX
protected static final int DEFAULT_CLASS_INDEX
Fallback class index.- See Also:
- Constant Field Values
-
id
private java.lang.String id
The identifier of an object.
-
pcs
private java.beans.PropertyChangeSupport pcs
An optional property change container, allocated on demand.
-
ID_ATTRIBUTE_TAG
public static final java.lang.String ID_ATTRIBUTE_TAG
XML tag name for identifier attribute.- See Also:
- Constant Field Values
-
ARRAY_SIZE_TAG
public static final java.lang.String ARRAY_SIZE_TAG
XML tag name for array elements.- See Also:
- Constant Field Values
-
PARTIAL_ATTRIBUTE_TAG
public static final java.lang.String PARTIAL_ATTRIBUTE_TAG
XML attribute tag to denote partial updates.- See Also:
- Constant Field Values
-
VALUE_TAG
protected static final java.lang.String VALUE_TAG
XML tag name for value attributes, used in many places.- See Also:
- Constant Field Values
-
-
Method Detail
-
getFreeColObjectClassByName
public static <T extends FreeColObject> java.lang.Class<T> getFreeColObjectClassByName(java.lang.String name)
Get the FreeColObject class corresponding to a class name.- Type Parameters:
T
- A FreeColObject subclass.- Parameters:
name
- The class name.- Returns:
- The class, or null if none found.
-
getFreeColObjectClass
public <T extends FreeColObject> java.lang.Class<T> getFreeColObjectClass()
Get the FreeColObject class for this object.- Type Parameters:
T
- A FreeColObject subclass.- Returns:
- The class, or null on error.
-
getId
public java.lang.String getId()
Get the object unique identifier.- Specified by:
getId
in interfaceObjectWithId
- Returns:
- The identifier.
-
setId
public void setId(java.lang.String newId)
Set the object identifier.- Parameters:
newId
- The new object identifier.
-
idEquals
public boolean idEquals(FreeColObject other)
Does another FreeColObject have the same identifier?- Parameters:
other
- The otherFreeColObject
to check.- Returns:
- True if the identifiers are non-null and equal.
-
getSuffix
public final java.lang.String getSuffix(java.lang.String prefix)
Gets the identifier of this object with the given prefix removed if the id of the object starts with the prefix, and the entire id otherwise.- Parameters:
prefix
- The prefix to test.- Returns:
- An identifier.
-
getSuffix
public final java.lang.String getSuffix()
Gets the usual suffix of this object's identifier, that is everything after the last '.'.- Returns:
- The usual identifier suffix.
-
getIdTypeByName
public static java.lang.String getIdTypeByName(java.lang.String id)
Get the type part of the identifier.- Parameters:
id
- The identifier to examine.- Returns:
- The type part of the identifier, or null on error.
-
getIdType
public java.lang.String getIdType()
Get the type part of the identifier of this object.- Returns:
- The type part of the identifier, or null on error.
-
getIdNumber
public int getIdNumber()
Gets the numeric part of the identifier.- Returns:
- The numeric part of the identifier, or negative on error.
-
compareIds
public static int compareIds(FreeColObject fco1, FreeColObject fco2)
Compare two FreeColObjects by their identifiers.- Parameters:
fco1
- The firstFreeColObject
to compare.fco2
- The secondFreeColObject
to compare.- Returns:
- The comparison result.
-
getClassIndex
public int getClassIndex()
Accessor for the class index.- Returns:
- The class index used by
ClientOptions
.
-
getObjectClassIndex
public static int getObjectClassIndex(java.lang.Object o)
Get the class index, handling null and non-FCO objects.- Parameters:
o
- The object to examine.- Returns:
- The class index.
-
getSpecification
public Specification getSpecification()
Get the specification.- Returns:
- The
Specification
used by this object.
-
setSpecification
protected void setSpecification(Specification specification)
Sets the specification for this object.- Parameters:
specification
- TheSpecification
to use.
-
getGame
public Game getGame()
Gets the game this object belongs to.- Returns:
- The
Game
this object belongs to.
-
setGame
public void setGame(Game game)
Sets the game object this object belongs to.- Parameters:
game
- TheGame
to set.
-
getPropertyChangeSupport
protected java.beans.PropertyChangeSupport getPropertyChangeSupport()
-
addPropertyChangeListener
public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
-
addPropertyChangeListener
public void addPropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
-
fireIndexedPropertyChange
public void fireIndexedPropertyChange(java.lang.String propertyName, int index, boolean oldValue, boolean newValue)
-
fireIndexedPropertyChange
public void fireIndexedPropertyChange(java.lang.String propertyName, int index, int oldValue, int newValue)
-
fireIndexedPropertyChange
public void fireIndexedPropertyChange(java.lang.String propertyName, int index, java.lang.Object oldValue, java.lang.Object newValue)
-
firePropertyChange
public void firePropertyChange(java.beans.PropertyChangeEvent event)
-
firePropertyChange
public void firePropertyChange(java.lang.String propertyName, boolean oldValue, boolean newValue)
-
firePropertyChange
public void firePropertyChange(java.lang.String propertyName, int oldValue, int newValue)
-
firePropertyChange
public void firePropertyChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)
-
getPropertyChangeListeners
public java.beans.PropertyChangeListener[] getPropertyChangeListeners()
-
getPropertyChangeListeners
public java.beans.PropertyChangeListener[] getPropertyChangeListeners(java.lang.String propertyName)
-
hasListeners
public boolean hasListeners(java.lang.String propertyName)
-
removePropertyChangeListener
public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
-
removePropertyChangeListener
public void removePropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
-
getFeatureContainer
public FeatureContainer getFeatureContainer()
Gets the feature container for this object, if any.- Returns:
- The
FeatureContainer
for this object.
-
hasAbility
public final boolean hasAbility(java.lang.String id)
Is an ability present in this object?- Parameters:
id
- The object identifier.- Returns:
- True if the ability is present.
-
hasAbility
public final boolean hasAbility(java.lang.String id, FreeColSpecObjectType fcgot)
Is an ability present in this object?- Parameters:
id
- The object identifier.fcgot
- An optionalFreeColSpecObjectType
the ability applies to.- Returns:
- True if the ability is present.
-
hasAbility
public final boolean hasAbility(java.lang.String id, FreeColSpecObjectType fcgot, Turn turn)
Is an ability present in this object?- Parameters:
id
- The object identifier.fcgot
- An optionalFreeColSpecObjectType
the ability applies to.turn
- An optional applicableTurn
.- Returns:
- True if the ability is present.
-
containsAbilityKey
public boolean containsAbilityKey(java.lang.String key)
Checks if this object contains a given ability key.- Parameters:
key
- The key to check.- Returns:
- True if the key is present.
-
getSortedAbilities
public final java.util.List<Ability> getSortedAbilities()
Gets a sorted copy of the abilities of this object.- Returns:
- A list of abilities.
-
getAbilities
public final java.util.stream.Stream<Ability> getAbilities()
Gets a copy of the abilities of this object.- Returns:
- A stream of abilities.
-
getAbilities
public final java.util.stream.Stream<Ability> getAbilities(java.lang.String id)
Gets the set of abilities with the given identifier from this object.- Parameters:
id
- The object identifier.- Returns:
- A stream of abilities.
-
getAbilities
public final java.util.stream.Stream<Ability> getAbilities(java.lang.String id, FreeColSpecObjectType fcgot)
Gets the set of abilities with the given identifier from this object.- Parameters:
id
- The object identifier.fcgot
- An optionalFreeColSpecObjectType
the ability applies to.- Returns:
- A stream of abilities.
-
getAbilities
public java.util.stream.Stream<Ability> getAbilities(java.lang.String id, FreeColSpecObjectType fcgot, Turn turn)
Gets the set of abilities with the given identifier from this object. Subclasses with complex ability handling should override this as all prior routines are derived from it.- Parameters:
id
- The object identifier.fcgot
- An optionalFreeColSpecObjectType
the ability applies to.turn
- An optional applicableTurn
.- Returns:
- A set of abilities.
-
addAbility
public boolean addAbility(Ability ability)
Add the given ability to this object.- Parameters:
ability
- AnAbility
to add.- Returns:
- True if the ability was added.
-
removeAbility
public Ability removeAbility(Ability ability)
Remove the given ability from this object.- Parameters:
ability
- AnAbility
to remove.- Returns:
- The ability removed or null on failure.
-
removeAbilities
public void removeAbilities(java.lang.String id)
Remove all abilities with a given identifier.- Parameters:
id
- The object identifier.
-
hasModifier
public final boolean hasModifier(java.lang.String id)
Is an modifier present in this object?- Parameters:
id
- The object identifier.- Returns:
- True if the modifier is present.
-
hasModifier
public final boolean hasModifier(java.lang.String id, FreeColSpecObjectType fcgot)
Is an modifier present in this object?- Parameters:
id
- The object identifier.fcgot
- An optionalFreeColSpecObjectType
the modifier applies to.- Returns:
- True if the modifier is present.
-
hasModifier
public boolean hasModifier(java.lang.String id, FreeColSpecObjectType fcgot, Turn turn)
Is an modifier present in this object?- Parameters:
id
- The object identifier.fcgot
- An optionalFreeColSpecObjectType
the modifier applies to.turn
- An optional applicableTurn
.- Returns:
- True if the modifier is present.
-
containsModifierKey
public final boolean containsModifierKey(java.lang.String key)
Checks if this object contains a given modifier key.- Parameters:
key
- The key to check.- Returns:
- True if the key is present.
-
getSortedModifiers
public final java.util.List<Modifier> getSortedModifiers()
Gets a sorted copy of the modifiers of this object.- Returns:
- A list of modifiers.
-
getModifiers
public final java.util.stream.Stream<Modifier> getModifiers()
Gets a copy of the modifiers of this object.- Returns:
- A set of modifiers.
-
getModifiers
public final java.util.stream.Stream<Modifier> getModifiers(java.lang.String id)
Gets the set of modifiers with the given identifier from this object.- Parameters:
id
- The object identifier.- Returns:
- A set of modifiers.
-
getModifiers
public final java.util.stream.Stream<Modifier> getModifiers(java.lang.String id, FreeColSpecObjectType fcgot)
Gets the set of modifiers with the given identifier from this object.- Parameters:
id
- The object identifier.fcgot
- An optionalFreeColSpecObjectType
the modifier applies to.- Returns:
- A set of modifiers.
-
getModifiers
public java.util.stream.Stream<Modifier> getModifiers(java.lang.String id, FreeColSpecObjectType fcgot, Turn turn)
Gets the set of modifiers with the given identifier from this object. Subclasses with complex modifier handling may override this routine.- Parameters:
id
- The object identifier.fcgot
- An optionalFreeColSpecObjectType
the modifier applies to.turn
- An optional applicableTurn
.- Returns:
- A set of modifiers.
-
apply
public final float apply(float number, Turn turn, java.lang.String id)
Applies this objects modifiers with the given identifier to the given number.- Parameters:
number
- The number to modify.turn
- An optional applicableTurn
.id
- The object identifier.- Returns:
- The modified number.
-
apply
public final float apply(float number, Turn turn, java.lang.String id, FreeColSpecObjectType fcgot)
Applies this objects modifiers with the given identifier to the given number.- Parameters:
number
- The number to modify.turn
- An optional applicableTurn
.id
- The object identifier.fcgot
- An optionalFreeColSpecObjectType
the modifier applies to.- Returns:
- The modified number.
-
applyModifiers
public static final float applyModifiers(float number, Turn turn, java.util.stream.Stream<Modifier> mods)
Applies a stream of modifiers to the given number.- Parameters:
number
- The number to modify.turn
- An optional applicableTurn
.mods
- TheModifier
s to apply.- Returns:
- The modified number.
-
applyModifiers
public static final float applyModifiers(float number, Turn turn, java.util.Collection<Modifier> mods)
Applies a collection of modifiers to the given number.- Parameters:
number
- The number to modify.turn
- An optional applicableTurn
.mods
- TheModifier
s to apply.- Returns:
- The modified number.
-
addModifier
public boolean addModifier(Modifier modifier)
Add the given modifier to this object.- Parameters:
modifier
- AnModifier
to add.- Returns:
- True if the modifier was added.
-
removeModifier
public Modifier removeModifier(Modifier modifier)
Remove the given modifier from this object.- Parameters:
modifier
- AnModifier
to remove.- Returns:
- The modifier removed.
-
removeModifiers
public void removeModifiers(java.lang.String id)
Remove all abilities with a given identifier.- Parameters:
id
- The object identifier.
-
addFeatures
public void addFeatures(FreeColObject fco)
Adds all the features in an object to this object.- Parameters:
fco
- TheFreeColObject
to add features from.
-
removeFeatures
public void removeFeatures(FreeColObject fco)
Removes all the features in an object from this object.- Parameters:
fco
- TheFreeColObject
to find features to remove in.
-
getDefenceModifiers
public java.util.List<Modifier> getDefenceModifiers()
Get the defence modifiers applicable to this object.- Returns:
- A list of defence
Modifier
s.
-
compareTo
public int compareTo(FreeColObject other)
Base for Comparable implementations.- Specified by:
compareTo
in interfacejava.lang.Comparable<FreeColObject>
- Parameters:
other
- The otherFreeColObject
subclass to compare.- Returns:
- The comparison result.
-
logFreeColObjects
public static <T extends FreeColObject> void logFreeColObjects(java.util.Collection<T> c, LogBuilder lb)
Log a collection ofFreeColObject
s.- Type Parameters:
T
- The collection member type.- Parameters:
c
- TheCollection
to log.lb
- ALogBuilder
to log to.
-
invokeMethod
protected <T> T invokeMethod(java.lang.String methodName, java.lang.Class<T> returnClass, T defaultValue)
Invoke a method for this object.- Type Parameters:
T
- The actual return type.- Parameters:
methodName
- The name of the method.returnClass
- The expected return class.defaultValue
- The default value.- Returns:
- The result of invoking the method, or the default value on failure.
-
getDisplayObject
public FreeColObject getDisplayObject()
Get an object to display when showing the user messages for this object. Example: If this object is a Building, the object to display will be the BuildingType.- Returns:
- A suitable
FreeColObject
to display, defaults to this.
-
dumpObject
public void dumpObject()
Debugging tool, dump object XML to System.err.
-
save
public boolean save(java.io.File file)
Writes the object to the given file.- Parameters:
file
- TheFile
to write to.- Returns:
- True if the save proceeded without error.
-
save
public boolean save(java.io.File file, FreeColXMLWriter.WriteScope scope)
Writes the object to the given file.- Parameters:
file
- TheFile
to write to.scope
- TheWriteScope
to use.- Returns:
- True if the save proceeded without error.
-
save
public boolean save(java.io.File file, FreeColXMLWriter.WriteScope scope, boolean pretty)
Writes the object to the given file.- Parameters:
file
- TheFile
to write to.scope
- TheWriteScope
to use.pretty
- Attempt to indent the output nicely.- Returns:
- True if the save proceeded without error.
-
save
public boolean save(java.io.OutputStream out, FreeColXMLWriter.WriteScope scope, boolean pretty)
Writes the object to the given output stream- Parameters:
out
- TheOutputStream
to write to.scope
- TheWriteScope
to use.pretty
- Attempt to indent the output nicely.- Returns:
- True if the save proceeded without error.
-
serialize
public java.lang.String serialize() throws javax.xml.stream.XMLStreamException
Serialize this FreeColObject to a string.- Returns:
- The serialized object, or null if the stream could not be created.
- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
serialize
public java.lang.String serialize(Player player) throws javax.xml.stream.XMLStreamException
Serialize this FreeColObject to a string for a target player.- Parameters:
player
- ThePlayer
to serialize the object to.- Returns:
- The serialized object, or null if the stream could not be created.
- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
serialize
public java.lang.String serialize(FreeColXMLWriter.WriteScope scope) throws javax.xml.stream.XMLStreamException
Serialize this FreeColObject to a string.- Parameters:
scope
- The write scope to use.- Returns:
- The serialized object, or null if the stream could not be created.
- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
serialize
public java.lang.String serialize(FreeColXMLWriter.WriteScope scope, java.util.List<java.lang.String> fields) throws javax.xml.stream.XMLStreamException
Serialize this FreeColObject to a string, possibly partially.- Parameters:
scope
- The write scope to use.fields
- A list of field names, which if non-null indicates this should be a partial write.- Returns:
- The serialized object, or null if the stream could not be created.
- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
copy
public <T extends FreeColObject> T copy(Game game)
Copy a FreeColObject.- Type Parameters:
T
- The actual return type.- Parameters:
game
- TheGame
to add the object to.- Returns:
- The copied object, or null on error.
-
copy
public <T extends FreeColObject> T copy(Game game, Player player)
Copy a FreeColObject for a player.- Type Parameters:
T
- The actual return type.- Parameters:
game
- TheGame
to add the object to.player
- ThePlayer
to copy for,- Returns:
- The copied object, or null on error.
-
copy
public <T extends FreeColObject> T copy(Game game, java.lang.Class<T> returnClass)
Copy a FreeColObject. The copied object and its internal descendents will be identical to the original objects, but not present in the game. Newly created objects will prefer to refer to other newly created objects. Thus if you copy a tile, an internal colony on the tile will also be copied, and the copied tile will refer to the copied colony and the copied colony refer to the copied tile, but both will refer to the original uncopied owning player.- Type Parameters:
T
- The actual return type.- Parameters:
game
- TheGame
to add the object to.returnClass
- The expected return class.- Returns:
- The copied object, or null on error.
-
copy
public <T extends FreeColObject> T copy(Game game, java.lang.Class<T> returnClass, Player player)
Copy a FreeColObject for a target player.- Type Parameters:
T
- The actual return type.- Parameters:
game
- TheGame
to add the object to.returnClass
- The expected return class.player
- ThePlayer
that will see the result.- Returns:
- The copied object, or null on error.
-
copyIn
public <T extends FreeColObject> boolean copyIn(T other)
Copy another FreeColObject into this one if it is compatible.- Type Parameters:
T
- TheFreeColObject
subclass of the object to copy in.- Parameters:
other
- The other object.- Returns:
- True if the copy in is succesful.
-
copyInCast
protected <T extends FreeColObject,R extends FreeColObject> R copyInCast(T other, java.lang.Class<R> returnClass)
If another object can be copied into this one,- Type Parameters:
T
- TheFreeColObject
subclass of the object to copy in.R
- TheFreeColObject
subclass to copy in to.- Parameters:
other
- TheFreeColObject
to copy in.returnClass
- The return class.- Returns:
- The other object cast to the return class, or null if incompatible in any way.
-
toXML
public void toXML(FreeColXMLWriter xw) throws javax.xml.stream.XMLStreamException
This method writes an XML-representation of this object to the given stream. All attributes will be made visible.- Parameters:
xw
- TheFreeColXMLWriter
to write to.- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
toXML
public void toXML(FreeColXMLWriter xw, java.lang.String tag) throws javax.xml.stream.XMLStreamException
This method writes an XML-representation of this object with a specified tag to the given stream. Almost all FreeColObjects end up calling these, and implementing their own write{Attributes,Children} methods which begin by calling their superclass. This allows a clean nesting of the serialization routines throughout the class hierarchy. All attributes will be made visible.- Parameters:
xw
- TheFreeColXMLWriter
to write to.tag
- The tag to use.- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
writeAttributes
protected void writeAttributes(FreeColXMLWriter xw) throws javax.xml.stream.XMLStreamException
Write the attributes of this object to a stream. To be overridden if required by any object that has attributes and uses the toXML(FreeColXMLWriter, String) call.- Parameters:
xw
- TheFreeColXMLWriter
to write to.- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
writeChildren
protected void writeChildren(FreeColXMLWriter xw) throws javax.xml.stream.XMLStreamException
Write the children of this object to a stream. To be overridden if required by any object that has children and uses the toXML(FreeColXMLWriter, String) call.- Parameters:
xw
- TheFreeColXMLWriter
to write to.- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
toXMLPartial
public final void toXMLPartial(FreeColXMLWriter xw, java.lang.String[] fields) throws javax.xml.stream.XMLStreamException
This method writes a partial XML-representation of this object to the given stream using only the mandatory and specified fields. All attributes are considered visible as this is server-to-owner-client functionality, but it depends ultimately on the presence of a getFieldName() method that returns a type compatible with String.valueOf.- Parameters:
xw
- TheFreeColXMLWriter
to write to.fields
- The fields to write.- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
toXMLPartial
public final void toXMLPartial(FreeColXMLWriter xw, java.util.List<java.lang.String> fields) throws javax.xml.stream.XMLStreamException
Simpler version of toXMLPartial, where the fields are pre-filled with key,value pairs. We thus no longer need call the introspector.- Parameters:
xw
- TheFreeColXMLWriter
to write to.fields
- The fields to write.- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
readFromXML
public void readFromXML(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Initializes this object from an XML-representation of this object, unless the PARTIAL_ATTRIBUTE tag is present which indicates a partial update of an existing object.- Parameters:
xr
- The input stream with the XML.- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems reading the stream.
-
readAttributes
protected void readAttributes(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads the attributes of this object from an XML stream.- Parameters:
xr
- TheFreeColXMLReader
to read from.- Throws:
javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
readChildren
protected void readChildren(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads the children of this object from an XML stream.- Parameters:
xr
- TheFreeColXMLReader
to read from.- Throws:
javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
readChild
protected void readChild(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads a single child object. Subclasses must override to read their enclosed elements. This particular instance of the routine always throws XMLStreamException because we should never arrive here. However it is very useful to always call super.readChild() when an unexpected tag is encountered, as the exception thrown here provides some useful debugging context.- Parameters:
xr
- TheFreeColXMLReader
to read from.- Throws:
javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
readFromXMLPartial
public final void readFromXMLPartial(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Updates this object from an XML-representation of this object. All attributes are considered visible as this is server-to-owner-client functionality. It depends ultimately on the presence of a setFieldName() method that takes a parameter type T where T.valueOf(String) exists.- Parameters:
xr
- The input stream with the XML.- Throws:
javax.xml.stream.XMLStreamException
- if a problem was encountered during parsing.
-
arrayKey
public static java.lang.String arrayKey(int i)
Make the standard array key.- Parameters:
i
- The array index.- Returns:
- The array key.
-
getXMLTagName
public abstract java.lang.String getXMLTagName()
Get the serialization tag for this object.- Returns:
- The tag.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-