a39f9863054acbdd50e5391b2d891315e5ab3039.svn-base 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. if (!dojo._hasResource["dojox.uuid.Uuid"]) { // _hasResource checks added by
  2. // build. Do not use
  3. // _hasResource directly in your
  4. // code.
  5. dojo._hasResource["dojox.uuid.Uuid"] = true;
  6. dojo.provide("dojox.uuid.Uuid");
  7. dojo.require("dojox.uuid");
  8. dojox.uuid.Uuid = function(/* String? */input) {
  9. // summary:
  10. // This is the constructor for the Uuid class. The Uuid class offers
  11. // methods for inspecting existing UUIDs.
  12. // input: A 36-character string that conforms to the UUID spec.
  13. // examples:
  14. // var uuid;
  15. // uuid = new dojox.uuid.Uuid("3b12f1df-5232-4804-897e-917bf397618a");
  16. // uuid = new dojox.uuid.Uuid(); //
  17. // "00000000-0000-0000-0000-000000000000"
  18. // uuid = new dojox.uuid.Uuid(dojox.uuid.generateRandomUuid());
  19. // uuid = new dojox.uuid.Uuid(dojox.uuid.generateTimeBasedUuid());
  20. // dojox.uuid.Uuid.setGenerator(dojox.uuid.generateRandomUuid);
  21. // uuid = new dojox.uuid.Uuid();
  22. // dojox.uuid.assert(!uuid.isEqual(dojox.uuid.NIL_UUID));
  23. this._uuidString = dojox.uuid.NIL_UUID;
  24. if (input) {
  25. dojox.uuid.assert(dojo.isString(input));
  26. this._uuidString = input.toLowerCase();
  27. dojox.uuid.assert(this.isValid());
  28. } else {
  29. var ourGenerator = dojox.uuid.Uuid.getGenerator();
  30. if (ourGenerator) {
  31. this._uuidString = ourGenerator();
  32. dojox.uuid.assert(this.isValid());
  33. }
  34. }
  35. };
  36. dojox.uuid.Uuid.compare = function(/* dojox.uuid.Uuid */uuidOne, /* dojox.uuid.Uuid */
  37. uuidTwo) {
  38. // summary:
  39. // Given two UUIDs to compare, this method returns 0, 1, or -1.
  40. // description:
  41. // This method is designed to be used by sorting routines, like the
  42. // JavaScript built-in Array sort() method. This implementation is
  43. // intended to match the sample implementation in IETF RFC 4122:
  44. // http://www.ietf.org/rfc/rfc4122.txt
  45. // uuidOne: Any object that has toString() method that returns a
  46. // 36-character string that conforms to the UUID spec.
  47. // uuidTwo: Any object that has toString() method that returns a
  48. // 36-character string that conforms to the UUID spec.
  49. // examples:
  50. // var uuid;
  51. // var generator = dojox.uuid.TimeBasedGenerator;
  52. // var a = new dojox.uuid.Uuid(generator);
  53. // var b = new dojox.uuid.Uuid(generator);
  54. // var c = new dojox.uuid.Uuid(generator);
  55. // var array = new Array(a, b, c);
  56. // array.sort(dojox.uuid.Uuid.compare);
  57. var uuidStringOne = uuidOne.toString();
  58. var uuidStringTwo = uuidTwo.toString();
  59. if (uuidStringOne > uuidStringTwo)
  60. return 1; // integer
  61. if (uuidStringOne < uuidStringTwo)
  62. return -1; // integer
  63. return 0; // integer (either 0, 1, or -1)
  64. };
  65. dojox.uuid.Uuid.setGenerator = function(/* Function? */generator) {
  66. // summary:
  67. // Sets the default generator, which will be used by the
  68. // "new dojox.uuid.Uuid()" constructor if no parameters
  69. // are passed in.
  70. // generator: A UUID generator function, such as
  71. // dojox.uuid.generateTimeBasedUuid.
  72. dojox.uuid.assert(!generator || dojo.isFunction(generator));
  73. dojox.uuid.Uuid._ourGenerator = generator;
  74. };
  75. dojox.uuid.Uuid.getGenerator = function() {
  76. // summary:
  77. // Returns the default generator. See setGenerator().
  78. return dojox.uuid.Uuid._ourGenerator; // generator (A UUID generator,
  79. // such as
  80. // dojox.uuid.TimeBasedGenerator).
  81. };
  82. dojox.uuid.Uuid.prototype.toString = function() {
  83. // summary:
  84. // This method returns a standard 36-character string representing
  85. // the UUID, such as "3b12f1df-5232-4804-897e-917bf397618a".
  86. return this._uuidString; // string
  87. };
  88. dojox.uuid.Uuid.prototype.compare = function(/* dojox.uuid.Uuid */otherUuid) {
  89. // summary:
  90. // Compares this UUID to another UUID, and returns 0, 1, or -1.
  91. // description:
  92. // This implementation is intended to match the sample implementation
  93. // in IETF RFC 4122: http://www.ietf.org/rfc/rfc4122.txt
  94. // otherUuid: Any object that has toString() method that returns a
  95. // 36-character string that conforms to the UUID spec.
  96. return dojox.uuid.Uuid.compare(this, otherUuid); // integer (either
  97. // 0, 1, or -1)
  98. };
  99. dojox.uuid.Uuid.prototype.isEqual = function(/* dojox.uuid.Uuid */otherUuid) {
  100. // summary:
  101. // Returns true if this UUID is equal to the otherUuid, or false
  102. // otherwise.
  103. // otherUuid: Any object that has toString() method that returns a
  104. // 36-character string that conforms to the UUID spec.
  105. return (this.compare(otherUuid) == 0); // boolean
  106. };
  107. dojox.uuid.Uuid.prototype.isValid = function() {
  108. // summary:
  109. // Returns true if the UUID was initialized with a valid value.
  110. return dojox.uuid.isValid(this);
  111. };
  112. dojox.uuid.Uuid.prototype.getVariant = function() {
  113. // summary:
  114. // Returns a variant code that indicates what type of UUID this is.
  115. // Returns one of the enumerated dojox.uuid.variant values.
  116. // example:
  117. // var uuid = new
  118. // dojox.uuid.Uuid("3b12f1df-5232-4804-897e-917bf397618a");
  119. // var variant = uuid.getVariant();
  120. // dojox.uuid.assert(variant == dojox.uuid.variant.DCE);
  121. // example:
  122. // "3b12f1df-5232-4804-897e-917bf397618a"
  123. // ^
  124. // |
  125. // (variant "10__" == DCE)
  126. return dojox.uuid.getVariant(this);
  127. };
  128. dojox.uuid.Uuid.prototype.getVersion = function() {
  129. // summary:
  130. // Returns a version number that indicates what type of UUID this is.
  131. // Returns one of the enumerated dojox.uuid.version values.
  132. // example:
  133. // var uuid = new
  134. // dojox.uuid.Uuid("b4308fb0-86cd-11da-a72b-0800200c9a66");
  135. // var version = uuid.getVersion();
  136. // dojox.uuid.assert(version == dojox.uuid.version.TIME_BASED);
  137. // exceptions:
  138. // Throws an Error if this is not a DCE Variant UUID.
  139. if (!this._versionNumber) {
  140. this._versionNumber = dojox.uuid.getVersion(this);
  141. }
  142. return this._versionNumber; // dojox.uuid.version
  143. };
  144. dojox.uuid.Uuid.prototype.getNode = function() {
  145. // summary:
  146. // If this is a version 1 UUID (a time-based UUID), getNode() returns a
  147. // 12-character string with the "node" or "pseudonode" portion of the
  148. // UUID,
  149. // which is the rightmost 12 characters.
  150. // exceptions:
  151. // Throws an Error if this is not a version 1 UUID.
  152. if (!this._nodeString) {
  153. this._nodeString = dojox.uuid.getNode(this);
  154. }
  155. return this._nodeString; // String (a 12-character string, which will
  156. // look something like "917bf397618a")
  157. };
  158. dojox.uuid.Uuid.prototype.getTimestamp = function(/* String? */returnType) {
  159. // summary:
  160. // If this is a version 1 UUID (a time-based UUID), this method returns
  161. // the timestamp value encoded in the UUID. The caller can ask for the
  162. // timestamp to be returned either as a JavaScript Date object or as a
  163. // 15-character string of hex digits.
  164. // returnType: Any of these five values: "string", String, "hex",
  165. // "date", Date
  166. // returns:
  167. // Returns the timestamp value as a JavaScript Date object or a
  168. // 15-character string of hex digits.
  169. // examples:
  170. // var uuid = new
  171. // dojox.uuid.Uuid("b4308fb0-86cd-11da-a72b-0800200c9a66");
  172. // var date, string, hexString;
  173. // date = uuid.getTimestamp(); // returns a JavaScript Date
  174. // date = uuid.getTimestamp(Date); //
  175. // string = uuid.getTimestamp(String); // "Mon, 16 Jan 2006 20:21:41
  176. // GMT"
  177. // hexString = uuid.getTimestamp("hex"); // "1da86cdb4308fb0"
  178. // exceptions:
  179. // Throws an Error if this is not a version 1 UUID.
  180. if (!returnType) {
  181. returnType = null
  182. };
  183. switch (returnType) {
  184. case "string" :
  185. case String :
  186. return this.getTimestamp(Date).toUTCString(); // String (e.g.
  187. // "Mon, 16 Jan
  188. // 2006 20:21:41
  189. // GMT")
  190. break;
  191. case "hex" :
  192. // Return a 15-character string of hex digits containing the
  193. // timestamp for this UUID, with the high-order bits first.
  194. if (!this._timestampAsHexString) {
  195. this._timestampAsHexString = dojox.uuid.getTimestamp(this,
  196. "hex");
  197. }
  198. return this._timestampAsHexString; // String (e.g.
  199. // "1da86cdb4308fb0")
  200. break;
  201. case null : // no returnType was specified, so default to Date
  202. case "date" :
  203. case Date :
  204. // Return a JavaScript Date object.
  205. if (!this._timestampAsDate) {
  206. this._timestampAsDate = dojox.uuid.getTimestamp(this, Date);
  207. }
  208. return this._timestampAsDate; // Date
  209. break;
  210. default :
  211. // we got passed something other than a valid returnType
  212. dojox.uuid.assert(false,
  213. "The getTimestamp() method dojox.uuid.Uuid was passed a bogus returnType: "
  214. + returnType);
  215. break;
  216. }
  217. };
  218. }