-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathPropertyCollection.cs
More file actions
316 lines (282 loc) · 9.66 KB
/
PropertyCollection.cs
File metadata and controls
316 lines (282 loc) · 9.66 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
using System.Collections;
using System.Collections.Generic;
namespace IniParser.Model
{
/// <summary>
/// Represents a collection of Keydata.
/// </summary>
public class PropertyCollection : IDeepCloneable<PropertyCollection>, IEnumerable<Property>
{
#region Initialization
/// <summary>
/// Initializes a new instance of the <see cref="PropertyCollection"/> class.
/// </summary>
public PropertyCollection()
: this(EqualityComparer<string>.Default)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="PropertyCollection"/> class with a given
/// search comparer
/// </summary>
/// <param name="searchComparer">
/// Search comparer used to find the key by name in the collection
/// </param>
public PropertyCollection(IEqualityComparer<string> searchComparer)
{
_searchComparer = searchComparer;
_properties = new Dictionary<string, Property>(_searchComparer);
}
/// <summary>
/// Initializes a new instance of the <see cref="PropertyCollection"/> class
/// from a previous instance of <see cref="PropertyCollection"/>.
/// </summary>
/// <remarks>
/// Data from the original KeyDataCollection instance is deeply copied
/// </remarks>
/// <param name="ori">
/// The instance of the <see cref="PropertyCollection"/> class
/// used to create the new instance.
/// </param>
public PropertyCollection(PropertyCollection ori, IEqualityComparer<string> searchComparer)
: this(searchComparer)
{
foreach (Property property in ori)
{
if (_properties.ContainsKey(property.Key))
{
_properties[property.Key] = property.DeepClone();
}
else
{
_properties.Add(property.Key, property.DeepClone());
}
}
}
#endregion
/// <summary>
/// Gets or sets the value of a property.
/// </summary>
/// <remarks>
/// If we try to assign the value of a property which doesn't exists,
/// a new one is added with the key and the value specified
/// </remarks>
/// <param name="keyName">
/// key of the property
/// </param>
public string this[string keyName]
{
get
{
if (_properties.ContainsKey(keyName))
return _properties[keyName].Value;
return null;
}
set
{
if (!_properties.ContainsKey(keyName))
{
Add(keyName);
}
_properties[keyName].Value = value;
}
}
/// <summary>
/// Return the number of keys in the collection
/// </summary>
public int Count
{
get { return _properties.Count; }
}
/// <summary>
/// Adds a new key with the specified name and empty value and comments
/// </summary>
/// <param name="key">
/// New key to be added.
/// </param>
/// <returns>
/// true if the key was added false if a key with the same name already exist
/// in the collection
/// </returns>
public bool Add(string key)
{
if (!_properties.ContainsKey(key))
{
AddPropertyInternal(new Property(key, string.Empty));
return true;
}
return false;
}
/// <summary>
/// Adds a new property to the collection
/// </summary>
/// <param name="property">
/// Property instance.
/// </param>
/// <returns>
/// true if the property was added false if a property with the
/// same key already exist in the collection
/// </returns>
public bool Add(Property property)
{
if (!_properties.ContainsKey(property.Key))
{
AddPropertyInternal(property);
return true;
}
return false;
}
/// <summary>
/// Adds a new property with the specified key and value to the collection
/// </summary>
/// <param name="key">
/// key of the new property to be added.
/// </param>
/// <param name="value">
/// Value associated to the property.
/// </param>
/// <returns>
/// true if the property was added, false if a key with the same
/// name already exist in the collection.
/// </returns>
public bool Add(string key, string value)
{
if (!_properties.ContainsKey(key))
{
AddPropertyInternal(new Property(key, value));
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 specified property with the given key name exists in
/// the collection.
/// </summary>
/// <param name="keyName">
/// Key name to search
/// </param>
/// <returns>
/// true if a property with the given exists in the collection
/// false otherwise
/// </returns>
public bool Contains(string keyName)
{
return _properties.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="Property"/> instance holding
/// the key information or <c>null</c> if the key wasn't found.
/// </returns>
public Property FindByKey(string keyName)
{
if (_properties.ContainsKey(keyName))
return _properties[keyName];
return null;
}
/// <summary>
/// Merges other Property into this, adding new properties if they
/// did not existed or overwriting values if the properties already
/// existed.
/// </summary>
/// <remarks>
/// Comments are also merged but they are always added, not overwritten.
/// </remarks>
/// <param name="propertyToMerge"></param>
public void Merge(PropertyCollection propertyToMerge)
{
foreach (var keyData in propertyToMerge)
{
Add(keyData.Key);
this[keyData.Key] = keyData.Value;
FindByKey(keyData.Key).Comments.AddRange(keyData.Comments);
}
}
/// <summary>
/// Deletes all properties in this collection.
/// </summary>
public void Clear()
{
_properties.Clear();
}
/// <summary>
/// Deletes a previously existing key, including its associated data.
/// </summary>
/// <param name="keyName">The key to be removed.</param>
/// <returns>
/// true if a key with the specified name was removed
/// false otherwise.
/// </returns>
public bool Remove(string keyName)
{
return _properties.Remove(keyName);
}
#region IEnumerable<KeyData> Members
/// <summary>
/// Allows iteration through the collection.
/// </summary>
/// <returns>A strong-typed IEnumerator </returns>
public IEnumerator<Property> GetEnumerator()
{
foreach (string key in _properties.Keys)
yield return _properties[key];
}
#region IEnumerable Members
/// <summary>
/// Implementation needed
/// </summary>
/// <returns>A weak-typed IEnumerator.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return _properties.GetEnumerator();
}
#endregion
#endregion
#region IDeepCloneable 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 PropertyCollection DeepClone()
{
return new PropertyCollection(this, _searchComparer);
}
#endregion
#region Helpers
// Adds a property w/out checking if it is already contained in the dictionary
internal void AddPropertyInternal(Property property)
{
_lastAdded = property;
_properties.Add(property.Key, property);
}
#endregion
#region Non-public Members
// Hack for getting the last property value (if exists) w/out using LINQ
// that will cause allocations
Property _lastAdded;
internal Property GetLast()
{
return _lastAdded;
}
/// <summary>
/// Collection of Property for a given section
/// </summary>
private readonly Dictionary<string, Property> _properties;
readonly IEqualityComparer<string> _searchComparer;
#endregion
}
}