Package net.sf.freecol.common.model
Class DiplomaticTrade
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- net.sf.freecol.common.model.FreeColGameObject
-
- net.sf.freecol.common.model.DiplomaticTrade
-
- All Implemented Interfaces:
java.lang.Comparable<FreeColObject>
,ObjectWithId
public class DiplomaticTrade extends FreeColGameObject
The classDiplomaticTrade
represents an offer one player can make another. This has to be a FCGO so that it can be serialized, but instances are not interned.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DiplomaticTrade.TradeContext
A context for the trade.static class
DiplomaticTrade.TradeStatus
A type for the trade status.
-
Field Summary
Fields Modifier and Type Field Description private DiplomaticTrade.TradeContext
context
The context of this agreement.private static java.lang.String
CONTEXT_TAG
private java.util.List<TradeItem>
items
The individual items the trade consists of.private Player
recipient
The player who is to accept this agreement.private static java.lang.String
RECIPIENT_TAG
private Player
sender
The player who proposed agreement.private static java.lang.String
SENDER_TAG
private DiplomaticTrade.TradeStatus
status
The status of this agreement.private static java.lang.String
STATUS_TAG
static java.lang.String
TAG
private int
version
Counter for the number of iterations on this attempt to agree.private static java.lang.String
VERSION_TAG
-
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 DiplomaticTrade(Game game, java.lang.String id)
Simple constructor, used in Game.newInstance.DiplomaticTrade(Game game, DiplomaticTrade.TradeContext context, Player sender, Player recipient, java.util.List<TradeItem> items, int version)
Creates a newDiplomaticTrade
instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(TradeItem newItem)
Add to the DiplomaticTrade.void
clear()
Remove all trade items from this agreement.<T extends FreeColObject>
booleancopyIn(T other)
Copy another FreeColObject into this one if it is compatible.java.util.List<Colony>
getColoniesGivenBy(Player player)
Get a list of colonies offered in this trade.DiplomaticTrade.TradeContext
getContext()
Get the trade context.int
getGoldGivenBy(Player player)
Get the gold offered in this trade by a given player.java.util.List<Goods>
getGoodsGivenBy(Player player)
Get the goods being offered.java.util.List<TradeItem>
getItems()
Get a list of all items to trade.java.util.List<TradeItem>
getItemsGivenBy(Player player)
Get the items offered by a particular player.Player
getOtherPlayer(Player player)
Get the other player in a trade.StringTemplate
getReceiveMessage(Player player)
Handy utility to get the message associated with sending this agreement from a player to a settlement owner.Player
getRecipient()
Get the recipient player.Player
getSender()
Get the sending player.StringTemplate
getSendMessage(Player player, Settlement settlement)
Handy utility to get the message associated with sending this agreement from a player to a settlement owner.Stance
getStance()
Get the stance being offered.DiplomaticTrade.TradeStatus
getStatus()
Get the trade status.java.util.List<Unit>
getUnitsGivenBy(Player player)
Get a list of units offered in this trade.int
getVersion()
Gets the version of this agreement.Player
getVictim()
Get the player being incited against.java.lang.String
getXMLTagName()
Get the serialization tag for this object.void
incrementVersion()
Increment the version of this agreement.boolean
isEmpty()
Are there no trade items present?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.static DiplomaticTrade
makePeaceTreaty(DiplomaticTrade.TradeContext context, Player sender, Player recipient)
Make a new diplomatic trade for a given context that establishes peace between two given players.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
remove(int index)
Remove from the DiplomaticTrade.void
remove(TradeItem newItem)
Remove a from the DiplomaticTrade.void
removeType(java.lang.Class<? extends TradeItem> itemClass)
Removes all trade items of the same class as the given argument.void
setRecipient(Player newRecipient)
Set the recieving player.void
setSender(Player newSender)
Set the sending player.void
setStatus(DiplomaticTrade.TradeStatus status)
Set the trade status.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
-
TAG
public static final java.lang.String TAG
- See Also:
- Constant Field Values
-
context
private DiplomaticTrade.TradeContext context
The context of this agreement.
-
status
private DiplomaticTrade.TradeStatus status
The status of this agreement.
-
sender
private Player sender
The player who proposed agreement.
-
recipient
private Player recipient
The player who is to accept this agreement.
-
items
private final java.util.List<TradeItem> items
The individual items the trade consists of.
-
version
private int version
Counter for the number of iterations on this attempt to agree.
-
CONTEXT_TAG
private static final java.lang.String CONTEXT_TAG
- See Also:
- Constant Field Values
-
RECIPIENT_TAG
private static final java.lang.String RECIPIENT_TAG
- See Also:
- Constant Field Values
-
SENDER_TAG
private static final java.lang.String SENDER_TAG
- See Also:
- Constant Field Values
-
STATUS_TAG
private static final java.lang.String STATUS_TAG
- See Also:
- Constant Field Values
-
VERSION_TAG
private static final java.lang.String VERSION_TAG
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DiplomaticTrade
public DiplomaticTrade(Game game, java.lang.String id)
Simple constructor, used in Game.newInstance.- Parameters:
game
- The enclosingGame
.id
- The identifier (ignored).
-
DiplomaticTrade
public DiplomaticTrade(Game game, DiplomaticTrade.TradeContext context, Player sender, Player recipient, java.util.List<TradeItem> items, int version)
Creates a newDiplomaticTrade
instance.- Parameters:
game
- The enclosingGame
.context
- TheTradeContext
for this agreement.sender
- The sendingPlayer
.recipient
- The recipientPlayer
.items
- A list of items to trade.version
- The trade version number.
-
-
Method Detail
-
makePeaceTreaty
public static DiplomaticTrade makePeaceTreaty(DiplomaticTrade.TradeContext context, Player sender, Player recipient)
Make a new diplomatic trade for a given context that establishes peace between two given players.- Parameters:
context
- TheTradeContext
the peace arises in.sender
- The sendingPlayer
.recipient
- The recipientPlayer
.- Returns:
- A suitable
DiplomaticTrade
.
-
getContext
public DiplomaticTrade.TradeContext getContext()
Get the trade context.- Returns:
- The context of this agreement.
-
getStatus
public DiplomaticTrade.TradeStatus getStatus()
Get the trade status.- Returns:
- The status of this agreement.
-
setStatus
public void setStatus(DiplomaticTrade.TradeStatus status)
Set the trade status.- Parameters:
status
- The newTradeStatus
for this agreement.
-
getSender
public final Player getSender()
Get the sending player.- Returns:
- The sending
Player
.
-
setSender
public final void setSender(Player newSender)
Set the sending player.- Parameters:
newSender
- The new sendingPlayer
.
-
getRecipient
public final Player getRecipient()
Get the recipient player.- Returns:
- The recipient
Player
.
-
setRecipient
public final void setRecipient(Player newRecipient)
Set the recieving player.- Parameters:
newRecipient
- The new recipientPlayer
.
-
getOtherPlayer
public Player getOtherPlayer(Player player)
Get the other player in a trade.- Parameters:
player
- The knownPlayer
.- Returns:
- The other player, not the supplied known one.
-
getSendMessage
public StringTemplate getSendMessage(Player player, Settlement settlement)
Handy utility to get the message associated with sending this agreement from a player to a settlement owner.- Parameters:
player
- The sendingPlayer
.settlement
- TheSettlement
to send to.- Returns:
- A
StringTemplate
for the message.
-
getReceiveMessage
public StringTemplate getReceiveMessage(Player player)
Handy utility to get the message associated with sending this agreement from a player to a settlement owner.- Parameters:
player
- ThePlayer
the offer came from.- Returns:
- A
StringTemplate
for the message.
-
add
public void add(TradeItem newItem)
Add to the DiplomaticTrade.- Parameters:
newItem
- TheTradeItem
to add.
-
remove
public void remove(TradeItem newItem)
Remove a from the DiplomaticTrade.- Parameters:
newItem
- TheTradeItem
to remove.
-
remove
public void remove(int index)
Remove from the DiplomaticTrade.- Parameters:
index
- The index of theTradeItem
to remove
-
removeType
public void removeType(java.lang.Class<? extends TradeItem> itemClass)
Removes all trade items of the same class as the given argument.- Parameters:
itemClass
- TheClass
ofTradeItem
to remove.
-
clear
public void clear()
Remove all trade items from this agreement.
-
getItems
public final java.util.List<TradeItem> getItems()
Get a list of all items to trade.- Returns:
- A list of all the
TradeItems
.
-
isEmpty
public final boolean isEmpty()
Are there no trade items present?- Returns:
- True if there are no trade items present.
-
getItemsGivenBy
public java.util.List<TradeItem> getItemsGivenBy(Player player)
Get the items offered by a particular player.- Parameters:
player
- ThePlayer
to check.- Returns:
- A list of
TradeItem
s offered by the player.
-
getStance
public Stance getStance()
Get the stance being offered.- Returns:
- The
Stance
offered in this trade, or null if none.
-
getColoniesGivenBy
public java.util.List<Colony> getColoniesGivenBy(Player player)
Get a list of colonies offered in this trade.- Parameters:
player
- ThePlayer
offering the colonies.- Returns:
- A list of
Colony
s offered in this trade.
-
getGoldGivenBy
public int getGoldGivenBy(Player player)
Get the gold offered in this trade by a given player.- Parameters:
player
- ThePlayer
to check.- Returns:
- The gold offered in this trade.
-
getGoodsGivenBy
public java.util.List<Goods> getGoodsGivenBy(Player player)
Get the goods being offered.- Parameters:
player
- ThePlayer
offering the goods.- Returns:
- A list of
Goods
offered in this trade.
-
getVictim
public Player getVictim()
Get the player being incited against.- Returns:
- The
Player
to be incited against.
-
getUnitsGivenBy
public java.util.List<Unit> getUnitsGivenBy(Player player)
Get a list of units offered in this trade.- Parameters:
player
- ThePlayer
offering the units.- Returns:
- A list of
Unit
s offered in this trade.
-
getVersion
public int getVersion()
Gets the version of this agreement.- Returns:
- The version number.
-
incrementVersion
public void incrementVersion()
Increment the version of this agreement.
-
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
-
-