Package net.sf.freecol.common.model
Class AbstractUnit
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- net.sf.freecol.common.model.AbstractUnit
-
- All Implemented Interfaces:
java.lang.Comparable<FreeColObject>
,ObjectWithId
public class AbstractUnit extends FreeColObject
Contains the information necessary to create a new unit.
-
-
Field Summary
Fields Modifier and Type Field Description private int
number
The number of units.private static java.lang.String
NUMBER_TAG
private static java.lang.String
ROLE_TAG
private java.lang.String
roleId
The role identifier of this AbstractUnit.static java.lang.String
TAG
-
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE_TAG, DEFAULT_CLASS_INDEX, fcoComparator, ID_ATTRIBUTE_TAG, logger, PARTIAL_ATTRIBUTE_TAG, VALUE_TAG
-
-
Constructor Summary
Constructors Constructor Description AbstractUnit()
Deliberately empty constructor, for Game.newInstance.AbstractUnit(java.lang.String id, java.lang.String roleId, int number)
Create a new AbstractUnit.AbstractUnit(FreeColXMLReader xr)
Creates a newAbstractUnit
instance.AbstractUnit(UnitType unitType, java.lang.String roleId, int number)
Create a new AbstractUnit.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addToNumber(int diff)
Add to the number.static double
calculateStrength(Specification spec, java.util.List<AbstractUnit> units)
Calculate the approximate offence power of a list of units.<T extends FreeColObject>
booleancopyIn(T other)
Copy another FreeColObject into this one if it is compatible.static java.util.List<AbstractUnit>
deepCopy(java.util.List<AbstractUnit> units)
Get a deep copy of a list of abstract units.java.lang.String
getDescription()
Get a description of this abstract unit.StringTemplate
getLabel()
Gets a template describing this abstract unit.private static StringTemplate
getLabelInternal(java.lang.String typeId, java.lang.String roleId, int number)
Gets a template describing an arbitrary abstract unit.static StringTemplate
getListLabel(java.lang.String base, java.util.List<AbstractUnit> units)
Get a template for a list of abstract units.int
getNumber()
Get the number of units.double
getOffence(Specification spec)
Get the approximate offence power that an instantiated unit corresponding to this abstract form would have.Role
getRole(Specification spec)
Convenience accessor for the role.java.lang.String
getRoleId()
Get the role identifier.StringTemplate
getSingleLabel()
Gets a template describing this abstract unit with a fixed count of 1.UnitType
getType(Specification spec)
Convenience accessor for the unit type.java.lang.String
getXMLTagName()
Get the serialization tag for this object.boolean
matchAll(AbstractUnit other)
Does another AbstractUnit match in all fields.static java.util.function.Predicate<AbstractUnit>
matcher(AbstractUnit au)
Create a predicate to match the type+role of an abstract unit.static java.util.function.Predicate<AbstractUnit>
matcher(Unit unit)
Create a predicate to match the type+role of a unit.static java.util.function.Predicate<AbstractUnit>
matcher(UnitType ut, java.lang.String roleId)
Create a predicate to match the type+role of an abstract unit.static boolean
matchUnits(java.util.List<AbstractUnit> l1, java.util.List<AbstractUnit> l2)
Do two lists of abstract units match completely.protected void
readAttributes(FreeColXMLReader xr)
Reads the attributes of this object from an XML stream.void
setNumber(int newNumber)
Set the number of units.void
setRoleId(java.lang.String roleId)
Set the role identifier.java.lang.String
toString()
protected void
writeAttributes(FreeColXMLWriter xw)
Write the attributes of this object to a stream.-
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addAbility, addFeatures, addModifier, addPropertyChangeListener, addPropertyChangeListener, apply, apply, applyModifiers, applyModifiers, arrayKey, compareIds, compareTo, containsAbilityKey, containsModifierKey, copy, copy, copy, copy, copyInCast, dumpObject, equals, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAbilities, getAbilities, getAbilities, getAbilities, getClassIndex, getDefenceModifiers, getDisplayObject, getFeatureContainer, getFreeColObjectClass, getFreeColObjectClassByName, getGame, getId, getIdNumber, getIdType, getIdTypeByName, getModifiers, getModifiers, getModifiers, getModifiers, getObjectClassIndex, getPropertyChangeListeners, getPropertyChangeListeners, getPropertyChangeSupport, getSortedAbilities, getSortedModifiers, getSpecification, getSuffix, getSuffix, hasAbility, hasAbility, hasAbility, hashCode, hasListeners, hasModifier, hasModifier, hasModifier, idEquals, invokeMethod, logFreeColObjects, readChild, readChildren, readFromXML, readFromXMLPartial, removeAbilities, removeAbility, removeFeatures, removeModifier, removeModifiers, removePropertyChangeListener, removePropertyChangeListener, save, save, save, save, serialize, serialize, serialize, serialize, setGame, setId, setSpecification, toXML, toXML, toXMLPartial, toXMLPartial, writeChildren
-
-
-
-
Field Detail
-
TAG
public static final java.lang.String TAG
- See Also:
- Constant Field Values
-
roleId
private java.lang.String roleId
The role identifier of this AbstractUnit.
-
number
private int number
The number of units.
-
ROLE_TAG
private static final java.lang.String ROLE_TAG
- See Also:
- Constant Field Values
-
NUMBER_TAG
private static final java.lang.String NUMBER_TAG
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
AbstractUnit
public AbstractUnit()
Deliberately empty constructor, for Game.newInstance.
-
AbstractUnit
public AbstractUnit(java.lang.String id, java.lang.String roleId, int number)
Create a new AbstractUnit.- Parameters:
id
- The object identifier.roleId
- The unit role identifier.number
- A number of units.
-
AbstractUnit
public AbstractUnit(UnitType unitType, java.lang.String roleId, int number)
Create a new AbstractUnit.- Parameters:
unitType
- The type of unit to create.roleId
- The unit role identifier.number
- The number of units.
-
AbstractUnit
public AbstractUnit(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Creates a newAbstractUnit
instance.- Parameters:
xr
- TheFreeColXMLReader
to read from.- Throws:
javax.xml.stream.XMLStreamException
- if an error occurs
-
-
Method Detail
-
getRoleId
public final java.lang.String getRoleId()
Get the role identifier.- Returns:
- The role identifier.
-
setRoleId
public final void setRoleId(java.lang.String roleId)
Set the role identifier.- Parameters:
roleId
- The new role identifier.
-
getNumber
public final int getNumber()
Get the number of units.- Returns:
- The number of units.
-
setNumber
public final void setNumber(int newNumber)
Set the number of units.- Parameters:
newNumber
- The new number of units.
-
addToNumber
public final void addToNumber(int diff)
Add to the number.- Parameters:
diff
- The amount to add.
-
getSingleLabel
public StringTemplate getSingleLabel()
Gets a template describing this abstract unit with a fixed count of 1.- Returns:
- A
StringTemplate
describing the abstract unit.
-
getLabel
public StringTemplate getLabel()
Gets a template describing this abstract unit.- Returns:
- A
StringTemplate
describing the abstract unit.
-
getLabelInternal
private static StringTemplate getLabelInternal(java.lang.String typeId, java.lang.String roleId, int number)
Gets a template describing an arbitrary abstract unit.- Parameters:
typeId
- The unit type identifier.roleId
- The role identifier.number
- The number of units.- Returns:
- A
StringTemplate
describing the abstract unit.
-
getDescription
public java.lang.String getDescription()
Get a description of this abstract unit.- Returns:
- A
String
describing this abstract unit.
-
getType
public UnitType getType(Specification spec)
Convenience accessor for the unit type.- Parameters:
spec
- ASpecification
to look up the type in.- Returns:
- The
UnitType
of this abstract unit.
-
getRole
public Role getRole(Specification spec)
Convenience accessor for the role.- Parameters:
spec
- ASpecification
to look up the role in.- Returns:
- The
Role
of this abstract unit.
-
getOffence
public double getOffence(Specification spec)
Get the approximate offence power that an instantiated unit corresponding to this abstract form would have.- Parameters:
spec
- ASpecification
to look up.- Returns:
- The approximate offence power.
-
calculateStrength
public static double calculateStrength(Specification spec, java.util.List<AbstractUnit> units)
Calculate the approximate offence power of a list of units.- Parameters:
spec
- ASpecification
to look up the type in.units
- A list ofAbstractUnit
s.- Returns:
- The approximate offence power.
-
deepCopy
public static java.util.List<AbstractUnit> deepCopy(java.util.List<AbstractUnit> units)
Get a deep copy of a list of abstract units.- Parameters:
units
- The list ofAbstractUnit
s to copy.- Returns:
- A list of
AbstractUnit
s.
-
getListLabel
public static StringTemplate getListLabel(java.lang.String base, java.util.List<AbstractUnit> units)
Get a template for a list of abstract units.- Parameters:
base
- The label template base.units
- The list ofAbstractUnit
s to use.- Returns:
- A suitable
StringTemplate
.
-
matchAll
public boolean matchAll(AbstractUnit other)
Does another AbstractUnit match in all fields.- Parameters:
other
- The otherAbstractUnit
to test.- Returns:
- True if all fields match.
-
matcher
public static java.util.function.Predicate<AbstractUnit> matcher(UnitType ut, java.lang.String roleId)
Create a predicate to match the type+role of an abstract unit.- Parameters:
ut
- TheUnitType
to match.roleId
- The role identifier to match.- Returns:
- A suitable
Predicate
.
-
matcher
public static java.util.function.Predicate<AbstractUnit> matcher(AbstractUnit au)
Create a predicate to match the type+role of an abstract unit.- Parameters:
au
- TheAbstractUnit
to match.- Returns:
- A suitable
Predicate
.
-
matcher
public static java.util.function.Predicate<AbstractUnit> matcher(Unit unit)
Create a predicate to match the type+role of a unit.- Parameters:
unit
- TheUnit
to match.- Returns:
- A suitable
Predicate
.
-
matchUnits
public static boolean matchUnits(java.util.List<AbstractUnit> l1, java.util.List<AbstractUnit> l2)
Do two lists of abstract units match completely.- Parameters:
l1
- The firstAbstractUnit
.l2
- The firstAbstractUnit
.- Returns:
- True if the lists match.
-
copyIn
public <T extends FreeColObject> boolean copyIn(T other)
Copy another FreeColObject into this one if it is compatible.- Overrides:
copyIn
in classFreeColObject
- Type Parameters:
T
- TheFreeColObject
subclass of the object to copy in.- Parameters:
other
- The other object.- Returns:
- True if the copy in is succesful.
-
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.- Overrides:
writeAttributes
in classFreeColObject
- Parameters:
xw
- TheFreeColXMLWriter
to write to.- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
readAttributes
protected final void readAttributes(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads the attributes of this object from an XML stream.- Overrides:
readAttributes
in classFreeColObject
- Parameters:
xr
- TheFreeColXMLReader
to read from.- Throws:
javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
getXMLTagName
public java.lang.String getXMLTagName()
Get the serialization tag for this object.- Specified by:
getXMLTagName
in classFreeColObject
- Returns:
- The tag.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classFreeColObject
-
-