You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by tr...@apache.org on 2009/07/06 16:42:03 UTC
svn commit: r791494 - in /qpid/trunk/qpid/dotnet/client-010:
client/transport/util/UUID.cs test/transport/util/UUIDTest.cs
Author: tross
Date: Mon Jul 6 14:42:03 2009
New Revision: 791494
URL: http://svn.apache.org/viewvc?rev=791494&view=rev
Log:
QPID-1897 - Patch from Julien Lavigne du Cadet
ToString() on UUID class should override and not hide Object.ToString()
Modified:
qpid/trunk/qpid/dotnet/client-010/client/transport/util/UUID.cs
qpid/trunk/qpid/dotnet/client-010/test/transport/util/UUIDTest.cs
Modified: qpid/trunk/qpid/dotnet/client-010/client/transport/util/UUID.cs
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/dotnet/client-010/client/transport/util/UUID.cs?rev=791494&r1=791493&r2=791494&view=diff
==============================================================================
--- qpid/trunk/qpid/dotnet/client-010/client/transport/util/UUID.cs (original)
+++ qpid/trunk/qpid/dotnet/client-010/client/transport/util/UUID.cs Mon Jul 6 14:42:03 2009
@@ -28,6 +28,7 @@
private long _mostSigBits;
private long _leastSigBits;
+ private static readonly Random _random = new Random();
public UUID(long mostSigBits, long leastSigBits)
@@ -61,8 +62,7 @@
public static UUID randomUUID()
{
byte[] randomBytes = new byte[16];
- Random random = new Random();
- random.NextBytes(randomBytes);
+ _random.NextBytes(randomBytes);
randomBytes[6] &= 0x0f;
randomBytes[6] |= 0x40;
randomBytes[8] &= 0x3f;
@@ -70,7 +70,7 @@
return new UUID(randomBytes);
}
- public new String ToString()
+ public override String ToString()
{
return (digits(_mostSigBits >> 32, 8) + "-" +
digits(_mostSigBits >> 16, 4) + "-" +
@@ -84,5 +84,40 @@
long hi = 1L << (digits * 4);
return Convert.ToString((hi | (val & (hi - 1))), 16);
}
+
+ #region equality
+ public bool Equals(UUID other)
+ {
+ if (ReferenceEquals(null, other)) return false;
+ if (ReferenceEquals(this, other)) return true;
+ return other._mostSigBits == _mostSigBits && other._leastSigBits == _leastSigBits;
+ }
+
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ if (ReferenceEquals(this, obj)) return true;
+ if (obj.GetType() != typeof (UUID)) return false;
+ return Equals((UUID) obj);
+ }
+
+ public override int GetHashCode()
+ {
+ unchecked
+ {
+ return (_mostSigBits.GetHashCode()*397) ^ _leastSigBits.GetHashCode();
+ }
+ }
+
+ public static bool operator ==(UUID left, UUID right)
+ {
+ return Equals(left, right);
+ }
+
+ public static bool operator !=(UUID left, UUID right)
+ {
+ return !Equals(left, right);
+ }
+ #endregion
}
}
Modified: qpid/trunk/qpid/dotnet/client-010/test/transport/util/UUIDTest.cs
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/dotnet/client-010/test/transport/util/UUIDTest.cs?rev=791494&r1=791493&r2=791494&view=diff
==============================================================================
--- qpid/trunk/qpid/dotnet/client-010/test/transport/util/UUIDTest.cs (original)
+++ qpid/trunk/qpid/dotnet/client-010/test/transport/util/UUIDTest.cs Mon Jul 6 14:42:03 2009
@@ -37,5 +37,26 @@
UUID uuid2 = UUID.randomUUID();
Assert.AreNotSame(uuid, uuid2);
}
+
+ [Test]
+ public void ToString_should_override_and_not_hide_base()
+ {
+ UUID uuid = UUID.randomUUID();
+
+ string uuidStr = uuid.ToString();
+ string uuidConcat = "Test." + uuid;
+
+ Assert.AreEqual("Test." + uuidStr, uuidConcat);
+ }
+
+ [Test]
+ public void two_uuid_with_same_value_should_have_same_hash_code()
+ {
+ UUID uuid = UUID.randomUUID();
+ UUID uuid2 = new UUID(uuid.MostSignificantBits, uuid.LeastSignificantBits);
+
+ Assert.AreEqual(uuid, uuid2);
+ Assert.AreEqual(uuid.GetHashCode(), uuid2.GetHashCode());
+ }
}
}
---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project: http://qpid.apache.org
Use/Interact: mailto:commits-subscribe@qpid.apache.org