- //--------------- Protected Instance Properties ----------------\r
- /// <summary>\r
- /// SDK: Gets or sets a value indicating whether the NameObjectCollectionBase instance is read-only.\r
- /// </summary>\r
- protected bool IsReadOnly \r
- {\r
- get{\r
- return m_readonly;\r
- }\r
- set{\r
- m_readonly=value;\r
- }\r
- }\r
- \r
- //--------------- Protected Instance Methods -------------------\r
- /// <summary>\r
- /// Adds an Item with the specified key and value into the <see cref="NameObjectCollectionBase"/>NameObjectCollectionBase instance.\r
- /// </summary>\r
- /// <param name="name"></param>\r
- /// <param name="value"></param>\r
- protected void BaseAdd( string name, object value )\r
- {\r
- if (this.IsReadOnly)\r
- throw new NotSupportedException("Collection is read-only");\r
- \r
- _Item newitem=new _Item(name, value);\r
-\r
- if (name==null){\r
- //todo: consider nullkey entry\r
- if (m_NullKeyItem==null)\r
- m_NullKeyItem = newitem;\r
- }\r
- else\r
- if (m_ItemsContainer[name]==null){\r
- m_ItemsContainer.Add(name,newitem);\r
- }\r
- m_ItemsArray.Add(newitem);\r
- }
-
- protected void BaseClear()\r
- {\r
- if (this.IsReadOnly)\r
- throw new NotSupportedException("Collection is read-only");\r
- Init();\r
- //throw new Exception("Not implemented yet");\r
- }\r
- /// <summary>\r
- /// SDK: Gets the value of the entry at the specified index of the NameObjectCollectionBase instance.\r
- /// </summary>\r
- /// <param name="index"></param>\r
- /// <returns></returns>\r
- protected object BaseGet( int index )\r
- {\r
- return ((_Item)m_ItemsArray[index]).value;\r
- //throw new Exception("Not implemented yet");\r
- }\r
- /// <summary>\r
- /// SDK: Gets the value of the first entry with the specified key from the NameObjectCollectionBase instance.\r
- /// </summary>\r
- /// <remark>CAUTION: The BaseGet method does not distinguish between a null reference which is returned because the specified key is not found and a null reference which is returned because the value associated with the key is a null reference.</remark>\r
- /// <param name="name"></param>\r
- /// <returns></returns>\r
- protected object BaseGet( string name )\r
- {\r
- _Item item = FindFirstMatchedItem(name);\r
- /// CAUTION: The BaseGet method does not distinguish between a null reference which is returned because the specified key is not found and a null reference which is returned because the value associated with the key is a null reference.\r
- if (item==null)\r
- return null;\r
- else\r
- return item.value;\r
- }\r
-\r
- /// <summary>\r
- /// SDK:Returns a String array that contains all the keys in the NameObjectCollectionBase instance.\r
- /// </summary>\r
- /// <returns>A String array that contains all the keys in the NameObjectCollectionBase instance.</returns>\r
- protected string[] BaseGetAllKeys()\r
- {\r
- int cnt = m_ItemsArray.Count;\r
- string[] allKeys = new string[cnt];\r
- for(int i=0; i<cnt; i++)\r
- allKeys[i] = BaseGetKey(i);//((_Item)m_ItemsArray[i]).key;\r
- \r
- return allKeys;\r
- }\r
-\r
- /// <summary>\r
- /// SDK: Returns an Object array that contains all the values in the NameObjectCollectionBase instance.\r
- /// </summary>\r
- /// <returns>An Object array that contains all the values in the NameObjectCollectionBase instance.</returns>\r
- protected object[] BaseGetAllValues()\r
- {\r
- int cnt = m_ItemsArray.Count;\r
- object[] allValues = new object[cnt];\r
- for(int i=0; i<cnt; i++)\r
- allValues[i] = BaseGet(i);\r
- \r
- return allValues;\r
- }
-\r
- protected object[] BaseGetAllValues( Type type )\r
- {\r
- if (type == null)\r
- throw new ArgumentNullException("'type' argument can't be null");\r
- int cnt = m_ItemsArray.Count;\r
- object[] allValues = (object[]) Array.CreateInstance (type, cnt);\r
- for(int i=0; i<cnt; i++)\r
- allValues[i] = BaseGet(i);\r
- \r
- return allValues;\r
- }\r
- \r
- protected string BaseGetKey( int index )\r
- {\r
- return ((_Item)m_ItemsArray[index]).key;\r
- //throw new Exception("Not implemented yet");\r
- }\r
- /// <summary>\r
- /// Gets a value indicating whether the NameObjectCollectionBase instance contains entries whose keys are not a null reference \r
- /// </summary>\r
- /// <returns>true if the NameObjectCollectionBase instance contains entries whose keys are not a null reference otherwise, false.</returns>\r
- protected bool BaseHasKeys()\r
- {\r
- return (m_ItemsContainer.Count>0);\r
-// throw new Exception("Not implemented yet");\r
- }\r
-
- protected void BaseRemove( string name )\r
- {\r
- int cnt = 0;\r
- String key;\r
- if (this.IsReadOnly)\r
- throw new NotSupportedException("Collection is read-only");\r
- if (name!=null)\r
- {\r
- m_ItemsContainer.Remove(name);\r
- }\r
- else {\r
- m_NullKeyItem = null;\r
- }\r
- \r
- cnt = m_ItemsArray.Count;\r
- for (int i=0 ; i< cnt; ){\r
- key=BaseGetKey(i);\r
- if (m_comparer.Compare (key, name) == 0) {\r
- m_ItemsArray.RemoveAt(i);\r
- cnt--;\r
- }\r
- else \r
- i++;\r
- \r
- }\r
- }
-\r
- /// <summary>\r
- /// \r
- /// </summary>\r
- /// <param name="index"></param>\r
- /// <LAME>This function implemented the way Microsoft implemented it - \r
- /// item is removed from hashtable and array without considering the case when there are two items with the same key but different values in array.\r
- /// E.g. if\r
- /// hashtable is [("Key1","value1")] and array contains [("Key1","value1")("Key1","value2")] then\r
- /// after RemoveAt(1) the collection will be in following state:\r
- /// hashtable:[] \r
- /// array: [("Key1","value1")] \r
- /// It's ok only then the key is uniquely assosiated with the value\r
- /// To fix it a comparsion of objects stored under the same key in the hashtable and in the arraylist should be added \r
- /// </LAME>>\r
- [MonoTODO]\r
- protected void BaseRemoveAt( int index )\r
- {\r
- if (this.IsReadOnly)\r
- throw new NotSupportedException("Collection is read-only");\r
- string key = BaseGetKey(index);\r
- if (key!=null){\r
- // TODO: see LAME description above\r
- m_ItemsContainer.Remove(key);\r
- }\r
- else\r
- m_NullKeyItem = null;\r
- m_ItemsArray.RemoveAt(index);\r
-// throw new Exception("Not implemented yet");\r
- }\r
- /// <summary>\r
- /// SDK: Sets the value of the entry at the specified index of the NameObjectCollectionBase instance.\r
- /// </summary>\r
- /// <param name="index"></param>\r
- /// <param name="value"></param>\r
- protected void BaseSet( int index, object value )\r
- {\r
- if (this.IsReadOnly)\r
- throw new NotSupportedException("Collection is read-only");\r
- _Item item = (_Item)m_ItemsArray[index];\r
- item.value = value;\r
- //throw new Exception("Not implemented yet");\r
- }\r
- /// <summary>\r
- /// Sets the value of the first entry with the specified key in the NameObjectCollectionBase instance, if found; otherwise, adds an entry with the specified key and value into the NameObjectCollectionBase instance.\r
- /// </summary>\r
- /// <param name="name">The String key of the entry to set. The key can be a null reference </param>\r
- /// <param name="value">The Object that represents the new value of the entry to set. The value can be a null reference</param>\r
- protected void BaseSet( string name, object value )\r
- {\r
- if (this.IsReadOnly)\r
- throw new NotSupportedException("Collection is read-only");\r
- _Item item = FindFirstMatchedItem(name);\r
- if (item!=null)\r
- item.value=value;\r
- else \r
- BaseAdd(name, value);\r
-\r
- //throw new Exception("Not implemented yet");\r
- }\r
- [MonoTODO]\r
- private _Item FindFirstMatchedItem(string name)\r
- {\r
- if (name!=null)\r
- return (_Item)m_ItemsContainer[name];\r
- else {\r
- //TODO: consider null key case\r
- return m_NullKeyItem;\r
- //throw new Exception("Not implemented yet");\r
- }\r
-\r
- }\r
- //~Object();\r
- \r
- }\r
-}\r
+
+ //--------------- Protected Instance Properties ----------------
+ /// <summary>
+ /// SDK: Gets or sets a value indicating whether the NameObjectCollectionBase instance is read-only.
+ /// </summary>
+ protected bool IsReadOnly
+ {
+ get{
+ return m_readonly;
+ }
+ set{
+ m_readonly=value;
+ }
+ }
+
+ //--------------- Protected Instance Methods -------------------
+ /// <summary>
+ /// Adds an Item with the specified key and value into the <see cref="NameObjectCollectionBase"/>NameObjectCollectionBase instance.
+ /// </summary>
+ /// <param name="name"></param>
+ /// <param name="value"></param>
+ protected void BaseAdd( string name, object value )
+ {
+ if (this.IsReadOnly)
+ throw new NotSupportedException("Collection is read-only");
+
+ _Item newitem=new _Item(name, value);
+
+ if (name==null){
+ //todo: consider nullkey entry
+ if (m_NullKeyItem==null)
+ m_NullKeyItem = newitem;
+ }
+ else
+ if (m_ItemsContainer[name]==null){
+ m_ItemsContainer.Add(name,newitem);
+ }
+ m_ItemsArray.Add(newitem);
+ }
+
+ protected void BaseClear()
+ {
+ if (this.IsReadOnly)
+ throw new NotSupportedException("Collection is read-only");
+ Init();
+ }
+
+ /// <summary>
+ /// SDK: Gets the value of the entry at the specified index of the NameObjectCollectionBase instance.
+ /// </summary>
+ /// <param name="index"></param>
+ /// <returns></returns>
+ protected object BaseGet( int index )
+ {
+ return ((_Item)m_ItemsArray[index]).value;
+ }
+
+ /// <summary>
+ /// SDK: Gets the value of the first entry with the specified key from the NameObjectCollectionBase instance.
+ /// </summary>
+ /// <remark>CAUTION: The BaseGet method does not distinguish between a null reference which is returned because the specified key is not found and a null reference which is returned because the value associated with the key is a null reference.</remark>
+ /// <param name="name"></param>
+ /// <returns></returns>
+ protected object BaseGet( string name )
+ {
+ _Item item = FindFirstMatchedItem(name);
+ /// CAUTION: The BaseGet method does not distinguish between a null reference which is returned because the specified key is not found and a null reference which is returned because the value associated with the key is a null reference.
+ if (item==null)
+ return null;
+ else
+ return item.value;
+ }
+
+ /// <summary>
+ /// SDK:Returns a String array that contains all the keys in the NameObjectCollectionBase instance.
+ /// </summary>
+ /// <returns>A String array that contains all the keys in the NameObjectCollectionBase instance.</returns>
+ protected string[] BaseGetAllKeys()
+ {
+ int cnt = m_ItemsArray.Count;
+ string[] allKeys = new string[cnt];
+ for(int i=0; i<cnt; i++)
+ allKeys[i] = BaseGetKey(i);//((_Item)m_ItemsArray[i]).key;
+
+ return allKeys;
+ }
+
+ /// <summary>
+ /// SDK: Returns an Object array that contains all the values in the NameObjectCollectionBase instance.
+ /// </summary>
+ /// <returns>An Object array that contains all the values in the NameObjectCollectionBase instance.</returns>
+ protected object[] BaseGetAllValues()
+ {
+ int cnt = m_ItemsArray.Count;
+ object[] allValues = new object[cnt];
+ for(int i=0; i<cnt; i++)
+ allValues[i] = BaseGet(i);
+
+ return allValues;
+ }
+
+ protected object[] BaseGetAllValues( Type type )
+ {
+ if (type == null)
+ throw new ArgumentNullException("'type' argument can't be null");
+ int cnt = m_ItemsArray.Count;
+ object[] allValues = (object[]) Array.CreateInstance (type, cnt);
+ for(int i=0; i<cnt; i++)
+ allValues[i] = BaseGet(i);
+
+ return allValues;
+ }
+
+ protected string BaseGetKey( int index )
+ {
+ return ((_Item)m_ItemsArray[index]).key;
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether the NameObjectCollectionBase instance contains entries whose keys are not a null reference
+ /// </summary>
+ /// <returns>true if the NameObjectCollectionBase instance contains entries whose keys are not a null reference otherwise, false.</returns>
+ protected bool BaseHasKeys()
+ {
+ return (m_ItemsContainer.Count>0);
+ }
+
+ protected void BaseRemove( string name )
+ {
+ int cnt = 0;
+ String key;
+ if (this.IsReadOnly)
+ throw new NotSupportedException("Collection is read-only");
+ if (name!=null)
+ {
+ m_ItemsContainer.Remove(name);
+ }
+ else {
+ m_NullKeyItem = null;
+ }
+
+ cnt = m_ItemsArray.Count;
+ for (int i=0 ; i< cnt; ){
+ key=BaseGetKey(i);
+ if (Equals (key, name)) {
+ m_ItemsArray.RemoveAt(i);
+ cnt--;
+ }
+ else
+ i++;
+ }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="index"></param>
+ /// <LAME>This function implemented the way Microsoft implemented it -
+ /// item is removed from hashtable and array without considering the case when there are two items with the same key but different values in array.
+ /// E.g. if
+ /// hashtable is [("Key1","value1")] and array contains [("Key1","value1")("Key1","value2")] then
+ /// after RemoveAt(1) the collection will be in following state:
+ /// hashtable:[]
+ /// array: [("Key1","value1")]
+ /// 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>>
+ protected void BaseRemoveAt( int index )
+ {
+ if (this.IsReadOnly)
+ throw new NotSupportedException("Collection is read-only");
+ string key = BaseGetKey(index);
+ if (key!=null){
+ // TODO: see LAME description above
+ m_ItemsContainer.Remove(key);
+ }
+ else
+ m_NullKeyItem = null;
+ m_ItemsArray.RemoveAt(index);
+ }
+
+ /// <summary>
+ /// SDK: Sets the value of the entry at the specified index of the NameObjectCollectionBase instance.
+ /// </summary>
+ /// <param name="index"></param>
+ /// <param name="value"></param>
+ protected void BaseSet( int index, object value )
+ {
+ if (this.IsReadOnly)
+ throw new NotSupportedException("Collection is read-only");
+ _Item item = (_Item)m_ItemsArray[index];
+ item.value = value;
+ }
+
+ /// <summary>
+ /// Sets the value of the first entry with the specified key in the NameObjectCollectionBase instance, if found; otherwise, adds an entry with the specified key and value into the NameObjectCollectionBase instance.
+ /// </summary>
+ /// <param name="name">The String key of the entry to set. The key can be a null reference </param>
+ /// <param name="value">The Object that represents the new value of the entry to set. The value can be a null reference</param>
+ protected void BaseSet( string name, object value )
+ {
+ if (this.IsReadOnly)
+ throw new NotSupportedException("Collection is read-only");
+ _Item item = FindFirstMatchedItem(name);
+ if (item!=null)
+ item.value=value;
+ else
+ BaseAdd(name, value);
+ }
+
+ [MonoTODO]
+ private _Item FindFirstMatchedItem(string name)
+ {
+ if (name!=null)
+ return (_Item)m_ItemsContainer[name];
+ else {
+ //TODO: consider null key case
+ return m_NullKeyItem;
+ }
+ }
+
+ internal bool Equals (string s1, string s2)
+ {
+ if (m_comparer != null)
+ return (m_comparer.Compare (s1, s2) == 0);
+ else
+ return equality_comparer.Equals (s1, s2);
+ }
+ }
+}