Generic list of items that is stored in a given block.

Note: If you make any changes to the item contents manually, do not forget to notify the list about the changes by calling updated() method.

Note: Each list item should at least hold a numerical ID ("id" field). The ID of a list item should be unique in that list. If the items have an amount accociated, the field "amount" should also be given. You can get the next unique free ID from nextID property.

Note: If you remove the items from the list bypassing the removeItem() call, do not forget to call updated() method to notify the list about the changes.

This class is described in detail in Generic Containers article.

Constructor

new (b:Block, a:String)

Creates a new generic list of items on top of cache block.

Parameters:

b

Cache block.

a

Block attribute name.

Fields

read only length:Int

List length.

read only nextID:Int

Next free unique item ID. Always increments.

Methods

addItem (item:Item):String

Adds an item to the list.

Parameters:

item

New item.

Returns:

Operation status. ok - operation successful. idExists - item with this ID exists in the list.

clear ():Void

Clears the list.

decrease (item:Item, ?amount:Int):Bool

Decreases list item amount field by given amount. Removes the item if amount equals zero.

Parameters:

item

List item.

amount

Amount to decrease by.

Returns:

True if operation was successful, false if the item was not in the list.

inline getBlock ():Block

Returns the block.

Returns:

Underlying cache block.

getByField (field:String, val:Dynamic):Item

Returns the first list item with a given field value.

Parameters:

field

List item field.

val

List item value.

Returns:

List item. Returns null if not found.

getIndex (id:Int):Int

Returns list item index by its ID.

Parameters:

id

List item ID.

Returns:

List item index. Returns -1 if not found.

getItem (id:Int):Item

Returns list item by its ID.

Parameters:

id

List item ID.

Returns:

List item. Returns null if not found.

inline getList ():Array<Item>

Returns the list of items from block.

Returns:

Array of list items.

hasItem (id:Int):Bool

Returns whether the list has an item with this ID.

Parameters:

id

List item ID.

Returns:

True if the list has an item with this ID. False otherwise.

increase (item:Item, ?amount:Int):Bool

Increases list item amount field by given amount.

Parameters:

item

List item.

amount

Amount to increase by.

Returns:

True if operation was successful, false if the item was not in the list.

iterator ():Iterator<Item>

Returns the items iterator.

Returns:

Iterator of list items.

removeItem (id:Int):Void

Removes the item with this ID from the list.

Parameters:

id

Unique item ID.

updated ():Void

Notifies the data block about the changes made to the list.