-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyDataCollection.cs
More file actions
342 lines (293 loc) · 10.1 KB
/
KeyDataCollection.cs
File metadata and controls
342 lines (293 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using System;
using System.Collections;
using System.Collections.Generic;
namespace IniParser.Model
{
/// <summary>
/// Represents a collection of Keydata.
/// </summary>
public class KeyDataCollection : ICloneable, IEnumerable<KeyData>
{
readonly IEqualityComparer<string> _searchComparer;
#region Initialization
/// <summary>
/// Initializes a new instance of the <see cref="KeyDataCollection"/> class.
/// </summary>
public KeyDataCollection()
: this(EqualityComparer<string>.Default)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="KeyDataCollection"/> class with a given
/// search comparer
/// </summary>
/// <param name="searchComparer">
/// Search comparer used to find the key by name in the collection
/// </param>
public KeyDataCollection(IEqualityComparer<string> searchComparer)
{
_searchComparer = searchComparer;
_keyData = new Dictionary<string, KeyData>(_searchComparer);
}
/// <summary>
/// Initializes a new instance of the <see cref="KeyDataCollection"/> class
/// from a previous instance of <see cref="KeyDataCollection"/>.
/// </summary>
/// <remarks>
/// Data from the original KeyDataCollection instance is deeply copied
/// </remarks>
/// <param name="ori">
/// The instance of the <see cref="KeyDataCollection"/> class
/// used to create the new instance.
/// </param>
public KeyDataCollection(KeyDataCollection ori, IEqualityComparer<string> searchComparer)
: this(searchComparer)
{
foreach (KeyData key in ori)
{
if (_keyData.ContainsKey(key.KeyName))
{
_keyData[key.KeyName] = (KeyData)key.Clone();
}
else
{
_keyData.Add(key.KeyName, (KeyData)key.Clone());
}
}
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the value of a concrete key.
/// </summary>
/// <remarks>
/// If we try to assign the value of a key which doesn't exists,
/// a new key is added with the name and the value is assigned to it.
/// </remarks>
/// <param name="keyName">
/// Name of the key
/// </param>
/// <returns>
/// The string with key's value or null if the key was not found.
/// </returns>
public string this[string keyName]
{
get
{
if (_keyData.ContainsKey(keyName))
return _keyData[keyName].Value;
return null;
}
set
{
if (!_keyData.ContainsKey(keyName))
{
this.AddKey(keyName);
}
_keyData[keyName].Value = value;
}
}
/// <summary>
/// Return the number of keys in the collection
/// </summary>
public int Count
{
get { return _keyData.Count; }
}
#endregion
#region Operations
/// <summary>
/// Adds a new key with the specified name and empty value and comments
/// </summary>
/// <param name="keyName">
/// New key to be added.
/// </param>
/// <c>true</c> if the key was added <c>false</c> if a key with the same name already exist
/// in the collection
/// </returns>
public bool AddKey(string keyName)
{
if (!_keyData.ContainsKey(keyName))
{
_keyData.Add(keyName, new KeyData(keyName));
return true;
}
return false;
}
[Obsolete("Pottentially buggy method! Use AddKey(KeyData keyData) instead (See comments in code for an explanation of the bug)")]
public bool AddKey(string keyName, KeyData keyData)
{
// BUG: this actually can allow you to add the keyData having
// keyData.KeyName different from the argument 'keyName' in this method
// which doesn't make any sense
if (AddKey(keyName))
{
_keyData[keyName] = keyData;
return true;
}
return false;
}
/// <summary>
/// Adds a new key to the collection
/// </summary>
/// <param name="keyData">
/// KeyData instance.
/// </param>
/// <returns>
/// <c>true</c> if the key was added <c>false</c> if a key with the same name already exist
/// in the collection
/// </returns>
public bool AddKey(KeyData keyData)
{
if (AddKey(keyData.KeyName))
{
_keyData[keyData.KeyName] = keyData;
return true;
}
return false;
}
/// <summary>
/// Adds a new key with the specified name and value to the collection
/// </summary>
/// <param name="keyName">
/// Name of the new key to be added.
/// </param>
/// <param name="keyValue">
/// Value associated to the key.
/// </param>
/// <returns>
/// <c>true</c> if the key was added <c>false</c> if a key with the same name already exist
/// in the collection.
/// </returns>
public bool AddKey(string keyName, string keyValue)
{
if (AddKey(keyName))
{
_keyData[keyName].Value = keyValue;
return true;
}
return false;
}
/// <summary>
/// Clears all comments of this section
/// </summary>
public void ClearComments()
{
foreach (var keydata in this)
{
keydata.Comments.Clear();
}
}
/// <summary>
/// Gets if a specifyed key name exists in the collection.
/// </summary>
/// <param name="keyName">Key name to search</param>
/// <returns><c>true</c> if a key with the specified name exists in the collectoin
/// <c>false</c> otherwise</returns>
public bool ContainsKey(string keyName)
{
return _keyData.ContainsKey(keyName);
}
/// <summary>
/// Retrieves the data for a specified key given its name
/// </summary>
/// <param name="keyName">Name of the key to retrieve.</param>
/// <returns>
/// A <see cref="KeyData"/> instance holding
/// the key information or <c>null</c> if the key wasn't found.
/// </returns>
public KeyData GetKeyData(string keyName)
{
if (_keyData.ContainsKey(keyName))
return _keyData[keyName];
return null;
}
public void Merge(KeyDataCollection keyDataToMerge)
{
foreach (var keyData in keyDataToMerge)
{
AddKey(keyData.KeyName);
GetKeyData(keyData.KeyName).Comments.AddRange(keyData.Comments);
this[keyData.KeyName] = keyData.Value;
}
}
/// <summary>
/// Deletes all keys in this collection.
/// </summary>
public void RemoveAllKeys()
{
_keyData.Clear();
}
/// <summary>
/// Deletes a previously existing key, including its associated data.
/// </summary>
/// <param name="keyName">The key to be removed.</param>
/// <returns>
/// <c>true</c> if a key with the specified name was removed
/// <c>false</c> otherwise.
/// </returns>
public bool RemoveKey(string keyName)
{
return _keyData.Remove(keyName);
}
/// <summary>
/// Sets the key data associated to a specified key.
/// </summary>
/// <param name="data">The new <see cref="KeyData"/> for the key.</param>
public void SetKeyData(KeyData data)
{
if (data == null) return;
if (_keyData.ContainsKey(data.KeyName))
RemoveKey(data.KeyName);
AddKey(data);
}
#endregion
#region IEnumerable<KeyData> Members
/// <summary>
/// Allows iteration througt the collection.
/// </summary>
/// <returns>A strong-typed IEnumerator </returns>
public IEnumerator<KeyData> GetEnumerator()
{
foreach (string key in _keyData.Keys)
yield return _keyData[key];
}
#region IEnumerable Members
/// <summary>
/// Implementation needed
/// </summary>
/// <returns>A weak-typed IEnumerator.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return _keyData.GetEnumerator();
}
#endregion
#endregion
#region ICloneable Members
/// <summary>
/// Creates a new object that is a copy of the current instance.
/// </summary>
/// <returns>
/// A new object that is a copy of this instance.
/// </returns>
public object Clone()
{
return new KeyDataCollection(this, _searchComparer);
}
#endregion
#region Non-public Members
// Hack for getting the last key value (if exists) w/out using LINQ
// and maintain support for earlier versions of .NET
internal KeyData GetLast()
{
KeyData result = null;
if (_keyData.Keys.Count <= 0) return result;
foreach (var k in _keyData.Keys) result = _keyData[k];
return result;
}
/// <summary>
/// Collection of KeyData for a given section
/// </summary>
private readonly Dictionary<string, KeyData> _keyData;
#endregion
}
}