Package net.sf.freecol.common.model
Class NativeTrade
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- net.sf.freecol.common.model.FreeColGameObject
-
- net.sf.freecol.common.model.NativeTrade
-
- All Implemented Interfaces:
java.lang.Comparable<FreeColObject>
,ObjectWithId
public class NativeTrade extends FreeColGameObject
Container class for the information that persists while a native trade session is underway.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
NativeTrade.NativeTradeAction
The type of native trade command.
-
Field Summary
Fields Modifier and Type Field Description private static StringTemplate
abortTrade
A template to use as a magic cookie for aborted trades.private boolean
buy
True if no purchases made in this trade.private static java.lang.String
BUY_TAG
private int
count
How many times this trade has been tried.private static java.lang.String
COUNT_TAG
private boolean
gift
True if no gifts made in this trade.private static java.lang.String
GIFT_TAG
private IndianSettlement
is
The settlement to trade with.private NativeTradeItem
item
An item under consideration for a transaction.private static java.util.logging.Logger
logger
private boolean
sell
True if no sales made in this trade.private static java.lang.String
SELL_TAG
private static java.lang.String
SETTLEMENT_TAG
private static java.lang.String
SETTLEMENT_TO_UNIT_TAG
private java.util.List<NativeTradeItem>
settlementToUnit
The goods in the settlement that are being offered for purchase by the unit.static java.lang.String
TAG
private Unit
unit
The unit that is trading.private static java.lang.String
UNIT_TAG
private static java.lang.String
UNIT_TO_SETTLEMENT_TAG
private java.util.List<NativeTradeItem>
unitToSettlement
The goods on the unit that are being offered for purchase by the settlement.-
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
-
-
Constructor Summary
Constructors Constructor Description NativeTrade(Game game, java.lang.String id)
Simple constructor, used in Game.newInstance.NativeTrade(Unit unit, IndianSettlement is)
Create a new trade session.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addToUnit(NativeTradeItem nti)
Add an item to the unit list of items.private boolean
atWar()
Check if the trade participants are at war.boolean
canBuy()
Can the unit owner buy more items in this session at present?boolean
canGift()
Can the unit owner give more items in this session at present?boolean
canSell()
Can the unit owner buy more items in this session at present?<T extends FreeColObject>
booleancopyIn(T other)
Copy another FreeColObject into this one if it is compatible.boolean
getBuy()
Is purchasing available in this transaction?int
getCount()
Get the transaction count.boolean
getDone()
Is this transaction complete?boolean
getGift()
Is giving available in this transaction?IndianSettlement
getIndianSettlement()
Get the settlement that is trading.NativeTradeItem
getItem()
Get the item being traded.java.lang.String
getKey()
Get a key for this transaction.static java.lang.String
getNativeTradeKey(Unit unit, IndianSettlement is)
Make a transaction key for a native trade.boolean
getSell()
Is selling available in this transaction?java.util.List<NativeTradeItem>
getSettlementToUnit()
Get the list of items the settlement is able to offer the unit.Unit
getUnit()
Get the unit that is trading.java.util.List<NativeTradeItem>
getUnitToSettlement()
Get the list of items the unit is able to offer the settlement.java.lang.String
getXMLTagName()
Get the serialization tag for this object.static int
haggleDown(int price)
Choose the next available downward haggling price.static int
haggleUp(int price)
Choose the next available upward haggling price.boolean
hasNotTraded()
Have no trades been performed in this transaction?void
initialize()
Raw initialization of the unit and settlement list.boolean
isCompatible(NativeTrade nt)
Is another native trade compatible with this one?boolean
isInternable()
Should this object be interned into its Game? Usually true, but there are some special containers that have to be FCGOs but are unsuitable to be interned.void
limitSettlementToUnit(int n)
Limit the number of items offered by the settlement.void
mergeFrom(NativeTrade nt)
Merge another compatible native trade into this one.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
removeFromUnit(NativeTradeItem nti)
Remove an item from the unit list of items.void
setBuy(boolean buy)
Set the purchase state.void
setCount(int count)
Set the transaction count.void
setDone()
Set this transaction as complete.void
setGift(boolean gift)
Set the gift state.void
setItem(NativeTradeItem nti)
Set the item being traded.void
setSell(boolean sell)
Set the sale state.java.lang.String
toString()
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, getLinkTarget, getSpecification, hashCode, intern, internId, isDisposed, isInitialized, 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
-
abortTrade
private static final StringTemplate abortTrade
A template to use as a magic cookie for aborted trades.
-
unit
private Unit unit
The unit that is trading.
-
is
private IndianSettlement is
The settlement to trade with.
-
count
private int count
How many times this trade has been tried.
-
buy
private boolean buy
True if no purchases made in this trade.
-
sell
private boolean sell
True if no sales made in this trade.
-
gift
private boolean gift
True if no gifts made in this trade.
-
item
private NativeTradeItem item
An item under consideration for a transaction.
-
unitToSettlement
private java.util.List<NativeTradeItem> unitToSettlement
The goods on the unit that are being offered for purchase by the settlement.
-
settlementToUnit
private java.util.List<NativeTradeItem> settlementToUnit
The goods in the settlement that are being offered for purchase by the unit.
-
BUY_TAG
private static final java.lang.String BUY_TAG
- See Also:
- Constant Field Values
-
COUNT_TAG
private static final java.lang.String COUNT_TAG
- See Also:
- Constant Field Values
-
GIFT_TAG
private static final java.lang.String GIFT_TAG
- See Also:
- Constant Field Values
-
SELL_TAG
private static final java.lang.String SELL_TAG
- See Also:
- Constant Field Values
-
SETTLEMENT_TAG
private static final java.lang.String SETTLEMENT_TAG
- See Also:
- Constant Field Values
-
SETTLEMENT_TO_UNIT_TAG
private static final java.lang.String SETTLEMENT_TO_UNIT_TAG
- See Also:
- Constant Field Values
-
UNIT_TAG
private static final java.lang.String UNIT_TAG
- See Also:
- Constant Field Values
-
UNIT_TO_SETTLEMENT_TAG
private static final java.lang.String UNIT_TO_SETTLEMENT_TAG
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
NativeTrade
public NativeTrade(Game game, java.lang.String id)
Simple constructor, used in Game.newInstance.- Parameters:
game
- The enclosingGame
.id
- The identifier (ignored).
-
NativeTrade
public NativeTrade(Unit unit, IndianSettlement is)
Create a new trade session.- Parameters:
unit
- TheUnit
that is trading.is
- TheIndianSettlement
to trade with.
-
-
Method Detail
-
atWar
private boolean atWar()
Check if the trade participants are at war.- Returns:
- True if the traders are at war.
-
getKey
public java.lang.String getKey()
Get a key for this transaction.- Returns:
- A suitable key.
-
getNativeTradeKey
public static java.lang.String getNativeTradeKey(Unit unit, IndianSettlement is)
Make a transaction key for a native trade.- Parameters:
unit
- TheUnit
that is trading.is
- TheIndianSettlement
that is trading.- Returns:
- A suitable key.
-
getUnit
public Unit getUnit()
Get the unit that is trading.- Returns:
- The
Unit
that started the trade.
-
getIndianSettlement
public IndianSettlement getIndianSettlement()
Get the settlement that is trading.- Returns:
- The
IndianSettlement
that is traded with.
-
getBuy
public boolean getBuy()
Is purchasing available in this transaction?- Returns:
- True if no blocking purchase has been made.
-
canBuy
public boolean canBuy()
Can the unit owner buy more items in this session at present?- Returns:
- True if not blocked, at peace, there are purchases to be made and space available.
-
setBuy
public void setBuy(boolean buy)
Set the purchase state.- Parameters:
buy
- The new purchase state.
-
getSell
public boolean getSell()
Is selling available in this transaction?- Returns:
- True if no blocking sale has been made.
-
canSell
public boolean canSell()
Can the unit owner buy more items in this session at present?- Returns:
- True if not blocked, at peace, and there are sales to be made.
-
setSell
public void setSell(boolean sell)
Set the sale state.- Parameters:
sell
- The new sale state.
-
getGift
public boolean getGift()
Is giving available in this transaction?- Returns:
- True if no blocking gift has been made.
-
canGift
public boolean canGift()
Can the unit owner give more items in this session at present?- Returns:
- True if not blocked, and the unit has gifts to give.
-
setGift
public void setGift(boolean gift)
Set the gift state.- Parameters:
gift
- The new gift state.
-
hasNotTraded
public boolean hasNotTraded()
Have no trades been performed in this transaction?- Returns:
- True if no blocking purchase, sale or gift has occurred.
-
getCount
public int getCount()
Get the transaction count.- Returns:
- The transaction count.
-
setCount
public void setCount(int count)
Set the transaction count.- Parameters:
count
- The new transaction count.
-
getDone
public boolean getDone()
Is this transaction complete?- Returns:
- True if the transaction is over.
-
setDone
public void setDone()
Set this transaction as complete.
-
getItem
public NativeTradeItem getItem()
Get the item being traded.- Returns:
- The current
NativeTradeItem
.
-
setItem
public void setItem(NativeTradeItem nti)
Set the item being traded.- Parameters:
nti
- The newNativeTradeItem
.
-
getUnitToSettlement
public java.util.List<NativeTradeItem> getUnitToSettlement()
Get the list of items the unit is able to offer the settlement. Note: some of these items might be currently invalid.- Returns:
- A list of
NativeTradeItem
s the unit might sell.
-
getSettlementToUnit
public java.util.List<NativeTradeItem> getSettlementToUnit()
Get the list of items the settlement is able to offer the unit. Note: some of these items might be currently invalid.- Returns:
- A list of
NativeTradeItem
s the unit might buy.
-
addToUnit
public void addToUnit(NativeTradeItem nti)
Add an item to the unit list of items.- Parameters:
nti
- TheNativeTradeItem
to add.
-
removeFromUnit
public void removeFromUnit(NativeTradeItem nti)
Remove an item from the unit list of items.- Parameters:
nti
- TheNativeTradeItem
to remove.
-
isCompatible
public boolean isCompatible(NativeTrade nt)
Is another native trade compatible with this one?- Parameters:
nt
- The otherNativeTrade
.- Returns:
- True if the other trade is compatible.
-
initialize
public void initialize()
Raw initialization of the unit and settlement list. Does not do pricing!
-
mergeFrom
public void mergeFrom(NativeTrade nt)
Merge another compatible native trade into this one.- Parameters:
nt
- TheNativeTrade
to merge.
-
limitSettlementToUnit
public void limitSettlementToUnit(int n)
Limit the number of items offered by the settlement. Used in the client to implement a Col1 restriction.- Parameters:
n
- The number of items to offer.
-
haggleUp
public static int haggleUp(int price)
Choose the next available upward haggling price.- Parameters:
price
- The initial price.- Returns:
- The new upward haggled price.
-
haggleDown
public static int haggleDown(int price)
Choose the next available downward haggling price.- Parameters:
price
- The initial price.- Returns:
- The new downward haggled price.
-
isInternable
public boolean isInternable()
Should this object be interned into its Game? Usually true, but there are some special containers that have to be FCGOs but are unsuitable to be interned. These classes will override this routine.- Overrides:
isInternable
in classFreeColGameObject
- Returns:
- True if this object should be interned.
-
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
-
-