#region License
/**
* Ingenious MVC : An MVC framework for .NET 2.0
* Copyright (C) 2006, JDP Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors: - Kent Boogaart (kentcb@internode.on.net)
*/
#endregion
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using Ingenious.Mvc.Util;
namespace Ingenious.Mvc{
/// <include file='IdCollection.doc.xml' path='/doc/member[@name="T:IdCollection`1"]/*'/>
[Serializable]
public sealed class IdCollection<T> : ICollection<T>
where T : struct
{
private IList<T> _ids;
/// <include file='IdCollection.doc.xml' path='/doc/member[@name="P:Item"]/*'/>
public T this[int index]
{
get
{
return _ids[index];
}
set
{
OnIdRemoved(_ids[index]);
_ids[index] = value;
OnIdAdded(value);
}
}
/// <include file='IdCollection.doc.xml' path='/doc/member[@name="P:Count"]/*'/>
public int Count
{
get
{
return _ids.Count;
}
}
/// <include file='IdCollection.doc.xml' path='/doc/member[@name="P:IsReadOnly"]/*'/>
public bool IsReadOnly
{
get
{
return _ids.IsReadOnly;
}
}
/// <include file='IdCollection.doc.xml' path='/doc/member[@name="E:IdAdded"]/*'/>
[field: NonSerialized]
public event EventHandler<IdEventArgs<T>> IdAdded;
/// <include file='IdCollection.doc.xml' path='/doc/member[@name="E:IdRemoved"]/*'/>
[field: NonSerialized]
public event EventHandler<IdEventArgs<T>> IdRemoved;
/// <include file='IdCollection.doc.xml' path='/doc/member[@name="M:.ctor()"]/*'/>
public IdCollection()
{
_ids = new List<T>();
}
/// <include file='IdCollection.doc.xml' path='/doc/member[@name="M:Add()"]/*'/>
public void Add(T id)
{
_ids.Add(id);
OnIdAdded(id);
}
/// <include file='IdCollection.doc.xml' path='/doc/member[@name="M:AddRange()"]/*'/>
public void AddRange(ICollection<T> ids)
{
ArgumentHelper.AssertNotNull(ids, "ids");
foreach (T id in ids)
{
Add(id);
}
}
public ReadOnlyCollection<T> AsReadOnly()
{
return (_ids as List<T>).AsReadOnly();
}
/// <include file='IdCollection.doc.xml' path='/doc/member[@name="M:Clear()"]/*'/>
public void Clear()
{
_ids.Clear();
}
/// <include file='IdCollection.doc.xml' path='/doc/member[@name="M:Contains()"]/*'/>
public bool Contains(T id)
{
return _ids.Contains(id);
}
/// <include file='IdCollection.doc.xml' path='/doc/member[@name="M:CopyTo(,System.Int32)"]/*'/>
public void CopyTo(T[] array, int index)
{
_ids.CopyTo(array, index);
}
/// <include file='IdCollection.doc.xml' path='/doc/member[@name="M:IndexOf()"]/*'/>
public int IndexOf(T id)
{
return _ids.IndexOf(id);
}
/// <include file='IdCollection.doc.xml' path='/doc/member[@name="M:Insert(System.Int32,)"]/*'/>
public void Insert(int index, T id)
{
_ids.Insert(index, id);
OnIdAdded(id);
}
/// <include file='IdCollection.doc.xml' path='/doc/member[@name="M:Remove()"]/*'/>
public bool Remove(T id)
{
bool retVal = _ids.Remove(id);
if (retVal)
{
OnIdRemoved(id);
}
return retVal;
}
/// <include file='IdCollection.doc.xml' path='/doc/member[@name="M:RemoveAt(System.Int32)"]/*'/>
public void RemoveAt(int index)
{
T id = this[index];
_ids.RemoveAt(index);
OnIdRemoved(id);
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return _ids.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _ids.GetEnumerator();
}
private void OnIdAdded(T id)
{
EventHelper.Raise(IdAdded, this, new IdEventArgs<T>(id));
}
private void OnIdRemoved(T id)
{
EventHelper.Raise(IdRemoved, this, new IdEventArgs<T>(id));
}
}
}
|