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 intnumberThe number of units.private static java.lang.StringNUMBER_TAGprivate static java.lang.StringROLE_TAGprivate java.lang.StringroleIdThe role identifier of this AbstractUnit.static java.lang.StringTAG-
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 newAbstractUnitinstance.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 voidaddToNumber(int diff)Add to the number.static doublecalculateStrength(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.StringgetDescription()Get a description of this abstract unit.StringTemplategetLabel()Gets a template describing this abstract unit.private static StringTemplategetLabelInternal(java.lang.String typeId, java.lang.String roleId, int number)Gets a template describing an arbitrary abstract unit.static StringTemplategetListLabel(java.lang.String base, java.util.List<AbstractUnit> units)Get a template for a list of abstract units.intgetNumber()Get the number of units.doublegetOffence(Specification spec)Get the approximate offence power that an instantiated unit corresponding to this abstract form would have.RolegetRole(Specification spec)Convenience accessor for the role.java.lang.StringgetRoleId()Get the role identifier.StringTemplategetSingleLabel()Gets a template describing this abstract unit with a fixed count of 1.UnitTypegetType(Specification spec)Convenience accessor for the unit type.java.lang.StringgetXMLTagName()Get the serialization tag for this object.booleanmatchAll(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 booleanmatchUnits(java.util.List<AbstractUnit> l1, java.util.List<AbstractUnit> l2)Do two lists of abstract units match completely.protected voidreadAttributes(FreeColXMLReader xr)Reads the attributes of this object from an XML stream.voidsetNumber(int newNumber)Set the number of units.voidsetRoleId(java.lang.String roleId)Set the role identifier.java.lang.StringtoString()protected voidwriteAttributes(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 newAbstractUnitinstance.- Parameters:
xr- TheFreeColXMLReaderto 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
StringTemplatedescribing the abstract unit.
-
getLabel
public StringTemplate getLabel()
Gets a template describing this abstract unit.- Returns:
- A
StringTemplatedescribing 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
StringTemplatedescribing the abstract unit.
-
getDescription
public java.lang.String getDescription()
Get a description of this abstract unit.- Returns:
- A
Stringdescribing this abstract unit.
-
getType
public UnitType getType(Specification spec)
Convenience accessor for the unit type.- Parameters:
spec- ASpecificationto look up the type in.- Returns:
- The
UnitTypeof this abstract unit.
-
getRole
public Role getRole(Specification spec)
Convenience accessor for the role.- Parameters:
spec- ASpecificationto look up the role in.- Returns:
- The
Roleof 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- ASpecificationto 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- ASpecificationto look up the type in.units- A list ofAbstractUnits.- 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 ofAbstractUnits to copy.- Returns:
- A list of
AbstractUnits.
-
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 ofAbstractUnits to use.- Returns:
- A suitable
StringTemplate.
-
matchAll
public boolean matchAll(AbstractUnit other)
Does another AbstractUnit match in all fields.- Parameters:
other- The otherAbstractUnitto 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- TheUnitTypeto 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- TheAbstractUnitto 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- TheUnitto 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:
copyInin classFreeColObject- Type Parameters:
T- TheFreeColObjectsubclass 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:
writeAttributesin classFreeColObject- Parameters:
xw- TheFreeColXMLWriterto 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:
readAttributesin classFreeColObject- Parameters:
xr- TheFreeColXMLReaderto 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:
getXMLTagNamein classFreeColObject- Returns:
- The tag.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classFreeColObject
-
-