/*********************************************************************/
/* */
/* AgentService Framework */
/* www.agentservice.it */
/* */
/* Copyright (C) 2007 l.i.d.o. - DIST, University of Genoa, Italy */
/* GNU Lesser General Public License */
/* */
/* 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, */
/* version 2.1 of the License. */
/* */
/* 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., 59 Temple Place - Suite 330, */
/* Boston, MA 02111-1307, USA. */
/* */
/*********************************************************************/
using System;
using System.Collections;
namespace AgentService.Agent.Behaviour
{
///
/// BehaviourThreadUnitCollectionCollection. Specialized Collection of BehaviourThreadUnit objects
///
[Serializable]
public class BehaviourThreadUnitCollection : ICollection, IEnumerator, IEnumerable
{
///
/// InitialDimension, default value of the Size of the collection
///
private readonly static int InitialDimension = 10;
///
/// incFactor, defines the increment factor when the internal data structure
/// needs to increase in Size
///
private int incFactor = 2;
///
/// Size of the collection
///
private int size = 0;
///
/// index of the current element in the collection
///
private int index = 0;
///
/// internal data structure that stores the BehaviourThreadUniteCollection objects
///
private BehaviourThreadUnit[] items;
///
/// Constructs an empty collection with initial Size equal to
/// BehaviourThreadUnitCollectionCollection.InitialDimension
///
public BehaviourThreadUnitCollection()
{
this.items = new BehaviourThreadUnit[BehaviourThreadUnitCollection.InitialDimension];
this.index = -1;
}
///
/// Constructs an empty collection with initial Size equal to
/// the argument, if the argument is less than 1 the collection
/// is constructed by using the default Size
///
/// initial dimension of the collection
public BehaviourThreadUnitCollection(int initialDimension)
{
if (initialDimension > 0)
{
this.items = new BehaviourThreadUnit[initialDimension];
}
else
{
this.items = new BehaviourThreadUnit[BehaviourThreadUnitCollection.InitialDimension];
}
this.index = -1;
}
///
/// Constructs an empty collection with initial Size equal to the first argument
/// and increment factor equal to second parameter. If the supplied initial Size is less than 1
/// the constructor will use the default Size, while if the increment factor is less than 1 it
/// will be ignored
///
/// initial Size of the collection
/// increment factor
public BehaviourThreadUnitCollection(int initialDimension, int incFactor)
{
if (initialDimension > 0)
{
this.items = new BehaviourThreadUnit[initialDimension];
}
else
{
this.items = new BehaviourThreadUnit[BehaviourThreadUnitCollection.InitialDimension];
}
if (incFactor > 0)
{
this.incFactor = incFactor;
}
this.index = -1;
}
#region ICollection Methods
///
/// Gets the number of the elements actually stored in the collection
///
public int Count
{
get
{
return size;
}
}
///
/// true if the collection is synchronized
///
public bool IsSynchronized
{
get
{
return false;
}
}
///
/// Gets the sinchronization object for the collection
///
public object SyncRoot
{
get
{
return null;
}
}
///
/// Copies the elements of the collection into a System.Array object, starting at the given index.
/// Throws NullArgumetException if the supplied array is null.
/// Throws ArgumentOutOfRangeException if
/// - the index argument is negative
/// - the index argument is greater than the array's Size
/// - the sum of the index argument and of the collection Size is greater then the array's Size
/// - the rank of the array argument is greater than 1
///
/// array where the items of the collection will be copied to
/// starting index
public void CopyTo(System.Array array, int index)
{
if (array == null) throw new ArgumentNullException("array", "the argument cannot be null");
if (index < 0) throw new ArgumentOutOfRangeException("index",index,"the index must be >= 0");
if (array.Length <= index) throw new ArgumentOutOfRangeException("index",index,"index is greater then array Size");
if (array.Length < index + this.size) throw new ArgumentOutOfRangeException("index", index, "the index value is too forward for the insertion of the elements in the collection");
if (array.Rank != 1)throw new ArgumentOutOfRangeException("array",array,"the array has more than one dimension");
for(int ndx=0; ndx
/// returns the enumerator object for the collection
///
/// enumerator for the collection
public IEnumerator GetEnumerator()
{
this.Reset();
return this;
}
#endregion
#region IEnumerator Methods
///
/// gets the current object for the collection
///
public object Current
{
get
{
if ((this.index < this.size) && (this.index > -1))
return this.items[index];
else
return null;
}
}
///
/// resets the collection enumerator
///
public void Reset()
{
this.index = -1;
}
///
/// moves to the next element of the collection.
///
/// false if there are no more elements to navigate, true otherwise
public bool MoveNext()
{
this.index++;
return (this.index < this.size);
}
#endregion
///
/// Adds a non null item to the collection
///
/// item to Add
public void Add(BehaviourThreadUnit item)
{
if (item == null)
throw new ArgumentNullException("item","cannot Add a null item");
if (this.size >= this.items.Length)
{
int ndx;
BehaviourThreadUnit[] backing = new BehaviourThreadUnit[this.size*this.incFactor];
for(ndx = 0; ndx
/// Removes the first occurrence of the element equal to the argument
///
/// item to Remove
public void Remove(BehaviourThreadUnit item)
{
if (item != null)
{
int found = size;
int finalSize = size;
for(int ndx = 0; ndx
/// Returns true if the collection Contains the supplied item
///
/// item candidate
/// true if found false otherwise
public bool Contains(BehaviourThreadUnit item)
{
return (this.IndexOf(item) != -1);
}
///
/// Returns the index of the item passed as argument
///
/// item to found
/// zero-based index of the item, -1 otherwise
public int IndexOf(BehaviourThreadUnit item)
{
int retval = -1;
for(int ndx = 0; ndx