Class UnitLocation

  • All Implemented Interfaces:
    java.lang.Comparable<FreeColObject>, Location, ObjectWithId
    Direct Known Subclasses:
    Europe, GoodsLocation, HighSeas, Tile, WorkLocation

    public abstract class UnitLocation
    extends FreeColGameObject
    implements Location
    The UnitLocation is a place where a Unit can be put. The UnitLocation can not store any other Locatables, such as Goods, or TileItems. Be *very* careful with use of the units list for low level routines as distinct from use of getUnits()/getUnitList()/getUnitCount() etc. Beware that Colony is a UnitLocation, but it does *not* store its units in the unit list, they live in its WorkLocations. Therefore Colony overrides several such routines to do the right thing, which can lead to confusion especially when debugging.
    See Also:
    Locatable
    • Field Detail

      • logger

        private static final java.util.logging.Logger logger
      • units

        private final java.util.List<Unit> units
        The Units present in this Location.
    • Constructor Detail

      • UnitLocation

        public UnitLocation​(Game game)
        Creates a new UnitLocation instance.
        Parameters:
        game - The enclosing Game.
      • UnitLocation

        public UnitLocation​(Game game,
                            java.lang.String id)
        Creates a new UnitLocation instance.
        Parameters:
        game - The enclosing Game.
        id - The object identifier.
    • Method Detail

      • addUnit

        private boolean addUnit​(Unit u)
        Internal addition of a unit to this location.
        Parameters:
        u - The Unit to add.
        Returns:
        True if the unit is added.
      • isEmpty

        public final boolean isEmpty()
        Is this unit location empty?
        Returns:
        a boolean value
      • isFull

        public final boolean isFull()
        Is this unit location full?
        Returns:
        True if this location is full.
      • getFirstUnit

        public final Unit getFirstUnit()
        Gets the first Unit at this unit location.
        Returns:
        The first Unit.
      • getLastUnit

        public final Unit getLastUnit()
        Gets the last Unit at this unit location.
        Returns:
        The last Unit.
      • getTotalUnitCount

        public int getTotalUnitCount()
        Gets the total amount of Units at this Location, including units on a carrier.
        Returns:
        The total number of Units at this Location.
      • hasCarrierWithSpace

        public boolean hasCarrierWithSpace​(int space)
        Checks if there is a useable carrier unit with a specified minimum amount of space available in this location.
        Parameters:
        space - The amount of space to require.
        Returns:
        True if there is a suitable unit present.
        See Also:
        Unit.isCarrier()
      • getNavalUnits

        public java.util.List<Unit> getNavalUnits()
        Gets a list of all naval units here.
        Returns:
        A list of naval Units present.
      • getCarrierForUnit

        public Unit getCarrierForUnit​(Unit unit)
        Gets a carrier for the supplied unit, if one exists.
        Parameters:
        unit - The Unit to carry.
        Returns:
        A suitable carrier or null if none found.
      • getDisposables

        public java.util.stream.Stream<FreeColGameObject> getDisposables()
        Collect this object and all its subparts that should be disposed of when this object goes away. Overriding routines should call upwards towards this routine, arranging that the object itself is last.
        Overrides:
        getDisposables in class FreeColGameObject
        Returns:
        A stream of FreeColGameObjects to dispose of.
      • 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
      • getTile

        public Tile getTile()
        Gets the Tile associated with this Location.
        Specified by:
        getTile in interface Location
        Returns:
        The Tile associated with this Location, or null if none found.
      • getLocationLabelFor

        public StringTemplate getLocationLabelFor​(Player player)
        Get a label for this location for a particular player.
        Specified by:
        getLocationLabelFor in interface Location
        Parameters:
        player - The Player to return the name for.
        Returns:
        A label for this location.
      • add

        public boolean add​(Locatable locatable)
        Adds a Locatable to this Location.
        Specified by:
        add in interface Location
        Parameters:
        locatable - The Locatable to add to this Location.
        Returns:
        True if the locatable was added.
      • remove

        public boolean remove​(Locatable locatable)
        Removes a Locatable from this Location.
        Specified by:
        remove in interface Location
        Parameters:
        locatable - The Locatable to remove from this Location.
        Returns:
        True if the locatable was removed.
      • contains

        public boolean contains​(Locatable locatable)
        Checks if this Location contains the specified Locatable.
        Specified by:
        contains in interface Location
        Parameters:
        locatable - The Locatable to test the presence of.
        Returns:
        True if the locatable is present at this location.
      • canAdd

        public boolean canAdd​(Locatable locatable)
        Checks whether or not the specified locatable may be added to this Location.
        Specified by:
        canAdd in interface Location
        Parameters:
        locatable - The Locatable to add.
        Returns:
        True if the locatable can be added to this location.
      • getUnitCount

        public int getUnitCount()
        Gets the number of units at this Location.
        Specified by:
        getUnitCount in interface Location
        Returns:
        The number of units at this Location.
      • getUnits

        public java.util.stream.Stream<Unit> getUnits()
        Gets a stream of all the units present at this location.
        Specified by:
        getUnits in interface Location
        Returns:
        A stream of all the units at this location.
      • getUnitList

        public java.util.List<Unit> getUnitList()
        Gets a list of all the units present at this location.
        Specified by:
        getUnitList in interface Location
        Returns:
        A list of all the units at this location.
      • getGoodsContainer

        public GoodsContainer getGoodsContainer()
        Gets the GoodsContainer this Location use for storing it's goods.
        Specified by:
        getGoodsContainer in interface Location
        Returns:
        The GoodsContainer or null if the Location cannot store any goods.
      • getSettlement

        public Settlement getSettlement()
        Gets the Settlement this Location is located in.
        Specified by:
        getSettlement in interface Location
        Returns:
        The associated Settlement, or null if none.
      • getColony

        public Colony getColony()
        Get the colony at this location.
        Specified by:
        getColony in interface Location
        Returns:
        A Colony at this location if any, or null if none found.
      • getIndianSettlement

        public IndianSettlement getIndianSettlement()
        Gets the native settlement at this location.
        Specified by:
        getIndianSettlement in interface Location
        Returns:
        The IndianSettlement at this location if any, or null if none found.
      • getSpaceTaken

        public int getSpaceTaken()
        Gets the current space taken by the units in this location. Note that Units are also unit locations, but their space taken is derived from the spec, so this routine must be overrideable.
        Returns:
        The sum of the space taken by the units in this location.
      • moveToFront

        public void moveToFront​(Unit u)
        Move the given unit to the front of the units list.
        Parameters:
        u - The Unit to move to the front.
      • clearUnitList

        protected void clearUnitList()
        Clear the units from this container.
      • getNoAddReason

        public UnitLocation.NoAddReason getNoAddReason​(Locatable locatable)
        Gets the reason why a given Locatable can not be added to this Location. Be careful to test for unit presence last before success (NoAddReason.NONE) except perhaps for the capacity test, so that we can treat ALREADY_PRESENT as success in some cases (e.g. if the unit changes type --- does it still have a required skill?) FIXME: consider moving this up to Location?
        Parameters:
        locatable - The Locatable to test.
        Returns:
        The reason why adding would fail.
      • getUnitCapacity

        public int getUnitCapacity()
        Gets the maximum number of Units this Location can hold. To be overridden by subclasses.
        Returns:
        Integer.MAX_VALUE, denoting no effective limit.
      • priceGoods

        public int priceGoods​(java.util.List<AbstractGoods> goods)
                       throws FreeColException
        What would the price be for a collection of goods at this location?
        Parameters:
        goods - The list of AbstractGoods to check for.
        Returns:
        The net cost of acquiring the goods at this location.
        Throws:
        FreeColException - if the goods are unavailable for any reason.
      • equipForRole

        public boolean equipForRole​(Unit unit,
                                    Role role,
                                    int roleCount)
        Equip a unit for a role using resources at this location.
        Parameters:
        unit - The Unit to equip.
        role - The Role to build for.
        roleCount - The role count.
        Returns:
        True if the equipping succeeded.
      • 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.
      • 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.