Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.Configuration / System.Configuration / CommaDelimitedStringCollection.cs
1 //
2 // System.Configuration.CommaDelimitedStringCollection.cs
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
27 //
28
29 using System;
30 using System.Collections.Specialized;
31
32 namespace System.Configuration {
33
34         /* i really hate all these "new"s... maybe
35          * StringCollection marks these methods as virtual in
36          * 2.0? */
37         public sealed class CommaDelimitedStringCollection : StringCollection {
38
39                 bool modified;
40                 bool readOnly;
41                 int originalStringHash = 0;
42
43                 public bool IsModified {
44                         get { 
45                                 if (modified)
46                                         return true;
47
48                                 string str = ToString ();
49                                 if (str == null)
50                                         return false;
51
52                                 return str.GetHashCode () != originalStringHash;
53                         }
54                 }
55
56                 public new bool IsReadOnly {
57                         get { return readOnly; }
58                 }
59
60                 public new string this [int index] {
61                         get { return base [index]; }
62                         set {
63                                 if (readOnly) throw new ConfigurationErrorsException ("The configuration is read only");
64
65                                 base [index] = value;
66                                 modified = true;
67                         }
68                 }
69
70                 public new void Add (string value)
71                 {
72                         if (readOnly) throw new ConfigurationErrorsException ("The configuration is read only");
73
74                         base.Add (value);
75                         modified = true;
76                 }
77
78                 public new void AddRange (string[] range)
79                 {
80                         if (readOnly) throw new ConfigurationErrorsException ("The configuration is read only");
81
82                         base.AddRange (range);
83                         modified = true;
84                 }
85
86                 public new void Clear ()
87                 {
88                         if (readOnly) throw new ConfigurationErrorsException ("The configuration is read only");
89
90                         base.Clear ();
91                         modified = true;
92                 }
93
94                 public CommaDelimitedStringCollection Clone ()
95                 {
96                         CommaDelimitedStringCollection col = new CommaDelimitedStringCollection();
97                         string[] contents = new string[this.Count];
98                         CopyTo (contents, 0);
99                         
100                         col.AddRange (contents);
101                         col.originalStringHash = originalStringHash;
102
103                         return col;
104                 }
105
106                 public new void Insert (int index, string value)
107                 {
108                         if (readOnly) throw new ConfigurationErrorsException ("The configuration is read only");
109
110                         base.Insert (index, value);
111                         modified = true;
112                 }
113
114                 public new void Remove (string value)
115                 {
116                         if (readOnly) throw new ConfigurationErrorsException ("The configuration is read only");
117
118                         base.Remove (value);
119                         modified = true;
120                 }
121
122                 public void SetReadOnly ()
123                 {
124                         readOnly = true;
125                 }
126
127                 public override string ToString ()
128                 {
129                         if (this.Count == 0)
130                                 return null;
131
132                         string[] contents = new string[this.Count];
133
134                         CopyTo (contents, 0);
135
136                         return String.Join (",", contents);
137                 }
138
139                 internal void UpdateStringHash ()
140                 {
141                         string str = ToString ();
142                         if (str == null)
143                                 originalStringHash = 0;
144                         else
145                                 originalStringHash = str.GetHashCode ();
146                 }
147         }
148
149 }