Package net.sf.freecol.common.model
Class Limit
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- net.sf.freecol.common.model.FreeColSpecObject
-
- net.sf.freecol.common.model.FreeColSpecObjectType
-
- net.sf.freecol.common.model.Limit
-
- All Implemented Interfaces:
java.lang.Comparable<FreeColObject>,Named,ObjectWithId
public final class Limit extends FreeColSpecObjectType
TheLimitclass encapsulates a limit on the availability of FreeColObjects. It consists of a left hand side, an operator and a right hand side, and can be used to limit the number of units of a particular type (e.g. wagon trains) to the number of a player's colonies, for example. The left hand side must apply to the object on which a limit is to be placed.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classLimit.OperatorThe basic operation used in evaluating this limit.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringLEFT_HAND_SIDE_TAGprivate OperandleftHandSideThe left hand side term of the limit expression.private static java.lang.StringOLD_LEFT_HAND_SIDE_TAGprivate static java.lang.StringOLD_RIGHT_HAND_SIDE_TAGprivate Limit.OperatoroperatorThe operator to apply when evaluating the limit expression.private static java.lang.StringOPERATOR_TAGprivate static java.lang.StringRIGHT_HAND_SIDE_TAGprivate OperandrightHandSideThe right hand side term of the limit expression.static java.lang.StringTAG-
Fields inherited from class net.sf.freecol.common.model.FreeColSpecObjectType
DELETE_TAG, EXTENDS_TAG, PRESERVE_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 Limit(java.lang.String id, Operand lhs, Limit.Operator op, Operand rhs)Create a new limit.Limit(FreeColXMLReader xr, Specification specification)Create a new limit.Limit(Specification specification)Create a new limit.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanappliesTo(FreeColObject object)Does this limit apply to an object?<T extends FreeColObject>
booleancopyIn(T other)Copy another FreeColObject into this one if it is compatible.private booleanevaluate(java.lang.Integer lhs, java.lang.Integer rhs)Evaluate two integers using the limit operator.booleanevaluate(Game game)Evaluate this limit within a game.booleanevaluate(Player player)Evaluate this limit with respect to a player.booleanevaluate(Settlement settlement)Evaluate this limit with respect to a player.OperandgetLeftHandSide()Get the left hand sideOperand.Limit.OperatorgetOperator()Get theOperatorvalue.OperandgetRightHandSide()Get the right hand sideOperand.java.lang.StringgetXMLTagName()Get the serialization tag for this object.booleanhasOperandType(Operand.OperandType type)Check if at least one of the Operands has a given OperandType.protected voidreadAttributes(FreeColXMLReader xr)Reads the attributes of this object from an XML stream.protected voidreadChild(FreeColXMLReader xr)Reads a single child object.voidsetLeftHandSide(Operand newLeftHandSide)Set the left hand sideOperand.voidsetOperator(Limit.Operator newOperator)voidsetRightHandSide(Operand newRightHandSide)Set the right hand sideOperand.java.lang.StringtoString()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.-
Methods inherited from class net.sf.freecol.common.model.FreeColSpecObjectType
addScope, copyScopes, getDescriptionKey, getFeatureContainer, getIndex, getNameKey, getScopeList, getScopes, isAbstractType, readChildren, removeScope, setIndex
-
Methods inherited from class net.sf.freecol.common.model.FreeColSpecObject
getSpecification, setSpecification
-
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, getFreeColObjectClass, getFreeColObjectClassByName, getGame, getId, getIdNumber, getIdType, getIdTypeByName, getModifiers, getModifiers, getModifiers, getModifiers, getObjectClassIndex, getPropertyChangeListeners, getPropertyChangeListeners, getPropertyChangeSupport, getSortedAbilities, getSortedModifiers, getSuffix, getSuffix, hasAbility, hasAbility, hasAbility, hashCode, hasListeners, hasModifier, hasModifier, hasModifier, idEquals, invokeMethod, logFreeColObjects, readFromXML, readFromXMLPartial, removeAbilities, removeAbility, removeFeatures, removeModifier, removeModifiers, removePropertyChangeListener, removePropertyChangeListener, save, save, save, save, serialize, serialize, serialize, serialize, setGame, setId, toXML, toXML, toXMLPartial, toXMLPartial
-
-
-
-
Field Detail
-
TAG
public static final java.lang.String TAG
- See Also:
- Constant Field Values
-
operator
private Limit.Operator operator
The operator to apply when evaluating the limit expression.
-
leftHandSide
private Operand leftHandSide
The left hand side term of the limit expression.
-
rightHandSide
private Operand rightHandSide
The right hand side term of the limit expression.
-
LEFT_HAND_SIDE_TAG
private static final java.lang.String LEFT_HAND_SIDE_TAG
- See Also:
- Constant Field Values
-
OPERATOR_TAG
private static final java.lang.String OPERATOR_TAG
- See Also:
- Constant Field Values
-
RIGHT_HAND_SIDE_TAG
private static final java.lang.String RIGHT_HAND_SIDE_TAG
- See Also:
- Constant Field Values
-
OLD_LEFT_HAND_SIDE_TAG
private static final java.lang.String OLD_LEFT_HAND_SIDE_TAG
- See Also:
- Constant Field Values
-
OLD_RIGHT_HAND_SIDE_TAG
private static final java.lang.String OLD_RIGHT_HAND_SIDE_TAG
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Limit
public Limit(Specification specification)
Create a new limit.- Parameters:
specification- TheSpecificationto refer to.
-
Limit
public Limit(FreeColXMLReader xr, Specification specification) throws javax.xml.stream.XMLStreamException
Create a new limit.- Parameters:
xr- TheFreeColXMLReaderto read from.specification- TheSpecificationto refer to.- Throws:
javax.xml.stream.XMLStreamException- if there is a problem reading.
-
Limit
public Limit(java.lang.String id, Operand lhs, Limit.Operator op, Operand rhs)Create a new limit.- Parameters:
id- The object identifier.lhs- The left hand sideOperand.op- TheOperatorto apply.rhs- The right hand sideOperand.
-
-
Method Detail
-
getOperator
public Limit.Operator getOperator()
Get theOperatorvalue.- Returns:
- The
Operatorof this limit.
-
setOperator
public void setOperator(Limit.Operator newOperator)
-
getLeftHandSide
public Operand getLeftHandSide()
Get the left hand sideOperand.- Returns:
- The left hand side
Operand.
-
setLeftHandSide
public void setLeftHandSide(Operand newLeftHandSide)
Set the left hand sideOperand.- Parameters:
newLeftHandSide- The new left hand sideOperand.
-
getRightHandSide
public Operand getRightHandSide()
Get the right hand sideOperand.- Returns:
- The right hand side
Operand.
-
setRightHandSide
public void setRightHandSide(Operand newRightHandSide)
Set the right hand sideOperand.- Parameters:
newRightHandSide- The new right hand sideOperand.
-
appliesTo
public boolean appliesTo(FreeColObject object)
Does this limit apply to an object?- Overrides:
appliesToin classFreeColSpecObjectType- Parameters:
object- The object to test.- Returns:
- True if the limit is applicable.
-
evaluate
public boolean evaluate(Game game)
Evaluate this limit within a game.- Parameters:
game- TheGameto use.- Returns:
- The result of the evaluation.
-
evaluate
public boolean evaluate(Player player)
Evaluate this limit with respect to a player.- Parameters:
player- ThePlayerto use.- Returns:
- The result of the evaluation.
-
evaluate
public boolean evaluate(Settlement settlement)
Evaluate this limit with respect to a player.- Parameters:
settlement- TheSettlementto use.- Returns:
- The result of the evaluation.
-
hasOperandType
public boolean hasOperandType(Operand.OperandType type)
Check if at least one of the Operands has a given OperandType.- Parameters:
type- TheOperandTypeto check for.- Returns:
- True if the type is present.
-
evaluate
private boolean evaluate(java.lang.Integer lhs, java.lang.Integer rhs)Evaluate two integers using the limit operator.- Parameters:
lhs- The left hand sideInteger.rhs- The right hand sideInteger.- Returns:
- The result of the evaluation.
-
copyIn
public <T extends FreeColObject> boolean copyIn(T other)
Copy another FreeColObject into this one if it is compatible.- Overrides:
copyInin classFreeColSpecObjectType- 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.
-
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.- Overrides:
writeChildrenin classFreeColSpecObjectType- Parameters:
xw- TheFreeColXMLWriterto write to.- Throws:
javax.xml.stream.XMLStreamException- if there are any problems writing to the stream.
-
readAttributes
protected void readAttributes(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads the attributes of this object from an XML stream.- Overrides:
readAttributesin classFreeColSpecObjectType- 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.- Overrides:
readChildin classFreeColSpecObjectType- 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 classFreeColSpecObjectType
-
-