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
TheLimit
class 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 class
Limit.Operator
The basic operation used in evaluating this limit.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
LEFT_HAND_SIDE_TAG
private Operand
leftHandSide
The left hand side term of the limit expression.private static java.lang.String
OLD_LEFT_HAND_SIDE_TAG
private static java.lang.String
OLD_RIGHT_HAND_SIDE_TAG
private Limit.Operator
operator
The operator to apply when evaluating the limit expression.private static java.lang.String
OPERATOR_TAG
private static java.lang.String
RIGHT_HAND_SIDE_TAG
private Operand
rightHandSide
The right hand side term of the limit expression.static java.lang.String
TAG
-
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 boolean
appliesTo(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 boolean
evaluate(java.lang.Integer lhs, java.lang.Integer rhs)
Evaluate two integers using the limit operator.boolean
evaluate(Game game)
Evaluate this limit within a game.boolean
evaluate(Player player)
Evaluate this limit with respect to a player.boolean
evaluate(Settlement settlement)
Evaluate this limit with respect to a player.Operand
getLeftHandSide()
Get the left hand sideOperand
.Limit.Operator
getOperator()
Get theOperator
value.Operand
getRightHandSide()
Get the right hand sideOperand
.java.lang.String
getXMLTagName()
Get the serialization tag for this object.boolean
hasOperandType(Operand.OperandType type)
Check if at least one of the Operands has a given OperandType.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.void
setLeftHandSide(Operand newLeftHandSide)
Set the left hand sideOperand
.void
setOperator(Limit.Operator newOperator)
void
setRightHandSide(Operand newRightHandSide)
Set the right hand sideOperand
.java.lang.String
toString()
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.-
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
- TheSpecification
to refer to.
-
Limit
public Limit(FreeColXMLReader xr, Specification specification) throws javax.xml.stream.XMLStreamException
Create a new limit.- Parameters:
xr
- TheFreeColXMLReader
to read from.specification
- TheSpecification
to 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
- TheOperator
to apply.rhs
- The right hand sideOperand
.
-
-
Method Detail
-
getOperator
public Limit.Operator getOperator()
Get theOperator
value.- Returns:
- The
Operator
of 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:
appliesTo
in 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
- TheGame
to use.- Returns:
- The result of the evaluation.
-
evaluate
public boolean evaluate(Player player)
Evaluate this limit with respect to a player.- Parameters:
player
- ThePlayer
to use.- Returns:
- The result of the evaluation.
-
evaluate
public boolean evaluate(Settlement settlement)
Evaluate this limit with respect to a player.- Parameters:
settlement
- TheSettlement
to 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
- TheOperandType
to 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:
copyIn
in classFreeColSpecObjectType
- 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.
-
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:
writeChildren
in classFreeColSpecObjectType
- Parameters:
xw
- TheFreeColXMLWriter
to 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:
readAttributes
in classFreeColSpecObjectType
- 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.- Overrides:
readChild
in classFreeColSpecObjectType
- 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 classFreeColSpecObjectType
-
-