@@ -122,49 +122,38 @@ public static Collation Simple
122
122
/// <returns>A Collation instance.</returns>
123
123
public static Collation FromBsonDocument ( BsonDocument document )
124
124
{
125
- var locale = document [ "locale" ] . AsString ;
126
-
127
- bool ? caseLevel = null ;
128
- CollationCaseFirst ? caseFirst = null ;
129
- CollationStrength ? strength = null ;
130
- bool ? numericOrdering = null ;
131
125
CollationAlternate ? alternate = null ;
126
+ bool ? backwards = null ;
127
+ CollationCaseFirst ? caseFirst = null ;
128
+ bool ? caseLevel = null ;
129
+ string locale = null ;
132
130
CollationMaxVariable ? maxVariable = null ;
133
131
bool ? normalization = null ;
134
- bool ? backwards = null ;
132
+ bool ? numericOrdering = null ;
133
+ CollationStrength ? strength = null ;
135
134
136
- BsonValue value ;
137
- if ( document . TryGetValue ( "caseLevel" , out value ) )
138
- {
139
- caseLevel = value . ToBoolean ( ) ;
140
- }
141
- if ( document . TryGetValue ( "caseFirst" , out value ) )
135
+ foreach ( var element in document )
142
136
{
143
- caseFirst = ToCollationCaseFirst ( value . AsString ) ;
137
+ var value = element . Value ;
138
+ switch ( element . Name )
139
+ {
140
+ case "alternate" : alternate = ToCollationAlternate ( value . AsString ) ; break ;
141
+ case "backwards" : backwards = value . ToBoolean ( ) ; break ;
142
+ case "caseFirst" : caseFirst = ToCollationCaseFirst ( value . AsString ) ; break ;
143
+ case "caseLevel" : caseLevel = value . ToBoolean ( ) ; break ;
144
+ case "locale" : locale = value . AsString ; break ;
145
+ case "maxVariable" : maxVariable = ToCollationMaxVariable ( value . AsString ) ; break ;
146
+ case "normalization" : normalization = value . ToBoolean ( ) ; break ;
147
+ case "numericOrdering" : numericOrdering = value . ToBoolean ( ) ; break ;
148
+ case "strength" : strength = ToCollationStrength ( value . ToInt32 ( ) ) ; break ;
149
+ default :
150
+ throw new ArgumentException ( $ "Unrecognized element '{ element . Name } ' when constructing a Collation object from a BsonDocument.") ;
151
+ }
144
152
}
145
- if ( document . TryGetValue ( "strength" , out value ) )
146
- {
147
- strength = ToCollationStrength ( value . ToInt32 ( ) ) ;
148
- }
149
- if ( document . TryGetValue ( "numericOrdering" , out value ) )
150
- {
151
- numericOrdering = value . ToBoolean ( ) ;
152
- }
153
- if ( document . TryGetValue ( "alternate" , out value ) )
154
- {
155
- alternate = ToCollationAlternate ( value . AsString ) ;
156
- }
157
- if ( document . TryGetValue ( "maxVariable" , out value ) )
158
- {
159
- maxVariable = ToCollationMaxVariable ( value . AsString ) ;
160
- }
161
- if ( document . TryGetValue ( "normalization" , out value ) )
162
- {
163
- normalization = value . ToBoolean ( ) ;
164
- }
165
- if ( document . TryGetValue ( "backwards" , out value ) )
153
+
154
+ if ( locale == null )
166
155
{
167
- backwards = value . ToBoolean ( ) ;
156
+ throw new ArgumentException ( $ "Element 'locale' missing when constructing a Collation object from a BsonDocument." ) ;
168
157
}
169
158
170
159
return new Collation (
@@ -179,8 +168,8 @@ public static Collation FromBsonDocument(BsonDocument document)
179
168
backwards ) ;
180
169
}
181
170
182
- // private static methods
183
- private static CollationAlternate ToCollationAlternate ( string value )
171
+ // internal static methods
172
+ internal static CollationAlternate ToCollationAlternate ( string value )
184
173
{
185
174
switch ( value )
186
175
{
@@ -190,7 +179,7 @@ private static CollationAlternate ToCollationAlternate(string value)
190
179
}
191
180
}
192
181
193
- private static CollationCaseFirst ToCollationCaseFirst ( string value )
182
+ internal static CollationCaseFirst ToCollationCaseFirst ( string value )
194
183
{
195
184
switch ( value )
196
185
{
@@ -201,7 +190,7 @@ private static CollationCaseFirst ToCollationCaseFirst(string value)
201
190
}
202
191
}
203
192
204
- private static CollationMaxVariable ToCollationMaxVariable ( string value )
193
+ internal static CollationMaxVariable ToCollationMaxVariable ( string value )
205
194
{
206
195
switch ( value )
207
196
{
@@ -211,7 +200,7 @@ private static CollationMaxVariable ToCollationMaxVariable(string value)
211
200
}
212
201
}
213
202
214
- private static CollationStrength ToCollationStrength ( int value )
203
+ internal static CollationStrength ToCollationStrength ( int value )
215
204
{
216
205
switch ( value )
217
206
{
@@ -220,7 +209,51 @@ private static CollationStrength ToCollationStrength(int value)
220
209
case 3 : return CollationStrength . Tertiary ;
221
210
case 4 : return CollationStrength . Quaternary ;
222
211
case 5 : return CollationStrength . Identical ;
223
- default : throw new ArgumentException ( $ "Invalid CollationStrength value: { value } .") ;
212
+ default : throw new ArgumentOutOfRangeException ( $ "Invalid CollationStrength value: { value } .") ;
213
+ }
214
+ }
215
+
216
+ internal static int ToInt32 ( CollationStrength strength )
217
+ {
218
+ switch ( strength )
219
+ {
220
+ case CollationStrength . Primary : return 1 ;
221
+ case CollationStrength . Secondary : return 2 ;
222
+ case CollationStrength . Tertiary : return 3 ;
223
+ case CollationStrength . Quaternary : return 4 ;
224
+ case CollationStrength . Identical : return 5 ;
225
+ default : throw new ArgumentException ( $ "Invalid strength: { strength } .", nameof ( strength ) ) ;
226
+ }
227
+ }
228
+
229
+ internal static string ToString ( CollationAlternate alternate )
230
+ {
231
+ switch ( alternate )
232
+ {
233
+ case CollationAlternate . NonIgnorable : return "non-ignorable" ;
234
+ case CollationAlternate . Shifted : return "shifted" ;
235
+ default : throw new ArgumentException ( $ "Invalid alternate: { alternate } .", nameof ( alternate ) ) ;
236
+ }
237
+ }
238
+
239
+ internal static string ToString ( CollationCaseFirst caseFirst )
240
+ {
241
+ switch ( caseFirst )
242
+ {
243
+ case CollationCaseFirst . Lower : return "lower" ;
244
+ case CollationCaseFirst . Off : return "off" ;
245
+ case CollationCaseFirst . Upper : return "upper" ;
246
+ default : throw new ArgumentException ( $ "Invalid caseFirst: { caseFirst } .", nameof ( caseFirst ) ) ;
247
+ }
248
+ }
249
+
250
+ internal static string ToString ( CollationMaxVariable maxVariable )
251
+ {
252
+ switch ( maxVariable )
253
+ {
254
+ case CollationMaxVariable . Punctuation : return "punct" ;
255
+ case CollationMaxVariable . Space : return "space" ;
256
+ default : throw new ArgumentException ( $ "Invalid maxVariable: { maxVariable } .", nameof ( maxVariable ) ) ;
224
257
}
225
258
}
226
259
#endregion
@@ -360,20 +393,21 @@ public bool Equals(Collation other)
360
393
}
361
394
362
395
return
363
- _alternate == other . _alternate &&
364
- _backwards == other . _backwards &&
365
- _caseFirst == other . _caseFirst &&
366
- _caseLevel == other . _caseLevel &&
367
- _locale == other . _locale &&
368
- _maxVariable == other . _maxVariable &&
369
- _numericOrdering == other . _numericOrdering &&
370
- _strength == other . _strength ;
396
+ _alternate . Equals ( other . _alternate ) &&
397
+ _backwards . Equals ( other . _backwards ) &&
398
+ _caseFirst . Equals ( other . _caseFirst ) &&
399
+ _caseLevel . Equals ( other . _caseLevel ) &&
400
+ _locale . Equals ( other . _locale ) &&
401
+ _maxVariable . Equals ( other . _maxVariable ) &&
402
+ _normalization . Equals ( other . _normalization ) &&
403
+ _numericOrdering . Equals ( other . _numericOrdering ) &&
404
+ _strength . Equals ( other . _strength ) ;
371
405
}
372
406
373
407
/// <inheritdoc/>
374
408
public override bool Equals ( object obj )
375
409
{
376
- return base . Equals ( obj ) ;
410
+ return Equals ( obj as Collation ) ;
377
411
}
378
412
379
413
/// <inheritdoc/>
@@ -386,6 +420,7 @@ public override int GetHashCode()
386
420
. Hash ( _caseLevel )
387
421
. Hash ( _locale )
388
422
. Hash ( _maxVariable )
423
+ . Hash ( _normalization )
389
424
. Hash ( _numericOrdering )
390
425
. Hash ( _strength )
391
426
. GetHashCode ( ) ;
@@ -397,14 +432,14 @@ public BsonDocument ToBsonDocument()
397
432
return new BsonDocument
398
433
{
399
434
{ "locale" , _locale } ,
400
- { "caseLevel" , ( ) => _caseLevel . Value , _caseLevel != null } ,
401
- { "caseFirst" , ( ) => ToString ( _caseFirst . Value ) , _caseFirst != null } ,
402
- { "strength" , ( ) => ToString ( _strength . Value ) , _strength != null } ,
403
- { "numericOrdering" , ( ) => _numericOrdering . Value , _numericOrdering != null } ,
404
- { "alternate" , ( ) => ToString ( _alternate . Value ) , _alternate != null } ,
405
- { "maxVariable" , ( ) => ToString ( _maxVariable . Value ) , _maxVariable != null } ,
406
- { "normalization" , ( ) => _normalization . Value , _normalization != null } ,
407
- { "backwards" , ( ) => _backwards . Value , _backwards != null }
435
+ { "caseLevel" , ( ) => _caseLevel . Value , _caseLevel . HasValue } ,
436
+ { "caseFirst" , ( ) => ToString ( _caseFirst . Value ) , _caseFirst . HasValue } ,
437
+ { "strength" , ( ) => ToInt32 ( _strength . Value ) , _strength . HasValue } ,
438
+ { "numericOrdering" , ( ) => _numericOrdering . Value , _numericOrdering . HasValue } ,
439
+ { "alternate" , ( ) => ToString ( _alternate . Value ) , _alternate . HasValue } ,
440
+ { "maxVariable" , ( ) => ToString ( _maxVariable . Value ) , _maxVariable . HasValue } ,
441
+ { "normalization" , ( ) => _normalization . Value , _normalization . HasValue } ,
442
+ { "backwards" , ( ) => _backwards . Value , _backwards . HasValue }
408
443
} ;
409
444
}
410
445
@@ -449,50 +484,5 @@ public Collation With(
449
484
normalization . WithDefault ( _normalization ) ,
450
485
backwards . WithDefault ( _backwards ) ) ;
451
486
}
452
-
453
- // private methods
454
- private string ToString ( CollationAlternate alternate )
455
- {
456
- switch ( alternate )
457
- {
458
- case CollationAlternate . NonIgnorable : return "non-ignorable" ;
459
- case CollationAlternate . Shifted : return "shifted" ;
460
- default : throw new ArgumentException ( $ "Invalid alternate: { alternate } .", nameof ( alternate ) ) ;
461
- }
462
- }
463
-
464
- private string ToString ( CollationMaxVariable maxVariable )
465
- {
466
- switch ( maxVariable )
467
- {
468
- case CollationMaxVariable . Punctuation : return "punct" ;
469
- case CollationMaxVariable . Space : return "space" ;
470
- default : throw new ArgumentException ( $ "Invalid maxVariable: { maxVariable } .", nameof ( maxVariable ) ) ;
471
- }
472
- }
473
-
474
- private string ToString ( CollationCaseFirst caseFirst )
475
- {
476
- switch ( caseFirst )
477
- {
478
- case CollationCaseFirst . Lower : return "lower" ;
479
- case CollationCaseFirst . Off : return "off" ;
480
- case CollationCaseFirst . Upper : return "upper" ;
481
- default : throw new ArgumentException ( $ "Invalid caseFirst: { caseFirst } .", nameof ( caseFirst ) ) ;
482
- }
483
- }
484
-
485
- private int ToString ( CollationStrength strength )
486
- {
487
- switch ( strength )
488
- {
489
- case CollationStrength . Primary : return 1 ;
490
- case CollationStrength . Secondary : return 2 ;
491
- case CollationStrength . Tertiary : return 3 ;
492
- case CollationStrength . Quaternary : return 4 ;
493
- case CollationStrength . Identical : return 5 ;
494
- default : throw new ArgumentException ( $ "Invalid strength: { strength } .", nameof ( strength ) ) ;
495
- }
496
- }
497
487
}
498
488
}
0 commit comments