using System.Collections.Generic; using Chernobyl.Event; namespace Chernobyl.Collections.Generic.Event { /// /// Type that is designed to make the implementation of new /// easier to create. /// /// The type that is to be held within the list. public abstract class EventList : EventCollection, IList { /// /// Determines the index of the specified item within this list. /// /// The item to find the index for. /// Returns the index of the item or -1 if the item could /// not be found. public int IndexOf(T item) { return ListDecorated.IndexOf(item); } /// /// Inserts an item into the list at the specified index. After the item /// is inserted, the event EventList{T}.OnItemsAdded will be invoked. /// /// The index where the item should be placed in the /// list. /// The item to insert. public void Insert(int index, T item) { ListDecorated.Insert(index, item); if (ItemsAddedHandler != null) ItemsAddedHandler(this, new ItemsEventArgs(item)); } /// /// Removes an item at the specified index. If the item is removed, /// the event will be /// invoked. /// /// The zero-based index of the item to remove. public void RemoveAt(int index) { if (ItemsRemovedHandler != null) { T item = ListDecorated[index]; ListDecorated.RemoveAt(index); ItemsRemovedHandler(this, new ItemsEventArgs(item)); } else ListDecorated.RemoveAt(index); } /// /// Gets or sets the item at the specified index. If the item is set, /// then the event EventList{T}.ItemChangeEventArgs will be invoked. /// /// The index of the item to retrieve or set. /// The item at the specified index. public T this[int index] { get { return ListDecorated[index]; } set { // Store the old item but only if we need to since the search // for that item could be expensive. ItemsEventArgs itemRemoved = null; if (ItemsRemovedHandler != null) itemRemoved = new ItemsEventArgs(ListDecorated[index]); ListDecorated[index] = value; if (ItemsRemovedHandler != null) ItemsRemovedHandler(this, itemRemoved); if (ItemsAddedHandler != null) ItemsAddedHandler(this, new ItemsEventArgs(value)); } } /// /// The being decorated or extended with event /// capabilities. /// protected abstract IList ListDecorated { get; } /// /// The being decorated or extended with /// event capabilities. /// protected override ICollection CollectionDecorated { get { return ListDecorated; } } } }