New test.
[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 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34
35 using System;
36 using System.Collections;
37 using System.Diagnostics;
38 using System.Globalization;
39
40 namespace System.Diagnostics {
41
42         public class TraceListenerCollection : IList, ICollection, IEnumerable {
43
44                 private ArrayList listeners = ArrayList.Synchronized (new ArrayList (1));
45
46                 internal TraceListenerCollection ()
47                 {
48                         Add (new DefaultTraceListener ());
49                 }
50
51                 public int Count{
52                         get {return listeners.Count;}
53                 }
54
55                 public TraceListener this [string name] {
56                         get {
57                                 lock (listeners.SyncRoot) {
58                                         foreach (TraceListener listener in listeners) {
59                                                 if (listener.Name == name)
60                                                         return listener;
61                                         }
62                                 }
63                                 return null;
64                         }
65                 }
66
67                 public TraceListener this [int index] {
68                         get {return (TraceListener) listeners[index];}
69                         set {
70                                 InitializeListener (value);
71                                 listeners[index] = value;
72                         }
73                 }
74
75                 object IList.this [int index] {
76                         get {return listeners[index];}
77                         set {
78                                 TraceListener l = (TraceListener) value;
79                                 InitializeListener (l);
80                                 this[index] = l;
81                         }
82                 }
83
84                 bool ICollection.IsSynchronized {
85                         get {return listeners.IsSynchronized;}
86                 }
87
88                 object ICollection.SyncRoot {
89                         get {return listeners.SyncRoot;}
90                 }
91
92                 bool IList.IsFixedSize {
93                         get {return listeners.IsFixedSize;}
94                 }
95
96                 bool IList.IsReadOnly {
97                         get {return listeners.IsReadOnly;}
98                 }
99
100                 public int Add (TraceListener listener)
101                 {
102                         InitializeListener (listener);
103                         return listeners.Add (listener);
104                 }
105
106                 private void InitializeListener (TraceListener listener)
107                 {
108                         listener.IndentLevel = TraceImpl.IndentLevel;
109                         listener.IndentSize = TraceImpl.IndentSize;
110                 }
111
112                 private void InitializeRange (IList listeners)
113                 {
114                         int e = listeners.Count;
115                         for (int i = 0; i != e; ++i)
116                                 InitializeListener (
117                                         (TraceListener) listeners[i]);
118                 }
119
120                 public void AddRange (TraceListener[] value)
121                 {
122                         InitializeRange (value);
123                         listeners.AddRange (value);
124                 }
125
126                 public void AddRange (TraceListenerCollection value)
127                 {
128                         InitializeRange (value);
129                         listeners.AddRange (value.listeners);
130                 }
131
132                 public void Clear ()
133                 {
134                         listeners.Clear ();
135                 }
136
137                 public bool Contains (TraceListener listener)
138                 {
139                         return listeners.Contains (listener);
140                 }
141
142                 public void CopyTo (TraceListener[] listeners, int index)
143                 {
144                         listeners.CopyTo (listeners, index);
145                 }
146
147                 public IEnumerator GetEnumerator ()
148                 {
149                         return listeners.GetEnumerator ();
150                 }
151
152                 void ICollection.CopyTo (Array array, int index)
153                 {
154                         listeners.CopyTo (array, index);
155                 }
156
157                 int IList.Add (object value)
158                 {
159                         if (value is TraceListener)
160                                 return Add ((TraceListener) value);
161                         throw new NotSupportedException (Locale.GetText (
162                                 "You can only add TraceListener objects to the collection"));
163                 }
164
165                 bool IList.Contains (object value)
166                 {
167                         if (value is TraceListener)
168                                 return listeners.Contains (value);
169                         return false;
170                 }
171
172                 int IList.IndexOf (object value)
173                 {
174                         if (value is TraceListener)
175                                 return listeners.IndexOf (value);
176                         return -1;
177                 }
178
179                 void IList.Insert (int index, object value)
180                 {
181                         if (value is TraceListener) {
182                                 Insert (index, (TraceListener) value);
183                                 return;
184                         }
185                         throw new NotSupportedException (Locale.GetText (
186                                 "You can only insert TraceListener objects into the collection"));
187                 }
188
189                 void IList.Remove (object value)
190                 {
191                         if (value is TraceListener)
192                                 listeners.Remove (value);
193                 }
194
195                 public int IndexOf (TraceListener listener)
196                 {
197                         return listeners.IndexOf (listener);
198                 }
199
200                 public void Insert (int index, TraceListener listener)
201                 {
202                         InitializeListener (listener);
203                         listeners.Insert (index, listener);
204                 }
205
206                 public void Remove (string name)
207                 {
208                         TraceListener found = null;
209
210                         lock (listeners.SyncRoot) {
211                                 foreach (TraceListener listener in listeners) {
212                                         if (listener.Name == name) {
213                                                 found = listener;
214                                                 break;
215                                         }
216                                 }
217
218                                 if (found != null)
219                                         listeners.Remove (found);
220                                 else
221                                         throw new ArgumentException (Locale.GetText (
222                                                 "TraceListener " + name + " was not in the collection"));
223                         }
224                 }
225
226                 public void Remove (TraceListener listener)
227                 {
228                         listeners.Remove (listener);
229                 }
230
231                 public void RemoveAt (int index)
232                 {
233                         listeners.RemoveAt (index);
234                 }
235         }
236 }
237