2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / nunit20 / util / SettingsGroup.cs
1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
3 '
4 ' Copyright  2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright  2000-2002 Philip A. Craig
6 '
7 ' This software is provided 'as-is', without any express or implied warranty. In no 
8 ' event will the authors be held liable for any damages arising from the use of this 
9 ' software.
10
11 ' Permission is granted to anyone to use this software for any purpose, including 
12 ' commercial applications, and to alter it and redistribute it freely, subject to the 
13 ' following restrictions:
14 '
15 ' 1. The origin of this software must not be misrepresented; you must not claim that 
16 ' you wrote the original software. If you use this software in a product, an 
17 ' acknowledgment (see the following) in the product documentation is required.
18 '
19 ' Portions Copyright  2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright  2000-2002 Philip A. Craig
21 '
22 ' 2. Altered source versions must be plainly marked as such, and must not be 
23 ' misrepresented as being the original software.
24 '
25 ' 3. This notice may not be removed or altered from any source distribution.
26 '
27 '***********************************************************************************/
28 #endregion
29
30 namespace NUnit.Util
31 {
32         using System;
33         using System.Collections;
34
35         /// <summary>
36         /// SettingsGroup is the base class representing a group
37         /// of user or system settings. A pimpl idiom is used
38         /// to provide implementation-independence.
39         /// </summary>
40         public class SettingsGroup : IDisposable
41         {
42                 #region Instance Variables
43                 /// <summary>
44                 /// The name of this group of settings
45                 /// </summary>
46                 private string name;
47
48                 /// <summary>
49                 /// If not null, the storage implementation holding the group settings.
50                 /// </summary>
51                 private SettingsStorage storageImpl;
52                 
53                 /// <summary>
54                 /// If not null, the settings group that contains this one.
55                 /// </summary>
56                 private SettingsGroup parentSettings;
57
58                 #endregion
59
60                 #region Construction and Disposal
61
62                 /// <summary>
63                 /// Construct a settings group based on a storage implementation.
64                 /// </summary>
65                 /// <param name="name">Name of the group</param>
66                 /// <param name="storageImpl">Storage for the group settings</param>
67                 public SettingsGroup( string name, SettingsStorage storageImpl )
68                 {
69                         this.name = name;
70                         this.storageImpl = storageImpl;
71                 }
72
73                 /// <summary>
74                 /// Construct a settings group based on a parent group that contains it.
75                 /// </summary>
76                 /// <param name="name">Name of the group</param>
77                 /// <param name="parentSettings">Containing  group</param>
78                 public SettingsGroup( string name, SettingsGroup parentSettings )
79                 {
80                         this.name = name;
81                         this.parentSettings = parentSettings;
82                         this.storageImpl = parentSettings.Storage.MakeChildStorage( name );
83                 }
84
85                 /// <summary>
86                 /// Dispose of this group by disposing of it's storage implementation
87                 /// </summary>
88                 public void Dispose()
89                 {
90                         if ( storageImpl != null )
91                         {
92                                 storageImpl.Dispose();
93                                 storageImpl = null;
94                         }
95                 }
96
97                 #endregion
98
99                 #region Properties
100
101                 /// <summary>
102                 /// The name of the group
103                 /// </summary>
104                 public string Name
105                 {
106                         get { return name; }
107                 }
108
109                 /// <summary>
110                 /// The storage used for the group settings
111                 /// </summary>
112                 public SettingsStorage Storage
113                 {
114                         get { return storageImpl; }
115                 }
116
117                 /// <summary>
118                 /// The number of settings in this group
119                 /// </summary>
120                 public int SettingsCount
121                 {
122                         get { return storageImpl.SettingsCount; }
123                 }
124
125                 #endregion
126
127                 #region Methods
128
129                 /// <summary>
130                 /// Clear all settings and subgroups in this group
131                 /// </summary>
132                 public virtual void Clear()
133                 {
134                         storageImpl.Clear();
135                 }
136
137                 /// <summary>
138                 /// Load the value of one of the group's settings
139                 /// </summary>
140                 /// <param name="settingName">Name of setting to load</param>
141                 /// <returns>Value of the setting or null</returns>
142                 public object LoadSetting( string settingName )
143                 {
144                         return storageImpl.LoadSetting( settingName );
145                 }
146
147                 /// <summary>
148                 /// Load the value of one of the group's integer settings
149                 /// in a type-safe manner.
150                 /// </summary>
151                 /// <param name="settingName">Name of setting to load</param>
152                 /// <returns>Value of the setting or null</returns>
153                 public int LoadIntSetting( string settingName )
154                 {
155                         return storageImpl.LoadIntSetting( settingName );
156                 }
157
158                 /// <summary>
159                 /// Load the value of one of the group's string settings
160                 /// in a type-safe manner.
161                 /// </summary>
162                 /// <param name="settingName">Name of setting to load</param>
163                 /// <returns>Value of the setting or null</returns>
164                 public string LoadStringSetting( string settingName )
165                 {
166                         return storageImpl.LoadStringSetting( settingName );
167                 }
168
169                 /// <summary>
170                 /// Load the value of one of the group's settings or return a default value
171                 /// </summary>
172                 /// <param name="settingName">Name of setting to load</param>
173                 /// <param name="defaultValue">Value to return if the seeting is not present</param>
174                 /// <returns>Value of the setting or the default</returns>
175                 public object LoadSetting( string settingName, object defaultValue )
176                 {
177                         return storageImpl.LoadSetting( settingName, defaultValue );
178                 }
179
180                 /// <summary>
181                 /// Load the value of one of the group's integer settings
182                 /// in a type-safe manner or return a default value
183                 /// </summary>
184                 /// <param name="settingName">Name of setting to load</param>
185                 /// <param name="defaultValue">Value to return if the seeting is not present</param>
186                 /// <returns>Value of the setting or the default</returns>
187                 public int LoadIntSetting( string settingName, int defaultValue )
188                 {
189                         return storageImpl.LoadIntSetting( settingName, defaultValue );
190                 }
191
192                 /// <summary>
193                 /// Load the value of one of the group's string settings
194                 /// in a type-safe manner or return a default value
195                 /// </summary>
196                 /// <param name="settingName">Name of setting to load</param>
197                 /// <param name="defaultValue">Value to return if the seeting is not present</param>
198                 /// <returns>Value of the setting or the default</returns>
199                 public string LoadStringSetting( string settingName, string defaultValue )
200                 {
201                         return storageImpl.LoadStringSetting( settingName, defaultValue );
202                 }
203
204                 /// <summary>
205                 /// Remove a setting from the group
206                 /// </summary>
207                 /// <param name="settingName">Name of the setting to remove</param>
208                 public void RemoveSetting( string settingName )
209                 {
210                         storageImpl.RemoveSetting( settingName );
211                 }
212
213                 /// <summary>
214                 /// Save the value of one of the group's settings
215                 /// </summary>
216                 /// <param name="settingName">Name of the setting to save</param>
217                 /// <param name="settingValue">Value to be saved</param>
218                 public void SaveSetting( string settingName, object settingValue )
219                 {
220                         storageImpl.SaveSetting( settingName, settingValue );
221                 }
222
223                 /// <summary>
224                 /// Save the value of one of the group's integer settings
225                 /// in a type-safe manner.
226                 /// </summary>
227                 /// <param name="settingName">Name of the setting to save</param>
228                 /// <param name="settingValue">Value to be saved</param>
229                 public void SaveIntSetting( string settingName, int settingValue )
230                 {
231                         storageImpl.SaveSetting( settingName, settingValue );
232                 }
233
234                 /// <summary>
235                 /// Save the value of one of the group's string settings
236                 /// in a type-safe manner.
237                 /// </summary>
238                 /// <param name="settingName">Name of the setting to save</param>
239                 /// <param name="settingValue">Value to be saved</param>
240                 public void SaveStringSetting( string settingName, string settingValue )
241                 {
242                         storageImpl.SaveSetting( settingName, settingValue );
243                 }
244
245                 #endregion
246         }
247 }