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 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.


Nested Class Summary
static class Market.Access
          Constant for specifying the access to this Market when selling goods.
 
Field Summary
private  java.util.Map<GoodsType,MarketData> marketData
           
static int MINIMUM_AMOUNT
          European markets are bottomless.
private  Player owner
           
private  java.util.ArrayList<TransactionListener> transactionListeners
           
 
Fields inherited from class net.sf.freecol.common.model.FreeColGameObject
UNITS_TAG_NAME
 
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE, ID_ATTRIBUTE, ID_ATTRIBUTE_TAG, INFINITY, NO_ID, PARTIAL_ATTRIBUTE, UNDEFINED, VALUE_TAG
 
Constructor Summary
Market(Game game, Player player)
          Main constructor for creating a market for a new 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
 void 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 flushPriceChange(GoodsType goodsType)
          Clear any price changes for a type of goods.
 int getAmountInMarket(GoodsType goodsType)
          Gets the amount of a goods type in the market.
 int getArrears(GoodsType goodsType)
          Gets the arrears for of a given goods type.
 int getBidPrice(GoodsType type, int amount)
          Gets the price of a given goods when the Player buys.
 int getCostToBuy(GoodsType type)
          Determines the cost to buy a single unit of a particular type of good.
 int getIncomeAfterTaxes(GoodsType goodsType)
          Gets the income after taxes for a type of goods.
 int getIncomeBeforeTaxes(GoodsType goodsType)
          Gets the income before taxes for a type of goods.
 int getInitialPrice(GoodsType goodsType)
          Gets the initial price of a given goods type.
 MarketData getMarketData(GoodsType goodsType)
          Return the market data for a type of goods.
 Player getOwner()
          Gets the owner of this Market.
 int getPaidForSale(GoodsType type)
          Determines the price paid for the sale of a single unit of a particular type of goods.
 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.
 int getSales(GoodsType goodsType)
          Gets the sales of a type of goods.
 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.
 boolean hasBeenTraded(GoodsType type)
          Has a type of goods been traded in this market?
 boolean hasPriceChanged(GoodsType goodsType)
          Has the price of a type of goods changed in this market?
 ModelMessage makePriceChangeMessage(GoodsType goodsType)
          Make up a ModelMessage describing the change in this Market for a specified type of goods.
 void modifyIncomeAfterTaxes(GoodsType goodsType, int amount)
          Modifies the income after taxes on sales of a type of goods.
 void modifyIncomeBeforeTaxes(GoodsType goodsType, int amount)
          Modifies the income before taxes on sales of a type of goods.
 void modifySales(GoodsType goodsType, int amount)
          Modifies the sales of a type of goods.
private  void putMarketData(GoodsType goodsType, MarketData data)
          Describe putMarketData method here.
 void randomizeInitialPrice(java.util.Random random)
          Makes a slight randomization to all the new world and luxury goods types.
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
private  MarketData requireMarketData(GoodsType goodsType)
          Gets the market data for a specified goods type, creating it if it does not exist yet.
 void setArrears(GoodsType goodsType, int value)
          Sets the arrears associated with a type of goods.
 void setInitialPrice(GoodsType goodsType, int amount)
          Sets the initial price of a given goods type.
 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
dispose, disposeList, equals, equals, fundamentalDispose, getFreeColGameObject, getFreeColGameObject, getGame, getIntegerID, getSpecification, hashCode, isDisposed, isUninitialized, newLocation, readFromXML, readFromXMLPartialByClass, setDefaultId, setGame, setId, toString, toXML, toXMLImpl, toXMLPartialByClass, updateFreeColGameObject
 
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addPropertyChangeListener, addPropertyChangeListener, dumpObject, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAttribute, getAttribute, getAttribute, getAttribute, getAttribute, getId, getPropertyChangeListeners, getPropertyChangeListeners, hasAbility, hasAttribute, hasListeners, readAttributes, readAttributes, readChild, readChild, readChildren, readChildren, readFromArrayElement, readFromArrayElement, readFromListElement, readFromXMLElement, readFromXMLImpl, readFromXMLPartialImpl, removePropertyChangeListener, removePropertyChangeListener, save, save, setSpecification, toXML, toXML, toXMLElement, toXMLElement, toXMLElement, toXMLElement, toXMLElementPartial, toXMLPartialImpl, writeAttribute, writeAttributes, writeChildren
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

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

owner

private Player owner

transactionListeners

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

Market

public Market(Game game,
              Player player)
Main constructor for creating a market for a new 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,
              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

putMarketData

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

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

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.

randomizeInitialPrice

public void randomizeInitialPrice(java.util.Random random)
Makes a slight randomization to all the new world and luxury goods types. Used at the start of game by the PreGameController.

Parameters:
random - A pseudo-random number source.

getMarketData

public MarketData getMarketData(GoodsType goodsType)
Return the market data for a type of goods. This one is public so the server can send individual MarketData updates.

Parameters:
goodsType - a GoodsType value
Returns:
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 to own this Market.

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 void addGoodsToMarket(GoodsType goodsType,
                             int amount)
Add (or remove) some goods to this market.

Parameters:
goodsType - The GoodsType to add.
amount - The amount of goods.

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

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

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.

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

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 price() 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:
"market".