Class GoodsContainer

    • Field Detail

      • logger

        private static final java.util.logging.Logger logger
      • 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 this GoodsContainer. Always accessed synchronized (except I/O).
      • oldStoredGoods

        private final java.util.Map<GoodsType,​java.lang.Integer> oldStoredGoods
        The previous list of Goods stored in this GoodsContainer. 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 this GoodsContainer.
      • 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
    • Constructor Detail

      • GoodsContainer

        public GoodsContainer​(Game game,
                              Location parent)
        Creates an empty GoodsContainer.
        Parameters:
        game - The enclosing Game.
        parent - The Location this GoodsContainer contains goods for.
      • GoodsContainer

        public GoodsContainer​(Game game,
                              java.lang.String id)
        Create a new GoodsContainer.
        Parameters:
        game - The enclosing Game.
        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 - The Location 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 specified Goods 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 - The GoodsType 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 - The GoodsType 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 - The GoodsType to add.
        amount - The amount of goods to add.
        Returns:
        True if the addition succeeds.
      • removeGoods

        public <T extends AbstractGoodsGoods 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 - The GoodsType 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 - The GoodsType 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 - The GoodsType 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. Each Goods 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 one Goods for each distinct GoodsType.
        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 source GoodsContainer.
        goodsType - The GoodsType to move.
        amount - The amount of goods to move.
        dst - The new GoodsContainer.
      • getOwner

        public Player getOwner()
        Gets the owner of this Ownable.
        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.
      • 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 class FreeColGameObject
      • copyIn

        public <T extends FreeColObject> boolean copyIn​(T other)
        Copy another FreeColObject into this one if it is compatible.
        Overrides:
        copyIn in class FreeColGameObject
        Type Parameters:
        T - The FreeColObject 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 class FreeColObject
        Parameters:
        xw - The FreeColXMLWriter 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 - The FreeColXMLWriter 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 class FreeColObject
        Parameters:
        xr - The FreeColXMLReader 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 class FreeColObject
        Parameters:
        xr - The FreeColXMLReader 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 - The FreeColXMLReader 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 class FreeColObject
        Returns:
        The tag.