OSUUID.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using libsecondlife;
  29. namespace OpenSim.Framework
  30. {
  31. [Serializable]
  32. public class OSUUID : IComparable
  33. {
  34. public static readonly OSUUID Zero = new OSUUID();
  35. public Guid UUID;
  36. public OSUUID()
  37. {
  38. }
  39. /* Constructors */
  40. public OSUUID(string s)
  41. {
  42. if (s == null)
  43. UUID = new Guid();
  44. else
  45. UUID = new Guid(s);
  46. }
  47. public OSUUID(Guid g)
  48. {
  49. UUID = g;
  50. }
  51. public OSUUID(LLUUID l)
  52. {
  53. UUID = l.UUID;
  54. }
  55. public OSUUID(ulong u)
  56. {
  57. UUID = new Guid(0, 0, 0, BitConverter.GetBytes(u));
  58. }
  59. #region IComparable Members
  60. public int CompareTo(object obj)
  61. {
  62. if (obj is OSUUID)
  63. {
  64. OSUUID ID = (OSUUID) obj;
  65. return UUID.CompareTo(ID.UUID);
  66. }
  67. throw new ArgumentException("object is not a OSUUID");
  68. }
  69. #endregion
  70. // out conversion
  71. public override string ToString()
  72. {
  73. return UUID.ToString();
  74. }
  75. public LLUUID ToLLUUID()
  76. {
  77. return new LLUUID(UUID);
  78. }
  79. // for comparison bits
  80. public override int GetHashCode()
  81. {
  82. return UUID.GetHashCode();
  83. }
  84. public override bool Equals(object o)
  85. {
  86. if (!(o is LLUUID)) return false;
  87. OSUUID uuid = (OSUUID) o;
  88. return UUID == uuid.UUID;
  89. }
  90. // Static methods
  91. public static OSUUID Random()
  92. {
  93. return new OSUUID(Guid.NewGuid());
  94. }
  95. }
  96. }