org.starhope.appius.game.inventory
Class Inventory

java.lang.Object
  extended by org.starhope.appius.util.SimpleDataRecord<SimpleDataRecordSet<DataRecordClass,DataRecordSetClass>>
      extended by org.starhope.appius.util.SimpleDataRecordSet<InventoryItem,Inventory>
          extended by org.starhope.appius.game.inventory.Inventory
All Implemented Interfaces:
com.whirlycott.cache.Cacheable, Serializable, Comparable<SimpleDataRecordSet<InventoryItem,Inventory>>, Iterable<InventoryItem>, Collection<InventoryItem>, CTime, DataRecord, DataRecordSet<InventoryItem>, HasSubversionRevision

public class Inventory
extends SimpleDataRecordSet<InventoryItem,Inventory>

The Inventory represent an SQL-backed set of InventoryItem objects owned by a given user.

Author:
brpocock@star-hope.org, twheys@gmail.com
See Also:
Serialized Form

Field Summary
(package private)  ConcurrentSkipListSet<InventoryItem> items
          The internal cache of the user's inventory is kept in this set
private  int ownerID
          The user whose inventory is represented by this object
private static long serialVersionUID
          WRITEME: Document this brpocock@star-hope.org
 
Fields inherited from class org.starhope.appius.util.SimpleDataRecord
myLoader, timeLastChanged, timeLastSaved
 
Constructor Summary
Inventory(RecordLoader<SimpleDataRecordSet<InventoryItem,Inventory>> loader)
          WRITEME: Document this constructor brpocock@star-hope.org
 
Method Summary
 void add(GenericItemReference example)
          WRITEME: Document this method brpocock@star-hope.org
 InventoryItem add(int itemID)
          WRITEME: Document this method brpocock@star-hope.org
 boolean add(InventoryItem item)
          This is an overriding method.
 boolean addAll(Collection<? extends InventoryItem> c)
          This is an overriding method.
 InventoryItem addDefaultFreeItem(int i)
           
 InventoryItem addDefaultFreeItem(int id, boolean forceActive)
          Add an item which every user gets for free to the user's inventory, if it does not already exist.
 void clear()
          This is an overriding method.
 boolean contains(Object o)
          This is an overriding method.
 boolean containsAll(Collection<?> c)
          This is an overriding method.
 void doff(InventoryItem item)
          Doff a wearable item
 void doff(InventoryItemType type)
          Un-equip all items of a type
 void don(InventoryItem item, Colour colour)
          Put on a wearable item, to include Pivitz as well as clothing and so forth.
 InventoryItem findItem(int id)
          Checks if an item with the ID of parameter id is contained in the set, and if so, returns it.
private  void flush(Object o)
          Update an individual item in the database if necessary.
 org.json.JSONObject getActiveClothing_JSON()
          Get the set of all clothing being worn (as a JSON object).
 Collection<InventoryItem> getActiveClothing()
          Get all active clothing items in a collection.
 InventoryItem getActiveItemByType(InventoryItemType inventoryItemType)
          Find a singular active item of a type.
 Collection<InventoryItem> getActiveItemsByType(Collection<InventoryItemType> types)
           
 Collection<InventoryItem> getActiveItemsByType(InventoryItemType t)
           
 int getCacheableID()
          Note that all records must have either a numeric ID ( DataRecord.getCacheableID() or a string identifier ( DataRecord.getCacheableIdent() or both.
 String getCacheableIdent()
          Note that all records must have either a numeric ID ( DataRecord.getCacheableID() or a string identifier ( DataRecord.getCacheableIdent() or both.
 org.json.JSONObject getEquippedItems_JSON()
           
 Collection<InventoryItem> getEquippedItems(boolean includePassive)
          Get the set of equipped items which are either ACTIVE or PASSIVE.
 InventoryItem getFurnitureBySlot(int slotNumber)
          Get a home décor (furniture or structure) item from this user's inventory by its slot number.
 Collection<InventoryItem> getItemsByType(Collection<InventoryItemType> types)
          get all items of given types
 Collection<InventoryItem> getItemsByType(InventoryItemType inventoryItemType)
          WRITEME: Document this method brpocock@star-hope.org
 AbstractUser getOwner()
           
 int getOwnerID()
           
 String getSubversionRevision()
           Return the Subversion revision level of this class's source code file, as supplied via the special "$Rev: " sequence.
 boolean hasEquipped(int itemID)
           
 boolean hasItem(int id)
          Checks if an item with the ID of parameter id is contained in the set.
 boolean isEmpty()
          This is an overriding method.
 Iterator<InventoryItem> iterator()
          This is an overriding method.
 void notifyEquipChange()
          Notify the owner of a change to equipped items
 boolean remove(Object o)
          This is an overriding method.
 boolean removeAll(Collection<?> c)
          This is an overriding method.
 boolean retainAll(Collection<?> c)
          This is an overriding method.
 void setOwnerID(int userID)
          WRITEME: Document this method brpocock@star-hope.org
 void setStructure(InventoryItem item)
          Activate one structural element in lieu of any others that occupy the same slot.
 int size()
          This is an overriding method.
 Object[] toArray()
          This is an overriding method.
<T> T[]
toArray(T[] a)
          This is an overriding method.
 String toString()
           
 
Methods inherited from class org.starhope.appius.util.SimpleDataRecord
changed, checkStale, compareTo, equals, finalize, getRecordLoader, getTimeLastChanged, getTimeLastSaved, hashCode, isBeingLoaded, markAsLoaded, markAsSaved, markForReload, onRemove, onRetrieve, onStore, save, setRecordLoader
 
Methods inherited from class java.lang.Object
clone, equals, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
equals, hashCode
 
Methods inherited from interface org.starhope.appius.util.DataRecord
checkStale, getRecordLoader, isBeingLoaded, markAsLoaded, markAsSaved, setRecordLoader
 
Methods inherited from interface org.starhope.appius.util.CTime
getTimeLastChanged, getTimeLastSaved
 
Methods inherited from interface com.whirlycott.cache.Cacheable
onRemove, onRetrieve, onStore
 

Field Detail

serialVersionUID

private static final long serialVersionUID
WRITEME: Document this brpocock@star-hope.org

See Also:
Constant Field Values

items

ConcurrentSkipListSet<InventoryItem> items
The internal cache of the user's inventory is kept in this set


ownerID

private int ownerID
The user whose inventory is represented by this object

Constructor Detail

Inventory

public Inventory(RecordLoader<SimpleDataRecordSet<InventoryItem,Inventory>> loader)
WRITEME: Document this constructor brpocock@star-hope.org

Parameters:
loader - WRITEME
Method Detail

add

public void add(GenericItemReference example)
WRITEME: Document this method brpocock@star-hope.org

Parameters:
example - WRITEME

add

public InventoryItem add(int itemID)
WRITEME: Document this method brpocock@star-hope.org

Parameters:
itemID - the ID of a new item to be added
Returns:
the item created and added

add

public boolean add(InventoryItem item)
This is an overriding method.

Specified by:
add in interface Collection<InventoryItem>
Overrides:
add in class SimpleDataRecordSet<InventoryItem,Inventory>
See Also:
Collection.add(java.lang.Object)

addAll

public boolean addAll(Collection<? extends InventoryItem> c)
This is an overriding method.

Specified by:
addAll in interface Collection<InventoryItem>
Overrides:
addAll in class SimpleDataRecordSet<InventoryItem,Inventory>
See Also:
Collection.addAll(java.util.Collection)

addDefaultFreeItem

public InventoryItem addDefaultFreeItem(int i)
Parameters:
i - item ID
Returns:
the item added
See Also:
addDefaultFreeItem(int, boolean)

addDefaultFreeItem

public InventoryItem addDefaultFreeItem(int id,
                                        boolean forceActive)
Add an item which every user gets for free to the user's inventory, if it does not already exist. This method is used to enforce a minimum inventory upon users. Examples of default free items include the Basic 8 Toots patterns, and the default TootBook theme.

Parameters:
id - the item ID
forceActive - if true, force the item to be active upon adding it to the user's inventory
Returns:
an item of that kind; either one that was already in inventory and found, or a new one that was placed there.

clear

public void clear()
This is an overriding method. XXX: contains SQL

Specified by:
clear in interface Collection<InventoryItem>
Overrides:
clear in class SimpleDataRecordSet<InventoryItem,Inventory>
See Also:
Collection.clear()

contains

public boolean contains(Object o)
This is an overriding method.

Specified by:
contains in interface Collection<InventoryItem>
Overrides:
contains in class SimpleDataRecordSet<InventoryItem,Inventory>
See Also:
Collection.contains(java.lang.Object)

containsAll

public boolean containsAll(Collection<?> c)
This is an overriding method.

Specified by:
containsAll in interface Collection<InventoryItem>
Overrides:
containsAll in class SimpleDataRecordSet<InventoryItem,Inventory>
See Also:
Collection.containsAll(java.util.Collection)

doff

public void doff(InventoryItem item)
Doff a wearable item

Parameters:
item - the item to be removed
See Also:
don(InventoryItem,Colour)

doff

public void doff(InventoryItemType type)
Un-equip all items of a type

Parameters:
type - the item type, of which all items are to be deactivated

don

public void don(InventoryItem item,
                Colour colour)
Put on a wearable item, to include Pivitz as well as clothing and so forth.

Parameters:
item - The item to be worn
colour - The color to set for the item. If the colour is not being overridden, set this to null.

findItem

public InventoryItem findItem(int id)
Checks if an item with the ID of parameter id is contained in the set, and if so, returns it.

Parameters:
id - the item ID of the item being checked
Returns:
an instance of the given item ID, if any is in inventory.

flush

private void flush(Object o)
Update an individual item in the database if necessary. Handles some typecasting badness required to make everyone happy.

Parameters:
o - an object that's probably an InventoryItem

getActiveClothing

public Collection<InventoryItem> getActiveClothing()
Get all active clothing items in a collection. Clothing item types are defined in the configuration file as org.starhope.appius.game.inventory.clothingTypes; this routine, in turn, uses getActiveItemsByType(Collection).

Returns:
The collection of all active inventory items of any type identified in the clothingTypes list

getActiveClothing_JSON

public org.json.JSONObject getActiveClothing_JSON()
Get the set of all clothing being worn (as a JSON object). This uses getActiveClothing() to identify clothes, and creates a JSON array-type map

Returns:
all clothing items that are active (being worn)

getActiveItemByType

public InventoryItem getActiveItemByType(InventoryItemType inventoryItemType)
                                  throws NotFoundException
Find a singular active item of a type. This call will guarantee that some item will be active, and only one; other active items of the type will be ignored, but if no item is active, one will be forced to become active.

Parameters:
inventoryItemType - a type
Returns:
a single active item
Throws:
NotFoundException - if the user has no items of the given type

getActiveItemsByType

public Collection<InventoryItem> getActiveItemsByType(Collection<InventoryItemType> types)
Parameters:
types - a set of types of items to be searched
Returns:
all items of any of the types which are active

getActiveItemsByType

public Collection<InventoryItem> getActiveItemsByType(InventoryItemType t)
Parameters:
t - the item type in question
Returns:
all active items of that type

getCacheableID

public int getCacheableID()
                   throws NotFoundException
Description copied from interface: DataRecord
Note that all records must have either a numeric ID ( DataRecord.getCacheableID() or a string identifier ( DataRecord.getCacheableIdent() or both.

Returns:
an unique ID number
Throws:
NotFoundException - if the item doesn't have a distinct numeric ID
See Also:
DataRecord.getCacheableID()

getCacheableIdent

public String getCacheableIdent()
                         throws NotFoundException
Description copied from interface: DataRecord
Note that all records must have either a numeric ID ( DataRecord.getCacheableID() or a string identifier ( DataRecord.getCacheableIdent() or both.

Returns:
an unique identifier string
Throws:
NotFoundException - if the item doesn't have a distinct string identifier
See Also:
DataRecord.getCacheableIdent()

getEquippedItems

public Collection<InventoryItem> getEquippedItems(boolean includePassive)
Get the set of equipped items which are either ACTIVE or PASSIVE.

Parameters:
includePassive - whether to include PASSIVE equipped items, as well.
Returns:
the set of all equipped ACTIVE items and possibly all PASSIVE items as well

getEquippedItems_JSON

public org.json.JSONObject getEquippedItems_JSON()
Returns:
the output as a JSON list
See Also:
getEquippedItems(boolean)

getFurnitureBySlot

public InventoryItem getFurnitureBySlot(int slotNumber)
                                 throws NotFoundException
Get a home décor (furniture or structure) item from this user's inventory by its slot number. NOTE also works for any other type of item, now

Parameters:
slotNumber - the slot number for the user's inventory
Returns:
the home décor item in the given inventory slot; or null, if none is found
Throws:
NotFoundException - if the item is not found.

getItemsByType

public Collection<InventoryItem> getItemsByType(Collection<InventoryItemType> types)
get all items of given types

Parameters:
types - the types
Returns:
all items of the given types

getItemsByType

public Collection<InventoryItem> getItemsByType(InventoryItemType inventoryItemType)
WRITEME: Document this method brpocock@star-hope.org

Parameters:
inventoryItemType - WRITEME
Returns:
WRITEME

getOwner

public AbstractUser getOwner()
Returns:
the owner of this inventory

getOwnerID

public int getOwnerID()
Returns:
the owner's user ID

getSubversionRevision

public String getSubversionRevision()
Description copied from interface: HasSubversionRevision

Return the Subversion revision level of this class's source code file, as supplied via the special "$Rev: " sequence.

As an example,the Subversion revision string for this file is "$Rev: 1968 $"

 
 @Override
 public String getSubversionRevision () {
        return "$Rev: ";
 }
 

Returns:
The Subversion revision level for this class's source code file.
See Also:
HasSubversionRevision.getSubversionRevision()

hasEquipped

public boolean hasEquipped(int itemID)
Parameters:
itemID - the type of item being searched-for
Returns:
true, if the user has such an item equipped (active)

hasItem

public boolean hasItem(int id)
Checks if an item with the ID of parameter id is contained in the set.

Parameters:
id - the item ID of the item being checked
Returns:
true if the item is in the set, false if the item is not in the set

isEmpty

public boolean isEmpty()
This is an overriding method.

Specified by:
isEmpty in interface Collection<InventoryItem>
Overrides:
isEmpty in class SimpleDataRecordSet<InventoryItem,Inventory>
See Also:
Collection.isEmpty()

iterator

public Iterator<InventoryItem> iterator()
This is an overriding method.

Specified by:
iterator in interface Iterable<InventoryItem>
Specified by:
iterator in interface Collection<InventoryItem>
Overrides:
iterator in class SimpleDataRecordSet<InventoryItem,Inventory>
See Also:
Collection.iterator()

notifyEquipChange

public void notifyEquipChange()
Notify the owner of a change to equipped items


remove

public boolean remove(Object o)
This is an overriding method.

Specified by:
remove in interface Collection<InventoryItem>
Overrides:
remove in class SimpleDataRecordSet<InventoryItem,Inventory>
See Also:
Collection.remove(java.lang.Object)

removeAll

public boolean removeAll(Collection<?> c)
This is an overriding method.

Specified by:
removeAll in interface Collection<InventoryItem>
Overrides:
removeAll in class SimpleDataRecordSet<InventoryItem,Inventory>
See Also:
Collection.removeAll(java.util.Collection)

retainAll

public boolean retainAll(Collection<?> c)
This is an overriding method.

Specified by:
retainAll in interface Collection<InventoryItem>
Overrides:
retainAll in class SimpleDataRecordSet<InventoryItem,Inventory>
See Also:
Collection.retainAll(java.util.Collection)

setOwnerID

public void setOwnerID(int userID)
WRITEME: Document this method brpocock@star-hope.org

Parameters:
userID - WRITEME

setStructure

public void setStructure(InventoryItem item)
Activate one structural element in lieu of any others that occupy the same slot.

Parameters:
item - the structural item

size

public int size()
This is an overriding method.

Specified by:
size in interface Collection<InventoryItem>
Overrides:
size in class SimpleDataRecordSet<InventoryItem,Inventory>
See Also:
Collection.size()

toArray

public Object[] toArray()
This is an overriding method.

Specified by:
toArray in interface Collection<InventoryItem>
Overrides:
toArray in class SimpleDataRecordSet<InventoryItem,Inventory>
See Also:
Collection.toArray()

toArray

public <T> T[] toArray(T[] a)
This is an overriding method.

Specified by:
toArray in interface Collection<InventoryItem>
Overrides:
toArray in class SimpleDataRecordSet<InventoryItem,Inventory>
See Also:
java.util.Collection#toArray(T[])

toString

public String toString()
Overrides:
toString in class SimpleDataRecord<SimpleDataRecordSet<InventoryItem,Inventory>>
See Also:
Object.toString()