From 306ff6372fd8108ee47e2f026339a857775d5240 Mon Sep 17 00:00:00 2001 From: Ted Ross Date: Mon, 6 Jul 2009 14:42:03 +0000 Subject: QPID-1897 - Patch from Julien Lavigne du Cadet ToString() on UUID class should override and not hide Object.ToString() git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@791494 13f79535-47bb-0310-9956-ffa450edef68 --- .../client-010/client/transport/util/UUID.cs | 41 ++++++++++++++++++++-- .../client-010/test/transport/util/UUIDTest.cs | 21 +++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) (limited to 'qpid/dotnet') diff --git a/qpid/dotnet/client-010/client/transport/util/UUID.cs b/qpid/dotnet/client-010/client/transport/util/UUID.cs index 788c5b99c8..ff237f689e 100644 --- a/qpid/dotnet/client-010/client/transport/util/UUID.cs +++ b/qpid/dotnet/client-010/client/transport/util/UUID.cs @@ -28,6 +28,7 @@ namespace org.apache.qpid.transport.util private long _mostSigBits; private long _leastSigBits; + private static readonly Random _random = new Random(); public UUID(long mostSigBits, long leastSigBits) @@ -61,8 +62,7 @@ namespace org.apache.qpid.transport.util 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 @@ namespace org.apache.qpid.transport.util 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 @@ namespace org.apache.qpid.transport.util 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 } } diff --git a/qpid/dotnet/client-010/test/transport/util/UUIDTest.cs b/qpid/dotnet/client-010/test/transport/util/UUIDTest.cs index c073f1139a..83faec49f8 100644 --- a/qpid/dotnet/client-010/test/transport/util/UUIDTest.cs +++ b/qpid/dotnet/client-010/test/transport/util/UUIDTest.cs @@ -37,5 +37,26 @@ namespace test.transport.util 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()); + } } } -- cgit v1.2.1