using System;
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 this
/// .
public abstract class EventCollection : EventEnumerable, IEventCollection
{
///
/// Adds an item to the . After the
/// item is added, the event
/// will be invoked.
///
/// The object to add to the
/// .
/// The
/// is read-only.
public void Add(T item)
{
CollectionDecorated.Add(item);
if (ItemsAddedHandler != null)
ItemsAddedHandler(this, new ItemsEventArgs(item));
}
///
/// Removes all items from the . If any
/// items are cleared from the list, the event
/// will be invoked.
///
/// The
/// is read-only.
public void Clear()
{
if (CollectionDecorated.Count > 0)
{
if (ItemsRemovedHandler != null)
{
// grab all the items that are being removed then clear the list
T[] items = new T[CollectionDecorated.Count];
CollectionDecorated.CopyTo(items, 0);
CollectionDecorated.Clear();
// send out the event
ItemsRemovedHandler(this, new ItemsEventArgs(items));
}
else
CollectionDecorated.Clear();
}
}
///
/// Determines whether the contains a
/// specific value.
///
/// The object to locate in the
/// .
///
/// true if is found in the
/// ; otherwise, false.
///
public bool Contains(T item)
{
return CollectionDecorated.Contains(item);
}
///
/// Copies the elements of the to an
/// , starting at a particular
/// index.
///
/// The one-dimensional
/// that is the destination of the elements copied from
/// . The must have
/// zero-based indexing.
/// The zero-based index in
/// at which copying begins.
///
/// is null.
///
/// is less than 0.
///
/// is multidimensional.-or-
/// is equal to or greater than the length
/// of .-or-The number of elements in the source
/// is greater than the available space
/// from to the end of the destination
/// .-or-Type cannot be cast
/// automatically to the type of the destination .
///
public void CopyTo(T[] array, int arrayIndex)
{
CollectionDecorated.CopyTo(array, arrayIndex);
}
///
/// Gets the number of elements contained in the
/// .
///
///
/// The number of elements contained in the
/// .
public int Count
{
get { return CollectionDecorated.Count; }
}
///
/// Gets a value indicating whether the
/// is read-only.
///
///
/// true if the
/// is read-only; otherwise, false.
public bool IsReadOnly
{
get { return CollectionDecorated.IsReadOnly; }
}
///
/// Removes the first occurrence of a specific object from the
/// . If the
/// item is successfully removed, then the event
/// will be invoked.
///
/// The object to remove from the
/// .
///
/// true if was successfully removed from the
/// ; otherwise,
/// false. This method also returns false if is
/// not found in the original .
///
/// The
/// is read-only.
public bool Remove(T item)
{
bool returnResult = CollectionDecorated.Remove(item);
if (returnResult && ItemsRemovedHandler != null)
ItemsRemovedHandler(this, new ItemsEventArgs(item));
return returnResult;
}
///
/// The being decorated or extended with
/// event capabilities.
///
protected abstract ICollection CollectionDecorated { get; }
///
/// The being decorated or extended
/// with event capabilities.
///
protected override IEnumerable GenericEnumerableDecorated
{
get { return CollectionDecorated; }
}
}
}