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.StringARRAY_SIZE_TAGXML tag name for array elements.protected static intDEFAULT_CLASS_INDEXFallback class index.static java.util.Comparator<? super FreeColObject>fcoComparatorComparator by FCO identifier.private java.lang.StringidThe identifier of an object.static java.lang.StringID_ATTRIBUTE_TAGXML tag name for identifier attribute.protected static java.util.logging.Loggerloggerstatic java.lang.StringPARTIAL_ATTRIBUTE_TAGXML attribute tag to denote partial updates.private java.beans.PropertyChangeSupportpcsAn optional property change container, allocated on demand.protected static java.lang.StringVALUE_TAGXML 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 booleanaddAbility(Ability ability)Add the given ability to this object.voidaddFeatures(FreeColObject fco)Adds all the features in an object to this object.booleanaddModifier(Modifier modifier)Add the given modifier to this object.voidaddPropertyChangeListener(java.beans.PropertyChangeListener listener)voidaddPropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)floatapply(float number, Turn turn, java.lang.String id)Applies this objects modifiers with the given identifier to the given number.floatapply(float number, Turn turn, java.lang.String id, FreeColSpecObjectType fcgot)Applies this objects modifiers with the given identifier to the given number.static floatapplyModifiers(float number, Turn turn, java.util.Collection<Modifier> mods)Applies a collection of modifiers to the given number.static floatapplyModifiers(float number, Turn turn, java.util.stream.Stream<Modifier> mods)Applies a stream of modifiers to the given number.static java.lang.StringarrayKey(int i)Make the standard array key.static intcompareIds(FreeColObject fco1, FreeColObject fco2)Compare two FreeColObjects by their identifiers.intcompareTo(FreeColObject other)Base for Comparable implementations.booleancontainsAbilityKey(java.lang.String key)Checks if this object contains a given ability key.booleancontainsModifierKey(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,voiddumpObject()Debugging tool, dump object XML to System.err.booleanequals(java.lang.Object o)voidfireIndexedPropertyChange(java.lang.String propertyName, int index, boolean oldValue, boolean newValue)voidfireIndexedPropertyChange(java.lang.String propertyName, int index, int oldValue, int newValue)voidfireIndexedPropertyChange(java.lang.String propertyName, int index, java.lang.Object oldValue, java.lang.Object newValue)voidfirePropertyChange(java.beans.PropertyChangeEvent event)voidfirePropertyChange(java.lang.String propertyName, boolean oldValue, boolean newValue)voidfirePropertyChange(java.lang.String propertyName, int oldValue, int newValue)voidfirePropertyChange(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.intgetClassIndex()Accessor for the class index.java.util.List<Modifier>getDefenceModifiers()Get the defence modifiers applicable to this object.FreeColObjectgetDisplayObject()Get an object to display when showing the user messages for this object.FeatureContainergetFeatureContainer()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.GamegetGame()Gets the game this object belongs to.java.lang.StringgetId()Get the object unique identifier.intgetIdNumber()Gets the numeric part of the identifier.java.lang.StringgetIdType()Get the type part of the identifier of this object.static java.lang.StringgetIdTypeByName(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 intgetObjectClassIndex(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.PropertyChangeSupportgetPropertyChangeSupport()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.SpecificationgetSpecification()Get the specification.java.lang.StringgetSuffix()Gets the usual suffix of this object's identifier, that is everything after the last '.'.java.lang.StringgetSuffix(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.StringgetXMLTagName()Get the serialization tag for this object.booleanhasAbility(java.lang.String id)Is an ability present in this object?booleanhasAbility(java.lang.String id, FreeColSpecObjectType fcgot)Is an ability present in this object?booleanhasAbility(java.lang.String id, FreeColSpecObjectType fcgot, Turn turn)Is an ability present in this object?inthashCode()booleanhasListeners(java.lang.String propertyName)booleanhasModifier(java.lang.String id)Is an modifier present in this object?booleanhasModifier(java.lang.String id, FreeColSpecObjectType fcgot)Is an modifier present in this object?booleanhasModifier(java.lang.String id, FreeColSpecObjectType fcgot, Turn turn)Is an modifier present in this object?booleanidEquals(FreeColObject other)Does another FreeColObject have the same identifier?protected <T> TinvokeMethod(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 ofFreeColObjects.protected voidreadAttributes(FreeColXMLReader xr)Reads the attributes of this object from an XML stream.protected voidreadChild(FreeColXMLReader xr)Reads a single child object.protected voidreadChildren(FreeColXMLReader xr)Reads the children of this object from an XML stream.voidreadFromXML(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.voidreadFromXMLPartial(FreeColXMLReader xr)Updates this object from an XML-representation of this object.voidremoveAbilities(java.lang.String id)Remove all abilities with a given identifier.AbilityremoveAbility(Ability ability)Remove the given ability from this object.voidremoveFeatures(FreeColObject fco)Removes all the features in an object from this object.ModifierremoveModifier(Modifier modifier)Remove the given modifier from this object.voidremoveModifiers(java.lang.String id)Remove all abilities with a given identifier.voidremovePropertyChangeListener(java.beans.PropertyChangeListener listener)voidremovePropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)booleansave(java.io.File file)Writes the object to the given file.booleansave(java.io.File file, FreeColXMLWriter.WriteScope scope)Writes the object to the given file.booleansave(java.io.File file, FreeColXMLWriter.WriteScope scope, boolean pretty)Writes the object to the given file.booleansave(java.io.OutputStream out, FreeColXMLWriter.WriteScope scope, boolean pretty)Writes the object to the given output streamjava.lang.Stringserialize()Serialize this FreeColObject to a string.java.lang.Stringserialize(FreeColXMLWriter.WriteScope scope)Serialize this FreeColObject to a string.java.lang.Stringserialize(FreeColXMLWriter.WriteScope scope, java.util.List<java.lang.String> fields)Serialize this FreeColObject to a string, possibly partially.java.lang.Stringserialize(Player player)Serialize this FreeColObject to a string for a target player.voidsetGame(Game game)Sets the game object this object belongs to.voidsetId(java.lang.String newId)Set the object identifier.protected voidsetSpecification(Specification specification)Sets the specification for this object.java.lang.StringtoString()voidtoXML(FreeColXMLWriter xw)This method writes an XML-representation of this object to the given stream.voidtoXML(FreeColXMLWriter xw, java.lang.String tag)This method writes an XML-representation of this object with a specified tag to the given stream.voidtoXMLPartial(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.voidtoXMLPartial(FreeColXMLWriter xw, java.util.List<java.lang.String> fields)Simpler version of toXMLPartial, where the fields are pre-filled with key,value pairs.protected voidwriteAttributes(FreeColXMLWriter xw)Write the attributes of this object to a stream.protected voidwriteChildren(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:
getIdin 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 otherFreeColObjectto 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 firstFreeColObjectto compare.fco2- The secondFreeColObjectto 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
Specificationused by this object.
-
setSpecification
protected void setSpecification(Specification specification)
Sets the specification for this object.- Parameters:
specification- TheSpecificationto use.
-
getGame
public Game getGame()
Gets the game this object belongs to.- Returns:
- The
Gamethis object belongs to.
-
setGame
public void setGame(Game game)
Sets the game object this object belongs to.- Parameters:
game- TheGameto 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
FeatureContainerfor 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 optionalFreeColSpecObjectTypethe 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 optionalFreeColSpecObjectTypethe 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 optionalFreeColSpecObjectTypethe 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 optionalFreeColSpecObjectTypethe 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- AnAbilityto add.- Returns:
- True if the ability was added.
-
removeAbility
public Ability removeAbility(Ability ability)
Remove the given ability from this object.- Parameters:
ability- AnAbilityto 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 optionalFreeColSpecObjectTypethe 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 optionalFreeColSpecObjectTypethe 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 optionalFreeColSpecObjectTypethe 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 optionalFreeColSpecObjectTypethe 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 optionalFreeColSpecObjectTypethe 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- TheModifiers 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- TheModifiers to apply.- Returns:
- The modified number.
-
addModifier
public boolean addModifier(Modifier modifier)
Add the given modifier to this object.- Parameters:
modifier- AnModifierto add.- Returns:
- True if the modifier was added.
-
removeModifier
public Modifier removeModifier(Modifier modifier)
Remove the given modifier from this object.- Parameters:
modifier- AnModifierto 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- TheFreeColObjectto add features from.
-
removeFeatures
public void removeFeatures(FreeColObject fco)
Removes all the features in an object from this object.- Parameters:
fco- TheFreeColObjectto 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
Modifiers.
-
compareTo
public int compareTo(FreeColObject other)
Base for Comparable implementations.- Specified by:
compareToin interfacejava.lang.Comparable<FreeColObject>- Parameters:
other- The otherFreeColObjectsubclass to compare.- Returns:
- The comparison result.
-
logFreeColObjects
public static <T extends FreeColObject> void logFreeColObjects(java.util.Collection<T> c, LogBuilder lb)
Log a collection ofFreeColObjects.- Type Parameters:
T- The collection member type.- Parameters:
c- TheCollectionto log.lb- ALogBuilderto 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
FreeColObjectto 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- TheFileto 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- TheFileto write to.scope- TheWriteScopeto 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- TheFileto write to.scope- TheWriteScopeto 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- TheOutputStreamto write to.scope- TheWriteScopeto 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.XMLStreamExceptionSerialize 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- ThePlayerto 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- TheGameto 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- TheGameto add the object to.player- ThePlayerto 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- TheGameto 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- TheGameto add the object to.returnClass- The expected return class.player- ThePlayerthat 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- TheFreeColObjectsubclass 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- TheFreeColObjectsubclass of the object to copy in.R- TheFreeColObjectsubclass to copy in to.- Parameters:
other- TheFreeColObjectto 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- TheFreeColXMLWriterto 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- TheFreeColXMLWriterto 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- TheFreeColXMLWriterto 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- TheFreeColXMLWriterto 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- TheFreeColXMLWriterto 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- TheFreeColXMLWriterto 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- TheFreeColXMLReaderto 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- TheFreeColXMLReaderto 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- TheFreeColXMLReaderto 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:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-