Package net.sf.freecol.common.model
Class UnitLocation
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- net.sf.freecol.common.model.FreeColGameObject
-
- net.sf.freecol.common.model.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
TheUnitLocation
is a place where aUnit
can be put. The UnitLocation can not store any other Locatables, such asGoods
, orTileItem
s. 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
UnitLocation.NoAddReason
-
Field Summary
Fields Modifier and Type Field Description private static java.util.logging.Logger
logger
private java.util.List<Unit>
units
The Units present in this Location.-
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
-
Fields inherited from interface net.sf.freecol.common.model.Location
LOCATION_RANK_EUROPE, LOCATION_RANK_HIGHSEAS, LOCATION_RANK_NOWHERE
-
-
Constructor Summary
Constructors Constructor Description UnitLocation(Game game)
Creates a newUnitLocation
instance.UnitLocation(Game game, java.lang.String id)
Creates a newUnitLocation
instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(Locatable locatable)
Adds aLocatable
to this Location.private boolean
addUnit(Unit u)
Internal addition of a unit to this location.boolean
canAdd(Locatable locatable)
Checks whether or not the specified locatable may be added to thisLocation
.protected void
clearUnitList()
Clear the units from this container.boolean
contains(Locatable locatable)
Checks if thisLocation
contains the specifiedLocatable
.<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
equipForRole(Unit unit, Role role, int roleCount)
Equip a unit for a role using resources at this location.Unit
getCarrierForUnit(Unit unit)
Gets a carrier for the supplied unit, if one exists.Colony
getColony()
Get the colony at this location.java.util.stream.Stream<FreeColGameObject>
getDisposables()
Collect this object and all its subparts that should be disposed of when this object goes away.Unit
getFirstUnit()
Gets the first Unit at this unit location.GoodsContainer
getGoodsContainer()
Gets theGoodsContainer
thisLocation
use for storing it's goods.IndianSettlement
getIndianSettlement()
Gets the native settlement at this location.Unit
getLastUnit()
Gets the last Unit at this unit location.StringTemplate
getLocationLabel()
Get a label for this location.StringTemplate
getLocationLabelFor(Player player)
Get a label for this location for a particular player.java.util.List<Unit>
getNavalUnits()
Gets a list of all naval units here.UnitLocation.NoAddReason
getNoAddReason(Locatable locatable)
Gets the reason why a givenLocatable
can not be added to this Location.Settlement
getSettlement()
Gets theSettlement
thisLocation
is located in.int
getSpaceTaken()
Gets the current space taken by the units in this location.Tile
getTile()
Gets the Tile associated with this Location.int
getTotalUnitCount()
Gets the total amount of Units at this Location, including units on a carrier.int
getUnitCapacity()
Gets the maximum number ofUnits
this Location can hold.int
getUnitCount()
Gets the number of units at this Location.java.util.List<Unit>
getUnitList()
Gets a list of all the units present at this location.java.util.stream.Stream<Unit>
getUnits()
Gets a stream of all the units present at this location.boolean
hasCarrierWithSpace(int space)
Checks if there is a useable carrier unit with a specified minimum amount of space available in this location.void
intern()
Intern this object.boolean
isEmpty()
Is this unit location empty?boolean
isFull()
Is this unit location full?void
moveToFront(Unit u)
Move the given unit to the front of the units list.int
priceGoods(java.util.List<AbstractGoods> goods)
What would the price be for a collection of goods at this location?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.boolean
remove(Locatable locatable)
Removes aLocatable
from this Location.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, equals, fundamentalDispose, getClassIndex, getGame, getLinkTarget, getSpecification, hashCode, 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, getXMLTagName, 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, toString, toXML, toXML, toXMLPartial, toXMLPartial, writeAttributes
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface net.sf.freecol.common.model.Location
getId, getLocationImageKey, getRank, toShortString, up
-
-
-
-
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 newUnitLocation
instance.- Parameters:
game
- The enclosingGame
.
-
UnitLocation
public UnitLocation(Game game, java.lang.String id)
Creates a newUnitLocation
instance.- Parameters:
game
- The enclosingGame
.id
- The object identifier.
-
-
Method Detail
-
intern
public void intern()
Intern this object.- Overrides:
intern
in classFreeColGameObject
-
addUnit
private boolean addUnit(Unit u)
Internal addition of a unit to this location.- Parameters:
u
- TheUnit
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
Unit
s at thisLocation
.
-
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
Unit
s present.
-
getCarrierForUnit
public Unit getCarrierForUnit(Unit unit)
Gets a carrier for the supplied unit, if one exists.- Parameters:
unit
- TheUnit
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 classFreeColGameObject
- Returns:
- A stream of
FreeColGameObject
s 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 classFreeColGameObject
-
getTile
public Tile getTile()
Gets the Tile associated with this Location.
-
getLocationLabel
public StringTemplate getLocationLabel()
Get a label for this location.- Specified by:
getLocationLabel
in interfaceLocation
- Returns:
- A label for this location.
-
getLocationLabelFor
public StringTemplate getLocationLabelFor(Player player)
Get a label for this location for a particular player.- Specified by:
getLocationLabelFor
in interfaceLocation
- Parameters:
player
- ThePlayer
to return the name for.- Returns:
- A label for this location.
-
add
public boolean add(Locatable locatable)
Adds aLocatable
to this Location.
-
remove
public boolean remove(Locatable locatable)
Removes aLocatable
from this Location.
-
contains
public boolean contains(Locatable locatable)
Checks if thisLocation
contains the specifiedLocatable
.
-
canAdd
public boolean canAdd(Locatable locatable)
Checks whether or not the specified locatable may be added to thisLocation
.
-
getUnitCount
public int getUnitCount()
Gets the number of units at this Location.- Specified by:
getUnitCount
in interfaceLocation
- 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.
-
getUnitList
public java.util.List<Unit> getUnitList()
Gets a list of all the units present at this location.- Specified by:
getUnitList
in interfaceLocation
- Returns:
- A list of all the units at this location.
-
getGoodsContainer
public GoodsContainer getGoodsContainer()
Gets theGoodsContainer
thisLocation
use for storing it's goods.- Specified by:
getGoodsContainer
in interfaceLocation
- Returns:
- The
GoodsContainer
ornull
if theLocation
cannot store any goods.
-
getSettlement
public Settlement getSettlement()
Gets theSettlement
thisLocation
is located in.- Specified by:
getSettlement
in interfaceLocation
- Returns:
- The associated
Settlement
, or null if none.
-
getColony
public Colony getColony()
Get the colony at this location.
-
getIndianSettlement
public IndianSettlement getIndianSettlement()
Gets the native settlement at this location.- Specified by:
getIndianSettlement
in interfaceLocation
- 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
- TheUnit
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 givenLocatable
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
- TheLocatable
to test.- Returns:
- The reason why adding would fail.
-
getUnitCapacity
public int getUnitCapacity()
Gets the maximum number ofUnits
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 ofAbstractGoods
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
- TheUnit
to equip.role
- TheRole
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 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.
-
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.
-
-