Package net.sf.freecol.common.model
Class GoodsContainer
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- net.sf.freecol.common.model.FreeColGameObject
-
- net.sf.freecol.common.model.GoodsContainer
-
- All Implemented Interfaces:
java.lang.Comparable<FreeColObject>
,Ownable
,ObjectWithId
public class GoodsContainer extends FreeColGameObject implements Ownable
Contains goods and can be used by aLocation
to make certain tasks easier.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
AMOUNT_TAG
static int
CARGO_SIZE
The size of a standard `hold' of data.static int
HUGE_CARGO_SIZE
Value to use for apparent unlimited quantities of goods (e.g.private static java.util.logging.Logger
logger
static java.lang.String
OLD_STORED_GOODS_TAG
private java.util.Map<GoodsType,java.lang.Integer>
oldStoredGoods
The previous list of Goods stored in thisGoodsContainer
.private Location
parent
The location for thisGoodsContainer
.static java.lang.String
STORED_GOODS_TAG
private java.util.Map<GoodsType,java.lang.Integer>
storedGoods
The list of Goods stored in thisGoodsContainer
.static java.lang.String
TAG
static java.lang.String
TYPE_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 GoodsContainer(Game game, java.lang.String id)
Create a newGoodsContainer
.GoodsContainer(Game game, Location parent)
Creates an emptyGoodsContainer
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addGoods(GoodsType type, int amount)
Adds goods by type and amount to this goods container.<T extends AbstractGoods>
booleanaddGoods(T goods)
Adds goods to this goods container.private void
clearContainers()
Clear both containers.<T extends AbstractGoods>
booleancontains(T g)
Checks if the specifiedGoods
is in this container.<T extends FreeColObject>
booleancopyIn(T other)
Copy another FreeColObject into this one if it is compatible.void
disposeResources()
Dispose of the resources of this object, and finally remove it from the game.boolean
fireChanges()
Fire property changes for all goods that have seen level changes since the last saveState().java.util.List<Goods>
getCompactGoodsList()
Gets a list of all goods in this goods container.int
getGoodsCount(GoodsType type)
Gets the amount of one type of goods in this container.java.util.List<Goods>
getGoodsList()
Gets a list containing all holds of goods in this goods container.int
getOldGoodsCount(GoodsType type)
Gets the amount of one type of goods at the beginning of the turn.protected java.util.Map<GoodsType,java.lang.Integer>
getOldStoredGoods()
Get the old stored goods.Player
getOwner()
Gets the owner of thisOwnable
.protected Location
getParent()
Get the parent location.int
getSpaceTaken()
Gets the amount of space that the goods in this container will consume.protected java.util.Map<GoodsType,java.lang.Integer>
getStoredGoods()
Get the stored goods.java.lang.String
getXMLTagName()
Get the serialization tag for this object.boolean
hasChanged()
Has this goods containers contents changed from what was recorded last time the state was saved?boolean
hasReachedCapacity(int amount)
Checks if any storable type of goods has reached the given amount.static void
moveGoods(GoodsContainer src, GoodsType goodsType, int amount, GoodsContainer dst)
Utility to move goods from one location to another.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.private void
readStorage(FreeColXMLReader xr, java.util.Map<GoodsType,java.lang.Integer> storage)
Read a storage container from a stream.void
removeAbove(int newAmount)
Removes all goods above given amount, provided that the goods are storable and do not ignore warehouse limits.void
removeAll()
Remove all goods.Goods
removeGoods(GoodsType type)
Removes all goods of a given type from this goods container.Goods
removeGoods(GoodsType type, int amount)
Removes goods by type and amount from this goods container.<T extends AbstractGoods>
GoodsremoveGoods(T goods)
Removes goods from this goods container.void
restoreState()
Restore the current stored goods of this goods container to the old state.void
saveState()
Save the current stored goods of this goods container in the old stored goods.void
setAmount(GoodsType goodsType, int newAmount)
Set the amount of goods in this container.void
setLocation(Location location)
Set the goods location.protected void
setOldStoredGoods(java.util.Map<GoodsType,java.lang.Integer> goods)
Set the old stored goods.void
setOwner(Player p)
Sets the owner of thisOwnable
.protected void
setStoredGoods(java.util.Map<GoodsType,java.lang.Integer> goods)
Set the stored goods.java.lang.String
toString()
protected void
writeChildren(FreeColXMLWriter xw)
Write the children of this object to a stream.private void
writeStorage(FreeColXMLWriter xw, java.lang.String tag, java.util.Map<GoodsType,java.lang.Integer> storage)
Write a storage container to a stream.-
Methods inherited from class net.sf.freecol.common.model.FreeColGameObject
checkIntegrity, dispose, equals, fundamentalDispose, getClassIndex, getDisposables, getGame, getLinkTarget, getSpecification, hashCode, intern, internId, isDisposed, isInitialized, isInternable, readAttributes, 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, writeAttributes
-
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
TAG
public static final java.lang.String TAG
- See Also:
- Constant Field Values
-
CARGO_SIZE
public static final int CARGO_SIZE
The size of a standard `hold' of data.- See Also:
- Constant Field Values
-
HUGE_CARGO_SIZE
public static final int HUGE_CARGO_SIZE
Value to use for apparent unlimited quantities of goods (e.g. warehouse contents in Europe, amount of food a colony can import). Has to be signficantly bigger than any one unit could expect to carry, but not so huge as to look silly in user messages.- See Also:
- Constant Field Values
-
storedGoods
private final java.util.Map<GoodsType,java.lang.Integer> storedGoods
The list of Goods stored in thisGoodsContainer
. Always accessed synchronized (except I/O).
-
oldStoredGoods
private final java.util.Map<GoodsType,java.lang.Integer> oldStoredGoods
The previous list of Goods stored in thisGoodsContainer
. Always accessed synchronized and *after synchronized(storedGoods)*. This is only touched rarely so the extra lock is tolerable. (Not synchronized during I/O)
-
parent
private Location parent
The location for thisGoodsContainer
.
-
AMOUNT_TAG
public static final java.lang.String AMOUNT_TAG
- See Also:
- Constant Field Values
-
OLD_STORED_GOODS_TAG
public static final java.lang.String OLD_STORED_GOODS_TAG
- See Also:
- Constant Field Values
-
STORED_GOODS_TAG
public static final java.lang.String STORED_GOODS_TAG
- See Also:
- Constant Field Values
-
TYPE_TAG
public static final java.lang.String TYPE_TAG
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
GoodsContainer
public GoodsContainer(Game game, Location parent)
Creates an emptyGoodsContainer
.- Parameters:
game
- The enclosingGame
.parent
- TheLocation
thisGoodsContainer
contains goods for.
-
GoodsContainer
public GoodsContainer(Game game, java.lang.String id)
Create a newGoodsContainer
.- Parameters:
game
- The enclosingGame
.id
- The object identifier.
-
-
Method Detail
-
getParent
protected Location getParent()
Get the parent location.- Returns:
- The parent
Location
.
-
setLocation
public void setLocation(Location location)
Set the goods location.- Parameters:
location
- TheLocation
to set.
-
getStoredGoods
protected java.util.Map<GoodsType,java.lang.Integer> getStoredGoods()
Get the stored goods.- Returns:
- A map of the stored goods.
-
setStoredGoods
protected void setStoredGoods(java.util.Map<GoodsType,java.lang.Integer> goods)
Set the stored goods.- Parameters:
goods
- A map of the new stored goods.
-
getOldStoredGoods
protected java.util.Map<GoodsType,java.lang.Integer> getOldStoredGoods()
Get the old stored goods.- Returns:
- A map of the old stored goods.
-
setOldStoredGoods
protected void setOldStoredGoods(java.util.Map<GoodsType,java.lang.Integer> goods)
Set the old stored goods.- Parameters:
goods
- A map of the new old stored goods.
-
contains
public <T extends AbstractGoods> boolean contains(T g)
Checks if the specifiedGoods
is in this container.- Type Parameters:
T
- The base type of the goods.- Parameters:
g
- The goods to test the presence of.- Returns:
- True if there is enough of the specified goods present that it can be removed without error.
-
getGoodsCount
public int getGoodsCount(GoodsType type)
Gets the amount of one type of goods in this container.- Parameters:
type
- TheGoodsType
being looked for.- Returns:
- The amount of this type of goods in this container.
-
getOldGoodsCount
public int getOldGoodsCount(GoodsType type)
Gets the amount of one type of goods at the beginning of the turn.- Parameters:
type
- TheGoodsType
being looked for.- Returns:
- The amount of this type of goods in this container at the beginning of the turn
-
addGoods
public <T extends AbstractGoods> boolean addGoods(T goods)
Adds goods to this goods container.- Type Parameters:
T
- The base type of the goods.- Parameters:
goods
- The goods to add.- Returns:
- True if the addition succeeds.
-
addGoods
public boolean addGoods(GoodsType type, int amount)
Adds goods by type and amount to this goods container. Note: negative amounts are allowed.- Parameters:
type
- TheGoodsType
to add.amount
- The amount of goods to add.- Returns:
- True if the addition succeeds.
-
removeGoods
public <T extends AbstractGoods> Goods removeGoods(T goods)
Removes goods from this goods container.- Type Parameters:
T
- The base type of the goods.- Parameters:
goods
- The goods to remove from this container.- Returns:
- The
Goods
actually removed.
-
removeGoods
public Goods removeGoods(GoodsType type)
Removes all goods of a given type from this goods container.- Parameters:
type
- TheGoodsType
to remove.- Returns:
- The
Goods
actually removed.
-
removeGoods
public Goods removeGoods(GoodsType type, int amount)
Removes goods by type and amount from this goods container.- Parameters:
type
- TheGoodsType
to remove.amount
- The amount of goods to remove.- Returns:
- The
Goods
actually removed, which may have a lower actual amount, or null if nothing removed.
-
setAmount
public void setAmount(GoodsType goodsType, int newAmount)
Set the amount of goods in this container.- Parameters:
goodsType
- TheGoodsType
to set the amount of.newAmount
- The new amount.
-
removeAll
public void removeAll()
Remove all goods.
-
clearContainers
private void clearContainers()
Clear both containers.
-
removeAbove
public void removeAbove(int newAmount)
Removes all goods above given amount, provided that the goods are storable and do not ignore warehouse limits.- Parameters:
newAmount
- The threshold.
-
hasReachedCapacity
public boolean hasReachedCapacity(int amount)
Checks if any storable type of goods has reached the given amount.- Parameters:
amount
- The amount to check.- Returns:
- True if any storable, capacity limited goods has reached the given amount.
-
getSpaceTaken
public int getSpaceTaken()
Gets the amount of space that the goods in this container will consume. Each occupied cargo slot contains an amount in [1, CARGO_SIZE].- Returns:
- The amount of space taken by this containers goods.
-
getGoodsList
public java.util.List<Goods> getGoodsList()
Gets a list containing all holds of goods in this goods container. EachGoods
returned has a maximum amount of CARGO_SIZE.- Returns:
- A list of
Goods
.
-
getCompactGoodsList
public java.util.List<Goods> getCompactGoodsList()
Gets a list of all goods in this goods container. There is only oneGoods
for each distinctGoodsType
.- Returns:
- A list of
Goods
.
-
saveState
public void saveState()
Save the current stored goods of this goods container in the old stored goods.
-
restoreState
public void restoreState()
Restore the current stored goods of this goods container to the old state.
-
hasChanged
public boolean hasChanged()
Has this goods containers contents changed from what was recorded last time the state was saved?- Returns:
- True if the contents have changed.
-
fireChanges
public boolean fireChanges()
Fire property changes for all goods that have seen level changes since the last saveState().- Returns:
- True if something changed.
-
moveGoods
public static void moveGoods(GoodsContainer src, GoodsType goodsType, int amount, GoodsContainer dst)
Utility to move goods from one location to another.- Parameters:
src
- The sourceGoodsContainer
.goodsType
- TheGoodsType
to move.amount
- The amount of goods to move.dst
- The newGoodsContainer
.
-
getOwner
public Player getOwner()
Gets the owner of thisOwnable
.
-
setOwner
public void setOwner(Player p)
Sets the owner of thisOwnable
.
-
disposeResources
public void disposeResources()
Dispose of the resources of this object, and finally remove it from the game. To be extended by subclasses, but they must tail call up towards this.- Overrides:
disposeResources
in classFreeColGameObject
-
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.
-
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.
-
writeStorage
private void writeStorage(FreeColXMLWriter xw, java.lang.String tag, java.util.Map<GoodsType,java.lang.Integer> storage) throws javax.xml.stream.XMLStreamException
Write a storage container to a stream.- Parameters:
xw
- TheFreeColXMLWriter
to write to.tag
- The element tag.storage
- The storage container.- Throws:
javax.xml.stream.XMLStreamException
- if there is a problem writing to 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.
-
readStorage
private void readStorage(FreeColXMLReader xr, java.util.Map<GoodsType,java.lang.Integer> storage) throws javax.xml.stream.XMLStreamException
Read a storage container from a stream.- Parameters:
xr
- TheFreeColXMLReader
to read from.storage
- The storage container.- Throws:
javax.xml.stream.XMLStreamException
- if there is a problem reading from 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
-
-