Class Market

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

    public final class Market
    extends FreeColGameObject
    implements Ownable
    This class implements a simple economic model where a market holds all goods to be sold and the price of a particular goods type is determined solely by its availability in that market.
    • Field Detail

      • logger

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

        public static final int MINIMUM_AMOUNT
        European markets are bottomless. Goods present never decrease below this threshold.
        See Also:
        Constant Field Values
      • marketData

        private final java.util.Map<GoodsType,​MarketData> marketData
        The contents of the market, keyed by goods type.
      • owner

        private Player owner
        The owning player.
      • transactionListeners

        private final java.util.ArrayList<TransactionListener> transactionListeners
        Watching listeners.
    • Constructor Detail

      • Market

        public Market​(Game game,
                      Player player)
        Main constructor for creating a market for a new player.
        Parameters:
        game - The enclosing Game.
        player - The Player to own the market.
      • Market

        public Market​(Game game,
                      java.lang.String id)
        Creates a new Market with the given identifier. The object should be initialized later.
        Parameters:
        game - The enclosing Game.
        id - The object identifier.
    • Method Detail

      • getMarketData

        private java.util.Map<GoodsType,​MarketData> getMarketData()
        Get the market data.
        Returns:
        The map of goods type to market data.
      • getMarketDataValues

        public java.util.Collection<MarketData> getMarketDataValues()
        Get the market data values.
        Returns:
        The market data in this market.
      • getMarketData

        public MarketData getMarketData​(GoodsType goodsType)
        Gets the market data for a type of goods. Public so the server can send individual MarketData updates.
        Parameters:
        goodsType - The GoodsType to look for.
        Returns:
        The corresponding MarketData, or null if none.
      • putMarketData

        private void putMarketData​(GoodsType goodsType,
                                   MarketData data)
        Set the market data for a given goods type.
        Parameters:
        goodsType - The GoodsType to set the market data for.
        data - The new MarketData.
      • requireMarketData

        private MarketData requireMarketData​(GoodsType goodsType)
        Gets the market data for a specified goods type, creating it if it does not exist yet.
        Parameters:
        goodsType - The GoodsType to query.
        Returns:
        The MarketData required.
      • setMarketData

        private void setMarketData​(java.util.Map<GoodsType,​MarketData> data)
        Set the market data.
        Parameters:
        data - The new market data.
      • clearMarketData

        private void clearMarketData()
        Clear the market data.
      • hasBeenTraded

        public boolean hasBeenTraded​(GoodsType type)
        Has a type of goods been traded in this market?
        Parameters:
        type - The type of goods to consider.
        Returns:
        True if the goods type has been traded.
      • getCostToBuy

        public int getCostToBuy​(GoodsType type)
        Determines the cost to buy a single unit of a particular type of good.
        Parameters:
        type - A GoodsType value.
        Returns:
        The cost to buy a single unit of the given type of goods.
      • getPaidForSale

        public int getPaidForSale​(GoodsType type)
        Determines the price paid for the sale of a single unit of a particular type of goods.
        Parameters:
        type - A GoodsType value.
        Returns:
        The price for a single unit of the given type of goods if it is for sale.
      • addGoodsToMarket

        public boolean addGoodsToMarket​(GoodsType goodsType,
                                        int amount)
        Add (or remove) some goods to this market.
        Parameters:
        goodsType - The GoodsType to add.
        amount - The amount of goods.
        Returns:
        True if the price changes as a result of this addition.
      • getInitialPrice

        public int getInitialPrice​(GoodsType goodsType)
        Gets the initial price of a given goods type.
        Parameters:
        goodsType - The GoodsType to get the initial price of.
        Returns:
        The initial price.
      • setInitialPrice

        public void setInitialPrice​(GoodsType goodsType,
                                    int amount)
        Sets the initial price of a given goods type.
        Parameters:
        goodsType - The GoodsType to set the initial price of.
        amount - The new initial price.
      • getBidPrice

        public int getBidPrice​(GoodsType type,
                               int amount)
        Gets the price of a given goods when the Player buys.
        Parameters:
        type - a GoodsType value
        amount - The amount of goods.
        Returns:
        The bid price of the given goods.
      • getSalePrice

        public int getSalePrice​(GoodsType type,
                                int amount)
        Gets the price of a given goods when the Player sells.
        Parameters:
        type - a GoodsType value
        amount - The amount of goods.
        Returns:
        The sale price of the given goods.
      • getSalePrice

        public <T extends AbstractGoods> int getSalePrice​(T goods)
        Gets the price of a given goods when the Player sells.
        Type Parameters:
        T - The base type of the goods.
        Parameters:
        goods - The Goods to evaluate.
        Returns:
        The price.
      • getArrears

        public int getArrears​(GoodsType goodsType)
        Gets the arrears for of a given goods type.
        Parameters:
        goodsType - The GoodsType to get arrears for.
        Returns:
        The arrears.
      • setArrears

        public void setArrears​(GoodsType goodsType,
                               int value)
        Sets the arrears associated with a type of goods.
        Parameters:
        goodsType - The GoodsType to set the arrears for.
        value - The amount of arrears to set.
      • getSales

        public int getSales​(GoodsType goodsType)
        Gets the sales of a type of goods.
        Parameters:
        goodsType - The GoodsType to get the sales for.
        Returns:
        The current sales amount.
      • modifySales

        public void modifySales​(GoodsType goodsType,
                                int amount)
        Modifies the sales of a type of goods.
        Parameters:
        goodsType - The GoodsType to set the sales for.
        amount - The amount of sales to add to the current amount.
      • getIncomeBeforeTaxes

        public int getIncomeBeforeTaxes​(GoodsType goodsType)
        Gets the income before taxes for a type of goods.
        Parameters:
        goodsType - The GoodsType to get the income for.
        Returns:
        The current income before taxes.
      • modifyIncomeBeforeTaxes

        public void modifyIncomeBeforeTaxes​(GoodsType goodsType,
                                            int amount)
        Modifies the income before taxes on sales of a type of goods.
        Parameters:
        goodsType - The GoodsType to set the income for.
        amount - The amount of tax income to add to the current amount.
      • getIncomeAfterTaxes

        public int getIncomeAfterTaxes​(GoodsType goodsType)
        Gets the income after taxes for a type of goods.
        Parameters:
        goodsType - The GoodsType to get the income for.
        Returns:
        The current income after taxes.
      • modifyIncomeAfterTaxes

        public void modifyIncomeAfterTaxes​(GoodsType goodsType,
                                           int amount)
        Modifies the income after taxes on sales of a type of goods.
        Parameters:
        goodsType - The GoodsType to set the income for.
        amount - The amount of tax income to add to the current amount.
      • getAmountInMarket

        public int getAmountInMarket​(GoodsType goodsType)
        Gets the amount of a goods type in the market.
        Parameters:
        goodsType - The GoodsType to get the amount of.
        Returns:
        The current amount of the goods in the market.
      • hasPriceChanged

        public boolean hasPriceChanged​(GoodsType goodsType)
        Has the price of a type of goods changed in this market?
        Parameters:
        goodsType - The type of goods to consider.
        Returns:
        True if the price has changed.
      • flushPriceChange

        public void flushPriceChange​(GoodsType goodsType)
        Clear any price changes for a type of goods.
        Parameters:
        goodsType - The type of goods to consider.
      • makePriceChangeMessage

        public ModelMessage makePriceChangeMessage​(GoodsType goodsType)
        Make up a ModelMessage describing the change in this Market for a specified type of goods.
        Parameters:
        goodsType - The GoodsType that has changed price.
        Returns:
        A message describing the change.
      • update

        public void update​(GoodsType goodsType)
        Update the price for a type of goods, bypassing the price change clamping. Used to reset the prices when the initial price is randomized. Do not use during the game, the price change clamping mechanism should remain in effect.
        Parameters:
        goodsType - The GoodsType to update.
      • getSalePriceComparator

        public <T extends AbstractGoods> java.util.Comparator<T> getSalePriceComparator()
        Get a sale price comparator for this market.
        Type Parameters:
        T - The AbstractGoods type to compare.
        Returns:
        A suitable Comparator.
      • addTransactionListener

        public void addTransactionListener​(TransactionListener listener)
        Adds a transaction listener for notification of any transaction
        Parameters:
        listener - The TransactionListener to add.
      • removeTransactionListener

        public void removeTransactionListener​(TransactionListener listener)
        Removes a transaction listener
        Parameters:
        listener - The TransactionListener to remove.
      • getTransactionListener

        public TransactionListener[] getTransactionListener()
        Gets the listeners added to this market.
        Returns:
        An array of all the TransactionListeners added, or an empty array if none found.
      • getOwner

        public Player getOwner()
        Gets the owner of this Ownable.
        Specified by:
        getOwner in interface Ownable
        Returns:
        The Player controlling this Ownable.
      • setOwner

        public void setOwner​(Player owner)
        Sets the owner of this Ownable.
        Specified by:
        setOwner in interface Ownable
        Parameters:
        owner - The Player that should take ownership of this Ownable.
      • getLinkTarget

        public FreeColGameObject getLinkTarget​(Player player)
        Get a suitable game object to use as a clickable link in messages to a player. Objects do not have links by default, hence the null return here. However, for example, a player's colony should return itself as a link target.
        Overrides:
        getLinkTarget in class FreeColGameObject
        Parameters:
        player - The Player to make a link for.
        Returns:
        A suitable link target if available, although usually null.
      • 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.