5
5
namespace IniParser . Model
6
6
{
7
7
/// <summary>
8
- /// <para> Represents a collection of Keydata.</para>
8
+ /// Represents a collection of Keydata.
9
9
/// </summary>
10
10
public class KeyDataCollection : ICloneable , IEnumerable < KeyData >
11
11
{
12
12
IEqualityComparer < string > _searchComparer ;
13
13
#region Initialization
14
14
15
15
/// <summary>
16
- /// Initializes a new instance of the <see cref="KeyDataCollection"/> class.
16
+ /// Initializes a new instance of the <see cref="KeyDataCollection"/> class.
17
17
/// </summary>
18
18
public KeyDataCollection ( )
19
19
: this ( EqualityComparer < string > . Default )
20
20
{ }
21
21
22
22
/// <summary>
23
- /// Initializes a new instance of the <see cref="KeyDataCollection"/> class.
23
+ /// Initializes a new instance of the <see cref="KeyDataCollection"/> class with a given
24
+ /// search comparer
24
25
/// </summary>
26
+ /// <param name="searchComparer">
27
+ /// Search comparer used to find the key by name in the collection
28
+ /// </param>
25
29
public KeyDataCollection ( IEqualityComparer < string > searchComparer )
26
30
{
27
31
_searchComparer = searchComparer ;
28
32
_keyData = new Dictionary < string , KeyData > ( _searchComparer ) ;
29
33
}
30
34
31
35
/// <summary>
32
- /// Initializes a new instance of the <see cref="KeyDataCollection"/> class
33
- /// from a previous instance of <see cref="KeyDataCollection"/>.
36
+ /// Initializes a new instance of the <see cref="KeyDataCollection"/> class
37
+ /// from a previous instance of <see cref="KeyDataCollection"/>.
34
38
/// </summary>
35
39
/// <remarks>
36
- /// Data is deeply copied
40
+ /// Data from the original KeyDataCollection instance is deeply copied
37
41
/// </remarks>
38
42
/// <param name="ori">
39
- /// The instance of the <see cref="KeyDataCollection"/> class
40
- /// used to create the new instance.</param>
43
+ /// The instance of the <see cref="KeyDataCollection"/> class
44
+ /// used to create the new instance.
45
+ /// </param>
41
46
public KeyDataCollection ( KeyDataCollection ori , IEqualityComparer < string > searchComparer )
42
47
: this ( searchComparer )
43
48
{
@@ -57,17 +62,19 @@ public KeyDataCollection(KeyDataCollection ori, IEqualityComparer<string> search
57
62
#endregion
58
63
59
64
#region Properties
60
- /// <summary>
61
- /// Gets or sets the value of a concrete key.
65
+
66
+ /// <summary>
67
+ /// Gets or sets the value of a concrete key.
62
68
/// </summary>
63
69
/// <remarks>
64
- /// If we try to assign the value of a key which doesn't exists,
65
- /// a new key is added with the name and the value is assigned to it.
70
+ /// If we try to assign the value of a key which doesn't exists,
71
+ /// a new key is added with the name and the value is assigned to it.
66
72
/// </remarks>
67
- /// <param name="keyName">Name of the key</param>
73
+ /// <param name="keyName">
74
+ /// Name of the key
75
+ /// </param>
68
76
/// <returns>
69
- /// The string with key's value or null
70
- /// if the key was not found.
77
+ /// The string with key's value or null if the key was not found.
71
78
/// </returns>
72
79
public string this [ string keyName ]
73
80
{
@@ -92,9 +99,8 @@ public string this[string keyName]
92
99
}
93
100
94
101
/// <summary>
95
- /// Return the number of keys in the collection
102
+ /// Return the number of keys in the collection
96
103
/// </summary>
97
- /// <value>An integer with the number of keys in the collection.</value>
98
104
public int Count
99
105
{
100
106
get { return _keyData . Count ; }
@@ -105,17 +111,14 @@ public int Count
105
111
#region Operations
106
112
107
113
/// <summary>
108
- /// Adds a new key with the specified name and empty value and comments
114
+ /// Adds a new key with the specified name and empty value and comments
109
115
/// </summary>
110
- /// <remarks>
111
- /// A valid key name is a string with NO blank spaces.
112
- /// </remarks>
113
- /// <param name="keyName">New key to be added.</param>
114
- /// <returns>
115
- /// <c>true</c> if a new empty key was added
116
- /// <c>false</c> otherwise.
116
+ /// <param name="keyName">
117
+ /// New key to be added.
118
+ /// </param>
119
+ /// <c>true</c> if the key was added <c>false</c> if a key with the same name already exist
120
+ /// in the collection
117
121
/// </returns>
118
- /// <exception cref="ArgumentException">If the key name is not valid.</exception>
119
122
public bool AddKey ( string keyName )
120
123
{
121
124
if ( ! _keyData . ContainsKey ( keyName ) )
@@ -127,21 +130,12 @@ public bool AddKey(string keyName)
127
130
return false ;
128
131
}
129
132
130
- /// <summary>
131
- /// Adds a new key with the specified name and value and comments
132
- /// </summary>
133
- /// <remarks>
134
- /// A valid key name is a string with NO blank spaces.
135
- /// </remarks>
136
- /// <param name="keyName">New key to be added.</param>
137
- /// <param name="keyData">KeyData instance.</param>
138
- /// <returns>
139
- /// <c>true</c> if a new empty key was added
140
- /// <c>false</c> otherwise.
141
- /// </returns>
142
- /// <exception cref="ArgumentException">If the key name is not valid.</exception>
133
+ [ Obsolete ( "Pottentially buggy method! Use AddKey(KeyData keyData) instead (See comments in code for an explanation of the bug)" ) ]
143
134
public bool AddKey ( string keyName , KeyData keyData )
144
135
{
136
+ // BUG: this actually can allow you to add the keyData having
137
+ // keyData.KeyName different from the argument 'keyName' in this method
138
+ // which doesn't make any sense
145
139
if ( AddKey ( keyName ) )
146
140
{
147
141
_keyData [ keyName ] = keyData ;
@@ -153,18 +147,38 @@ public bool AddKey(string keyName, KeyData keyData)
153
147
}
154
148
155
149
/// <summary>
156
- /// Adds a new key with the specified name and value and comments
150
+ /// Adds a new key to the collection
157
151
/// </summary>
158
- /// <remarks>
159
- /// A valid key name is a string with NO blank spaces.
160
- /// </remarks>
161
- /// <param name="keyName">New key to be added.</param>
162
- /// <param name="keyValue">Value associated to the kyy.</param>
152
+ /// <param name="keyData">
153
+ /// KeyData instance.
154
+ /// </param>
163
155
/// <returns>
164
- /// <c>true</c> if a new empty key was added
165
- /// <c>false</c> otherwise.
156
+ /// <c>true</c> if the key was added <c>false</c> if a key with the same name already exist
157
+ /// in the collection
158
+ /// </returns>
159
+ public bool AddKey ( KeyData keyData )
160
+ {
161
+ if ( AddKey ( keyData . KeyName ) )
162
+ {
163
+ _keyData [ keyData . KeyName ] = keyData ;
164
+ return true ;
165
+ }
166
+
167
+ return false ;
168
+ }
169
+ /// <summary>
170
+ /// Adds a new key with the specified name and value to the collection
171
+ /// </summary>
172
+ /// <param name="keyName">
173
+ /// Name of the new key to be added.
174
+ /// </param>
175
+ /// <param name="keyValue">
176
+ /// Value associated to the key.
177
+ /// </param>
178
+ /// <returns>
179
+ /// <c>true</c> if the key was added <c>false</c> if a key with the same name already exist
180
+ /// in the collection.
166
181
/// </returns>
167
- /// <exception cref="ArgumentException">If the key name is not valid.</exception>
168
182
public bool AddKey ( string keyName , string keyValue )
169
183
{
170
184
if ( AddKey ( keyName ) )
@@ -251,13 +265,12 @@ public bool RemoveKey(string keyName)
251
265
/// <param name="data">The new <see cref="KeyData"/> for the key.</param>
252
266
public void SetKeyData ( KeyData data )
253
267
{
254
- if ( data != null )
255
- {
256
- if ( _keyData . ContainsKey ( data . KeyName ) )
257
- RemoveKey ( data . KeyName ) ;
268
+ if ( data == null ) return ;
258
269
259
- AddKey ( data . KeyName , data ) ;
260
- }
270
+ if ( _keyData . ContainsKey ( data . KeyName ) )
271
+ RemoveKey ( data . KeyName ) ;
272
+
273
+ AddKey ( data ) ;
261
274
}
262
275
263
276
#endregion
0 commit comments