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
ContainsTileItems and can be used by aTileto make certain tasks easier.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.logging.Loggerloggerprivate static java.lang.StringOLD_TILE_IMPROVEMENT_TAGstatic java.lang.StringTAGprivate TiletileThe tile owner for which this is the container.private static java.lang.StringTILE_TAGprivate java.util.List<TileItem>tileItemsAll 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 voidaddTileItem(TileItem item)Add a tile item to this container.booleancanProduce(GoodsType goodsType, UnitType unitType)Does this container contain an item that allows the tile to produce a goods type?Constants.IntegrityTypecheckIntegrity(boolean fix, LogBuilder lb)Checks the integrity of this game object.private voidclearTileItems()Clear the tile items list.booleancontains(TileItem t)Checks if the specifiedTileItemis in this container.voidcopyFrom(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.voiddisposeResources()Removes all references to this object.private TileItemfindTileItem(java.util.function.Predicate<TileItem> pred)Find a tile item matching a predicate.java.util.List<TileImprovement>getCompleteImprovements()Gets a list of the completedTileImprovements in thisTileItemContainer.java.util.List<TileItem>getCompleteItems()Get the completed tile items.TileImprovementgetImprovement(TileImprovementType type)Gets the tile improvement of the given type if any.java.util.List<TileImprovement>getImprovements()Gets a list of theTileImprovements in thisTileItemContainer.private java.util.List<TileImprovement>getImprovements(boolean completedOnly)Gets a list of theTileImprovements in thisTileItemContainer.LostCityRumourgetLostCityRumour()Gets any lost city rumour in this container.intgetMoveCost(Tile fromTile, Tile targetTile, int basicMoveCost)Determine the movement cost to thisTilefrom anotherTile.java.util.stream.Stream<Modifier>getProductionModifiers(GoodsType goodsType, UnitType unitType)Gets the production modifiers for the given type of goods and unit.ResourcegetResource()Gets any resource item.TileImprovementgetRiver()Gets any river improvement in this container.TileImprovementgetRoad()Gets any road improvement in this container.TilegetTile()Get the tile this container belongs to.private java.util.List<TileItem>getTileItems()Get a copy of the tile items list.intgetTotalBonusPotential(GoodsType goodsType, UnitType unitType, int potential, boolean onlyNatural)Determine the total bonus for a goods type.java.lang.StringgetXMLTagName()Get the serialization tag for this object.booleanhasImprovement(TileImprovementType type)Check whether this tile has a completed improvement of the given type.private voidinvalidateCache()Invalidate the production cache of the owning colony if any but only if the tile is actually being used.protected voidreadAttributes(FreeColXMLReader xr)Reads the attributes of this object from an XML stream.protected voidreadChild(FreeColXMLReader xr)Reads a single child object.protected voidreadChildren(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.voidremoveIncompatibleImprovements()Remove improvements incompatible with the given TileType.<T extends TileItem>
TremoveTileItem(T item)Removes aTileItemfrom this container.voidsetTileItems(java.util.List<TileItem> newTileItems)Set the tile items.java.lang.StringtoString()TileItemtryAddTileItem(TileItem item)Try to add aTileItemto this container.protected voidwriteAttributes(FreeColXMLWriter xw)Write the attributes of this object to a stream.protected voidwriteChildren(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- TheTilethisTileItemContainercontainsTileItemsfor.
-
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
TileItems.
-
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- TheTileItemto add.
-
tryAddTileItem
public final TileItem tryAddTileItem(TileItem item)
Try to add aTileItemto this container.- Parameters:
item- TheTileItemto add to this container.- Returns:
- The added
TileItemor the existingTileItemif of higher magnitude, or null on error.
-
removeTileItem
public final <T extends TileItem> T removeTileItem(T item)
Removes aTileItemfrom this container.- Type Parameters:
T- The actualTileItemtype.- Parameters:
item- TheTileItemto remove from this container.- Returns:
- The
TileItemthat 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 actualTileItemtype.- Parameters:
c- TheClassto remove.
-
getCompleteItems
public java.util.List<TileItem> getCompleteItems()
Get the completed tile items.- Returns:
- A list of complete
TileItems.
-
getImprovement
public TileImprovement getImprovement(TileImprovementType type)
Gets the tile improvement of the given type if any.- Parameters:
type- TheTileImprovementTypeto look for.- Returns:
- The
TileImprovementof the given type if present, otherwise null.
-
getImprovements
private java.util.List<TileImprovement> getImprovements(boolean completedOnly)
Gets a list of theTileImprovements in thisTileItemContainer.- Parameters:
completedOnly- If true select only the completed improvements.- Returns:
- A list of
TileImprovements.
-
getImprovements
public java.util.List<TileImprovement> getImprovements()
Gets a list of theTileImprovements in thisTileItemContainer.- Returns:
- A list of
TileImprovements.
-
getCompleteImprovements
public java.util.List<TileImprovement> getCompleteImprovements()
Gets a list of the completedTileImprovements in thisTileItemContainer.- Returns:
- A list of
TileImprovements.
-
findTileItem
private TileItem findTileItem(java.util.function.Predicate<TileItem> pred)
Find a tile item matching a predicate.- Parameters:
pred- ThePredicateto match.- Returns:
- The
TileItemfound, or null if none present.
-
getLostCityRumour
public final LostCityRumour getLostCityRumour()
Gets any lost city rumour in this container.- Returns:
- A
LostCityRumouritem if any, or null if not found.
-
getResource
public final Resource getResource()
Gets any resource item.- Returns:
- A
Resourceitem, or null is none found.
-
getRoad
public TileImprovement getRoad()
Gets any road improvement in this container.- Returns:
- A road
TileImprovementif any, or null if not found.
-
getRiver
public TileImprovement getRiver()
Gets any river improvement in this container.- Returns:
- A river
TileImprovementif 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- TheTileImprovementTypeto 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- TheGoodsTypeto check.unitType- TheUnitTypeto 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- TheGoodsTypeto produce.unitType- The optionalunitTypeto produce them.- Returns:
- A stream of the applicable
Modifiers.
-
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- TheGoodsTypeto produce.unitType- The optionalunitTypeto 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 thisTilefrom anotherTile. Does not consider special unit abilities.- Parameters:
fromTile- TheTileto move from.targetTile- TheTileto 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- TheTileItemContainerto copy from.layer- The maximumLayerto import from.
-
contains
public boolean contains(TileItem t)
Checks if the specifiedTileItemis in this container.- Parameters:
t- TheTileItemto test the presence of.- Returns:
- True if the tile item is present.
-
disposeResources
public void disposeResources()
Removes all references to this object.- Overrides:
disposeResourcesin classFreeColGameObject
-
checkIntegrity
public Constants.IntegrityType checkIntegrity(boolean fix, LogBuilder lb)
Checks the integrity of this game object.- Overrides:
checkIntegrityin classFreeColGameObject- Parameters:
fix- If true, fix problems if possible.lb- ALogBuilderto 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:
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.
-
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:
writeAttributesin classFreeColObject- Parameters:
xw- TheFreeColXMLWriterto 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:
writeChildrenin classFreeColObject- Parameters:
xw- TheFreeColXMLWriterto 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:
readAttributesin classFreeColGameObject- Parameters:
xr- TheFreeColXMLReaderto 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:
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.
-
getXMLTagName
public java.lang.String getXMLTagName()
Get the serialization tag for this object.- Specified by:
getXMLTagNamein classFreeColObject- Returns:
- The tag.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classFreeColObject
-
-