Class ProductionCache


  • public class ProductionCache
    extends java.lang.Object
    The ProductionCache is contains all relevant information about the production and consumption of the colony. This includes the production of all colony tiles and buildings, as well as the consumption of all units, buildings and build queues.
    • Field Detail

      • colony

        private final Colony colony
        The colony whose production is being cached. The goods stored in the colony may need to be considered in order to prevent excess production.
      • netProduction

        private final TypeCountMap<GoodsType> netProduction
        A map of net production by goods type.
      • productionAndConsumption

        private final java.util.Map<java.lang.Object,​ProductionInfo> productionAndConsumption
        A map of production info for various producers and consumers.
      • goodsUsed

        private final java.util.Set<GoodsType> goodsUsed
        A set of the goods used by the colony.
      • upToDate

        private boolean upToDate
        Flag to indicate whether the cache is up to date, or not and needs update() to be called. Synchronize access to this variable.
    • Constructor Detail

      • ProductionCache

        public ProductionCache​(Colony colony)
        Creates a new ProductionCache instance.
        Parameters:
        colony - a Colony value
    • Method Detail

      • update

        private void update()
        Updates all data structures. Front end for reallyUpdate, split out to keep the wrapping of upToDate distinct, and to make reallyUpdate visible when profiling.
      • reallyUpdate

        private void reallyUpdate()
        Really update everything. For now, there is a hard assumption that ColonyTiles do not consume but Buildings do. One day we may want to generalize this, which will require processing the goods types in an order sorted by the requirement dependencies. But not yet. This assumption is made explicit by getting the ProductionInfo from ColonyTiles with the simple getBasicProductionInfo() routine, but from Buildings with getAdjustedProductionInfo() which takes account of the input and output goods levels. FIXME: Ideally these should be unified into a WorkLocation.getProductionInfo with the Building-form arguments.
      • getGoodsCount

        private int getGoodsCount​(GoodsType type)
        Returns the number of goods of the given type stored in the colony.
        Parameters:
        type - a GoodsType value
        Returns:
        an int value
      • invalidate

        public void invalidate()
        Invalidates the production cache. This method needs to be called whenever global production modifiers change. This might be the case when a new FoundingFather is added, or when the colony's production bonus changes.
      • invalidate

        public void invalidate​(GoodsType goodsType)
        Invalidates the production cache if it produces or consumes the given GoodsType. This method needs to be called whenever goods are added to or removed from the colony.
        Parameters:
        goodsType - a GoodsType value
      • isProducing

        public boolean isProducing​(GoodsType goodsType)
        Does this production cache contain production of a goods type?
        Parameters:
        goodsType - The GoodsType to check.
        Returns:
        True if there is a production entry for the given type.
      • isConsuming

        public boolean isConsuming​(GoodsType goodsType)
        Does this production cache contain consumption of a goods type?
        Parameters:
        goodsType - The GoodsType to check.
        Returns:
        True if there is a consumption entry for the given type.
      • getNetProductionOf

        public int getNetProductionOf​(GoodsType type)
        Returns the net production, that is the total production minus the total consumption, of the given GoodsType.
        Parameters:
        type - a GoodsType value
        Returns:
        an int value
      • getProductionInfo

        public ProductionInfo getProductionInfo​(java.lang.Object object)
        Gets the ProductionInfo for the given WorkLocation or Consumer.
        Parameters:
        object - an Object value
        Returns:
        a ProductionInfo value
      • getProductionMap

        public TypeCountMap<GoodsType> getProductionMap()
        Gets a copy of the current production state.
        Returns:
        A copy of the current production state.