net.sf.freecol.common.model
Class Market

java.lang.Object
  extended by net.sf.freecol.common.model.FreeColObject
      extended by net.sf.freecol.common.model.FreeColGameObject
          extended by net.sf.freecol.common.model.Market
All Implemented Interfaces:
Ownable

public final class Market
extends FreeColGameObject
implements Ownable

This class implements a simple economic model whereby a market holds all goods that have been sold and the price of a particular type of good is determined solely by its availability in that market.


Field Summary
static int CUSTOM_HOUSE
          Constant for specifying the access to this Market when buying and selling goods.
static int EUROPE
          Constant for specifying the access to this Market when buying and selling goods.
private  java.util.Map<GoodsType,MarketData> marketData
           
static int MAXIMUM_PRICE
           
static int MINIMUM_PRICE
           
private  Player owner
           
private  java.util.ArrayList<TransactionListener> transactionListeners
           
 
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE, ID_ATTRIBUTE, ID_ATTRIBUTE_TAG, PARTIAL_ATTRIBUTE
 
Constructor Summary
Market(Game game, org.w3c.dom.Element e)
          Initiates a new Market from an XML representation.
Market(Game game, Player player)
           
Market(Game game, java.lang.String id)
          Initiates a new Market with the given ID.
Market(Game game, javax.xml.stream.XMLStreamReader in)
          Initiates a new Market from an XML representation.
 
Method Summary
 boolean addGoodsToMarket(GoodsType goodsType, int amount)
          Add (or remove) some goods to this market.
 void addTransactionListener(TransactionListener listener)
          Adds a transaction listener for notification of any transaction
 void buy(GoodsType goodsType, int amount, Player player)
          Buys an amount of a particular type of good with the cost being met by a supplied player.
 int costToBuy(GoodsType type)
          Determines the cost to buy a single unit of a particular type of good.
 int getBidPrice(GoodsType type, int amount)
          Gets the price of a given goods when the Player buys.
 MarketData getMarketData(GoodsType goodsType)
          Return the market data for a type of goods.
 Player getOwner()
          Gets the owner of this Market.
 int getSalePrice(Goods goods)
          Gets the price of a given goods when the Player sells.
 int getSalePrice(GoodsType type, int amount)
          Gets the price of a given goods when the Player sells.
 TransactionListener[] getTransactionListener()
          Returns an array of all the TransactionListener added to this Market.
static java.lang.String getXMLElementTagName()
          Returns the tag name of the root element representing this object.
 ModelMessage makePriceMessage(GoodsType goodsType)
          Make up a ModelMessage describing the change in this Market for a specified type of goods.
 int paidForSale(GoodsType type)
          Determines the price paid for the sale of a single unit of a particular type of goods.
private  boolean priceGoods(GoodsType goodsType)
          Adjust the price for a particular type of goods.
 void putMarketData(GoodsType goodsType, MarketData data)
          Describe putMarketData method here.
protected  void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
          Initialize this object from an XML-representation of this object.
 void removeTransactionListener(TransactionListener listener)
          Removes a transaction listener
 void sell(Goods goods, Player player)
          Sells an amount of a particular type of good with the proceeds of the sale being paid to a supplied player.
 void sell(GoodsType type, int amount, Player player)
          Sells an amount of a particular type of good with the proceeds of the sale being paid to a supplied player.
 void sell(GoodsType type, int amount, Player player, int marketAccess)
          Sells an amount of a particular type of good with the proceeds of the sale being paid to a supplied player.
 void setOwner(Player owner)
          Sets the owner of this Market.
protected  void toXMLImpl(javax.xml.stream.XMLStreamWriter out, Player player, boolean showAll, boolean toSavedGame)
          This method writes an XML-representation of this object to the given stream.
 
Methods inherited from class net.sf.freecol.common.model.FreeColGameObject
addModelMessage, addModelMessage, addModelMessage, addToRemoveElement, dispose, equals, equals, getFreeColGameObject, getFreeColGameObject, getGame, getGameOptions, getIntegerID, hashCode, hasID, isDisposed, isUninitialized, readFromXML, readFromXMLPartialByClass, setFakeID, setGame, setId, toSavedXML, toString, toXML, toXMLImpl, toXMLPartialByClass, updateFreeColGameObject, updateID
 
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addPropertyChangeListener, addPropertyChangeListener, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAttribute, getAttribute, getAttribute, getAttribute, getId, getPropertyChangeListeners, getPropertyChangeListeners, hasAbility, hasAttribute, hasListeners, readAttributes, readChildren, readFromArrayElement, readFromArrayElement, readFromArrayElement, readFromListElement, readFromXMLElement, readFromXMLPartialImpl, removePropertyChangeListener, removePropertyChangeListener, toArrayElement, toArrayElement, toListElement, toXML, toXML, toXMLElement, toXMLElement, toXMLElement, toXMLElement, toXMLElementPartial, toXMLPartialImpl, writeAttribute, writeAttributes, writeChildren, writeFreeColGameObject
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

MINIMUM_PRICE

public static final int MINIMUM_PRICE
See Also:
Constant Field Values

MAXIMUM_PRICE

public static final int MAXIMUM_PRICE
See Also:
Constant Field Values

owner

private Player owner

EUROPE

public static final int EUROPE
Constant for specifying the access to this Market when buying and selling goods.

See Also:
Constant Field Values

CUSTOM_HOUSE

public static final int CUSTOM_HOUSE
Constant for specifying the access to this Market when buying and selling goods.

See Also:
Constant Field Values

marketData

private final java.util.Map<GoodsType,MarketData> marketData

transactionListeners

private java.util.ArrayList<TransactionListener> transactionListeners
Constructor Detail

Market

public Market(Game game,
              Player player)

Market

public Market(Game game,
              javax.xml.stream.XMLStreamReader in)
       throws javax.xml.stream.XMLStreamException
Initiates a new Market from an XML representation.

Parameters:
game - The Game this object belongs to.
in - The input stream containing the XML.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

Market

public Market(Game game,
              org.w3c.dom.Element e)
Initiates a new Market from an XML representation.

Parameters:
game - The Game this object belongs to.
e - An XML-element that will be used to initialize this object.

Market

public Market(Game game,
              java.lang.String id)
Initiates a new Market with the given ID. The object should later be initialized by calling either FreeColGameObject.readFromXML(XMLStreamReader) or FreeColObject.readFromXMLElement(Element).

Parameters:
game - The Game in which this object belong.
id - The unique identifier for this object.
Method Detail

priceGoods

private boolean priceGoods(GoodsType goodsType)
Adjust the price for a particular type of goods.

Parameters:
goodsType - a GoodsType value
Returns:
True if the price of the goods in the market has changed.

getMarketData

public MarketData getMarketData(GoodsType goodsType)
Return the market data for a type of goods.

Parameters:
goodsType - a GoodsType value
Returns:
a MarketData value

putMarketData

public void putMarketData(GoodsType goodsType,
                          MarketData data)
Describe putMarketData method here.

Parameters:
goodsType - a GoodsType value
data - a MarketData value

getOwner

public Player getOwner()
Gets the owner of this Market.

Specified by:
getOwner in interface Ownable
Returns:
The owner of this Market.

setOwner

public void setOwner(Player owner)
Sets the owner of this Market.

Specified by:
setOwner in interface Ownable
Parameters:
owner - The Player that shall own this Market.

costToBuy

public int costToBuy(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.

paidForSale

public int paidForSale(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.

sell

public void sell(Goods goods,
                 Player player)
Sells an amount of a particular type of good with the proceeds of the sale being paid to a supplied player. The goods is sold using EUROPE as the accesspoint for this market. Note that post-independence this no longer refers to a specific port.

Parameters:
goods - The Goods object being sold.
player - The Player selling the goods.

sell

public void sell(GoodsType type,
                 int amount,
                 Player player)
Sells an amount of a particular type of good with the proceeds of the sale being paid to a supplied player. The goods is sold using EUROPE as the accesspoint for this market.

Parameters:
type - The type of goods to be sold.
amount - The amount of goods to be sold.
player - The Player selling the goods.

sell

public void sell(GoodsType type,
                 int amount,
                 Player player,
                 int marketAccess)
Sells an amount of a particular type of good with the proceeds of the sale being paid to a supplied player.

Parameters:
type - The type of goods to be sold.
amount - The amount of goods to be sold.
player - The Player selling the goods.
marketAccess - The place where goods are traded.

buy

public void buy(GoodsType goodsType,
                int amount,
                Player player)
Buys an amount of a particular type of good with the cost being met by a supplied player.

Parameters:
goodsType - The type of the good that is being bought.
amount - The amount of goods that are being bought.
player - The Player buying the goods.
Throws:
java.lang.IllegalStateException - If the player cannot afford to buy the goods.

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 goods changes price due to this change.

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 int getSalePrice(Goods goods)
Gets the price of a given goods when the Player sells.

Parameters:
goods - a Goods value
Returns:
an int value

addTransactionListener

public void addTransactionListener(TransactionListener listener)
Adds a transaction listener for notification of any transaction

Parameters:
listener - the listener

removeTransactionListener

public void removeTransactionListener(TransactionListener listener)
Removes a transaction listener

Parameters:
listener - the listener

getTransactionListener

public TransactionListener[] getTransactionListener()
Returns an array of all the TransactionListener added to this Market.

Returns:
all of the TransactionListener added or an empty array if no listeners have been added

makePriceMessage

public ModelMessage makePriceMessage(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.

toXMLImpl

protected void toXMLImpl(javax.xml.stream.XMLStreamWriter out,
                         Player player,
                         boolean showAll,
                         boolean toSavedGame)
                  throws javax.xml.stream.XMLStreamException
This method writes an XML-representation of this object to the given stream.

Only attributes visible to the given Player will be added to that representation if showAll is set to false.

Specified by:
toXMLImpl in class FreeColGameObject
Parameters:
out - The target stream.
player - The Player this XML-representation should be made for, or null if showAll == true.
showAll - Only attributes visible to player will be added to the representation if showAll is set to false.
toSavedGame - If true then information that is only needed when saving a game is added.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

readFromXMLImpl

protected void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
                        throws javax.xml.stream.XMLStreamException
Initialize this object from an XML-representation of this object.

Overrides:
readFromXMLImpl in class FreeColObject
Parameters:
in - The input stream with the XML.
Throws:
javax.xml.stream.XMLStreamException - on problems with the stream. TODO: Get rid of the for-priceGoods() when the server sends all price changes.

getXMLElementTagName

public static java.lang.String getXMLElementTagName()
Returns the tag name of the root element representing this object.

Returns:
the tag name.