Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System / System.Collections.Specialized / NameObjectCollectionBase.cs
index 89f59108dcb62ac6e5046cdaa0c11b95476f1e49..a1a377da08f34a0e6090892905d848c419d1ba71 100644 (file)
@@ -49,13 +49,12 @@ namespace System.Collections.Specialized
                private bool m_readonly;
                SerializationInfo infoCopy;
                private KeysCollection keyscoll;
-#if NET_2_0
                private IEqualityComparer equality_comparer;
 
                internal IEqualityComparer EqualityComparer {
                        get { return equality_comparer; }
                }
-#endif
+
                internal IComparer Comparer {
                        get {return m_comparer;}
                }
@@ -101,7 +100,7 @@ namespace System.Collections.Specialized
                        }
                        public bool MoveNext()
                        {
-                               return ((++m_position)<m_collection.Count)?true:false;
+                               return ((++m_position) < m_collection.Count);
                        }
                        public void Reset()
                        {
@@ -128,17 +127,27 @@ namespace System.Collections.Specialized
                        }
                        
                        // ICollection methods -----------------------------------
-                       void ICollection.CopyTo(Array arr, int index)
+                       void ICollection.CopyTo (Array array, int arrayIndex)
                        {
-                               if (arr==null)
-                                       throw new ArgumentNullException("array can't be null");
-                               IEnumerator en = this.GetEnumerator();
-                               int i = index;
-                               while (en.MoveNext())
-                               {
-                                       arr.SetValue(en.Current,i);
-                                       i++;
-                               }                       
+                               ArrayList items = m_collection.m_ItemsArray;
+                               if (null == array)
+                                       throw new ArgumentNullException ("array");
+
+                               if (arrayIndex < 0)
+                                       throw new ArgumentOutOfRangeException ("arrayIndex");
+
+                               if ((array.Length > 0) && (arrayIndex >= array.Length))
+                                       throw new ArgumentException ("arrayIndex is equal to or greater than array.Length");
+
+                               if (arrayIndex + items.Count > array.Length)
+                                       throw new ArgumentException ("Not enough room from arrayIndex to end of array for this KeysCollection");
+
+                               if (array != null && array.Rank > 1)
+                                       throw new ArgumentException ("array is multidimensional");
+                               
+                               object[] objArray = (object[])array;
+                               for (int i = 0; i < items.Count; i++, arrayIndex++)
+                                       objArray [arrayIndex] = ((_Item)items [i]).key;
                        }
 
                        bool ICollection.IsSynchronized
@@ -186,13 +195,8 @@ namespace System.Collections.Specialized
                protected NameObjectCollectionBase ()
                {
                        m_readonly = false;
-#if NET_1_0
-                       m_hashprovider = CaseInsensitiveHashCodeProvider.Default;
-                       m_comparer = CaseInsensitiveComparer.Default;
-#else
                        m_hashprovider = CaseInsensitiveHashCodeProvider.DefaultInvariant;
                        m_comparer = CaseInsensitiveComparer.DefaultInvariant;
-#endif
                        m_defCapacity = 0;
                        Init();
                }
@@ -200,36 +204,34 @@ namespace System.Collections.Specialized
                protected NameObjectCollectionBase( int capacity )
                {
                        m_readonly = false;
-#if NET_1_0
-                       m_hashprovider = CaseInsensitiveHashCodeProvider.Default;
-                       m_comparer = CaseInsensitiveComparer.Default;
-#else
                        m_hashprovider = CaseInsensitiveHashCodeProvider.DefaultInvariant;
                        m_comparer = CaseInsensitiveComparer.DefaultInvariant;
-#endif
                        m_defCapacity = capacity;
                        Init();
-               }
+               }               
 
-#if NET_2_0
-               protected NameObjectCollectionBase (IEqualityComparer equalityComparer)
+               internal NameObjectCollectionBase (IEqualityComparer equalityComparer, IComparer comparer, IHashCodeProvider hcp)
                {
-                       m_readonly = false;
                        equality_comparer = equalityComparer;
+                       m_comparer = comparer;
+                       m_hashprovider = hcp;
+                       m_readonly = false;
                        m_defCapacity = 0;
-                       Init();
+                       Init ();
                }
 
+               protected NameObjectCollectionBase (IEqualityComparer equalityComparer) : this( (equalityComparer == null ? StringComparer.InvariantCultureIgnoreCase : equalityComparer), null, null)
+               {                       
+               }               
+
                [Obsolete ("Use NameObjectCollectionBase(IEqualityComparer)")]
-#endif
                protected NameObjectCollectionBase( IHashCodeProvider hashProvider, IComparer comparer )
-               {
-                       m_readonly = false;
-                       
-                       m_hashprovider = hashProvider;
+               {                       
                        m_comparer = comparer;
+                       m_hashprovider = hashProvider;
+                       m_readonly = false;
                        m_defCapacity = 0;
-                       Init();
+                       Init ();
                }
 
                protected NameObjectCollectionBase (SerializationInfo info, StreamingContext context)
@@ -237,17 +239,15 @@ namespace System.Collections.Specialized
                        infoCopy = info;
                }
 
-#if NET_2_0
                protected NameObjectCollectionBase (int capacity, IEqualityComparer equalityComparer)
                {
                        m_readonly = false;
-                       equality_comparer = equalityComparer;
+                       equality_comparer = (equalityComparer == null ? StringComparer.InvariantCultureIgnoreCase : equalityComparer);
                        m_defCapacity = capacity;
                        Init();
                }
 
                [Obsolete ("Use NameObjectCollectionBase(int,IEqualityComparer)")]
-#endif
                protected NameObjectCollectionBase( int capacity, IHashCodeProvider hashProvider, IComparer comparer )
                {
                        m_readonly = false;
@@ -260,14 +260,19 @@ namespace System.Collections.Specialized
                
                private void Init ()
                {
-#if NET_2_0
+                       if (m_ItemsContainer != null) {
+                               m_ItemsContainer.Clear ();
+                               m_ItemsContainer = null;
+                       }
+                       
+                       if (m_ItemsArray != null) {
+                               m_ItemsArray.Clear ();
+                               m_ItemsArray = null;
+                       }
                        if (equality_comparer != null)
                                m_ItemsContainer = new Hashtable (m_defCapacity, equality_comparer);
                        else
                                m_ItemsContainer = new Hashtable (m_defCapacity, m_hashprovider, m_comparer);
-#else
-                       m_ItemsContainer = new Hashtable (m_defCapacity, m_hashprovider, m_comparer);
-#endif
                        m_ItemsArray = new ArrayList();
                        m_NullKeyItem = null;   
                }
@@ -290,11 +295,7 @@ namespace System.Collections.Specialized
                /// <remark>This enumerator returns the keys of the collection as strings.</remark>
                /// </summary>
                /// <returns></returns>
-               public
-#if NET_2_0            
-               virtual
-#endif
-               IEnumerator GetEnumerator()
+               public virtual IEnumerator GetEnumerator()
                {
                        return new _KeysEnumerator(this);
                }
@@ -315,7 +316,6 @@ namespace System.Collections.Specialized
                                i++;
                        }
 
-#if NET_2_0
                        if (equality_comparer != null) {
                                info.AddValue ("KeyComparer", equality_comparer, typeof (IEqualityComparer));
                                info.AddValue ("Version", 4, typeof (int));
@@ -324,10 +324,6 @@ namespace System.Collections.Specialized
                                info.AddValue ("Comparer", m_comparer, typeof (IComparer));
                                info.AddValue ("Version", 2, typeof (int));
                        }
-#else
-                       info.AddValue ("HashProvider", m_hashprovider, typeof (IHashCodeProvider));
-                       info.AddValue ("Comparer", m_comparer, typeof (IComparer));
-#endif
                        info.AddValue("ReadOnly", m_readonly);
                        info.AddValue("Count", count);
                        info.AddValue("Keys", keys, typeof(string[]));
@@ -354,7 +350,7 @@ namespace System.Collections.Specialized
 
                void ICollection.CopyTo (Array array, int index)
                {
-                       (Keys as ICollection).CopyTo (array, index);
+                       ((ICollection)Keys).CopyTo (array, index);
                }
 
                // IDeserializationCallback
@@ -371,7 +367,6 @@ namespace System.Collections.Specialized
                        infoCopy = null;
                        m_hashprovider = (IHashCodeProvider) info.GetValue ("HashProvider",
                                                                            typeof (IHashCodeProvider));
-#if NET_2_0
                        if (m_hashprovider == null) {
                                equality_comparer = (IEqualityComparer) info.GetValue ("KeyComparer", typeof (IEqualityComparer));
                        } else {
@@ -379,14 +374,6 @@ namespace System.Collections.Specialized
                                if (m_comparer == null)
                                        throw new SerializationException ("The comparer is null");
                        }
-#else
-                       if (m_hashprovider == null)
-                               throw new SerializationException ("The hash provider is null");
-
-                       m_comparer = (IComparer) info.GetValue ("Comparer", typeof (IComparer));
-                       if (m_comparer == null)
-                               throw new SerializationException ("The comparer is null");
-#endif
                        m_readonly = info.GetBoolean ("ReadOnly");
                        string [] keys = (string []) info.GetValue ("Keys", typeof (string []));
                        if (keys == null)
@@ -568,7 +555,6 @@ namespace System.Collections.Specialized
                /// It's ok only then the key is uniquely assosiated with the value
                /// To fix it a comparsion of objects stored under the same key in the hashtable and in the arraylist should be added 
                /// </LAME>>
-               [MonoTODO]
                protected void BaseRemoveAt( int index )
                {
                        if (this.IsReadOnly)
@@ -625,14 +611,10 @@ namespace System.Collections.Specialized
 
                internal bool Equals (string s1, string s2)
                {
-#if NET_2_0
                        if (m_comparer != null)
                                return (m_comparer.Compare (s1, s2) == 0);
                        else
                                return equality_comparer.Equals (s1, s2);
-#else
-                       return (m_comparer.Compare (s1, s2) == 0);
-#endif
                }
        }
 }