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
TheUnitLocationis a place where aUnitcan be put. The UnitLocation can not store any other Locatables, such asGoods, orTileItems. 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 classUnitLocation.NoAddReason
-
Field Summary
Fields Modifier and Type Field Description private static java.util.logging.Loggerloggerprivate java.util.List<Unit>unitsThe 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 newUnitLocationinstance.UnitLocation(Game game, java.lang.String id)Creates a newUnitLocationinstance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(Locatable locatable)Adds aLocatableto this Location.private booleanaddUnit(Unit u)Internal addition of a unit to this location.booleancanAdd(Locatable locatable)Checks whether or not the specified locatable may be added to thisLocation.protected voidclearUnitList()Clear the units from this container.booleancontains(Locatable locatable)Checks if thisLocationcontains the specifiedLocatable.<T extends FreeColObject>
booleancopyIn(T other)Copy another FreeColObject into this one if it is compatible.voiddisposeResources()Dispose of the resources of this object, and finally remove it from the game.booleanequipForRole(Unit unit, Role role, int roleCount)Equip a unit for a role using resources at this location.UnitgetCarrierForUnit(Unit unit)Gets a carrier for the supplied unit, if one exists.ColonygetColony()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.UnitgetFirstUnit()Gets the first Unit at this unit location.GoodsContainergetGoodsContainer()Gets theGoodsContainerthisLocationuse for storing it's goods.IndianSettlementgetIndianSettlement()Gets the native settlement at this location.UnitgetLastUnit()Gets the last Unit at this unit location.StringTemplategetLocationLabel()Get a label for this location.StringTemplategetLocationLabelFor(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.NoAddReasongetNoAddReason(Locatable locatable)Gets the reason why a givenLocatablecan not be added to this Location.SettlementgetSettlement()Gets theSettlementthisLocationis located in.intgetSpaceTaken()Gets the current space taken by the units in this location.TilegetTile()Gets the Tile associated with this Location.intgetTotalUnitCount()Gets the total amount of Units at this Location, including units on a carrier.intgetUnitCapacity()Gets the maximum number ofUnitsthis Location can hold.intgetUnitCount()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.booleanhasCarrierWithSpace(int space)Checks if there is a useable carrier unit with a specified minimum amount of space available in this location.voidintern()Intern this object.booleanisEmpty()Is this unit location empty?booleanisFull()Is this unit location full?voidmoveToFront(Unit u)Move the given unit to the front of the units list.intpriceGoods(java.util.List<AbstractGoods> goods)What would the price be for a collection of goods at this location?protected voidreadChild(FreeColXMLReader xr)Reads a single child object.protected voidreadChildren(FreeColXMLReader xr)Reads the children of this object from an XML stream.booleanremove(Locatable locatable)Removes aLocatablefrom this Location.protected voidwriteChildren(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 newUnitLocationinstance.- Parameters:
game- The enclosingGame.
-
UnitLocation
public UnitLocation(Game game, java.lang.String id)
Creates a newUnitLocationinstance.- Parameters:
game- The enclosingGame.id- The object identifier.
-
-
Method Detail
-
intern
public void intern()
Intern this object.- Overrides:
internin classFreeColGameObject
-
addUnit
private boolean addUnit(Unit u)
Internal addition of a unit to this location.- Parameters:
u- TheUnitto add.- Returns:
- True if the unit is added.
-
isEmpty
public final boolean isEmpty()
Is this unit location empty?- Returns:
- a
booleanvalue
-
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 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
Units present.
-
getCarrierForUnit
public Unit getCarrierForUnit(Unit unit)
Gets a carrier for the supplied unit, if one exists.- Parameters:
unit- TheUnitto 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:
getDisposablesin classFreeColGameObject- 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:
disposeResourcesin classFreeColGameObject
-
getTile
public Tile getTile()
Gets the Tile associated with this Location.
-
getLocationLabel
public StringTemplate getLocationLabel()
Get a label for this location.- Specified by:
getLocationLabelin 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:
getLocationLabelForin interfaceLocation- Parameters:
player- ThePlayerto return the name for.- Returns:
- A label for this location.
-
add
public boolean add(Locatable locatable)
Adds aLocatableto this Location.
-
remove
public boolean remove(Locatable locatable)
Removes aLocatablefrom this Location.
-
contains
public boolean contains(Locatable locatable)
Checks if thisLocationcontains 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:
getUnitCountin 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:
getUnitListin interfaceLocation- Returns:
- A list of all the units at this location.
-
getGoodsContainer
public GoodsContainer getGoodsContainer()
Gets theGoodsContainerthisLocationuse for storing it's goods.- Specified by:
getGoodsContainerin interfaceLocation- Returns:
- The
GoodsContainerornullif theLocationcannot store any goods.
-
getSettlement
public Settlement getSettlement()
Gets theSettlementthisLocationis located in.- Specified by:
getSettlementin 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:
getIndianSettlementin interfaceLocation- Returns:
- The
IndianSettlementat 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- TheUnitto 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 givenLocatablecan 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- TheLocatableto test.- Returns:
- The reason why adding would fail.
-
getUnitCapacity
public int getUnitCapacity()
Gets the maximum number ofUnitsthis 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 ofAbstractGoodsto 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- TheUnitto equip.role- TheRoleto 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:
copyInin classFreeColGameObject- Type Parameters:
T- TheFreeColObjectsubclass 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:
writeChildrenin classFreeColObject- Parameters:
xw- TheFreeColXMLWriterto 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:
readChildrenin classFreeColObject- Parameters:
xr- TheFreeColXMLReaderto 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:
readChildin classFreeColObject- Parameters:
xr- TheFreeColXMLReaderto read from.- Throws:
javax.xml.stream.XMLStreamException- if there is a problem reading the stream.
-
-