Package net.sf.freecol.common.model
Class TileItemContainer
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- net.sf.freecol.common.model.FreeColGameObject
-
- net.sf.freecol.common.model.TileItemContainer
-
- All Implemented Interfaces:
java.lang.Comparable<FreeColObject>
,ObjectWithId
public class TileItemContainer extends FreeColGameObject
ContainsTileItem
s and can be used by aTile
to make certain tasks easier.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.logging.Logger
logger
private static java.lang.String
OLD_TILE_IMPROVEMENT_TAG
static java.lang.String
TAG
private Tile
tile
The tile owner for which this is the container.private static java.lang.String
TILE_TAG
private java.util.List<TileItem>
tileItems
All tile items, sorted by zIndex.-
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
-
-
Constructor Summary
Constructors Constructor Description TileItemContainer(Game game, java.lang.String id)
Create a newTileItemContainer
.TileItemContainer(Game game, Tile tile)
Create an emptyTileItemContainer
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
addTileItem(TileItem item)
Add a tile item to this container.boolean
canProduce(GoodsType goodsType, UnitType unitType)
Does this container contain an item that allows the tile to produce a goods type?Constants.IntegrityType
checkIntegrity(boolean fix, LogBuilder lb)
Checks the integrity of this game object.private void
clearTileItems()
Clear the tile items list.boolean
contains(TileItem t)
Checks if the specifiedTileItem
is in this container.void
copyFrom(TileItemContainer tic, Map.Layer layer)
Copy the tile items from anotherTileItemContainer
, observing a layer maximum.<T extends FreeColObject>
booleancopyIn(T other)
Copy another FreeColObject into this one if it is compatible.void
disposeResources()
Removes all references to this object.private TileItem
findTileItem(java.util.function.Predicate<TileItem> pred)
Find a tile item matching a predicate.java.util.List<TileImprovement>
getCompleteImprovements()
Gets a list of the completedTileImprovement
s in thisTileItemContainer
.java.util.List<TileItem>
getCompleteItems()
Get the completed tile items.TileImprovement
getImprovement(TileImprovementType type)
Gets the tile improvement of the given type if any.java.util.List<TileImprovement>
getImprovements()
Gets a list of theTileImprovement
s in thisTileItemContainer
.private java.util.List<TileImprovement>
getImprovements(boolean completedOnly)
Gets a list of theTileImprovement
s in thisTileItemContainer
.LostCityRumour
getLostCityRumour()
Gets any lost city rumour in this container.int
getMoveCost(Tile fromTile, Tile targetTile, int basicMoveCost)
Determine the movement cost to thisTile
from anotherTile
.java.util.stream.Stream<Modifier>
getProductionModifiers(GoodsType goodsType, UnitType unitType)
Gets the production modifiers for the given type of goods and unit.Resource
getResource()
Gets any resource item.TileImprovement
getRiver()
Gets any river improvement in this container.TileImprovement
getRoad()
Gets any road improvement in this container.Tile
getTile()
Get the tile this container belongs to.private java.util.List<TileItem>
getTileItems()
Get a copy of the tile items list.int
getTotalBonusPotential(GoodsType goodsType, UnitType unitType, int potential, boolean onlyNatural)
Determine the total bonus for a goods type.java.lang.String
getXMLTagName()
Get the serialization tag for this object.boolean
hasImprovement(TileImprovementType type)
Check whether this tile has a completed improvement of the given type.private void
invalidateCache()
Invalidate the production cache of the owning colony if any but only if the tile is actually being used.protected void
readAttributes(FreeColXMLReader xr)
Reads the attributes of this object from an XML stream.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.<T extends TileItem>
voidremoveAll(java.lang.Class<T> c)
Removes all tile items of a given class.void
removeIncompatibleImprovements()
Remove improvements incompatible with the given TileType.<T extends TileItem>
TremoveTileItem(T item)
Removes aTileItem
from this container.void
setTileItems(java.util.List<TileItem> newTileItems)
Set the tile items.java.lang.String
toString()
TileItem
tryAddTileItem(TileItem item)
Try to add aTileItem
to this container.protected void
writeAttributes(FreeColXMLWriter xw)
Write the attributes of this object to a stream.protected void
writeChildren(FreeColXMLWriter xw)
Write the children of this object to a stream.-
Methods inherited from class net.sf.freecol.common.model.FreeColGameObject
dispose, equals, fundamentalDispose, getClassIndex, getDisposables, getGame, getLinkTarget, getSpecification, hashCode, intern, internId, isDisposed, isInitialized, isInternable, 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, 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, toXML, toXML, toXMLPartial, toXMLPartial
-
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
TAG
public static final java.lang.String TAG
- See Also:
- Constant Field Values
-
tile
private Tile tile
The tile owner for which this is the container.
-
tileItems
private final java.util.List<TileItem> tileItems
All tile items, sorted by zIndex.
-
TILE_TAG
private static final java.lang.String TILE_TAG
- See Also:
- Constant Field Values
-
OLD_TILE_IMPROVEMENT_TAG
private static final java.lang.String OLD_TILE_IMPROVEMENT_TAG
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TileItemContainer
public TileItemContainer(Game game, Tile tile)
Create an emptyTileItemContainer
.- Parameters:
game
- The enclosingGame
.tile
- TheTile
thisTileItemContainer
containsTileItems
for.
-
TileItemContainer
public TileItemContainer(Game game, java.lang.String id)
Create a newTileItemContainer
.- Parameters:
game
- The enclosingGame
.id
- The object identifier.
-
-
Method Detail
-
getTile
public final Tile getTile()
Get the tile this container belongs to.- Returns:
- The owning
Tile
.
-
invalidateCache
private void invalidateCache()
Invalidate the production cache of the owning colony if any but only if the tile is actually being used.
-
getTileItems
private java.util.List<TileItem> getTileItems()
Get a copy of the tile items list.- Returns:
- A copy of the
TileItem
s.
-
clearTileItems
private void clearTileItems()
Clear the tile items list.
-
setTileItems
public final void setTileItems(java.util.List<TileItem> newTileItems)
Set the tile items.- Parameters:
newTileItems
- The new tile items list.
-
addTileItem
private final void addTileItem(TileItem item)
Add a tile item to this container.- Parameters:
item
- TheTileItem
to add.
-
tryAddTileItem
public final TileItem tryAddTileItem(TileItem item)
Try to add aTileItem
to this container.- Parameters:
item
- TheTileItem
to add to this container.- Returns:
- The added
TileItem
or the existingTileItem
if of higher magnitude, or null on error.
-
removeTileItem
public final <T extends TileItem> T removeTileItem(T item)
Removes aTileItem
from this container.- Type Parameters:
T
- The actualTileItem
type.- Parameters:
item
- TheTileItem
to remove from this container.- Returns:
- The
TileItem
that has been removed from this container (if any).
-
removeAll
public final <T extends TileItem> void removeAll(java.lang.Class<T> c)
Removes all tile items of a given class.- Type Parameters:
T
- The actualTileItem
type.- Parameters:
c
- TheClass
to remove.
-
getCompleteItems
public java.util.List<TileItem> getCompleteItems()
Get the completed tile items.- Returns:
- A list of complete
TileItem
s.
-
getImprovement
public TileImprovement getImprovement(TileImprovementType type)
Gets the tile improvement of the given type if any.- Parameters:
type
- TheTileImprovementType
to look for.- Returns:
- The
TileImprovement
of the given type if present, otherwise null.
-
getImprovements
private java.util.List<TileImprovement> getImprovements(boolean completedOnly)
Gets a list of theTileImprovement
s in thisTileItemContainer
.- Parameters:
completedOnly
- If true select only the completed improvements.- Returns:
- A list of
TileImprovement
s.
-
getImprovements
public java.util.List<TileImprovement> getImprovements()
Gets a list of theTileImprovement
s in thisTileItemContainer
.- Returns:
- A list of
TileImprovement
s.
-
getCompleteImprovements
public java.util.List<TileImprovement> getCompleteImprovements()
Gets a list of the completedTileImprovement
s in thisTileItemContainer
.- Returns:
- A list of
TileImprovement
s.
-
findTileItem
private TileItem findTileItem(java.util.function.Predicate<TileItem> pred)
Find a tile item matching a predicate.- Parameters:
pred
- ThePredicate
to match.- Returns:
- The
TileItem
found, or null if none present.
-
getLostCityRumour
public final LostCityRumour getLostCityRumour()
Gets any lost city rumour in this container.- Returns:
- A
LostCityRumour
item if any, or null if not found.
-
getResource
public final Resource getResource()
Gets any resource item.- Returns:
- A
Resource
item, or null is none found.
-
getRoad
public TileImprovement getRoad()
Gets any road improvement in this container.- Returns:
- A road
TileImprovement
if any, or null if not found.
-
getRiver
public TileImprovement getRiver()
Gets any river improvement in this container.- Returns:
- A river
TileImprovement
if any, or null if not found.
-
hasImprovement
public boolean hasImprovement(TileImprovementType type)
Check whether this tile has a completed improvement of the given type.- Parameters:
type
- TheTileImprovementType
to check for.- Returns:
- Whether the tile has the improvement and the improvement is completed.
-
removeIncompatibleImprovements
public void removeIncompatibleImprovements()
Remove improvements incompatible with the given TileType. This method is called whenever the type of the container's tile changes, i.e. due to clearing.
-
getTotalBonusPotential
public int getTotalBonusPotential(GoodsType goodsType, UnitType unitType, int potential, boolean onlyNatural)
Determine the total bonus for a goods type. Checks resources and all improvements, unless onlyNatural is true, in which case only natural improvements will be considered. This is not used for normal production, but is necessary to calculate colony center tile secondary production, which does not profit from artificial improvements such as plowing. It is also used to assess which goods are likely to be most productive on a tile.- Parameters:
goodsType
- TheGoodsType
to check.unitType
- TheUnitType
to check.potential
- The base potential production.onlyNatural
- Only allow natural improvements.- Returns:
- The resulting production.
-
getProductionModifiers
public java.util.stream.Stream<Modifier> getProductionModifiers(GoodsType goodsType, UnitType unitType)
Gets the production modifiers for the given type of goods and unit.- Parameters:
goodsType
- TheGoodsType
to produce.unitType
- The optionalunitType
to produce them.- Returns:
- A stream of the applicable
Modifier
s.
-
canProduce
public boolean canProduce(GoodsType goodsType, UnitType unitType)
Does this container contain an item that allows the tile to produce a goods type?- Parameters:
goodsType
- TheGoodsType
to produce.unitType
- The optionalunitType
to produce with.- Returns:
- True if this container allows the goods type to be produced.
-
getMoveCost
public int getMoveCost(Tile fromTile, Tile targetTile, int basicMoveCost)
Determine the movement cost to thisTile
from anotherTile
. Does not consider special unit abilities.- Parameters:
fromTile
- TheTile
to move from.targetTile
- TheTile
to move to.basicMoveCost
- The basic cost.- Returns:
- The movement cost.
-
copyFrom
public void copyFrom(TileItemContainer tic, Map.Layer layer)
Copy the tile items from anotherTileItemContainer
, observing a layer maximum. Note that some types need to be looked up in the spec as the tic parameter might be an import from a different game.- Parameters:
tic
- TheTileItemContainer
to copy from.layer
- The maximumLayer
to import from.
-
contains
public boolean contains(TileItem t)
Checks if the specifiedTileItem
is in this container.- Parameters:
t
- TheTileItem
to test the presence of.- Returns:
- True if the tile item is present.
-
disposeResources
public void disposeResources()
Removes all references to this object.- Overrides:
disposeResources
in classFreeColGameObject
-
checkIntegrity
public Constants.IntegrityType checkIntegrity(boolean fix, LogBuilder lb)
Checks the integrity of this game object.- Overrides:
checkIntegrity
in classFreeColGameObject
- Parameters:
fix
- If true, fix problems if possible.lb
- ALogBuilder
to log to.- Returns:
- A suitable
IntegrityType
.
-
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.
-
writeAttributes
protected void writeAttributes(FreeColXMLWriter xw) throws javax.xml.stream.XMLStreamException
Write the attributes of this object to a stream. To be overridden if required by any object that has attributes and uses the toXML(FreeColXMLWriter, String) call.- Overrides:
writeAttributes
in classFreeColObject
- Parameters:
xw
- TheFreeColXMLWriter
to write to.- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
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.
-
readAttributes
protected void readAttributes(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads the attributes of this object from an XML stream.- Overrides:
readAttributes
in classFreeColGameObject
- Parameters:
xr
- TheFreeColXMLReader
to read from.- Throws:
javax.xml.stream.XMLStreamException
- if there is a problem reading 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.
-
getXMLTagName
public java.lang.String getXMLTagName()
Get the serialization tag for this object.- Specified by:
getXMLTagName
in classFreeColObject
- Returns:
- The tag.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classFreeColObject
-
-