Implement MONO_TRACE support.
[mono.git] / mcs / class / System / System.Diagnostics / TraceListenerCollection.cs
index d81ec132d610e2a10aeecf70979cbdeadf7456f6..8f7240b077453f55b865cd69ea80427c83d8eef8 100644 (file)
-//\r
-// System.Diagnostics.TraceListenerCollection.cs\r
-//\r
-// Author: John R. Hicks <angryjohn69@nc.rr.com>\r
-//\r
-// (C) 2001\r
-//\r
-using System;\r
-using System.Collections;\r
-\r
-namespace System.Diagnostics\r
-{\r
-       \r
-       /// <summary>\r
-       /// Provides a list of TraceListener objects.\r
-       /// </summary>\r
-       public class TraceListenerCollection : IList, ICollection,\r
-               IEnumerable\r
-       {\r
-               private int count;\r
-               private bool isReadOnly;\r
-               private bool isFixedSize;\r
-               private bool isSynchronized;\r
-               private ArrayList listeners;\r
-               \r
-               /// <summary>\r
-               /// Gets the first TraceListener in the list with the\r
-               /// specified name.\r
-               /// </summary>\r
-               public TraceListener this[string name]\r
-               {\r
-                       get\r
-                       {\r
-                               int index = listeners.IndexOf(name);\r
-                               return (TraceListener)listeners[index];\r
-                       }\r
-               }\r
-               \r
-               public object this[int index]\r
-               {\r
-                       get\r
-                       {\r
-                               return listeners[index];\r
-                       }\r
-                       set\r
-                       {\r
-                               listeners[index] = value;\r
-                       }\r
-               }\r
-               \r
-               internal TraceListenerCollection()\r
-               {\r
-                       count = 0;\r
-                       isReadOnly = false;\r
-                       isFixedSize = false;\r
-                       isSynchronized = false;\r
-                       listeners = new ArrayList();\r
-                       listeners.Add(new DefaultTraceListener());\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Returns the number of items in the list\r
-               /// </summary>\r
-               /// <value>\r
-               /// The number of items\r
-               /// </value>\r
-               public int Count\r
-               {\r
-                       get\r
-                       {\r
-                               return count;\r
-                       }\r
-                       set\r
-                       {\r
-                               count = value;\r
-                       }\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Adds a TraceListener to the list.\r
-               /// </summary>\r
-               /// <param name="listener">\r
-               /// The TraceListener being added to the list.\r
-               /// </param>\r
-               /// <return>\r
-               /// The position in the list where the listener was inserted.\r
-               /// </return>\r
-               public int Add(object listener)\r
-               {\r
-                       return listeners.Add(listener);\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Adds an array of TraceListeners to the list.\r
-               /// </summary>\r
-               /// <param name="value">\r
-               /// Array of TraceListeners to add\r
-               /// </param>\r
-               public void AddRange(TraceListener[] value)\r
-               {\r
-                       listeners.AddRange(value);\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Adds the contents of another TraceListenerCollection to this one.\r
-               /// </summary>\r
-               /// <param name="value">\r
-               /// The TraceListenerCollection to copy values from.\r
-               /// </param>\r
-               [MonoTODO]\r
-               public void AddRange(TraceListenerCollection value)\r
-               {\r
-                       // TODO: use an iterator to copy the objects.\r
-                       for(int i = 0; i < value.count; i++)\r
-                       {\r
-                               listeners.Add(value[i]);\r
-                       }\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Clears all listeners from the list.\r
-               /// </summary>\r
-               public void Clear()\r
-               {\r
-                       listeners.Clear();\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Checks to see if the list contains the specified listener\r
-               /// </summary>\r
-               /// <param name="listener">\r
-               /// The listener to search for.\r
-               /// </param>\r
-               /// <return>\r
-               /// true if list contains listener; false otherwise.\r
-               /// </return>\r
-               public bool Contains(object listener)\r
-               {\r
-                       return listeners.Contains(listener);\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Copies a section of the current TraceListenerCollection to\r
-               /// the specified array at the specified index.\r
-               /// </summary>\r
-               /// <param name="listeners">\r
-               /// Array to copy listeners to.\r
-               /// </param>\r
-               /// <param name="index">\r
-               /// Starting index of copy\r
-               /// </param>\r
-               public void CopyTo(Array listeners, int index)\r
-               {\r
-                       try {\r
-                               this.listeners.CopyTo(listeners, index);\r
-                       } catch(Exception e) {\r
-                               \r
-                       }\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Returns an enumerator for the list of listeners.\r
-               /// </summary>\r
-               /// <return>\r
-               /// List Enumerator of type IEnumerator.\r
-               /// </return>\r
-               public IEnumerator GetEnumerator()\r
-               {\r
-                       return listeners.GetEnumerator();\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Gets the index of the specified listener.\r
-               /// </summary>\r
-               /// <param name="listener">\r
-               /// The listener to search for\r
-               /// </param>\r
-               /// <return>\r
-               /// The index of the listener in the list, if it exists.\r
-               /// </return>\r
-               [MonoTODO]\r
-               public int IndexOf(object listener)\r
-               {\r
-                       // TODO: we may have to add in some type-checking here.\r
-                       return listeners.IndexOf(listener);\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Inserts the specified listener into the list at the specified index.\r
-               /// </summary>\r
-               /// <param name="index">\r
-               /// Location in the list to insert the listener.\r
-               /// </param>\r
-               /// <param name="listener">\r
-               /// The TraceListener to insert into the list.\r
-               /// </param>\r
-               public void Insert(int index, object listener)\r
-               {\r
-                       listeners.Insert(index, listener);\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Removes the listener with the specified name from the list, if it exists.\r
-               /// </summary>\r
-               /// <param name="name">\r
-               /// Name of listener to remove\r
-               /// </param>\r
-               [MonoTODO]\r
-               public void Remove(object name)\r
-               {\r
-                       try {\r
-                               // TODO: may use an enumerator here.\r
-                               for(int i = 0; i < listeners.Count; i++)\r
-                               {\r
-                                       TraceListener listener = (TraceListener) listeners[i];\r
-                                       if(listener == null)\r
-                                               continue;\r
-                                       if(listener.Name.Equals(name))\r
-                                               listeners.Remove(listener);\r
-                               }\r
-                       } catch(Exception e) {\r
-                               throw new ArgumentException("Listener is not in list.");\r
-                       }\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Removes the specified listener from the list\r
-               /// </summary>\r
-               /// <param name="listener">\r
-               /// The listener to remove.\r
-               /// </param>\r
-               public void Remove(TraceListener listener)\r
-               {\r
-                       listeners.Remove(listener);\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Removes the listener at the specified index.\r
-               /// </summary>\r
-               /// <param name="index">\r
-               /// Location of the listener to remove.\r
-               /// </param>\r
-               public void RemoveAt(int index)\r
-               {\r
-                       try {\r
-                               listeners.RemoveAt(index);\r
-                       } catch(Exception e) {\r
-                               throw new ArgumentOutOfRangeException(e.ToString());\r
-                       }\r
-               }\r
-               \r
-               ~TraceListenerCollection()\r
-               {\r
-                       listeners = null;\r
-               }\r
-               \r
-               public bool IsReadOnly\r
-               {\r
-                       get\r
-                       {\r
-                               return isReadOnly;\r
-                       }\r
-               }\r
-               \r
-               public bool IsFixedSize\r
-               {\r
-                       get\r
-                       {\r
-                               return isFixedSize;\r
-                       }\r
-               }\r
-               \r
-               public object SyncRoot\r
-               {\r
-                       get\r
-                       {\r
-                               return this;\r
-                       }\r
-               }\r
-               \r
-               public bool IsSynchronized\r
-               {\r
-                       get\r
-                       {\r
-                               return isSynchronized;\r
-                       }\r
-               }\r
-       }\r
-}\r
+//
+// System.Diagnostics.TraceListenerCollection.cs
+//
+// Authors:
+//   Jonathan Pryor (jonpryor@vt.edu)
+//
+// Comments from John R. Hicks <angryjohn69@nc.rr.com> original implementation 
+// can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
+//
+// (C) 2002 Jonathan Pryor
+//
+
+
+using System;
+using System.Collections;
+using System.Diagnostics;
+using System.Globalization;
+
+namespace System.Diagnostics {
+
+       public class TraceListenerCollection : IList, ICollection, IEnumerable {
+
+               private ArrayList listeners = new ArrayList ();
+
+               internal TraceListenerCollection ()
+               {
+                       Add (new DefaultTraceListener ());
+               }
+
+               public int Count{
+                       get {return listeners.Count;}
+               }
+
+               public TraceListener this [string name] {
+                       get {
+                               foreach (TraceListener listener in listeners) {
+                                       if (listener.Name == name)
+                                               return listener;
+                               }
+                               return null;
+                       }
+               }
+
+               public TraceListener this [int index] {
+                       get {return (TraceListener) listeners[index];}
+                       set {listeners[index] = value;}
+               }
+
+               object IList.this [int index] {
+                       get {return listeners[index];}
+                       set {((IList)this).Insert (index, value);}
+               }
+
+               bool ICollection.IsSynchronized {
+                       get {return listeners.IsSynchronized;}
+               }
+
+               object ICollection.SyncRoot {
+                       get {return listeners.SyncRoot;}
+               }
+
+               bool IList.IsFixedSize {
+                       get {return listeners.IsFixedSize;}
+               }
+
+               bool IList.IsReadOnly {
+                       get {return listeners.IsReadOnly;}
+               }
+
+               public int Add (TraceListener listener)
+               {
+                       return listeners.Add (listener);
+               }
+
+               public void AddRange (TraceListener[] value)
+               {
+                       listeners.AddRange (value);
+               }
+
+               public void AddRange (TraceListenerCollection value)
+               {
+                       listeners.AddRange (value.listeners);
+               }
+
+               public void Clear ()
+               {
+                       listeners.Clear ();
+               }
+
+               public bool Contains (TraceListener listener)
+               {
+                       return listeners.Contains (listener);
+               }
+
+               public void CopyTo (TraceListener[] listeners, int index)
+               {
+                       listeners.CopyTo (listeners, index);
+               }
+
+               public IEnumerator GetEnumerator ()
+               {
+                       return listeners.GetEnumerator ();
+               }
+
+               void ICollection.CopyTo (Array array, int index)
+               {
+                       listeners.CopyTo (array, index);
+               }
+
+               int IList.Add (object value)
+               {
+                       if (value is TraceListener)
+                               return listeners.Add (value);
+                       throw new NotSupportedException (Locale.GetText (
+                               "You can only add TraceListener objects to the collection"));
+               }
+
+               bool IList.Contains (object value)
+               {
+                       if (value is TraceListener)
+                               return listeners.Contains (value);
+                       return false;
+               }
+
+               int IList.IndexOf (object value)
+               {
+                       if (value is TraceListener)
+                               return listeners.IndexOf (value);
+                       return -1;
+               }
+
+               void IList.Insert (int index, object value)
+               {
+                       if (value is TraceListener) {
+                               listeners.Insert (index, value);
+                               return;
+                       }
+                       throw new NotSupportedException (Locale.GetText (
+                               "You can only insert TraceListener objects into the collection"));
+               }
+
+               void IList.Remove (object value)
+               {
+                       if (value is TraceListener)
+                               listeners.Remove (value);
+               }
+
+               public int IndexOf (TraceListener listener)
+               {
+                       return listeners.IndexOf (listener);
+               }
+
+               public void Insert (int index, TraceListener listener)
+               {
+                       listeners.Insert (index, listener);
+               }
+
+               public void Remove (string name)
+               {
+                       TraceListener found = null;
+
+                       foreach (TraceListener listener in listeners) {
+                               if (listener.Name == name) {
+                                       found = listener;
+                                       break;
+                               }
+                       }
+
+                       if (found != null)
+                               listeners.Remove (found);
+                       else
+                               throw new ArgumentException (Locale.GetText (
+                                       "TraceListener " + name + " was not in the collection"));
+               }
+
+               public void Remove (TraceListener listener)
+               {
+                       listeners.Remove (listener);
+               }
+
+               public void RemoveAt (int index)
+               {
+                       listeners.RemoveAt (index);
+               }
+       }
+}
+