/*********************************************************************/ /* */ /* 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; using System.Collections.Generic; using System.Runtime.Serialization; using AgentService.Platform.MTS; namespace AgentService.Platform.MTS { /// /// MessageBodyItemCollection. Specialized Collection of MessageMessageBodyItemCollection objects /// [Serializable] [DataContract] [KnownType(typeof(Dictionary))] [KnownType(typeof(MessageBodyItem))] public class MessageBodyItemCollection : ICollection, 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 MessageMessageBodyItemCollection objects /// //private MessageBodyItem[] items; [DataMember] //private Hashtable items; // private Hashtable items1; private Dictionary items; /// /// Constructs an empty collection with initial Size equal to /// MessageMessageBodyItemCollectionCollection.InitialDimension /// public MessageBodyItemCollection() { //this.items = new MessageBodyItem[MessageBodyItemCollection.InitialDimension]; //this.index = -1; // this.items = new Hashtable(); this.items = new Dictionary(); } /// /// 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 MessageBodyItemCollection(int initialDimension) { if (initialDimension > 0) { // this.items = new MessageBodyItem[initialDimension]; //this.items = new Hashtable(initialDimension,0.2f); this.items = new Dictionary(); } else { //this.items = new MessageBodyItem[MessageBodyItemCollection.InitialDimension]; //this.items = new Hashtable(); this.items = new Dictionary(); } // 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 MessageBodyItemCollection(int initialDimension, int incFactor) // { // if (initialDimension > 0) // { // this.items = new MessageBodyItem[initialDimension]; // } // else // { // this.items = new MessageBodyItem[MessageBodyItemCollection.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 this.items.Count; //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() { return this.items.Values.GetEnumerator(); } #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 //TODO: delete /// /// Clears all the items in the collection. /// public void Clear() { this.items.Clear(); } /// /// adds a non null item to the collection /// /// item to Add public void Add(MessageBodyItem item) { if (item == null) throw new ArgumentNullException("item","cannot Add a null item"); // if (this.Size >= this.items.Length) // { // int ndx; // MessageBodyItem[] backing = new MessageBodyItem[this.Size*this.incFactor]; // for(ndx = 0; ndx /// Remove the first occurrence of the element equal to the argument /// /// item to Remove public void Remove(string itemId) { if (itemId != null) { // int found = Size; // int finalSize = Size; // for(int ndx = 0; ndx /// true if the collection Contains the supplied item /// /// item candidate /// true if found false otherwise public bool Contains(MessageBodyItem item) { //return (this.IndexOf(item) != -1); return this.items.ContainsKey(item.Identifier); } /// /// returns the index of the item passed as argument /// /// item to found /// zero-based index of the item, -1 otherwise // public int IndexOf(MessageBodyItem item) // { // int retval = -1; // for(int ndx = 0; ndx /// returns the item at the given position in the collection /// /// position of the item /// the item at the desired position public MessageBodyItem this[string itemId] { get { // if (index >= this.Size) // throw new IndexOutOfRangeException(String.Format("index {0} is greater or equal than the collection Size [{1}]", index, this.Size)); return this.items[itemId] as MessageBodyItem; } set { // if (index >= this.Size) // throw new IndexOutOfRangeException(String.Format("index {0} is greater or equal than the collection Size [{1}]", index, this.Size)); if (value == null) throw new NullReferenceException("Cannot insert a null item into the collection"); if (this.items.ContainsKey(itemId) == false) throw new System.ArgumentException(String.Format("Item [{0}] does not belong to the collection",itemId)); this.items[itemId] = value; } } } }