Advanced State System: Concept and Base

Appendix A: ListBase<T>

#region Using Statements
using System;
using System.Collections.Generic;
#endregion

namespace AdvancedStateSystem
{
    ///

    /// A List class that inherits from the generic List class and implements events for item addition and removals.
    /// 

    /// The type of the objects in the collection.
    class ListBase : List
    {
        #region Events
        ///

        /// Triggered whenever an item is added.
        /// 

        public event EventHandler ItemAdded;

        ///

        /// Triggered whenever an item is removed.
        /// 

        public event EventHandler ItemRemoved;
        #endregion

        #region Constructor
        ///

        /// Default constructor.
        /// 

        public ListBase()
        {
            // Add event handlers
            ItemAdded += OnItemAdded;
            ItemRemoved += OnItemRemoved;
        }
        #endregion

        #region Event Handlers
        ///

        /// Handles when an item is added to the collection.
        /// 

        ///
The item added.
        ///
Event Arguments.
        protected virtual void OnItemAdded(object sender, EventArgs e) { }

        ///

        /// Handles when an item is removed from the collection.
        /// 

        ///
The item removed.
        ///
Event Arguments.
        protected virtual void OnItemRemoved(object sender, EventArgs e) { }
        #endregion

        #region Adding
        ///

        /// Adds an item to the collection.
        /// 

        ///
The item to add.
        public new void Add(T item)
        {
            base.Add(item);

            if (ItemAdded != null)
                ItemAdded.Invoke(item, EventArgs.Empty);
        }

        ///

        /// Adds a range of items to the collection.
        /// 

        ///
Items to be added.
        public new void AddRange(IEnumerable items)
        {
            foreach (T item in items)
                Add(item);
        }
        #endregion

        #region Removing
        ///

        /// Removes a single item.
        /// 

        ///
The item to remove.
        /// True if the item was removed.
        public new bool Remove(T item)
        {
            bool _returned = base.Remove(item);

            if (_returned && ItemRemoved != null)
                ItemRemoved.Invoke(item, EventArgs.Empty);

            return _returned;
        }

        ///

        /// Removes all items that are matched.
        /// 

        ///
The predicate that provides matching.
        /// The number of items removed.
        public new int RemoveAll(Predicate match)
        {
            // If the predicate isn't valid, throw an exception.
            if (match == null)
                throw new ArgumentNullException("match", "Predicate was null.");

            int _count = 0;
            for (int i = 0; i < Count; i++)
            {
                T item = this[i];

                if (match(item))
                {
                    if (Remove(item)) _count++;
                }
            }

            return _count;
        }

        ///

        /// Remove an item at a specified index.
        /// 

        ///
The index at which to remove an item.
        /// True if the item was removed.
        public new bool RemoveAt(int index)
        {
            if (index < 0 || index >= Count)
                throw new IndexOutOfRangeException();

            return Remove(this[index]);
        }

        ///

        /// Removes a range of items from the collection.
        /// 

        ///
The start index for removing.
        ///
The number of items to remove.
        public new void RemoveRange(int index, int count)
        {
            if (index < 0)
                throw new IndexOutOfRangeException();

            if (count <= 0 || (index + count - 1) >= Count)
                throw new IndexOutOfRangeException();

            // Start from the back of the range to avoid
            // removal errors.
            for (int i = count + index - 1; i >= index; i--)
                Remove(this[i]);
        }

        ///

        /// Clears the collection of all items.
        /// 

        public new void Clear()
        {
            if (Count <= 0)
                return;

            RemoveRange(0, Count);
        }
        #endregion
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>