8f7240b077453f55b865cd69ea80427c83d8eef8
[mono.git] / mcs / class / System / System.Diagnostics / TraceListenerCollection.cs
1 //
2 // System.Diagnostics.TraceListenerCollection.cs
3 //
4 // Authors:
5 //   Jonathan Pryor (jonpryor@vt.edu)
6 //
7 // Comments from John R. Hicks <angryjohn69@nc.rr.com> original implementation 
8 // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
9 //
10 // (C) 2002 Jonathan Pryor
11 //
12
13
14 using System;
15 using System.Collections;
16 using System.Diagnostics;
17 using System.Globalization;
18
19 namespace System.Diagnostics {
20
21         public class TraceListenerCollection : IList, ICollection, IEnumerable {
22
23                 private ArrayList listeners = new ArrayList ();
24
25                 internal TraceListenerCollection ()
26                 {
27                         Add (new DefaultTraceListener ());
28                 }
29
30                 public int Count{
31                         get {return listeners.Count;}
32                 }
33
34                 public TraceListener this [string name] {
35                         get {
36                                 foreach (TraceListener listener in listeners) {
37                                         if (listener.Name == name)
38                                                 return listener;
39                                 }
40                                 return null;
41                         }
42                 }
43
44                 public TraceListener this [int index] {
45                         get {return (TraceListener) listeners[index];}
46                         set {listeners[index] = value;}
47                 }
48
49                 object IList.this [int index] {
50                         get {return listeners[index];}
51                         set {((IList)this).Insert (index, value);}
52                 }
53
54                 bool ICollection.IsSynchronized {
55                         get {return listeners.IsSynchronized;}
56                 }
57
58                 object ICollection.SyncRoot {
59                         get {return listeners.SyncRoot;}
60                 }
61
62                 bool IList.IsFixedSize {
63                         get {return listeners.IsFixedSize;}
64                 }
65
66                 bool IList.IsReadOnly {
67                         get {return listeners.IsReadOnly;}
68                 }
69
70                 public int Add (TraceListener listener)
71                 {
72                         return listeners.Add (listener);
73                 }
74
75                 public void AddRange (TraceListener[] value)
76                 {
77                         listeners.AddRange (value);
78                 }
79
80                 public void AddRange (TraceListenerCollection value)
81                 {
82                         listeners.AddRange (value.listeners);
83                 }
84
85                 public void Clear ()
86                 {
87                         listeners.Clear ();
88                 }
89
90                 public bool Contains (TraceListener listener)
91                 {
92                         return listeners.Contains (listener);
93                 }
94
95                 public void CopyTo (TraceListener[] listeners, int index)
96                 {
97                         listeners.CopyTo (listeners, index);
98                 }
99
100                 public IEnumerator GetEnumerator ()
101                 {
102                         return listeners.GetEnumerator ();
103                 }
104
105                 void ICollection.CopyTo (Array array, int index)
106                 {
107                         listeners.CopyTo (array, index);
108                 }
109
110                 int IList.Add (object value)
111                 {
112                         if (value is TraceListener)
113                                 return listeners.Add (value);
114                         throw new NotSupportedException (Locale.GetText (
115                                 "You can only add TraceListener objects to the collection"));
116                 }
117
118                 bool IList.Contains (object value)
119                 {
120                         if (value is TraceListener)
121                                 return listeners.Contains (value);
122                         return false;
123                 }
124
125                 int IList.IndexOf (object value)
126                 {
127                         if (value is TraceListener)
128                                 return listeners.IndexOf (value);
129                         return -1;
130                 }
131
132                 void IList.Insert (int index, object value)
133                 {
134                         if (value is TraceListener) {
135                                 listeners.Insert (index, value);
136                                 return;
137                         }
138                         throw new NotSupportedException (Locale.GetText (
139                                 "You can only insert TraceListener objects into the collection"));
140                 }
141
142                 void IList.Remove (object value)
143                 {
144                         if (value is TraceListener)
145                                 listeners.Remove (value);
146                 }
147
148                 public int IndexOf (TraceListener listener)
149                 {
150                         return listeners.IndexOf (listener);
151                 }
152
153                 public void Insert (int index, TraceListener listener)
154                 {
155                         listeners.Insert (index, listener);
156                 }
157
158                 public void Remove (string name)
159                 {
160                         TraceListener found = null;
161
162                         foreach (TraceListener listener in listeners) {
163                                 if (listener.Name == name) {
164                                         found = listener;
165                                         break;
166                                 }
167                         }
168
169                         if (found != null)
170                                 listeners.Remove (found);
171                         else
172                                 throw new ArgumentException (Locale.GetText (
173                                         "TraceListener " + name + " was not in the collection"));
174                 }
175
176                 public void Remove (TraceListener listener)
177                 {
178                         listeners.Remove (listener);
179                 }
180
181                 public void RemoveAt (int index)
182                 {
183                         listeners.RemoveAt (index);
184                 }
185         }
186 }
187