UniqueId.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // UniqueId.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005,2007 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. namespace System.Xml
  31. {
  32. public class UniqueId
  33. {
  34. Guid guid;
  35. string id;
  36. public UniqueId ()
  37. : this (Guid.NewGuid ())
  38. {
  39. }
  40. public UniqueId (byte [] id)
  41. : this (id, 0)
  42. {
  43. }
  44. public UniqueId (Guid id)
  45. {
  46. this.guid = id;
  47. }
  48. public UniqueId (string value)
  49. {
  50. if (value == null)
  51. throw new ArgumentNullException ("value cannot be null", "value");
  52. if (value.Length == 0)
  53. throw new FormatException ("UniqueId cannot be zero length");
  54. this.id = value;
  55. }
  56. [MonoTODO]
  57. public UniqueId (byte [] id, int offset)
  58. {
  59. if (id == null)
  60. throw new ArgumentNullException ();
  61. if (offset < 0 || offset >= id.Length)
  62. throw new ArgumentOutOfRangeException ("offset");
  63. if (id.Length - offset != 16)
  64. throw new NotImplementedException ();
  65. guid = new Guid (new ArraySegment<byte> (id, offset, id.Length - offset).Array);
  66. }
  67. [MonoTODO]
  68. public UniqueId (char [] id, int offset, int count)
  69. {
  70. throw new NotImplementedException ();
  71. }
  72. [MonoTODO]
  73. public int CharArrayLength {
  74. get { throw new NotImplementedException (); }
  75. }
  76. public bool IsGuid {
  77. get { return guid != default (Guid); }
  78. }
  79. [MonoTODO]
  80. public override bool Equals (Object obj)
  81. {
  82. UniqueId other = obj as UniqueId;
  83. if (other == null)
  84. return false;
  85. if (IsGuid && other.IsGuid) {
  86. Guid g1, g2;
  87. TryGetGuid (out g1);
  88. other.TryGetGuid (out g2);
  89. return g1.Equals (g2);
  90. }
  91. return ToString () == other.ToString ();
  92. }
  93. [MonoTODO]
  94. public override int GetHashCode ()
  95. {
  96. throw new NotImplementedException ();
  97. }
  98. public static bool operator == (UniqueId id1, UniqueId id2)
  99. {
  100. if ((object) id1 == null)
  101. return (object) id2 == null;
  102. if ((object) id2 == null)
  103. return false;
  104. return id1.Equals (id2);
  105. }
  106. public static bool operator != (UniqueId id1, UniqueId id2)
  107. {
  108. return ! (id1 == id2);
  109. }
  110. [MonoTODO]
  111. public int ToCharArray (char [] array, int offset)
  112. {
  113. throw new NotImplementedException ();
  114. }
  115. [MonoTODO]
  116. public override string ToString ()
  117. {
  118. if (IsGuid)
  119. return "urn:uuid:" + guid;
  120. return id;
  121. }
  122. public bool TryGetGuid (out Guid guid)
  123. {
  124. if (IsGuid) {
  125. guid = this.guid;
  126. return true;
  127. } else {
  128. guid = default (Guid);
  129. return false;
  130. }
  131. }
  132. public bool TryGetGuid (byte [] buffer, int offset)
  133. {
  134. Guid ret;
  135. if (!TryGetGuid (out ret))
  136. return false;
  137. byte [] bytes = ret.ToByteArray ();
  138. bytes.CopyTo (buffer, offset);
  139. return true;
  140. }
  141. }
  142. }
  143. #endif