net.sf.freecol.common.model
Class GoodsContainer

java.lang.Object
  extended by net.sf.freecol.common.model.FreeColObject
      extended by net.sf.freecol.common.model.FreeColGameObject
          extended by net.sf.freecol.common.model.GoodsContainer
All Implemented Interfaces:
Ownable

public class GoodsContainer
extends FreeColGameObject
implements Ownable

Contains goods and can be used by a Location to make certain tasks easier.


Field Summary
static int CARGO_SIZE
           
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 this GoodsContainer.
private  Location parent
          The owner of this GoodsContainer.
static java.lang.String STORED_GOODS_TAG
           
private  java.util.Map<GoodsType,java.lang.Integer> storedGoods
          The list of Goods stored in this GoodsContainer.
 
Fields inherited from class net.sf.freecol.common.model.FreeColGameObject
UNITS_TAG_NAME
 
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE, ID_ATTRIBUTE, ID_ATTRIBUTE_TAG, INFINITY, NO_ID, PARTIAL_ATTRIBUTE, UNDEFINED, VALUE_TAG
 
Constructor Summary
GoodsContainer(Game game, Location parent)
          Creates an empty GoodsContainer.
GoodsContainer(Game game, Location parent, org.w3c.dom.Element e)
          Initiates a new GoodsContainer from an Element.
GoodsContainer(Game game, Location parent, javax.xml.stream.XMLStreamReader in)
          Initiates a new GoodsContainer from an Element.
 
Method Summary
 boolean addGoods(AbstractGoods g)
          Adds a Goods to this containter.
 boolean addGoods(GoodsType type, int amount)
          Adds the given amount of the given type of goods.
 boolean contains(Goods g)
          Checks if the specified Goods is in this container.
 void dispose()
          Dispose of this GoodsContainer.
 java.util.List<FreeColGameObject> disposeList()
          Removes all references to this object.
 void fireChanges()
          Fire property changes for all goods that have seen level changes since the last saveState().
 java.util.List<Goods> getCompactGoods()
          Gets an Iterator of every Goods in this GoodsContainer.
 java.util.List<Goods> getFullGoods()
          Gets an Iterator of every Goods in this GoodsContainer.
 java.util.List<Goods> getGoods()
          Returns an ArrayList containing all Goods in this GoodsContainer.
 Goods getGoods(GoodsType goodsType)
           
 int getGoodsCount()
          Gets the number of goods-packages.
 int getGoodsCount(GoodsType type)
          Returns the amount of one type of Goods in this container.
 java.util.Iterator<Goods> getGoodsIterator()
          Gets an Iterator of every Goods in this GoodsContainer.
 int getOldGoodsCount(GoodsType type)
          Returns the amount of one type of Goods at the beginning of the turn.
 Player getOwner()
          Gets the owner of this GoodsContainer.
static java.lang.String getXMLElementTagName()
          Gets the tag name of the root element representing 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.
protected  void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
          Initialize this object from an XML-representation of this object.
protected  void readFromXMLPartialImpl(javax.xml.stream.XMLStreamReader in)
          Partial reader, so that "remove" messages can be brief.
 void removeAbove(int newAmount)
          Removes all goods above given amount, provided that the goods are storable and do not ignore warehouse limits.
 void removeAll()
          Removes all goods.
 Goods removeGoods(AbstractGoods g)
          Removes Goods from this containter.
 Goods removeGoods(GoodsType type)
           
 Goods removeGoods(GoodsType type, int amount)
          Removes the given amount of the given type of goods.
 void saveState()
          Prepares this GoodsContainer for a new turn.
 void setAmount(GoodsType goodsType, int newAmount)
          Set the amount of goods in this container.
 void setOwner(Player p)
          Sets the owner of this Ownable.
 java.lang.String toString()
          Creates a String representation of this GoodsContainer.
protected  void toXMLImpl(javax.xml.stream.XMLStreamWriter out, Player player, boolean showAll, boolean toSavedGame)
          This method writes an XML-representation of this object to the given stream.
protected  void toXMLPartialImpl(javax.xml.stream.XMLStreamWriter out, java.lang.String[] fields)
          Partial writer, so that "remove" messages can be brief.
private  void writeStorage(javax.xml.stream.XMLStreamWriter out, java.lang.String tag, java.util.Map<GoodsType,java.lang.Integer> storage)
           
 
Methods inherited from class net.sf.freecol.common.model.FreeColGameObject
equals, equals, fundamentalDispose, getFreeColGameObject, getFreeColGameObject, getGame, getIntegerID, getSpecification, hashCode, isDisposed, isUninitialized, newLocation, readFromXML, readFromXMLPartialByClass, setDefaultId, setGame, setId, toXML, toXMLImpl, toXMLPartialByClass, updateFreeColGameObject
 
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addPropertyChangeListener, addPropertyChangeListener, dumpObject, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAttribute, getAttribute, getAttribute, getAttribute, getAttribute, getId, getPropertyChangeListeners, getPropertyChangeListeners, hasAbility, hasAttribute, hasListeners, readAttributes, readAttributes, readChild, readChild, readChildren, readChildren, readFromArrayElement, readFromArrayElement, readFromListElement, readFromXMLElement, readFromXMLImpl, removePropertyChangeListener, removePropertyChangeListener, save, save, setSpecification, toXML, toXML, toXMLElement, toXMLElement, toXMLElement, toXMLElement, toXMLElementPartial, writeAttribute, writeAttributes, writeChildren
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

logger

private static final java.util.logging.Logger logger

CARGO_SIZE

public static final int CARGO_SIZE
See Also:
Constant Field Values

STORED_GOODS_TAG

public static final java.lang.String STORED_GOODS_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

storedGoods

private java.util.Map<GoodsType,java.lang.Integer> storedGoods
The list of Goods stored in this GoodsContainer.


oldStoredGoods

private java.util.Map<GoodsType,java.lang.Integer> oldStoredGoods
The previous list of Goods stored in this GoodsContainer.


parent

private final Location parent
The owner of this GoodsContainer.

Constructor Detail

GoodsContainer

public GoodsContainer(Game game,
                      Location parent)
Creates an empty GoodsContainer.

Parameters:
game - The Game in which this GoodsContainer belong.
parent - The Location this GoodsContainer will be containg goods for.

GoodsContainer

public GoodsContainer(Game game,
                      Location parent,
                      javax.xml.stream.XMLStreamReader in)
               throws javax.xml.stream.XMLStreamException
Initiates a new GoodsContainer from an Element.

Parameters:
game - The Game in which this GoodsContainer belong.
parent - The object using this GoodsContainer for storing it's goods.
in - The input stream containing the XML.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

GoodsContainer

public GoodsContainer(Game game,
                      Location parent,
                      org.w3c.dom.Element e)
Initiates a new GoodsContainer from an Element.

Parameters:
game - The Game in which this GoodsContainer belong.
parent - The object using this GoodsContainer for storing it's goods.
e - An XML-element that will be used to initialize this object.
Method Detail

getOwner

public Player getOwner()
Gets the owner of this GoodsContainer.

Specified by:
getOwner in interface Ownable
Returns:
The Player controlling this Ownable.

setOwner

public void setOwner(Player p)
Sets the owner of this Ownable.

Specified by:
setOwner in interface Ownable
Parameters:
p - The Player that should take ownership of this Ownable.
Throws:
java.lang.UnsupportedOperationException - if not implemented.

disposeList

public java.util.List<FreeColGameObject> disposeList()
Removes all references to this object.

Overrides:
disposeList in class FreeColGameObject
Returns:
A list of disposed objects.

dispose

public void dispose()
Dispose of this GoodsContainer.

Overrides:
dispose in class FreeColGameObject

addGoods

public boolean addGoods(AbstractGoods g)
Adds a Goods to this containter.

Parameters:
g - The Goods to add to this container.

addGoods

public boolean addGoods(GoodsType type,
                        int amount)
Adds the given amount of the given type of goods.

Parameters:
type - The type of goods to add.
amount - The type of amount to add.

removeGoods

public Goods removeGoods(AbstractGoods g)
Removes Goods from this containter.

Parameters:
g - The Goods to remove from this container.

removeGoods

public Goods removeGoods(GoodsType type)

removeGoods

public Goods removeGoods(GoodsType type,
                         int amount)
Removes the given amount of the given type of goods.

Parameters:
type - The type of goods to remove.
amount - The type of amount to remove.
Returns:
A Goods with the requested or available amount that has been removed

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 treshold.

setAmount

public void setAmount(GoodsType goodsType,
                      int newAmount)
Set the amount of goods in this container.

Parameters:
goodsType - The GoodsType to set the amount of.
newAmount - The new amount.

removeAll

public void removeAll()
Removes all goods.


hasReachedCapacity

public boolean hasReachedCapacity(int amount)
Checks if any storable type of goods has reached the given amount.

Parameters:
amount - The amount.
Returns:
true if any type of goods, except for Goods.FOOD, has reached the given amount.

contains

public boolean contains(Goods g)
Checks if the specified Goods is in this container.

Parameters:
g - The Goods to test the presence of.
Returns:
The result.

getGoodsCount

public int getGoodsCount(GoodsType type)
Returns the amount of one type of Goods in this container.

Parameters:
type - The type of Goods being looked for in this container.
Returns:
The amount of this type of Goods in this container.

getOldGoodsCount

public int getOldGoodsCount(GoodsType type)
Returns the amount of one type of Goods at the beginning of the turn.

Parameters:
type - The type of Goods being looked for in this container.
Returns:
The amount of this type of Goods in this container. at the beginning of the turn

getGoods

public Goods getGoods(GoodsType goodsType)

getGoodsCount

public int getGoodsCount()
Gets the number of goods-packages. A goods package contain between 1-CARGO_SIZE.

Returns:
The number of goods packages.

getGoodsIterator

public java.util.Iterator<Goods> getGoodsIterator()
Gets an Iterator of every Goods in this GoodsContainer. Each Goods have a maximum amount of CARGO_SIZE.

Returns:
The Iterator.
See Also:
getCompactGoods()

getGoods

public java.util.List<Goods> getGoods()
Returns an ArrayList containing all Goods in this GoodsContainer. Each Goods has a maximum amount of CARGO_SIZE.

Returns:
The ArrayList.
See Also:
getGoodsIterator()

getCompactGoods

public java.util.List<Goods> getCompactGoods()
Gets an Iterator of every Goods in this GoodsContainer. There is only one Goods for each type of goods.

Returns:
The Iterator.
See Also:
getGoodsIterator()

getFullGoods

public java.util.List<Goods> getFullGoods()
Gets an Iterator of every Goods in this GoodsContainer. There is only one Goods for each type of goods.

Returns:
The Iterator.
See Also:
getGoodsIterator()

saveState

public void saveState()
Prepares this GoodsContainer for a new turn.


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 void fireChanges()
Fire property changes for all goods that have seen level changes since the last saveState().


toXMLImpl

protected void toXMLImpl(javax.xml.stream.XMLStreamWriter out,
                         Player player,
                         boolean showAll,
                         boolean toSavedGame)
                  throws javax.xml.stream.XMLStreamException
This method writes an XML-representation of this object to the given stream.

Only attributes visible to the given Player will be added to that representation if showAll is set to false.

Specified by:
toXMLImpl in class FreeColGameObject
Parameters:
out - The target stream.
player - The Player this XML-representation should be made for, or null if showAll == true.
showAll - Only attributes visible to player will be added to the representation if showAll is set to false.
toSavedGame - If true then information that is only needed when saving a game is added.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

writeStorage

private void writeStorage(javax.xml.stream.XMLStreamWriter out,
                          java.lang.String tag,
                          java.util.Map<GoodsType,java.lang.Integer> storage)
                   throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

readFromXMLImpl

protected void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
                        throws javax.xml.stream.XMLStreamException
Initialize this object from an XML-representation of this object.

Overrides:
readFromXMLImpl in class FreeColObject
Parameters:
in - The input stream with the XML.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

toXMLPartialImpl

protected void toXMLPartialImpl(javax.xml.stream.XMLStreamWriter out,
                                java.lang.String[] fields)
                         throws javax.xml.stream.XMLStreamException
Partial writer, so that "remove" messages can be brief.

Overrides:
toXMLPartialImpl in class FreeColObject
Parameters:
out - The target stream.
fields - The fields to write.
Throws:
javax.xml.stream.XMLStreamException - If there are problems writing the stream.

readFromXMLPartialImpl

protected void readFromXMLPartialImpl(javax.xml.stream.XMLStreamReader in)
                               throws javax.xml.stream.XMLStreamException
Partial reader, so that "remove" messages can be brief.

Overrides:
readFromXMLPartialImpl in class FreeColObject
Parameters:
in - The input stream with the XML.
Throws:
javax.xml.stream.XMLStreamException - If there are problems reading the stream.

toString

public java.lang.String toString()
Creates a String representation of this GoodsContainer.

Overrides:
toString in class FreeColGameObject
Returns:
A string representation of the object.

getXMLElementTagName

public static java.lang.String getXMLElementTagName()
Gets the tag name of the root element representing this object.

Returns:
"goodsContainer".