net.sf.freecol.common.model
Class Building

java.lang.Object
  extended by net.sf.freecol.common.model.FreeColObject
      extended by net.sf.freecol.common.model.FreeColGameObject
          extended by net.sf.freecol.common.model.UnitLocation
              extended by net.sf.freecol.common.model.WorkLocation
                  extended by net.sf.freecol.common.model.Building
All Implemented Interfaces:
java.lang.Comparable<Building>, Consumer, Location, Named, Ownable
Direct Known Subclasses:
ServerBuilding

public class Building
extends WorkLocation
implements Named, java.lang.Comparable<Building>, Consumer

Represents a building in a colony.


Nested Class Summary
 
Nested classes/interfaces inherited from class net.sf.freecol.common.model.UnitLocation
UnitLocation.NoAddReason
 
Field Summary
protected  BuildingType buildingType
          The type of building.
private static java.util.logging.Logger logger
           
static java.lang.String UNIT_CHANGE
           
 
Fields inherited from class net.sf.freecol.common.model.FreeColGameObject
UNITS_TAG_NAME
 
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE, ID_ATTRIBUTE, ID_ATTRIBUTE_TAG, INFINITY, NO_ID, PARTIAL_ATTRIBUTE, UNDEFINED, VALUE_TAG
 
Fields inherited from interface net.sf.freecol.common.model.Consumer
BUILDING_PRIORITY, COLONY_PRIORITY, COMPARATOR, POPULATION_PRIORITY, UNIT_PRIORITY
 
Constructor Summary
protected Building()
          Constructor for ServerBuilding.
protected Building(Game game)
          Constructor for ServerBuilding.
  Building(Game game, org.w3c.dom.Element e)
          Initiates a new Building from an XML representation.
  Building(Game game, java.lang.String id)
          Initiates a new Building with the given ID.
  Building(Game game, javax.xml.stream.XMLStreamReader in)
          Initiates a new Building from an XML representation.
 
Method Summary
 boolean add(Locatable locatable)
          Adds the specified locatable to this building.
 boolean canAdd(UnitType unitType)
          Checks if the specified UnitType may be added to this WorkLocation.
 boolean canAutoProduce()
          Returns true if this building can produce goods without workers.
 boolean canBeDamaged()
          Returns whether this building can be damaged
 boolean canBuildNext()
          Checks if this building can have a higher level.
 int compareTo(Building other)
          
 boolean consumes(GoodsType goodsType)
          Returns true if this Consumer consumes the given GoodsType.
 boolean damage()
          Reduces this building to previous level (is set to UpgradesFrom attribute in BuildingType) or is destroyed if it's the first level
 int getAdditionalProductionNextTurn(Unit addUnit)
          Returns the additional production of new Unit at this building for next turn.
 java.util.List<AbstractGoods> getConsumedGoods()
          Returns a list of GoodsTypes this Consumer consumes.
 UnitType getExpertUnitType()
          Returns the unit type being an expert in this Building.
 GoodsType getGoodsInputType()
          Returns the type of goods this building needs for input, or null if the Building does not consume any goods.
 GoodsType getGoodsOutputType()
          Returns the type of goods this Building produces, or null if the Building does not produce any goods.
 int getLevel()
          Returns the level of this building.
 StringTemplate getLocationName()
          Returns the name of this location.
 int getMaximumProduction()
          Returns the maximum production of this building.
 java.util.Set<Modifier> getModifierSet(java.lang.String id)
          Returns the modifier set with the given id.
 java.lang.String getNameKey()
          Gets the name of this Named.
 java.lang.String getNextNameKey()
          Gets the name of the improved building of the same type.
 UnitLocation.NoAddReason getNoAddReason(Locatable locatable)
          Gets the reason why a given Locatable can not be added to this Location.
 UnitLocation.NoAddReason getNoWorkReason()
          Checks if this work location is available to the colony to be worked.
 int getPotentialProduction(UnitType unitType, GoodsType goodsType)
          Gets the potential productivity of a given goods type from using a unit of a given type in this building.
 int getPriority()
          The priority of this Consumer.
 int getProduction()
          Returns the actual production of this building.
 ProductionInfo getProductionInfo()
          Returns the ProductionInfo for this Building from the Colony's cache.
 ProductionInfo getProductionInfo(AbstractGoods output, java.util.List<AbstractGoods> input)
          Returns the ProductionInfo for this Building.
 java.util.List<Modifier> getProductionModifiers()
          Returns a List of all Modifiers that influence the total production of the Building.
 int getProductionOf(GoodsType goodsType)
          Returns the production of the given type of goods.
 int getProductionOf(Unit unit, GoodsType goodsType)
          Gets the production of the given type of goods produced by a unit.
private  int getProductivity(Unit... additionalUnits)
          Returns the maximum productivity of worker/s currently working in this building.
 BuildingType getType()
          Gets the type of this building.
 int getUnitCapacity()
          Gets the maximum number of Units this Location can hold.
 int getUnitProductivity(Unit prodUnit)
          Returns the maximum productivity of a unit working in this building.
static java.lang.String getXMLElementTagName()
          Gets the tag name of the root element representing this object.
 boolean hasAbility(java.lang.String id)
          Describe hasAbility method here.
protected  void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
          Initialize this object from an XML-representation of this object.
protected  void readFromXMLPartialImpl(javax.xml.stream.XMLStreamReader in)
          Partial reader, so that "remove" messages can be brief.
 boolean remove(Locatable locatable)
          Removes the specified locatable from this building.
private  void setType(BuildingType newBuildingType)
          Changes the type of the Building.
 java.lang.String toString()
          String converter for debugging.
protected  void toXMLImpl(javax.xml.stream.XMLStreamWriter out, Player player, boolean showAll, boolean toSavedGame)
          This method writes an XML-representation of this object to the given stream.
protected  void toXMLPartialImpl(javax.xml.stream.XMLStreamWriter out, java.lang.String[] fields)
          Partial writer, so that "remove" messages can be brief.
 boolean upgrade()
          Upgrades this building to next level (is set to UpgradesTo attribute in BuildingType)
 
Methods inherited from class net.sf.freecol.common.model.WorkLocation
canBeWorked, canTeach, getColony, getOwner, getSettlement, getTile, readAttributes, setColony, setOwner, writeAttributes
 
Methods inherited from class net.sf.freecol.common.model.UnitLocation
canAdd, contains, dispose, disposeList, getGoodsContainer, getLocationNameFor, getSpaceTaken, getUnitCount, getUnitIterator, getUnitList, isEmpty, isFull, readChild, readChildren, writeChildren, writeChildren
 
Methods inherited from class net.sf.freecol.common.model.FreeColGameObject
equals, equals, fundamentalDispose, getFreeColGameObject, getFreeColGameObject, getGame, getIntegerID, getSpecification, hashCode, isDisposed, isUninitialized, newLocation, readFromXML, readFromXMLPartialByClass, setDefaultId, setGame, setId, toXML, toXMLImpl, toXMLPartialByClass, updateFreeColGameObject
 
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addPropertyChangeListener, addPropertyChangeListener, dumpObject, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAttribute, getAttribute, getAttribute, getAttribute, getAttribute, getId, getPropertyChangeListeners, getPropertyChangeListeners, hasAttribute, hasListeners, readAttributes, readChild, readChildren, readFromArrayElement, readFromArrayElement, readFromListElement, readFromXMLElement, readFromXMLImpl, removePropertyChangeListener, removePropertyChangeListener, save, save, toXML, toXML, toXMLElement, toXMLElement, toXMLElement, toXMLElement, toXMLElementPartial, writeAttribute
 
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
 

Field Detail

logger

private static final java.util.logging.Logger logger

UNIT_CHANGE

public static final java.lang.String UNIT_CHANGE
See Also:
Constant Field Values

buildingType

protected BuildingType buildingType
The type of building.

Constructor Detail

Building

protected Building()
Constructor for ServerBuilding.


Building

protected Building(Game game)
Constructor for ServerBuilding.

Parameters:
game - The Game this object belongs to.

Building

public Building(Game game,
                javax.xml.stream.XMLStreamReader in)
         throws javax.xml.stream.XMLStreamException
Initiates a new Building from an XML representation.

Parameters:
game - The Game this object belongs to.
in - The input stream containing the XML.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

Building

public Building(Game game,
                org.w3c.dom.Element e)
Initiates a new Building from an XML representation.

Parameters:
game - The Game this object belongs to.
e - An XML-element that will be used to initialize this object.

Building

public Building(Game game,
                java.lang.String id)
Initiates a new Building with the given ID. The object should later be initialized by calling either FreeColGameObject.readFromXML(XMLStreamReader) or FreeColObject.readFromXMLElement(Element).

Parameters:
game - The Game in which this object belong.
id - The unique identifier for this object.
Method Detail

getNameKey

public java.lang.String getNameKey()
Gets the name of this Named.

Specified by:
getNameKey in interface Named
Returns:
The name of the Named.

getLevel

public int getLevel()
Returns the level of this building.

Returns:
an int value

getLocationName

public StringTemplate getLocationName()
Returns the name of this location.

Specified by:
getLocationName in interface Location
Overrides:
getLocationName in class UnitLocation
Returns:
The name of this location.

getNextNameKey

public java.lang.String getNextNameKey()
Gets the name of the improved building of the same type. An improved building is a building of a higher level.

Returns:
The name of the improved building or null if the improvement does not exist.

canBuildNext

public boolean canBuildNext()
Checks if this building can have a higher level.

Returns:
If this Building can have a higher level, that Adam Smith is present for manufactoring factory level buildings and that the Colony containing this Building has a sufficiently high population.

getType

public BuildingType getType()
Gets the type of this building.

Returns:
The type.

hasAbility

public boolean hasAbility(java.lang.String id)
Describe hasAbility method here.

Specified by:
hasAbility in interface Consumer
Overrides:
hasAbility in class FreeColObject
Parameters:
id - a String value
Returns:
a boolean value

canBeDamaged

public boolean canBeDamaged()
Returns whether this building can be damaged

Returns:
true if can be damaged
See Also:
damage()

damage

public boolean damage()
Reduces this building to previous level (is set to UpgradesFrom attribute in BuildingType) or is destroyed if it's the first level

Returns:
True if the building was damaged.

upgrade

public boolean upgrade()
Upgrades this building to next level (is set to UpgradesTo attribute in BuildingType)

Returns:
True if the upgrade succeeds.

setType

private void setType(BuildingType newBuildingType)
Changes the type of the Building. The type of a building may change when it is upgraded or damaged.

Parameters:
newBuildingType -
See Also:
upgrade(), damage()

getUnitCapacity

public int getUnitCapacity()
Gets the maximum number of Units this Location can hold. To be overridden by subclasses.

Overrides:
getUnitCapacity in class UnitLocation
Returns:
Integer.MAX_VALUE, denoting no effective limit.

getExpertUnitType

public UnitType getExpertUnitType()
Returns the unit type being an expert in this Building.

Returns:
The UnitType.

getNoWorkReason

public UnitLocation.NoAddReason getNoWorkReason()
Checks if this work location is available to the colony to be worked.

Specified by:
getNoWorkReason in class WorkLocation
Returns:
The reason why/not the work location can be worked.

getNoAddReason

public UnitLocation.NoAddReason getNoAddReason(Locatable locatable)
Gets the reason why a given Locatable can not be added to this Location.

Overrides:
getNoAddReason in class WorkLocation
Parameters:
locatable - The Locatable to test.
Returns:
The reason why adding would fail.

canAdd

public boolean canAdd(UnitType unitType)
Checks if the specified UnitType may be added to this WorkLocation.

Parameters:
unitType - the UnitType.
Returns:
true if the UnitType may be added and false otherwise.

add

public boolean add(Locatable locatable)
Adds the specified locatable to this building.

Specified by:
add in interface Location
Overrides:
add in class WorkLocation
Parameters:
locatable - The Locatable to add.

remove

public boolean remove(Locatable locatable)
Removes the specified locatable from this building.

Specified by:
remove in interface Location
Overrides:
remove in class WorkLocation
Parameters:
locatable - The Locatable to remove.

getGoodsOutputType

public GoodsType getGoodsOutputType()
Returns the type of goods this Building produces, or null if the Building does not produce any goods.

Returns:
The type of goods this Building produces

getGoodsInputType

public GoodsType getGoodsInputType()
Returns the type of goods this building needs for input, or null if the Building does not consume any goods.

Returns:
The type of goods this Building requires as input in order to produce it's output.

getProductionInfo

public ProductionInfo getProductionInfo()
Returns the ProductionInfo for this Building from the Colony's cache.

Returns:
a ProductionInfo object

getProductionInfo

public ProductionInfo getProductionInfo(AbstractGoods output,
                                        java.util.List<AbstractGoods> input)
Returns the ProductionInfo for this Building.

Parameters:
output - the output goods already available in the colony, necessary in order to avoid excess production
input - the input goods available
Returns:
a ProductionInfo object

getProduction

public int getProduction()
Returns the actual production of this building.

Returns:
The amount of goods being produced by this Building the current turn. The type of goods being produced is given by getGoodsOutputType().
See Also:
getMaximumProduction()

getMaximumProduction

public int getMaximumProduction()
Returns the maximum production of this building.

Returns:
The production of this building, with the current amount of workers, when there is enough "input goods".

getAdditionalProductionNextTurn

public int getAdditionalProductionNextTurn(Unit addUnit)
Returns the additional production of new Unit at this building for next turn. TODO: Make this work properly. In the past, the method never worked correctly anyway, since it did not take the possible decrease in the production of input goods into account that might be caused by moving a unit to this Building from another WorkLocation. To do this right, it would be necessary to re-calculate the production for the whole getColony().

Returns:
The production of this building the next turn.

canAutoProduce

public boolean canAutoProduce()
Returns true if this building can produce goods without workers.

Returns:
a boolean value

getProductivity

private int getProductivity(Unit... additionalUnits)
Returns the maximum productivity of worker/s currently working in this building.

Parameters:
additionalUnits - units to add before calculating result
Returns:
The maximum returns from workers in this building, assuming enough "input goods".

getUnitProductivity

public int getUnitProductivity(Unit prodUnit)
Returns the maximum productivity of a unit working in this building.

Returns:
The maximum returns from this unit if in this Building, assuming enough "input goods".

getProductionOf

public int getProductionOf(GoodsType goodsType)
Returns the production of the given type of goods.

Specified by:
getProductionOf in class WorkLocation
Parameters:
goodsType - The type of goods to get the production for.
Returns:
the production og the given goods this turn. This method will return the same as getProduction() if the given type of goods is the same as getGoodsOutputType() and 0 otherwise.

getProductionOf

public int getProductionOf(Unit unit,
                           GoodsType goodsType)
Gets the production of the given type of goods produced by a unit.

Specified by:
getProductionOf in class WorkLocation
Parameters:
unit - The unit to do the work.
goodsType - The type of goods to get the production of.
Returns:
The production of the given type of goods.

getPotentialProduction

public int getPotentialProduction(UnitType unitType,
                                  GoodsType goodsType)
Gets the potential productivity of a given goods type from using a unit of a given type in this building.

Specified by:
getPotentialProduction in class WorkLocation
Parameters:
unitType - The UnitType to check.
goodsType - The GoodsType to check.
Returns:
The amount of goods potentially produced.

getProductionModifiers

public java.util.List<Modifier> getProductionModifiers()
Returns a List of all Modifiers that influence the total production of the Building. In particular, the method does not return any Modifiers that influence of the productivity of individual units present in the Building, such as the colony production bonus.

Returns:
a List of all Modifiers that influence the total production of the Building

compareTo

public int compareTo(Building other)

Specified by:
compareTo in interface java.lang.Comparable<Building>

consumes

public boolean consumes(GoodsType goodsType)
Returns true if this Consumer consumes the given GoodsType.

Parameters:
goodsType - a GoodsType value
Returns:
a boolean value

getConsumedGoods

public java.util.List<AbstractGoods> getConsumedGoods()
Returns a list of GoodsTypes this Consumer consumes.

Specified by:
getConsumedGoods in interface Consumer
Returns:
a List value

getPriority

public int getPriority()
The priority of this Consumer. The higher the priority, the earlier will the Consumer be allowed to consume the goods it requires.

Specified by:
getPriority in interface Consumer
Returns:
an int value

getModifierSet

public java.util.Set<Modifier> getModifierSet(java.lang.String id)
Returns the modifier set with the given id. The modifier most relevant to consumers is "consumeOnlySurplusProduction", which implies that the consumer does not consume stored goods (used by the country and stables).

Specified by:
getModifierSet in interface Consumer
Parameters:
id - a String value
Returns:
a boolean value

toXMLImpl

protected void toXMLImpl(javax.xml.stream.XMLStreamWriter out,
                         Player player,
                         boolean showAll,
                         boolean toSavedGame)
                  throws javax.xml.stream.XMLStreamException
This method writes an XML-representation of this object to the given stream.

Only attributes visible to the given Player will be added to that representation if showAll is set to false.

Specified by:
toXMLImpl in class FreeColGameObject
Parameters:
out - The target stream.
player - The Player this XML-representation should be made for, or null if showAll == true.
showAll - Only attributes visible to player will be added to the representation if showAll is set to false.
toSavedGame - If true then information that is only needed when saving a game is added.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

readFromXMLImpl

protected void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
                        throws javax.xml.stream.XMLStreamException
Initialize this object from an XML-representation of this object.

Overrides:
readFromXMLImpl in class FreeColObject
Parameters:
in - The input stream with the XML.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

toXMLPartialImpl

protected void toXMLPartialImpl(javax.xml.stream.XMLStreamWriter out,
                                java.lang.String[] fields)
                         throws javax.xml.stream.XMLStreamException
Partial writer, so that "remove" messages can be brief.

Overrides:
toXMLPartialImpl in class FreeColObject
Parameters:
out - The target stream.
fields - The fields to write.
Throws:
javax.xml.stream.XMLStreamException - If there are problems writing the stream.

readFromXMLPartialImpl

protected void readFromXMLPartialImpl(javax.xml.stream.XMLStreamReader in)
                               throws javax.xml.stream.XMLStreamException
Partial reader, so that "remove" messages can be brief.

Overrides:
readFromXMLPartialImpl in class FreeColObject
Parameters:
in - The input stream with the XML.
Throws:
javax.xml.stream.XMLStreamException - If there are problems reading the stream.

toString

public java.lang.String toString()
String converter for debugging.

Overrides:
toString in class FreeColGameObject
Returns:
The name of the building.

getXMLElementTagName

public static java.lang.String getXMLElementTagName()
Gets the tag name of the root element representing this object.

Returns:
"building".