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 class
Market.Access
Constant for specifying the access to thisMarket
when selling goods.
-
Field Summary
Fields Modifier and Type Field Description private static java.util.logging.Logger
logger
private java.util.Map<GoodsType,MarketData>
marketData
The contents of the market, keyed by goods type.static int
MINIMUM_AMOUNT
European markets are bottomless.private Player
owner
The owning player.private static java.lang.String
OWNER_TAG
static java.lang.String
PRICE_CHANGE
static java.lang.String
TAG
private java.util.ArrayList<TransactionListener>
transactionListeners
Watching 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 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 transactionprivate void
clearMarketData()
Clear the market data.<T extends FreeColObject>
booleancopyIn(T other)
Copy another FreeColObject into this one if it is compatible.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 thePlayer
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.FreeColGameObject
getLinkTarget(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.MarketData
getMarketData(GoodsType goodsType)
Gets the market data for a type of goods.java.util.Collection<MarketData>
getMarketDataValues()
Get the market data values.Player
getOwner()
Gets the owner of thisOwnable
.int
getPaidForSale(GoodsType type)
Determines the price paid for the sale of a single unit of a particular type of goods.int
getSalePrice(GoodsType type, int amount)
Gets the price of a given goods when thePlayer
sells.<T extends AbstractGoods>
intgetSalePrice(T goods)
Gets the price of a given goods when thePlayer
sells.<T extends AbstractGoods>
java.util.Comparator<T>getSalePriceComparator()
Get a sale price comparator for this market.int
getSales(GoodsType goodsType)
Gets the sales of a type of goods.TransactionListener[]
getTransactionListener()
Gets the listeners added to this market.java.lang.String
getXMLTagName()
Get the serialization tag for 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 aModelMessage
describing the change in thisMarket
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)
Set the market data for a given goods type.protected void
readAttributes(FreeColXMLReader xr)
Reads the attributes of this object from an XML stream.protected void
readChild(FreeColXMLReader xr)
Reads a single child object.protected void
readChildren(FreeColXMLReader xr)
Reads the children of this object from an XML stream.void
removeTransactionListener(TransactionListener listener)
Removes a transaction listenerprivate 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.private void
setMarketData(java.util.Map<GoodsType,MarketData> data)
Set the market data.void
setOwner(Player owner)
Sets the owner of thisOwnable
.java.lang.String
toString()
void
update(GoodsType goodsType)
Update the price for a type of goods, bypassing the price change clamping.protected void
writeAttributes(FreeColXMLWriter xw)
Write the attributes of this object to a stream.protected void
writeChildren(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
- ThePlayer
to own the market.
-
Market
public Market(Game game, java.lang.String id)
Creates a newMarket
with 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
- TheGoodsType
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
- TheGoodsType
to 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
- TheGoodsType
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
- AGoodsType
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
- AGoodsType
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
- TheGoodsType
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
- TheGoodsType
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
- TheGoodsType
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 thePlayer
buys.- Parameters:
type
- aGoodsType
valueamount
- 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 thePlayer
sells.- Parameters:
type
- aGoodsType
valueamount
- 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 thePlayer
sells.- Type Parameters:
T
- The base type of the goods.- Parameters:
goods
- TheGoods
to evaluate.- Returns:
- The price.
-
getArrears
public int getArrears(GoodsType goodsType)
Gets the arrears for of a given goods type.- Parameters:
goodsType
- TheGoodsType
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
- TheGoodsType
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
- TheGoodsType
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
- TheGoodsType
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
- TheGoodsType
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
- TheGoodsType
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
- TheGoodsType
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
- TheGoodsType
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
- TheGoodsType
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 aModelMessage
describing the change in thisMarket
for a specified type of goods.- Parameters:
goodsType
- TheGoodsType
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
- TheGoodsType
to update.
-
getSalePriceComparator
public <T extends AbstractGoods> java.util.Comparator<T> getSalePriceComparator()
Get a sale price comparator for this market.- Type Parameters:
T
- TheAbstractGoods
type to compare.- Returns:
- A suitable
Comparator
.
-
addTransactionListener
public void addTransactionListener(TransactionListener listener)
Adds a transaction listener for notification of any transaction- Parameters:
listener
- TheTransactionListener
to add.
-
removeTransactionListener
public void removeTransactionListener(TransactionListener listener)
Removes a transaction listener- Parameters:
listener
- TheTransactionListener
to remove.
-
getTransactionListener
public TransactionListener[] getTransactionListener()
Gets the listeners added to this market.- Returns:
- An array of all the
TransactionListener
s 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:
getLinkTarget
in classFreeColGameObject
- Parameters:
player
- ThePlayer
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 classFreeColGameObject
- Type Parameters:
T
- TheFreeColObject
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 classFreeColObject
- Parameters:
xw
- TheFreeColXMLWriter
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 classFreeColObject
- Parameters:
xw
- TheFreeColXMLWriter
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 classFreeColGameObject
- Parameters:
xr
- TheFreeColXMLReader
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 classFreeColObject
- Parameters:
xr
- TheFreeColXMLReader
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 classFreeColObject
- Parameters:
xr
- TheFreeColXMLReader
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 classFreeColObject
- Returns:
- The tag.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classFreeColObject
-
-