DWC.Collections.Generic.CollectionBase
/*
Copyright (c) 2004 DigiTec Web Consultants, LLC. All rights reserved.
The use of this software is for test and performance purposes only.
You may not use this software in any commercial applications without
the express permission of the copyright holder. You may add to or
modify the code contained here-in to cause it to run slower without
contacting the copyright holder, however, any attempts to make this
code run faster should be documented on:
http://weblogs.asp.net/justin_rogers/articles/238524.aspx
I reserve the right to change or modify the publicly available version
of this code at any time. I will not provide version protection, so
if you have reliance on a particular build of this software, then make
your own back-ups.
You must laugh, at least a little, when reading this licensing agreement,
unless of course you don't have a sense of humor. In all seriousness,
excluding the laughter, laughter in itself does not void this license
agreement, nor compromise it's ability to legally bind you.
You must not remove this notice, or any other, from this software.
*/
/*
Version History:
0.1 - Initial Public Release Coinciding with a blog entry
*/
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
#endregion
namespace DWC.Collections.Generic {
public class CollectionBase<T> :
IList<T>, IList,
ICollection<T>, ICollection,
IEnumerable<T>, IEnumerable {
private List<T> innerList;
private bool sorted = true;
public CollectionBase() : this(10) { }
public CollectionBase(int initialCapacity) {
innerList = new List<T>( initialCapacity );
}
public virtual void Clear() {
if ( !this.OnClear() ) { return; }
this.innerList.Clear();
this.OnClearComplete();
}
#region Notification Events
protected virtual bool OnClear() {
return true;
}
protected virtual void OnClearComplete() {
}
protected virtual bool OnInsert( int index, T value ) {
return true;
}
protected virtual void OnInsertComplete(
int index, T value ) {
}
protected virtual bool OnRemove(
int index, T value ) {
return true;
}
protected virtual void OnRemoveComplete(
int index, T value ) {
}
protected virtual bool OnSet(
int index, T oldValue, T value ) {
return true;
}
protected virtual void OnSetComplete(
int index, T oldValue, T value ) {
}
protected virtual bool OnValidate( T value ) {
return true;
}
#endregion
#region IList<T> Members
public virtual int IndexOf( T item ) {
return innerList.IndexOf( item );
}
public virtual void Insert( int index, T item ) {
if ( !OnValidate( item ) ) return;
if ( !OnInsert( index, item ) ) return;
innerList.Insert( index, item );
OnInsertComplete( index, item );
}
public virtual void RemoveAt( int index ) {
T value = innerList[index];
if ( !OnValidate( value ) ) return;
if ( !OnRemove( index, value ) ) return;
innerList.RemoveAt( index );
OnRemoveComplete( index, value );
}
public virtual T this[int index] {
get {
return innerList[index];
}
set {
T oldValue = innerList[index];
if ( !OnValidate( value ) ) return;
if ( !OnSet( index, oldValue, value ) ) return;
innerList[index] = value;
OnSetComplete( index, oldValue, value );
}
}
#endregion
#region ICollection<T> Members
public virtual void Add( T item ) {
if ( !OnValidate( item ) ) return;
if ( !OnInsert( innerList.Count, item ) ) return;
innerList.Add( item );
OnInsertComplete( innerList.Count - 1, item );
}
public virtual bool Contains( T item ) {
return innerList.Contains( item );
}
public virtual void CopyTo( T[] array, int arrayIndex ) {
innerList.CopyTo( array, arrayIndex );
}
public virtual int Count {
get { return innerList.Count; }
}
public virtual bool IsReadOnly {
get { return ((ICollection<T>) innerList).IsReadOnly; }
}
public virtual bool Remove( T item ) {
int index = innerList.IndexOf( item );
if ( index < 0 ) return false;
if ( !OnValidate( item ) ) return false;
if ( !OnRemove( index, item ) ) return false;
innerList.Remove( item );
OnRemoveComplete( index, item );
return true;
}
#endregion
#region IEnumerable<T> Members
public virtual IEnumerator<T> GetEnumerator() {
return innerList.GetEnumerator();
}
#endregion
#region IList Members
public virtual int Add( object value ) {
int index = innerList.Count;
if ( !OnValidate( (T) value ) ) return -1;
if ( !OnInsert( index, (T) value ) ) return -1;
index = ((IList) innerList).Add( value );
OnInsertComplete( index, (T) value );
return index;
}
public virtual bool Contains( object value ) {
return ((IList) innerList).Contains( value );
}
public virtual int IndexOf( object value ) {
return ((IList) innerList).IndexOf( value );
}
public virtual void Insert( int index, object value ) {
if ( !OnValidate( (T) value ) ) return;
if ( !OnInsert( index, (T) value ) ) return;
((IList) innerList).Insert( index, value );
OnInsertComplete( index, (T) value );
}
public virtual bool IsFixedSize {
get { return ( (IList) innerList ).IsFixedSize; }
}
public virtual void Remove( object value ) {
int index = innerList.IndexOf( (T) value );
if ( index < 0 ) return;
if ( !OnValidate( (T) value ) ) return;
if ( !OnRemove( index, (T) value ) ) return;
((IList) innerList).Remove( value );
OnRemoveComplete( index, (T) value );
}
object IList.this[int index] {
get {
return innerList[index];
}
set {
T oldValue = innerList[index];
if ( !OnValidate( (T) value ) ) return;
if ( !OnSet( index, oldValue, (T) value ) ) return;
innerList[index] = (T) value;
OnSetComplete( index, oldValue, (T) value );
}
}
#endregion
#region ICollection Members
public virtual void CopyTo( Array array, int index ) {
((ICollection) innerList).CopyTo( array, index );
}
public virtual bool IsSynchronized {
get { return ( (ICollection) innerList ).IsSynchronized; }
}
public virtual object SyncRoot {
get { return ( (ICollection) innerList ).SyncRoot; }
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator() {
return ((IEnumerable) innerList).GetEnumerator();
}
#endregion
}
}