Class NativeTrade

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

    public class NativeTrade
    extends FreeColGameObject
    Container class for the information that persists while a native trade session is underway.
    • Field Detail

      • logger

        private static final java.util.logging.Logger logger
      • abortTrade

        private static final StringTemplate abortTrade
        A template to use as a magic cookie for aborted trades.
      • unit

        private Unit unit
        The unit that is trading.
      • count

        private int count
        How many times this trade has been tried.
      • buy

        private boolean buy
        True if no purchases made in this trade.
      • sell

        private boolean sell
        True if no sales made in this trade.
      • gift

        private boolean gift
        True if no gifts made in this trade.
      • item

        private NativeTradeItem item
        An item under consideration for a transaction.
      • unitToSettlement

        private java.util.List<NativeTradeItem> unitToSettlement
        The goods on the unit that are being offered for purchase by the settlement.
      • settlementToUnit

        private java.util.List<NativeTradeItem> settlementToUnit
        The goods in the settlement that are being offered for purchase by the unit.
      • SETTLEMENT_TO_UNIT_TAG

        private static final java.lang.String SETTLEMENT_TO_UNIT_TAG
        See Also:
        Constant Field Values
      • UNIT_TO_SETTLEMENT_TAG

        private static final java.lang.String UNIT_TO_SETTLEMENT_TAG
        See Also:
        Constant Field Values
    • Constructor Detail

      • NativeTrade

        public NativeTrade​(Game game,
                           java.lang.String id)
        Simple constructor, used in Game.newInstance.
        Parameters:
        game - The enclosing Game.
        id - The identifier (ignored).
      • NativeTrade

        public NativeTrade​(Unit unit,
                           IndianSettlement is)
        Create a new trade session.
        Parameters:
        unit - The Unit that is trading.
        is - The IndianSettlement to trade with.
    • Method Detail

      • atWar

        private boolean atWar()
        Check if the trade participants are at war.
        Returns:
        True if the traders are at war.
      • getKey

        public java.lang.String getKey()
        Get a key for this transaction.
        Returns:
        A suitable key.
      • getNativeTradeKey

        public static java.lang.String getNativeTradeKey​(Unit unit,
                                                         IndianSettlement is)
        Make a transaction key for a native trade.
        Parameters:
        unit - The Unit that is trading.
        is - The IndianSettlement that is trading.
        Returns:
        A suitable key.
      • getUnit

        public Unit getUnit()
        Get the unit that is trading.
        Returns:
        The Unit that started the trade.
      • getIndianSettlement

        public IndianSettlement getIndianSettlement()
        Get the settlement that is trading.
        Returns:
        The IndianSettlement that is traded with.
      • getBuy

        public boolean getBuy()
        Is purchasing available in this transaction?
        Returns:
        True if no blocking purchase has been made.
      • canBuy

        public boolean canBuy()
        Can the unit owner buy more items in this session at present?
        Returns:
        True if not blocked, at peace, there are purchases to be made and space available.
      • setBuy

        public void setBuy​(boolean buy)
        Set the purchase state.
        Parameters:
        buy - The new purchase state.
      • getSell

        public boolean getSell()
        Is selling available in this transaction?
        Returns:
        True if no blocking sale has been made.
      • canSell

        public boolean canSell()
        Can the unit owner buy more items in this session at present?
        Returns:
        True if not blocked, at peace, and there are sales to be made.
      • setSell

        public void setSell​(boolean sell)
        Set the sale state.
        Parameters:
        sell - The new sale state.
      • getGift

        public boolean getGift()
        Is giving available in this transaction?
        Returns:
        True if no blocking gift has been made.
      • canGift

        public boolean canGift()
        Can the unit owner give more items in this session at present?
        Returns:
        True if not blocked, and the unit has gifts to give.
      • setGift

        public void setGift​(boolean gift)
        Set the gift state.
        Parameters:
        gift - The new gift state.
      • hasNotTraded

        public boolean hasNotTraded()
        Have no trades been performed in this transaction?
        Returns:
        True if no blocking purchase, sale or gift has occurred.
      • getCount

        public int getCount()
        Get the transaction count.
        Returns:
        The transaction count.
      • setCount

        public void setCount​(int count)
        Set the transaction count.
        Parameters:
        count - The new transaction count.
      • getDone

        public boolean getDone()
        Is this transaction complete?
        Returns:
        True if the transaction is over.
      • setDone

        public void setDone()
        Set this transaction as complete.
      • getItem

        public NativeTradeItem getItem()
        Get the item being traded.
        Returns:
        The current NativeTradeItem.
      • setItem

        public void setItem​(NativeTradeItem nti)
        Set the item being traded.
        Parameters:
        nti - The new NativeTradeItem.
      • getUnitToSettlement

        public java.util.List<NativeTradeItem> getUnitToSettlement()
        Get the list of items the unit is able to offer the settlement. Note: some of these items might be currently invalid.
        Returns:
        A list of NativeTradeItems the unit might sell.
      • getSettlementToUnit

        public java.util.List<NativeTradeItem> getSettlementToUnit()
        Get the list of items the settlement is able to offer the unit. Note: some of these items might be currently invalid.
        Returns:
        A list of NativeTradeItems the unit might buy.
      • addToUnit

        public void addToUnit​(NativeTradeItem nti)
        Add an item to the unit list of items.
        Parameters:
        nti - The NativeTradeItem to add.
      • removeFromUnit

        public void removeFromUnit​(NativeTradeItem nti)
        Remove an item from the unit list of items.
        Parameters:
        nti - The NativeTradeItem to remove.
      • isCompatible

        public boolean isCompatible​(NativeTrade nt)
        Is another native trade compatible with this one?
        Parameters:
        nt - The other NativeTrade.
        Returns:
        True if the other trade is compatible.
      • initialize

        public void initialize()
        Raw initialization of the unit and settlement list. Does not do pricing!
      • mergeFrom

        public void mergeFrom​(NativeTrade nt)
        Merge another compatible native trade into this one.
        Parameters:
        nt - The NativeTrade to merge.
      • limitSettlementToUnit

        public void limitSettlementToUnit​(int n)
        Limit the number of items offered by the settlement. Used in the client to implement a Col1 restriction.
        Parameters:
        n - The number of items to offer.
      • haggleUp

        public static int haggleUp​(int price)
        Choose the next available upward haggling price.
        Parameters:
        price - The initial price.
        Returns:
        The new upward haggled price.
      • haggleDown

        public static int haggleDown​(int price)
        Choose the next available downward haggling price.
        Parameters:
        price - The initial price.
        Returns:
        The new downward haggled price.
      • isInternable

        public boolean isInternable()
        Should this object be interned into its Game? Usually true, but there are some special containers that have to be FCGOs but are unsuitable to be interned. These classes will override this routine.
        Overrides:
        isInternable in class FreeColGameObject
        Returns:
        True if this object should be interned.
      • copyIn

        public <T extends FreeColObject> boolean copyIn​(T other)
        Copy another FreeColObject into this one if it is compatible.
        Overrides:
        copyIn in class FreeColGameObject
        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 FreeColObject
        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 FreeColGameObject
        Parameters:
        xr - The FreeColXMLReader to read from.
        Throws:
        javax.xml.stream.XMLStreamException - if there is a problem reading the stream.
      • readChildren

        protected void readChildren​(FreeColXMLReader xr)
                             throws javax.xml.stream.XMLStreamException
        Reads the children of this object from an XML stream.
        Overrides:
        readChildren in class FreeColObject
        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 FreeColObject
        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.