You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-dev@logging.apache.org by ni...@apache.org on 2004/07/30 19:32:55 UTC

cvs commit: logging-log4net/src/Util ReadOnlyPropertiesDictionary.cs PropertiesDictionary.cs

nicko       2004/07/30 10:32:55

  Modified:    src/Util PropertiesDictionary.cs
  Added:       src/Util ReadOnlyPropertiesDictionary.cs
  Log:
  Added ReadOnlyPropertiesDictionary that is a read only mapping from String to Object.
  Changed the PropertiesDictionary to extend the ReadOnlyPropertiesDictionary.
  Added copy constructors to both to allows the dictionary to be initialised from another
  ReadOnlyPropertiesDictionary.
  
  Revision  Changes    Path
  1.6       +37 -85    logging-log4net/src/Util/PropertiesDictionary.cs
  
  Index: PropertiesDictionary.cs
  ===================================================================
  RCS file: /home/cvs/logging-log4net/src/Util/PropertiesDictionary.cs,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- PropertiesDictionary.cs	16 May 2004 21:04:49 -0000	1.5
  +++ PropertiesDictionary.cs	30 Jul 2004 17:32:55 -0000	1.6
  @@ -35,9 +35,9 @@
   	/// <author>Nicko Cadell</author>
   	/// <author>Gert Driesen</author>
   #if NETCF
  -	public class PropertiesDictionary : IDictionary
  +	public sealed class PropertiesDictionary : ReadOnlyPropertiesDictionary, IDictionary
   #else
  -	[Serializable] public sealed class PropertiesDictionary : ISerializable, IDictionary
  +	[Serializable] public sealed class PropertiesDictionary : ReadOnlyPropertiesDictionary, ISerializable, IDictionary
   #endif
   	{
   		#region Public Instance Constructors
  @@ -49,6 +49,14 @@
   		{
   		}
   
  +		/// <summary>
  +		/// Initializes a new instance of the <see cref="PropertiesDictionary" /> class.
  +		/// </summary>
  +		/// <param name="propertiesDictionary">properties to copy</param>
  +		public PropertiesDictionary(ReadOnlyPropertiesDictionary propertiesDictionary) : base(propertiesDictionary)
  +		{
  +		}
  +
   		#endregion Public Instance Constructors
   
   		#region Private Instance Constructors
  @@ -63,13 +71,8 @@
   		/// <remarks>
   		/// Because this class is sealed the serialization constructor is private.
   		/// </remarks>
  -		private PropertiesDictionary(SerializationInfo info, StreamingContext context)
  +		private PropertiesDictionary(SerializationInfo info, StreamingContext context) : base(info, context)
   		{
  -			foreach(SerializationEntry entry in info)
  -			{
  -				// The keys are stored as Xml encoded names
  -				m_ht[XmlConvert.DecodeName(entry.Name)] = entry.Value;
  -			}
   		}
   #endif
   
  @@ -78,18 +81,6 @@
   		#region Public Instance Properties
   
   		/// <summary>
  -		/// Gets the key names.
  -		/// </summary>
  -		/// <value>An array of key names.</value>
  -		/// <returns>An array of all the keys.</returns>
  -		public string[] GetKeys()
  -		{
  -			string[] keys = new String[m_ht.Count];
  -			m_ht.Keys.CopyTo(keys, 0);
  -			return keys;
  -		}
  -
  -		/// <summary>
   		/// Gets or sets the value of the  property with the specified key.
   		/// </summary>
   		/// <value>
  @@ -101,7 +92,7 @@
   		/// If it cannot be serialized it will be silently ignored if
   		/// a serialization operation is performed.
   		/// </remarks>
  -		public object this[string key]
  +		override public object this[string key]
   		{
   			get { return m_ht[key]; }
   			set { m_ht[key] = value; }
  @@ -109,37 +100,18 @@
   
   		#endregion Public Instance Properties
   
  -		#region Implementation of ISerializable
  +		#region Public Instance Methods
   
  -#if !NETCF
   		/// <summary>
  -		/// Serializes this object into the <see cref="SerializationInfo" /> provided.
  +		/// Remove the entry with the specified key from this dictionary
   		/// </summary>
  -		/// <param name="info">The <see cref="SerializationInfo" /> to populate with data.</param>
  -		/// <param name="context">The destination for this serialization.</param>
  -		[System.Security.Permissions.SecurityPermissionAttribute(System.Security.Permissions.SecurityAction.Demand, SerializationFormatter=true)]
  -		public void GetObjectData(SerializationInfo info, StreamingContext context)
  +		/// <param name="key">the key for the entry to remove</param>
  +		public void Remove(string key)
   		{
  -			foreach(DictionaryEntry entry in m_ht)
  -			{
  -				// If value is serializable then we add it to the list
  -				if (entry.Value.GetType().IsSerializable)
  -				{
  -					// Store the keys as an Xml encoded local name as it may contain colons (':') 
  -					// which are not escaped by the Xml Serialisation framework
  -					info.AddValue(XmlConvert.EncodeLocalName(entry.Key as string), entry.Value);
  -				}
  -			}
  +			m_ht.Remove(key);
   		}
  -#endif
  -
  -		#endregion Implementation of ISerializable
  -
  -		#region Private Instance Fields
   
  -		private Hashtable m_ht = new Hashtable();
  -
  -		#endregion Private Instance Fields
  +		#endregion Public Instance Methods
   
   		#region Implementation of IDictionary
   
  @@ -173,7 +145,7 @@
   		/// <summary>
   		/// Remove all properties from the properties collection
   		/// </summary>
  -		public void Clear()
  +		public override void Clear()
   		{
   			m_ht.Clear();
   		}
  @@ -185,7 +157,10 @@
   		/// <param name="value"></param>
   		void IDictionary.Add(object key, object value)
   		{
  -			if (!(key is string)) throw new ArgumentException("key must be a string");
  +			if (!(key is string))
  +			{
  +				throw new ArgumentException("key must be a string", "key");
  +			}
   			m_ht.Add(key, value);
   		}
   
  @@ -194,10 +169,7 @@
   		/// </summary>
   		bool IDictionary.IsReadOnly
   		{
  -			get
  -			{
  -				return m_ht.IsReadOnly;
  -			}
  +			get { return false; }
   		}
   
   		/// <summary>
  @@ -207,12 +179,18 @@
   		{
   			get
   			{
  -				if (!(key is string)) throw new ArgumentException("key must be a string");
  +				if (!(key is string))
  +				{
  +					throw new ArgumentException("key must be a string", "key");
  +				}
   				return m_ht[key];
   			}
   			set
   			{
  -				if (!(key is string)) throw new ArgumentException("key must be a string");
  +				if (!(key is string))
  +				{
  +					throw new ArgumentException("key must be a string", "key");
  +				}
   				m_ht[key] = value;
   			}
   		}
  @@ -222,10 +200,7 @@
   		/// </summary>
   		ICollection IDictionary.Values
   		{
  -			get
  -			{
  -				return m_ht.Values;
  -			}
  +			get { return m_ht.Values; }
   		}
   
   		/// <summary>
  @@ -233,10 +208,7 @@
   		/// </summary>
   		ICollection IDictionary.Keys
   		{
  -			get
  -			{
  -				return m_ht.Keys;
  -			}
  +			get { return m_ht.Keys; }
   		}
   
   		/// <summary>
  @@ -244,10 +216,7 @@
   		/// </summary>
   		bool IDictionary.IsFixedSize
   		{
  -			get
  -			{
  -				return m_ht.IsFixedSize;
  -			}
  +			get { return false; }
   		}
   
   		#endregion
  @@ -269,21 +238,7 @@
   		/// </summary>
   		bool ICollection.IsSynchronized
   		{
  -			get
  -			{
  -				return m_ht.IsSynchronized;
  -			}
  -		}
  -
  -		/// <summary>
  -		/// The number of properties in this collection
  -		/// </summary>
  -		public int Count
  -		{
  -			get
  -			{
  -				return m_ht.Count;
  -			}
  +			get { return m_ht.IsSynchronized; }
   		}
   
   		/// <summary>
  @@ -291,10 +246,7 @@
   		/// </summary>
   		object ICollection.SyncRoot
   		{
  -			get
  -			{
  -				return m_ht.SyncRoot;
  -			}
  +			get { return m_ht.SyncRoot; }
   		}
   
   		#endregion
  
  
  
  1.1                  logging-log4net/src/Util/ReadOnlyPropertiesDictionary.cs
  
  Index: ReadOnlyPropertiesDictionary.cs
  ===================================================================
  #region Copyright & License
  //
  // Copyright 2001-2004 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  // http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  //
  #endregion
  
  using System;
  using System.Collections;
  #if !NETCF
  using System.Runtime.Serialization;
  using System.Xml;
  #endif
  
  namespace log4net.Util
  {
  	/// <summary>
  	/// String keyed object map that is read only.
  	/// </summary>
  	/// <remarks>
  	/// Only member objects that are serializable will
  	/// be serialized along with this collection.
  	/// </remarks>
  	/// <author>Nicko Cadell</author>
  	/// <author>Gert Driesen</author>
  #if NETCF
  	public class ReadOnlyPropertiesDictionary : IDictionary
  #else
  	[Serializable] public class ReadOnlyPropertiesDictionary : ISerializable, IDictionary
  #endif
  	{
  		#region Private Instance Fields
  
  		/// <summary>
  		/// The Hashtable used to store the properties data
  		/// </summary>
  		protected Hashtable m_ht = new Hashtable();
  
  		#endregion Private Instance Fields
  
  		#region Public Instance Constructors
  
  		/// <summary>
  		/// Initializes a new instance of the <see cref="ReadOnlyPropertiesDictionary" /> class.
  		/// </summary>
  		public ReadOnlyPropertiesDictionary()
  		{
  		}
  
  		/// <summary>
  		/// Initializes a new instance of the <see cref="ReadOnlyPropertiesDictionary" /> class.
  		/// </summary>
  		/// <param name="propertiesDictionary">properties to copy</param>
  		public ReadOnlyPropertiesDictionary(ReadOnlyPropertiesDictionary propertiesDictionary)
  		{
  			foreach(DictionaryEntry entry in propertiesDictionary)
  			{
  				m_ht.Add(entry.Key, entry.Value);
  			}
  		}
  
  		#endregion Public Instance Constructors
  
  		#region Private Instance Constructors
  
  #if !NETCF
  		/// <summary>
  		/// Initializes a new instance of the <see cref="ReadOnlyPropertiesDictionary" /> class 
  		/// with serialized data.
  		/// </summary>
  		/// <param name="info">The <see cref="SerializationInfo" /> that holds the serialized object data.</param>
  		/// <param name="context">The <see cref="StreamingContext" /> that contains contextual information about the source or destination.</param>
  		protected ReadOnlyPropertiesDictionary(SerializationInfo info, StreamingContext context)
  		{
  			foreach(SerializationEntry entry in info)
  			{
  				// The keys are stored as Xml encoded names
  				m_ht[XmlConvert.DecodeName(entry.Name)] = entry.Value;
  			}
  		}
  #endif
  
  		#endregion Protected Instance Constructors
  
  		#region Public Instance Properties
  
  		/// <summary>
  		/// Gets the key names.
  		/// </summary>
  		/// <value>An array of key names.</value>
  		/// <returns>An array of all the keys.</returns>
  		public string[] GetKeys()
  		{
  			string[] keys = new String[m_ht.Count];
  			m_ht.Keys.CopyTo(keys, 0);
  			return keys;
  		}
  
  		/// <summary>
  		/// Gets or sets the value of the  property with the specified key.
  		/// </summary>
  		/// <value>
  		/// The value of the property with the specified key.
  		/// </value>
  		/// <param name="key">The key of the property to get or set.</param>
  		/// <remarks>
  		/// The property value will only be serialized if it is serializable.
  		/// If it cannot be serialized it will be silently ignored if
  		/// a serialization operation is performed.
  		/// </remarks>
  		public virtual object this[string key]
  		{
  			get { return m_ht[key]; }
  			set { throw new NotSupportedException("This is a Read Only Dictionary and can not be modified"); }
  		}
  
  		#endregion Public Instance Properties
  
  		#region Public Instance Methods
  
  		/// <summary>
  		/// Test if the dictionary contains a specified key
  		/// </summary>
  		/// <param name="key">the key to look for</param>
  		/// <returns>true if the dictionary contains the specified key</returns>
  		public bool Contains(string key)
  		{
  			return m_ht.Contains(key);
  		}
  
  		#endregion
  
  		#region Implementation of ISerializable
  
  #if !NETCF
  		/// <summary>
  		/// Serializes this object into the <see cref="SerializationInfo" /> provided.
  		/// </summary>
  		/// <param name="info">The <see cref="SerializationInfo" /> to populate with data.</param>
  		/// <param name="context">The destination for this serialization.</param>
  		[System.Security.Permissions.SecurityPermissionAttribute(System.Security.Permissions.SecurityAction.Demand, SerializationFormatter=true)]
  		public void GetObjectData(SerializationInfo info, StreamingContext context)
  		{
  			foreach(DictionaryEntry entry in m_ht)
  			{
  				// If value is serializable then we add it to the list
  				if (entry.Value.GetType().IsSerializable)
  				{
  					// Store the keys as an Xml encoded local name as it may contain colons (':') 
  					// which are not escaped by the Xml Serialization framework
  					info.AddValue(XmlConvert.EncodeLocalName(entry.Key as string), entry.Value);
  				}
  			}
  		}
  #endif
  
  		#endregion Implementation of ISerializable
  
  		#region Implementation of IDictionary
  
  		/// <summary>
  		/// See <see cref="IDictionary.GetEnumerator"/>
  		/// </summary>
  		IDictionaryEnumerator IDictionary.GetEnumerator()
  		{
  			return m_ht.GetEnumerator();
  		}
  
  		/// <summary>
  		/// See <see cref="IDictionary.Remove"/>
  		/// </summary>
  		/// <param name="key"></param>
  		void IDictionary.Remove(object key)
  		{
  			throw new NotSupportedException("This is a Read Only Dictionary and can not be modified");
  		}
  
  		/// <summary>
  		/// See <see cref="IDictionary.Contains"/>
  		/// </summary>
  		/// <param name="key"></param>
  		/// <returns></returns>
  		bool IDictionary.Contains(object key)
  		{
  			return m_ht.Contains(key);
  		}
  
  		/// <summary>
  		/// Remove all properties from the properties collection
  		/// </summary>
  		public virtual void Clear()
  		{
  			throw new NotSupportedException("This is a Read Only Dictionary and can not be modified");
  		}
  
  		/// <summary>
  		/// See <see cref="IDictionary.Add"/>
  		/// </summary>
  		/// <param name="key"></param>
  		/// <param name="value"></param>
  		void IDictionary.Add(object key, object value)
  		{
  			throw new NotSupportedException("This is a Read Only Dictionary and can not be modified");
  		}
  
  		/// <summary>
  		/// See <see cref="IDictionary.IsReadOnly"/>
  		/// </summary>
  		bool IDictionary.IsReadOnly
  		{
  			get { return true; }
  		}
  
  		/// <summary>
  		/// See <see cref="IDictionary.this"/>
  		/// </summary>
  		object IDictionary.this[object key]
  		{
  			get
  			{
  				if (!(key is string)) throw new ArgumentException("key must be a string");
  				return m_ht[key];
  			}
  			set
  			{
  				throw new NotSupportedException("This is a Read Only Dictionary and can not be modified");
  			}
  		}
  
  		/// <summary>
  		/// See <see cref="IDictionary.Values"/>
  		/// </summary>
  		ICollection IDictionary.Values
  		{
  			get { return m_ht.Values; }
  		}
  
  		/// <summary>
  		/// See <see cref="IDictionary.Keys"/>
  		/// </summary>
  		ICollection IDictionary.Keys
  		{
  			get { return m_ht.Keys; }
  		}
  
  		/// <summary>
  		/// See <see cref="IDictionary.IsFixedSize"/>
  		/// </summary>
  		bool IDictionary.IsFixedSize
  		{
  			get { return m_ht.IsFixedSize; }
  		}
  
  		#endregion
  
  		#region Implementation of ICollection
  
  		/// <summary>
  		/// See <see cref="ICollection.CopyTo"/>
  		/// </summary>
  		/// <param name="array"></param>
  		/// <param name="index"></param>
  		void ICollection.CopyTo(Array array, int index)
  		{
  			m_ht.CopyTo(array, index);
  		}
  
  		/// <summary>
  		/// See <see cref="ICollection.IsSynchronized"/>
  		/// </summary>
  		bool ICollection.IsSynchronized
  		{
  			get { return m_ht.IsSynchronized; }
  		}
  
  		/// <summary>
  		/// The number of properties in this collection
  		/// </summary>
  		public int Count
  		{
  			get { return m_ht.Count; }
  		}
  
  		/// <summary>
  		/// See <see cref="ICollection.SyncRoot"/>
  		/// </summary>
  		object ICollection.SyncRoot
  		{
  			get { return m_ht.SyncRoot; }
  		}
  
  		#endregion
  
  		#region Implementation of IEnumerable
  
  		/// <summary>
  		/// See <see cref="IEnumerable.GetEnumerator"/>
  		/// </summary>
  		IEnumerator IEnumerable.GetEnumerator()
  		{
  			return ((IEnumerable)m_ht).GetEnumerator();
  		}
  
  		#endregion
  	}
  }