Skip to content

Commit cb573d8

Browse files
author
rstam
committed
Simplified optimized implementation of GetHashCode and ToString for classes that can be frozen.
1 parent d103cab commit cb573d8

File tree

6 files changed

+61
-99
lines changed

6 files changed

+61
-99
lines changed

Driver/Core/MongoCollectionSettings.cs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ public MongoCollectionSettings Freeze()
208208
if (!_isFrozen)
209209
{
210210
_safeMode = _safeMode.FrozenCopy();
211-
_frozenHashCode = GetHashCodeHelper();
212-
_frozenStringRepresentation = ToStringHelper();
211+
_frozenHashCode = GetHashCode();
212+
_frozenStringRepresentation = ToString();
213213
_isFrozen = true;
214214
}
215215
return this;
@@ -241,31 +241,7 @@ public override int GetHashCode()
241241
{
242242
return _frozenHashCode;
243243
}
244-
else
245-
{
246-
return GetHashCodeHelper();
247-
}
248-
}
249-
250-
/// <summary>
251-
/// Returns a string representation of the settings.
252-
/// </summary>
253-
/// <returns>A string representation of the settings.</returns>
254-
public override string ToString()
255-
{
256-
if (_isFrozen)
257-
{
258-
return _frozenStringRepresentation;
259-
}
260-
else
261-
{
262-
return ToStringHelper();
263-
}
264-
}
265244

266-
// private methods
267-
private int GetHashCodeHelper()
268-
{
269245
// see Effective Java by Joshua Bloch
270246
int hash = 17;
271247
hash = 37 * hash + ((_collectionName == null) ? 0 : _collectionName.GetHashCode());
@@ -277,8 +253,17 @@ private int GetHashCodeHelper()
277253
return hash;
278254
}
279255

280-
private string ToStringHelper()
256+
/// <summary>
257+
/// Returns a string representation of the settings.
258+
/// </summary>
259+
/// <returns>A string representation of the settings.</returns>
260+
public override string ToString()
281261
{
262+
if (_isFrozen)
263+
{
264+
return _frozenStringRepresentation;
265+
}
266+
282267
return string.Format(
283268
"CollectionName={0};AssignIdOnInsert={1};DefaultDocumentType={2};GuidRepresentation={3};SafeMode={4};SlaveOk={5}",
284269
_collectionName, _assignIdOnInsert, _defaultDocumentType, _guidRepresentation, _safeMode, _slaveOk);

Driver/Core/MongoCredentialsStore.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class MongoCredentialsStore
2828
// private fields
2929
private Dictionary<string, MongoCredentials> _credentialsStore = new Dictionary<string,MongoCredentials>();
3030
private int _frozenHashCode;
31+
private string _frozenStringRepresentation;
3132
private bool _isFrozen;
3233

3334
// constructors
@@ -126,6 +127,7 @@ public MongoCredentialsStore Freeze()
126127
if (!_isFrozen)
127128
{
128129
_frozenHashCode = GetHashCode();
130+
_frozenStringRepresentation = ToString();
129131
_isFrozen = true;
130132
}
131133
return this;
@@ -160,6 +162,11 @@ public override int GetHashCode()
160162
/// <returns>A string representation of the credentials store.</returns>
161163
public override string ToString()
162164
{
165+
if (_isFrozen)
166+
{
167+
return _frozenStringRepresentation;
168+
}
169+
163170
var sb = new StringBuilder();
164171
sb.Append("{");
165172
var separator = "";

Driver/Core/MongoDatabaseSettings.cs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ public MongoDatabaseSettings Freeze()
198198
if (!_isFrozen)
199199
{
200200
_safeMode = _safeMode.FrozenCopy();
201-
_frozenHashCode = GetHashCodeHelper();
202-
_frozenStringRepresentation = ToStringHelper();
201+
_frozenHashCode = GetHashCode();
202+
_frozenStringRepresentation = ToString();
203203
_isFrozen = true;
204204
}
205205
return this;
@@ -231,10 +231,15 @@ public override int GetHashCode()
231231
{
232232
return _frozenHashCode;
233233
}
234-
else
235-
{
236-
return GetHashCodeHelper();
237-
}
234+
235+
// see Effective Java by Joshua Bloch
236+
int hash = 17;
237+
hash = 37 * hash + ((_databaseName == null) ? 0 : _databaseName.GetHashCode());
238+
hash = 37 * hash + ((_credentials == null) ? 0 : _credentials.GetHashCode());
239+
hash = 37 * hash + _guidRepresentation.GetHashCode();
240+
hash = 37 * hash + ((_safeMode == null) ? 0 : _safeMode.GetHashCode());
241+
hash = 37 * hash + _slaveOk.GetHashCode();
242+
return hash;
238243
}
239244

240245
/// <summary>
@@ -247,27 +252,7 @@ public override string ToString()
247252
{
248253
return _frozenStringRepresentation;
249254
}
250-
else
251-
{
252-
return ToStringHelper();
253-
}
254-
}
255-
256-
// private methods
257-
private int GetHashCodeHelper()
258-
{
259-
// see Effective Java by Joshua Bloch
260-
int hash = 17;
261-
hash = 37 * hash + ((_databaseName == null) ? 0 : _databaseName.GetHashCode());
262-
hash = 37 * hash + ((_credentials == null) ? 0 : _credentials.GetHashCode());
263-
hash = 37 * hash + _guidRepresentation.GetHashCode();
264-
hash = 37 * hash + ((_safeMode == null) ? 0 : _safeMode.GetHashCode());
265-
hash = 37 * hash + _slaveOk.GetHashCode();
266-
return hash;
267-
}
268255

269-
private string ToStringHelper()
270-
{
271256
return string.Format(
272257
"DatabaseName={0};Credentials={1};GuidRepresentation={2};SafeMode={3};SlaveOk={4}",
273258
_databaseName, _credentials, _guidRepresentation, _safeMode, _slaveOk);

Driver/Core/MongoServerSettings.cs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ public MongoServerSettings Freeze()
451451
{
452452
_credentialsStore.Freeze();
453453
_safeMode = _safeMode.FrozenCopy();
454-
_frozenHashCode = GetHashCodeHelper();
455-
_frozenStringRepresentation = ToStringHelper();
454+
_frozenHashCode = GetHashCode();
455+
_frozenStringRepresentation = ToString();
456456
_isFrozen = true;
457457
}
458458
return this;
@@ -514,31 +514,7 @@ public override int GetHashCode()
514514
{
515515
return _frozenHashCode;
516516
}
517-
else
518-
{
519-
return GetHashCodeHelper();
520-
}
521-
}
522-
523-
/// <summary>
524-
/// Returns a string representation of the settings.
525-
/// </summary>
526-
/// <returns>A string representation of the settings.</returns>
527-
public override string ToString()
528-
{
529-
if (_isFrozen)
530-
{
531-
return _frozenStringRepresentation;
532-
}
533-
else
534-
{
535-
return ToStringHelper();
536-
}
537-
}
538517

539-
// private methods
540-
private int GetHashCodeHelper()
541-
{
542518
// see Effective Java by Joshua Bloch
543519
int hash = 17;
544520
hash = 37 * hash + _connectionMode.GetHashCode();
@@ -567,8 +543,17 @@ private int GetHashCodeHelper()
567543
return hash;
568544
}
569545

570-
private string ToStringHelper()
546+
/// <summary>
547+
/// Returns a string representation of the settings.
548+
/// </summary>
549+
/// <returns>A string representation of the settings.</returns>
550+
public override string ToString()
571551
{
552+
if (_isFrozen)
553+
{
554+
return _frozenStringRepresentation;
555+
}
556+
572557
var sb = new StringBuilder();
573558
string serversString = null;
574559
if (_servers != null)

Driver/Core/SafeMode.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ public SafeMode Freeze()
427427
{
428428
if (!_isFrozen)
429429
{
430-
_frozenHashCode = GetHashCodeHelper();
430+
_frozenHashCode = GetHashCode();
431431
_isFrozen = true;
432432
}
433433
return this;
@@ -459,10 +459,16 @@ public override int GetHashCode()
459459
{
460460
return _frozenHashCode;
461461
}
462-
else
463-
{
464-
return GetHashCodeHelper();
465-
}
462+
463+
// see Effective Java by Joshua Bloch
464+
int hash = 17;
465+
hash = 37 * hash + _enabled.GetHashCode();
466+
hash = 37 * hash + _fsync.GetHashCode();
467+
hash = 37 * hash + _j.GetHashCode();
468+
hash = 37 * hash + _w.GetHashCode();
469+
hash = 37 * hash + ((_wmode == null) ? 0 : _wmode.GetHashCode());
470+
hash = 37 * hash + _wtimeout.GetHashCode();
471+
return hash;
466472
}
467473

468474
/// <summary>
@@ -506,19 +512,6 @@ public override string ToString()
506512
}
507513

508514
// private methods
509-
private int GetHashCodeHelper()
510-
{
511-
// see Effective Java by Joshua Bloch
512-
int hash = 17;
513-
hash = 37 * hash + _enabled.GetHashCode();
514-
hash = 37 * hash + _fsync.GetHashCode();
515-
hash = 37 * hash + _j.GetHashCode();
516-
hash = 37 * hash + _w.GetHashCode();
517-
hash = 37 * hash + ((_wmode == null) ? 0 : _wmode.GetHashCode());
518-
hash = 37 * hash + _wtimeout.GetHashCode();
519-
return hash;
520-
}
521-
522515
private void ThrowFrozenException()
523516
{
524517
throw new InvalidOperationException("SafeMode has been frozen and no further changes are allowed.");

Driver/GridFS/MongoGridFSSettings.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ public class MongoGridFSSettings : IEquatable<MongoGridFSSettings>
3030
private static MongoGridFSSettings __defaults = new MongoGridFSSettings();
3131

3232
// private fields
33-
private bool _isFrozen;
3433
private string _chunksCollectionName = "fs.chunks";
3534
private int _chunkSize = 256 * 1024; // 256KiB
3635
private string _filesCollectionName = "fs.files";
3736
private string _root = "fs";
3837
private SafeMode _safeMode = SafeMode.False;
38+
private bool _isFrozen;
39+
private int _frozenHashCode;
3940

4041
// constructors
4142
/// <summary>
@@ -211,6 +212,7 @@ public MongoGridFSSettings Freeze()
211212
if (!_isFrozen)
212213
{
213214
_safeMode = _safeMode.FrozenCopy();
215+
_frozenHashCode = GetHashCode();
214216
_isFrozen = true;
215217
}
216218
return this;
@@ -238,6 +240,11 @@ public MongoGridFSSettings FrozenCopy()
238240
/// <returns>The hash code.</returns>
239241
public override int GetHashCode()
240242
{
243+
if (_isFrozen)
244+
{
245+
return _frozenHashCode;
246+
}
247+
241248
// see Effective Java by Joshua Bloch
242249
int hash = 17;
243250
hash = 37 * hash + _chunkSize.GetHashCode();

0 commit comments

Comments
 (0)