Package net.sf.freecol.common.model
Class Market
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- net.sf.freecol.common.model.FreeColGameObject
-
- net.sf.freecol.common.model.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMarket.AccessConstant for specifying the access to thisMarketwhen selling goods.
-
Field Summary
Fields Modifier and Type Field Description private static java.util.logging.Loggerloggerprivate java.util.Map<GoodsType,MarketData>marketDataThe contents of the market, keyed by goods type.static intMINIMUM_AMOUNTEuropean markets are bottomless.private PlayerownerThe owning player.private static java.lang.StringOWNER_TAGstatic java.lang.StringPRICE_CHANGEstatic java.lang.StringTAGprivate java.util.ArrayList<TransactionListener>transactionListenersWatching listeners.-
Fields inherited from class net.sf.freecol.common.model.FreeColGameObject
initialized
-
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE_TAG, DEFAULT_CLASS_INDEX, fcoComparator, ID_ATTRIBUTE_TAG, PARTIAL_ATTRIBUTE_TAG, VALUE_TAG
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddGoodsToMarket(GoodsType goodsType, int amount)Add (or remove) some goods to this market.voidaddTransactionListener(TransactionListener listener)Adds a transaction listener for notification of any transactionprivate voidclearMarketData()Clear the market data.<T extends FreeColObject>
booleancopyIn(T other)Copy another FreeColObject into this one if it is compatible.voidflushPriceChange(GoodsType goodsType)Clear any price changes for a type of goods.intgetAmountInMarket(GoodsType goodsType)Gets the amount of a goods type in the market.intgetArrears(GoodsType goodsType)Gets the arrears for of a given goods type.intgetBidPrice(GoodsType type, int amount)Gets the price of a given goods when thePlayerbuys.intgetCostToBuy(GoodsType type)Determines the cost to buy a single unit of a particular type of good.intgetIncomeAfterTaxes(GoodsType goodsType)Gets the income after taxes for a type of goods.intgetIncomeBeforeTaxes(GoodsType goodsType)Gets the income before taxes for a type of goods.intgetInitialPrice(GoodsType goodsType)Gets the initial price of a given goods type.FreeColGameObjectgetLinkTarget(Player player)Get a suitable game object to use as a clickable link in messages to a player.private java.util.Map<GoodsType,MarketData>getMarketData()Get the market data.MarketDatagetMarketData(GoodsType goodsType)Gets the market data for a type of goods.java.util.Collection<MarketData>getMarketDataValues()Get the market data values.PlayergetOwner()Gets the owner of thisOwnable.intgetPaidForSale(GoodsType type)Determines the price paid for the sale of a single unit of a particular type of goods.intgetSalePrice(GoodsType type, int amount)Gets the price of a given goods when thePlayersells.<T extends AbstractGoods>
intgetSalePrice(T goods)Gets the price of a given goods when thePlayersells.<T extends AbstractGoods>
java.util.Comparator<T>getSalePriceComparator()Get a sale price comparator for this market.intgetSales(GoodsType goodsType)Gets the sales of a type of goods.TransactionListener[]getTransactionListener()Gets the listeners added to this market.java.lang.StringgetXMLTagName()Get the serialization tag for this object.booleanhasBeenTraded(GoodsType type)Has a type of goods been traded in this market?booleanhasPriceChanged(GoodsType goodsType)Has the price of a type of goods changed in this market?ModelMessagemakePriceChangeMessage(GoodsType goodsType)Make up aModelMessagedescribing the change in thisMarketfor a specified type of goods.voidmodifyIncomeAfterTaxes(GoodsType goodsType, int amount)Modifies the income after taxes on sales of a type of goods.voidmodifyIncomeBeforeTaxes(GoodsType goodsType, int amount)Modifies the income before taxes on sales of a type of goods.voidmodifySales(GoodsType goodsType, int amount)Modifies the sales of a type of goods.private voidputMarketData(GoodsType goodsType, MarketData data)Set the market data for a given goods type.protected voidreadAttributes(FreeColXMLReader xr)Reads the attributes of this object from an XML stream.protected voidreadChild(FreeColXMLReader xr)Reads a single child object.protected voidreadChildren(FreeColXMLReader xr)Reads the children of this object from an XML stream.voidremoveTransactionListener(TransactionListener listener)Removes a transaction listenerprivate MarketDatarequireMarketData(GoodsType goodsType)Gets the market data for a specified goods type, creating it if it does not exist yet.voidsetArrears(GoodsType goodsType, int value)Sets the arrears associated with a type of goods.voidsetInitialPrice(GoodsType goodsType, int amount)Sets the initial price of a given goods type.private voidsetMarketData(java.util.Map<GoodsType,MarketData> data)Set the market data.voidsetOwner(Player owner)Sets the owner of thisOwnable.java.lang.StringtoString()voidupdate(GoodsType goodsType)Update the price for a type of goods, bypassing the price change clamping.protected voidwriteAttributes(FreeColXMLWriter xw)Write the attributes of this object to a stream.protected voidwriteChildren(FreeColXMLWriter xw)Write the children of this object to a stream.-
Methods inherited from class net.sf.freecol.common.model.FreeColGameObject
checkIntegrity, dispose, disposeResources, equals, fundamentalDispose, getClassIndex, getDisposables, getGame, getSpecification, hashCode, intern, internId, isDisposed, isInitialized, isInternable, readFromXML, setGame, setSpecification
-
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addAbility, addFeatures, addModifier, addPropertyChangeListener, addPropertyChangeListener, apply, apply, applyModifiers, applyModifiers, arrayKey, compareIds, compareTo, containsAbilityKey, containsModifierKey, copy, copy, copy, copy, copyInCast, dumpObject, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAbilities, getAbilities, getAbilities, getAbilities, getDefenceModifiers, getDisplayObject, getFeatureContainer, getFreeColObjectClass, getFreeColObjectClassByName, getId, getIdNumber, getIdType, getIdTypeByName, getModifiers, getModifiers, getModifiers, getModifiers, getObjectClassIndex, getPropertyChangeListeners, getPropertyChangeListeners, getPropertyChangeSupport, getSortedAbilities, getSortedModifiers, getSuffix, getSuffix, hasAbility, hasAbility, hasAbility, hasListeners, hasModifier, hasModifier, hasModifier, idEquals, invokeMethod, logFreeColObjects, readFromXMLPartial, removeAbilities, removeAbility, removeFeatures, removeModifier, removeModifiers, removePropertyChangeListener, removePropertyChangeListener, save, save, save, save, serialize, serialize, serialize, serialize, setId, toXML, toXML, toXMLPartial, toXMLPartial
-
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
TAG
public static final java.lang.String TAG
- See Also:
- Constant Field Values
-
MINIMUM_AMOUNT
public static final int MINIMUM_AMOUNT
European markets are bottomless. Goods present never decrease below this threshold.- See Also:
- Constant Field Values
-
PRICE_CHANGE
public static final java.lang.String PRICE_CHANGE
- 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.
-
OWNER_TAG
private static final java.lang.String OWNER_TAG
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Market
public Market(Game game, Player player)
Main constructor for creating a market for a new player.- Parameters:
game- The enclosingGame.player- ThePlayerto own the market.
-
Market
public Market(Game game, java.lang.String id)
Creates a newMarketwith the given identifier. The object should be initialized later.- Parameters:
game- The enclosingGame.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- TheGoodsTypeto 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- TheGoodsTypeto set the market data for.data- The newMarketData.
-
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- TheGoodsTypeto query.- Returns:
- The
MarketDatarequired.
-
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- AGoodsTypevalue.- 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- AGoodsTypevalue.- 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- TheGoodsTypeto 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- TheGoodsTypeto 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- TheGoodsTypeto 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 thePlayerbuys.- Parameters:
type- aGoodsTypevalueamount- 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 thePlayersells.- Parameters:
type- aGoodsTypevalueamount- 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 thePlayersells.- Type Parameters:
T- The base type of the goods.- Parameters:
goods- TheGoodsto evaluate.- Returns:
- The price.
-
getArrears
public int getArrears(GoodsType goodsType)
Gets the arrears for of a given goods type.- Parameters:
goodsType- TheGoodsTypeto 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- TheGoodsTypeto 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- TheGoodsTypeto 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- TheGoodsTypeto 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- TheGoodsTypeto 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- TheGoodsTypeto 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- TheGoodsTypeto 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- TheGoodsTypeto 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- TheGoodsTypeto 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 aModelMessagedescribing the change in thisMarketfor a specified type of goods.- Parameters:
goodsType- TheGoodsTypethat 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- TheGoodsTypeto update.
-
getSalePriceComparator
public <T extends AbstractGoods> java.util.Comparator<T> getSalePriceComparator()
Get a sale price comparator for this market.- Type Parameters:
T- TheAbstractGoodstype to compare.- Returns:
- A suitable
Comparator.
-
addTransactionListener
public void addTransactionListener(TransactionListener listener)
Adds a transaction listener for notification of any transaction- Parameters:
listener- TheTransactionListenerto add.
-
removeTransactionListener
public void removeTransactionListener(TransactionListener listener)
Removes a transaction listener- Parameters:
listener- TheTransactionListenerto 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 thisOwnable.
-
setOwner
public void setOwner(Player owner)
Sets the owner of thisOwnable.
-
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:
getLinkTargetin classFreeColGameObject- Parameters:
player- ThePlayerto 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:
copyInin classFreeColGameObject- Type Parameters:
T- TheFreeColObjectsubclass 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:
writeAttributesin classFreeColObject- Parameters:
xw- TheFreeColXMLWriterto 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:
writeChildrenin classFreeColObject- Parameters:
xw- TheFreeColXMLWriterto 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:
readAttributesin classFreeColGameObject- Parameters:
xr- TheFreeColXMLReaderto 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:
readChildrenin classFreeColObject- Parameters:
xr- TheFreeColXMLReaderto 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:
readChildin classFreeColObject- Parameters:
xr- TheFreeColXMLReaderto 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:
getXMLTagNamein classFreeColObject- Returns:
- The tag.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classFreeColObject
-
-