/*
* ActiveUpdateList.cs
*
* Copyright © 2009
* Diversified Technical Systems, Inc.
* All Rights Reserved
*/
using System;
using System.Collections.Generic;
using System.Linq;
namespace DTS.Common.Utilities
{ ///
///
/// An extension of the standard class that provides active notification to
/// registered callbacks whenever items are added to or removed from the list.
///
///
///
/// The type of object contained in this list.
///
///
public class ActiveUpdateList : ExceptionalList
{ ///
///
/// Initialize an instance of this class.
///
///
public ActiveUpdateList()
{
}
///
/// Initialize an instance of this class.
///
///
///
/// The number of elements that the list can initially store.
///
///
public ActiveUpdateList(int capacity)
: base(capacity)
{
}
///
/// Initialize an instance of this class.
///
///
///
/// The collection whose elements are copied to the new list.
///
///
public ActiveUpdateList(IEnumerable collection)
: base(collection)
{
}
///
/// The argument type passed to "on items added" and "on items removed" callbacks. It contains
/// a list of the items that have been added/removed.
///
public class EventArgs : System.EventArgs
{
///
/// construct an empty collection of items
///
public EventArgs() { }
///
/// construct a collection of items
///
/// arguments to add
public EventArgs(IEnumerable items) { Items = items; }
public IEnumerable Items { get; private set; }
}
///
/// The type of the callbacks issued by this class.
///
///
///
/// The responsible for generating this event.
///
///
///
/// The for this event.
///
///
public delegate void EventHandler(object sender, EventArgs args);
///
/// The event generated whenever items are added to this object's list.
///
public event EventHandler OnItemsAdded;
///
/// The event generated whenever items are removed from this list.
///
public event EventHandler OnItemsRemoved;
///
/// Adds an object to the end of the .
///
///
///
/// The item to be added to this list.
///
///
///
/// All exceptions generated by this method will be nested in one of these.
///
///
public new void Add(T item)
{
try
{
base.Add(item);
var addList = new List();
addList.Add(item);
OnItemsAdded?.Invoke(this, new EventArgs(addList));
}
catch (System.Exception ex)
{
throw new Exception("encountered problem adding item to active update list", ex);
}
}
///
/// Adds the elements of the specified collection to the end of the .
///
///
///
/// The collection to the added to this list.
///
///
///
/// All exceptions generated by this method will be nested in one of these.
///
///
public new void AddRange(IEnumerable collection)
{
try
{
var enumerable = collection as T[] ?? collection.ToArray();
base.AddRange(enumerable);
OnItemsAdded?.Invoke(this, new EventArgs(enumerable));
}
catch (System.Exception ex)
{
throw new Exception("encountered problem adding range to active update list", ex);
}
}
///
/// Removes all elements from the .
///
///
///
/// All exceptions generated by this method will be nested in one of these.
///
///
public new void Clear()
{
try
{
var removedItems = new List();
foreach (var item in this)
removedItems.Add(item);
base.Clear();
OnItemsRemoved?.Invoke(this, new EventArgs(removedItems));
}
catch (System.Exception ex)
{
throw new Exception("encountered problem clearing active update list", ex);
}
}
///
/// Inserts the elements of a collection into the at the
/// specified index.
///
///
///
/// The zero-based index at which the new elements should be inserted.
///
///
///
/// The collection whose elements should be inserted into the T:ActiveUpdateList.
/// The collection itself cannot be null, but it can contain elements that are
/// null, if type T is a reference type.
///
///
///
/// All exceptions generated by this method will be nested in one of these.
///
///
public new void InsertRange(int index, IEnumerable collection)
{
try
{
var enumerable = collection as T[] ?? collection.ToArray();
base.InsertRange(index, enumerable);
OnItemsAdded?.Invoke(this, new EventArgs(enumerable));
}
catch (System.Exception ex)
{
throw new Exception("encountered problem inserting range in active update list", ex);
}
}
///
/// Removes the first occurrence of a specific object from the .
///
///
///
/// The object to remove from the . The value
/// can be null for reference types.
///
///
///
/// true if item is successfully removed; otherwise, false. This method also
/// returns false if item was not found in the .
///
///
///
/// All exceptions generated by this method will be nested in one of these.
///
///
public new bool Remove(T item)
{
try
{
var success = base.Remove(item);
var removeList = new List();
removeList.Add(item);
OnItemsRemoved?.Invoke(this, new EventArgs(removeList));
return success;
}
catch (System.Exception ex)
{
throw new Exception("encountered problem removing item from active update list", ex);
}
}
///
/// Removes the all the elements that match the conditions defined by the specified
/// predicate.
///
///
///
/// The delegate that defines the conditions of the elements
/// to remove.
///
///
///
/// The number of elements removed from the
///
///
///
/// All exceptions generated by this method will be nested in one of these.
///
///
public new int RemoveAll(Predicate match)
{
try
{
var removeList = FindAll(match);
var numberOfItemsRemoved = base.RemoveAll(match);
OnItemsRemoved?.Invoke(this, new EventArgs(removeList));
return numberOfItemsRemoved;
}
catch (System.Exception ex)
{
throw new Exception("encountered problem removing all matching items from active update list", ex);
}
}
///
/// Removes the element at the specified index of the .
///
///
///
/// The zero-based index of the element to remove.
///
///
///
/// All exceptions generated by this method will be nested in one of these.
///
///
public new void RemoveAt(int index)
{
try
{
var removeList = new List();
removeList.Add(base[index]);
base.RemoveAt(index);
OnItemsRemoved?.Invoke(this, new EventArgs(removeList));
}
catch (System.Exception ex)
{
throw new Exception("encountered problem removing item at specified index from active update list", ex);
}
}
///
/// Removes a range of elements from the .
///
///
///
/// The zero-based starting index of the range of elements to remove.
///
///
///
/// The number of elements to remove.
///
///
///
/// All exceptions generated by this method will be nested in one of these.
///
///
public new void RemoveRange(int index, int count)
{
try
{
var removeList = GetRange(index, count);
base.RemoveRange(index, count);
OnItemsRemoved?.Invoke(this, new EventArgs(removeList));
}
catch (System.Exception ex)
{
throw new Exception("encountered problem removing range from active update list", ex);
}
}
}
}