Add MIT license to System.dll
[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 = new ArrayList ();
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                                 foreach (TraceListener listener in listeners) {
58                                         if (listener.Name == name)
59                                                 return listener;
60                                 }
61                                 return null;
62                         }
63                 }
64
65                 public TraceListener this [int index] {
66                         get {return (TraceListener) listeners[index];}
67                         set {
68                                 InitializeListener (value);
69                                 listeners[index] = value;
70                         }
71                 }
72
73                 object IList.this [int index] {
74                         get {return listeners[index];}
75                         set {
76                                 this[index] = (TraceListener) value;
77                         }
78                 }
79
80                 bool ICollection.IsSynchronized {
81                         get {return listeners.IsSynchronized;}
82                 }
83
84                 object ICollection.SyncRoot {
85                         get {return listeners.SyncRoot;}
86                 }
87
88                 bool IList.IsFixedSize {
89                         get {return listeners.IsFixedSize;}
90                 }
91
92                 bool IList.IsReadOnly {
93                         get {return listeners.IsReadOnly;}
94                 }
95
96                 public int Add (TraceListener listener)
97                 {
98                         InitializeListener (listener);
99                         return listeners.Add (listener);
100                 }
101
102                 private void InitializeListener (TraceListener listener)
103                 {
104                         listener.IndentLevel = TraceImpl.IndentLevel;
105                         listener.IndentSize = TraceImpl.IndentSize;
106                 }
107
108                 private void InitializeRange (IList listeners)
109                 {
110                         int e = listeners.Count;
111                         for (int i = 0; i != e; ++i)
112                                 InitializeListener (
113                                         (TraceListener) listeners[i]);
114                 }
115
116                 public void AddRange (TraceListener[] value)
117                 {
118                         InitializeRange (value);
119                         listeners.AddRange (value);
120                 }
121
122                 public void AddRange (TraceListenerCollection value)
123                 {
124                         InitializeRange (value);
125                         listeners.AddRange (value.listeners);
126                 }
127
128                 public void Clear ()
129                 {
130                         listeners.Clear ();
131                 }
132
133                 public bool Contains (TraceListener listener)
134                 {
135                         return listeners.Contains (listener);
136                 }
137
138                 public void CopyTo (TraceListener[] listeners, int index)
139                 {
140                         listeners.CopyTo (listeners, index);
141                 }
142
143                 public IEnumerator GetEnumerator ()
144                 {
145                         return listeners.GetEnumerator ();
146                 }
147
148                 void ICollection.CopyTo (Array array, int index)
149                 {
150                         listeners.CopyTo (array, index);
151                 }
152
153                 int IList.Add (object value)
154                 {
155                         if (value is TraceListener)
156                                 return Add ((TraceListener) value);
157                         throw new NotSupportedException (Locale.GetText (
158                                 "You can only add TraceListener objects to the collection"));
159                 }
160
161                 bool IList.Contains (object value)
162                 {
163                         if (value is TraceListener)
164                                 return listeners.Contains (value);
165                         return false;
166                 }
167
168                 int IList.IndexOf (object value)
169                 {
170                         if (value is TraceListener)
171                                 return listeners.IndexOf (value);
172                         return -1;
173                 }
174
175                 void IList.Insert (int index, object value)
176                 {
177                         if (value is TraceListener) {
178                                 Insert (index, (TraceListener) value);
179                                 return;
180                         }
181                         throw new NotSupportedException (Locale.GetText (
182                                 "You can only insert TraceListener objects into the collection"));
183                 }
184
185                 void IList.Remove (object value)
186                 {
187                         if (value is TraceListener)
188                                 listeners.Remove (value);
189                 }
190
191                 public int IndexOf (TraceListener listener)
192                 {
193                         return listeners.IndexOf (listener);
194                 }
195
196                 public void Insert (int index, TraceListener listener)
197                 {
198                         InitializeListener (listener);
199                         listeners.Insert (index, listener);
200                 }
201
202                 public void Remove (string name)
203                 {
204                         TraceListener found = null;
205
206                         foreach (TraceListener listener in listeners) {
207                                 if (listener.Name == name) {
208                                         found = listener;
209                                         break;
210                                 }
211                         }
212
213                         if (found != null)
214                                 listeners.Remove (found);
215                         else
216                                 throw new ArgumentException (Locale.GetText (
217                                         "TraceListener " + name + " was not in the collection"));
218                 }
219
220                 public void Remove (TraceListener listener)
221                 {
222                         listeners.Remove (listener);
223                 }
224
225                 public void RemoveAt (int index)
226                 {
227                         listeners.RemoveAt (index);
228                 }
229         }
230 }
231