Skip to content

Commit 6313309

Browse files
author
rstam
committed
Standardized implementation of IEquatable, !=, ==, Equals and GetHashCode for MongoGridFSFileInfo.
1 parent 34578b5 commit 6313309

File tree

2 files changed

+58
-39
lines changed

2 files changed

+58
-39
lines changed

Driver/GridFS/MongoGridFSFileInfo.cs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -233,25 +233,38 @@ public DateTime UploadDate
233233

234234
// public operators
235235
/// <summary>
236-
/// Compares two MongoGridFSFileInfos.
236+
/// Determines whether two specified MongoGridFSFileInfo objects have different values.
237237
/// </summary>
238-
/// <param name="lhs">The first MongoGridFSFileInfo.</param>
239-
/// <param name="rhs">The other MongoGridFSFileInfo.</param>
240-
/// <returns>True if the two MongoGridFSFileInfos are not equal (or one is null and the other is not).</returns>
238+
/// <param name="lhs">The first value to compare, or null.</param>
239+
/// <param name="rhs">The second value to compare, or null.</param>
240+
/// <returns>True if the value of lhs is different from the value of rhs; otherwise, false.</returns>
241241
public static bool operator !=(MongoGridFSFileInfo lhs, MongoGridFSFileInfo rhs)
242242
{
243-
return !(lhs == rhs);
243+
return !MongoGridFSFileInfo.Equals(lhs, rhs);
244244
}
245245

246246
/// <summary>
247-
/// Compares two MongoGridFSFileInfos.
247+
/// Determines whether two specified MongoGridFSFileInfo objects have the same value.
248248
/// </summary>
249-
/// <param name="lhs">The first MongoGridFSFileInfo.</param>
250-
/// <param name="rhs">The other MongoGridFSFileInfo.</param>
251-
/// <returns>True if the two MongoGridFSFileInfos are equal (or both null).</returns>
249+
/// <param name="lhs">The first value to compare, or null.</param>
250+
/// <param name="rhs">The second value to compare, or null.</param>
251+
/// <returns>True if the value of lhs is the same as the value of rhs; otherwise, false.</returns>
252252
public static bool operator ==(MongoGridFSFileInfo lhs, MongoGridFSFileInfo rhs)
253253
{
254-
return object.Equals(lhs, rhs);
254+
return MongoGridFSFileInfo.Equals(lhs, rhs);
255+
}
256+
257+
// public static methods
258+
/// <summary>
259+
/// Determines whether two specified MongoGridFSFileInfo objects have the same value.
260+
/// </summary>
261+
/// <param name="lhs">The first value to compare, or null.</param>
262+
/// <param name="rhs">The second value to compare, or null.</param>
263+
/// <returns>True if the value of lhs is the same as the value of rhs; otherwise, false.</returns>
264+
public static bool Equals(MongoGridFSFileInfo lhs, MongoGridFSFileInfo rhs)
265+
{
266+
if ((object)lhs == null) { return (object)rhs == null; }
267+
return lhs.Equals(rhs);
255268
}
256269

257270
// public methods
@@ -334,13 +347,14 @@ public void Delete()
334347
}
335348

336349
/// <summary>
337-
/// Compares this MongoGridFSFileInfo to another MongoGridFSFileInfo.
350+
/// Determines whether this instance and another specified MongoGridFSFileInfo object have the same value.
338351
/// </summary>
339-
/// <param name="rhs">The other MongoGridFSFileInfo.</param>
340-
/// <returns>True if the two MongoGridFSFileInfos are equal.</returns>
352+
/// <param name="rhs">The MongoGridFSFileInfo object to compare to this instance.</param>
353+
/// <returns>True if the value of the rhs parameter is the same as this instance; otherwise, false.</returns>
341354
public bool Equals(MongoGridFSFileInfo rhs)
342355
{
343-
if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
356+
if ((object)rhs == null || GetType() != rhs.GetType()) { return false; }
357+
if ((object)this == (object)rhs) { return true; }
344358
return
345359
(_aliases == null && rhs._aliases == null || _aliases != null && rhs._aliases != null && _aliases.SequenceEqual(rhs._aliases)) &&
346360
_chunkSize == rhs._chunkSize &&
@@ -354,19 +368,19 @@ public bool Equals(MongoGridFSFileInfo rhs)
354368
}
355369

356370
/// <summary>
357-
/// Compares this MongoGridFSFileInfo to another object.
371+
/// Determines whether this instance and a specified object, which must also be a MongoGridFSFileInfo object, have the same value.
358372
/// </summary>
359-
/// <param name="obj">The other object.</param>
360-
/// <returns>True if the other object is a MongoGridFSFileInfo and equal to this one.</returns>
373+
/// <param name="obj">The MongoGridFSFileInfo object to compare to this instance.</param>
374+
/// <returns>True if obj is a MongoGridFSFileInfo object and its value is the same as this instance; otherwise, false.</returns>
361375
public override bool Equals(object obj)
362376
{
363377
return Equals(obj as MongoGridFSFileInfo); // works even if obj is null or of a different type
364378
}
365379

366380
/// <summary>
367-
/// Gets the hash code.
381+
/// Returns the hash code for this MongoGridFSFileInfo object.
368382
/// </summary>
369-
/// <returns>The hash code.</returns>
383+
/// <returns>A 32-bit signed integer hash code.</returns>
370384
public override int GetHashCode()
371385
{
372386
// see Effective Java by Joshua Bloch

DriverUnitTests/GridFS/MongoGridFSFileInfoTests.cs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,29 +72,34 @@ public void TestCreateWithRemoteFileNameAndCreateOptions()
7272
public void TestEquals()
7373
{
7474
var createOptions = new MongoGridFSCreateOptions { ChunkSize = 123 };
75-
var a = new MongoGridFSFileInfo(_gridFS, "f", createOptions);
76-
var b = new MongoGridFSFileInfo(_gridFS, "f", createOptions);
77-
var c = new MongoGridFSFileInfo(_gridFS, "g", createOptions);
78-
var n = (MongoCredentials)null;
75+
var a1 = new MongoGridFSFileInfo(_gridFS, "f", createOptions);
76+
var a2 = new MongoGridFSFileInfo(_gridFS, "f", createOptions);
77+
var a3 = a2;
78+
var b = new MongoGridFSFileInfo(_gridFS, "g", createOptions);
79+
var null1 = (MongoGridFSFileInfo)null;
80+
var null2 = (MongoGridFSFileInfo)null;
7981

80-
Assert.IsTrue(object.Equals(a, b));
81-
Assert.IsFalse(object.Equals(a, c));
82-
Assert.IsFalse(a.Equals(n));
83-
Assert.IsFalse(a.Equals(null));
82+
Assert.AreNotSame(a1, a2);
83+
Assert.AreSame(a2, a3);
84+
Assert.IsTrue(a1.Equals((object)a2));
85+
Assert.IsFalse(a1.Equals((object)null));
86+
Assert.IsFalse(a1.Equals((object)"x"));
8487

85-
Assert.IsTrue(a == b);
86-
Assert.IsFalse(a == c);
87-
Assert.IsFalse(a == null);
88-
Assert.IsFalse(null == a);
89-
Assert.IsTrue(n == null);
90-
Assert.IsTrue(null == n);
88+
Assert.IsTrue(a1 == a2);
89+
Assert.IsTrue(a2 == a3);
90+
Assert.IsFalse(a1 == b);
91+
Assert.IsFalse(a1 == null1);
92+
Assert.IsFalse(null1 == a1);
93+
Assert.IsTrue(null1 == null2);
9194

92-
Assert.IsFalse(a != b);
93-
Assert.IsTrue(a != c);
94-
Assert.IsTrue(a != null);
95-
Assert.IsTrue(null != a);
96-
Assert.IsFalse(n != null);
97-
Assert.IsFalse(null != n);
95+
Assert.IsFalse(a1 != a2);
96+
Assert.IsFalse(a2 != a3);
97+
Assert.IsTrue(a1 != b);
98+
Assert.IsTrue(a1 != null1);
99+
Assert.IsTrue(null1 != a1);
100+
Assert.IsFalse(null1 != null2);
101+
102+
Assert.AreEqual(a1.GetHashCode(), a2.GetHashCode());
98103
}
99104
}
100105
}

0 commit comments

Comments
 (0)