Class Limit

  • All Implemented Interfaces:
    java.lang.Comparable<FreeColObject>, Named, ObjectWithId

    public final class Limit
    extends FreeColSpecObjectType
    The Limit 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.
    • Field Detail

      • 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
      • 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 - The Specification to refer to.
      • Limit

        public Limit​(FreeColXMLReader xr,
                     Specification specification)
              throws javax.xml.stream.XMLStreamException
        Create a new limit.
        Parameters:
        xr - The FreeColXMLReader to read from.
        specification - The Specification 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 side Operand.
        op - The Operator to apply.
        rhs - The right hand side Operand.
    • Method Detail

      • getOperator

        public Limit.Operator getOperator()
        Get the Operator value.
        Returns:
        The Operator of this limit.
      • setOperator

        public void setOperator​(Limit.Operator newOperator)
      • getLeftHandSide

        public Operand getLeftHandSide()
        Get the left hand side Operand.
        Returns:
        The left hand side Operand.
      • setLeftHandSide

        public void setLeftHandSide​(Operand newLeftHandSide)
        Set the left hand side Operand.
        Parameters:
        newLeftHandSide - The new left hand side Operand.
      • getRightHandSide

        public Operand getRightHandSide()
        Get the right hand side Operand.
        Returns:
        The right hand side Operand.
      • setRightHandSide

        public void setRightHandSide​(Operand newRightHandSide)
        Set the right hand side Operand.
        Parameters:
        newRightHandSide - The new right hand side Operand.
      • appliesTo

        public boolean appliesTo​(FreeColObject object)
        Does this limit apply to an object?
        Overrides:
        appliesTo in class FreeColSpecObjectType
        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 - The Game to use.
        Returns:
        The result of the evaluation.
      • evaluate

        public boolean evaluate​(Player player)
        Evaluate this limit with respect to a player.
        Parameters:
        player - The Player to use.
        Returns:
        The result of the evaluation.
      • evaluate

        public boolean evaluate​(Settlement settlement)
        Evaluate this limit with respect to a player.
        Parameters:
        settlement - The Settlement 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 - The OperandType 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 side Integer.
        rhs - The right hand side Integer.
        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 class FreeColSpecObjectType
        Type Parameters:
        T - The FreeColObject 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 class FreeColObject
        Parameters:
        xw - The FreeColXMLWriter 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 class FreeColSpecObjectType
        Parameters:
        xw - The FreeColXMLWriter 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 class FreeColSpecObjectType
        Parameters:
        xr - The FreeColXMLReader 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 class FreeColSpecObjectType
        Parameters:
        xr - The FreeColXMLReader 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 class FreeColObject
        Returns:
        The tag.