Interface InventoryService

All Known Subinterfaces:
ContextualInventoryService
All Known Implementing Classes:
InventoryServiceImpl

public interface InventoryService

This basic inventory service checks and adjusts the current inventory of a sku. All Skus will be considered generally unavailable from an inventory perspective if Sku#isAvaliable() returns false or if Sku.isActive() returns false.

Skus with an InventoryType of null or 'ALWAYS_AVAILABLE' will be considered undefined from an inventory perspective, and will generally be considered available. However, a request for available quantities of Skus with a null or 'ALWAYS_AVAILABLE' inventory type will return null (as the Sku is available but no inventory strategy is defined).

For most implementations outside of the very basic inventory case, you will actually want to use the ContextualInventoryService. This is the version of the service that is invoked from the checkout workflow in DecrementInventoryActivity and where the main checks for inventory are in the CheckAddAvailabilityActivity

Author:
Kelly Tisdell, Phillip Verheyden (phillipuniverse)
  • Method Details

    • retrieveQuantityAvailable

      Integer retrieveQuantityAvailable(Sku sku)

      Retrieves the quantity available for a given sku. May return null if no inventory is maintained for the given sku when Sku.getInventoryType() == null or the InventoryType of the given sku is InventoryType.ALWAYS_AVAILABLE. Effectively, if the quantity returned is null, inventory is undefined, which most likely means it is available. However, rather than returning an arbitrary integer values (like Integer.MAX_VALUE), which has specific meaning, we return null as this can be interpreted by the client to mean whatever they define it as (including infinitely available), which is the most likely scenario.

      In practice, this is a convenience method to wrap retrieveQuantitiesAvailable(Collection)

      Returns:
      null if there is no inventory strategy defined (meaning, Sku.getInventoryType() is null or InventoryType.ALWAYS_AVAILABLE). Otherwise, this returns the quantity of the Sku
    • retrieveQuantitiesAvailable

      Map<Sku,Integer> retrieveQuantitiesAvailable(Collection<Sku> skus)

      Retrieves the quantity available for a given sku. May return null if no inventory is maintained for the given sku when Sku.getInventoryType() == null or the InventoryType of the given sku is InventoryType.ALWAYS_AVAILABLE. Effectively, if the quantity returned is null, inventory is undefined, which most likely means it is available. However, rather than returning an arbitrary integer values (like Integer.MAX_VALUE), which has specific meaning, we return null as this can be interpreted by the client to mean whatever they define it as (including infinitely available), which is the most likely scenario.

      Parameters:
      skus - the set of Skus to return inventory for
      Returns:
      a map of the given set of skus to the quantity as represented in the inventory system. The Map.keySet() is the same collection of given skus
    • isAvailable

      boolean isAvailable(Sku sku, int quantity)

      Indicates whether the given quantity is available for the particular skuId. The result will be true if Sku.getInventoryType() == null or Sku.getInventoryType().equals(InventoryType.ALWAYS_AVAILABLE).

      The result will be false if the checkBasicAvailablility(Sku) returns false, or if the quantity field is null, or if the quantity requested exceeds the quantity available.

      Parameters:
      sku - the Sku to see if enough quantity is available
      quantity - the quantity to check for the given sku
      Returns:
      true if there is available quantity
    • checkBasicAvailablility

      boolean checkBasicAvailablility(Sku sku)
      Without worrying about quantities, just checks to see if the given Sku is available. A Sku is generally available if any of these is true:
      1. Sku.getInventoryType() is null
      2. Sku.getInventoryType() is anything but InventoryType.UNAVAILABLE
      3. Sku.isActive() is is true or null
      4. Does not consider the now-deprecated Sku.isAvailable()

      * @param sku the Sku whose availability is being checked

      Returns:
      true or false according to the rules above
    • decrementInventory

      void decrementInventory(Sku sku, int quantity) throws InventoryUnavailableException

      Attempts to decrement inventory if it is available. If the Sku is marked as InventoryType.ALWAYS_AVAILABLE then this will be a no-op.

      This method is a convenience method to wrap decrementInventory(Map)

      Parameters:
      sku - the Sku to decrement inventory from
      quantity - the quantity to take inventory from
      Throws:
      InventoryUnavailableException - if there is not enough of the given quantity for the given sku
      IllegalArgumentException - if the given quantity is not greater than zero
    • decrementInventory

      void decrementInventory(Map<Sku,Integer> skuQuantities) throws InventoryUnavailableException

      Attempts to decrement inventory for a map of Skus and quantities

      Quantities must be greater than zero or an IllegalArgumentException will be thrown.

      If any of the given Skus inventory type is not InventoryType.CHECK_QUANTITY then this is a no-op and nothing actually happens

      Parameters:
      skuQuantities - a map from a Sku to the quantity attempting to decrement
      Throws:
      InventoryUnavailableException - if there is not enough inventory to decrement from any of the given skus or if checkBasicAvailablility(Sku) returns false
      IllegalArgumentException - if any of the quantities of the given skus are less than zero
    • incrementInventory

      void incrementInventory(Sku sku, int quantity)

      Attempts to increment inventory. Quantity must be greater than zero or an IllegalArgumentException will be thrown.

      This is a convenience method to wrap incrementInventory(Map)

      Parameters:
      sku - the Sku whose inventory should be incremented
      quantity - greater than zero
      Throws:
      IllegalArgumentException - if quantity is less than zero or retrieveQuantityAvailable(Sku) for the given sku returns null
    • incrementInventory

      void incrementInventory(Map<Sku,Integer> skuQuantities)
      Attempts to increment inventory for a map of Skus and quantities. All must not be null and must be greater than zero or an IllegalArgumentException will be thrown.

      If any of the given Skus inventory type is not InventoryType.CHECK_QUANTITY then this is a no-op and nothing actually happens

      Parameters:
      skuQuantities - the map of a Sku to the quantity that should be incremented
      Throws:
      IllegalArgumentException - if any of the quantities in the map values are null or less than zero, or if retrieveQuantityAvailable(Sku) for the Skus in the map is null