1 if(!dojo._hasResource["dojox.uuid.Uuid"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.uuid.Uuid"] = true;
3 dojo.provide("dojox.uuid.Uuid");
4 dojo.require("dojox.uuid");
6 dojox.uuid.Uuid = function(/*String?*/ input){
8 // This is the constructor for the Uuid class. The Uuid class offers
9 // methods for inspecting existing UUIDs.
10 // input: A 36-character string that conforms to the UUID spec.
13 // uuid = new dojox.uuid.Uuid("3b12f1df-5232-4804-897e-917bf397618a");
14 // uuid = new dojox.uuid.Uuid(); // "00000000-0000-0000-0000-000000000000"
15 // uuid = new dojox.uuid.Uuid(dojox.uuid.generateRandomUuid());
16 // uuid = new dojox.uuid.Uuid(dojox.uuid.generateTimeBasedUuid());
17 // dojox.uuid.Uuid.setGenerator(dojox.uuid.generateRandomUuid);
18 // uuid = new dojox.uuid.Uuid();
19 // dojox.uuid.assert(!uuid.isEqual(dojox.uuid.NIL_UUID));
20 this._uuidString = dojox.uuid.NIL_UUID;
22 dojox.uuid.assert(dojo.isString(input));
23 this._uuidString = input.toLowerCase();
24 dojox.uuid.assert(this.isValid());
26 var ourGenerator = dojox.uuid.Uuid.getGenerator();
28 this._uuidString = ourGenerator();
29 dojox.uuid.assert(this.isValid());
34 dojox.uuid.Uuid.compare = function(/*dojox.uuid.Uuid*/ uuidOne, /*dojox.uuid.Uuid*/ uuidTwo){
36 // Given two UUIDs to compare, this method returns 0, 1, or -1.
38 // This method is designed to be used by sorting routines, like the
39 // JavaScript built-in Array sort() method. This implementation is
40 // intended to match the sample implementation in IETF RFC 4122:
41 // http://www.ietf.org/rfc/rfc4122.txt
42 // uuidOne: Any object that has toString() method that returns a 36-character string that conforms to the UUID spec.
43 // uuidTwo: Any object that has toString() method that returns a 36-character string that conforms to the UUID spec.
47 // var generator = dojox.uuid.TimeBasedGenerator;
48 // var a = new dojox.uuid.Uuid(generator);
49 // var b = new dojox.uuid.Uuid(generator);
50 // var c = new dojox.uuid.Uuid(generator);
51 // var array = new Array(a, b, c);
52 // array.sort(dojox.uuid.Uuid.compare);
53 var uuidStringOne = uuidOne.toString();
54 var uuidStringTwo = uuidTwo.toString();
55 if (uuidStringOne > uuidStringTwo) return 1; // integer
56 if (uuidStringOne < uuidStringTwo) return -1; // integer
57 return 0; // integer (either 0, 1, or -1)
60 dojox.uuid.Uuid.setGenerator = function(/*Function?*/ generator){
62 // Sets the default generator, which will be used by the
63 // "new dojox.uuid.Uuid()" constructor if no parameters
65 // generator: A UUID generator function, such as dojox.uuid.generateTimeBasedUuid.
66 dojox.uuid.assert(!generator || dojo.isFunction(generator));
67 dojox.uuid.Uuid._ourGenerator = generator;
70 dojox.uuid.Uuid.getGenerator = function(){
72 // Returns the default generator. See setGenerator().
73 return dojox.uuid.Uuid._ourGenerator; // generator (A UUID generator, such as dojox.uuid.TimeBasedGenerator).
76 dojox.uuid.Uuid.prototype.toString = function(){
78 // This method returns a standard 36-character string representing
79 // the UUID, such as "3b12f1df-5232-4804-897e-917bf397618a".
80 return this._uuidString; // string
83 dojox.uuid.Uuid.prototype.compare = function(/*dojox.uuid.Uuid*/ otherUuid){
85 // Compares this UUID to another UUID, and returns 0, 1, or -1.
87 // This implementation is intended to match the sample implementation
88 // in IETF RFC 4122: http://www.ietf.org/rfc/rfc4122.txt
89 // otherUuid: Any object that has toString() method that returns a 36-character string that conforms to the UUID spec.
90 return dojox.uuid.Uuid.compare(this, otherUuid); // integer (either 0, 1, or -1)
93 dojox.uuid.Uuid.prototype.isEqual = function(/*dojox.uuid.Uuid*/ otherUuid){
95 // Returns true if this UUID is equal to the otherUuid, or false otherwise.
96 // otherUuid: Any object that has toString() method that returns a 36-character string that conforms to the UUID spec.
97 return (this.compare(otherUuid) == 0); // boolean
100 dojox.uuid.Uuid.prototype.isValid = function(){
102 // Returns true if the UUID was initialized with a valid value.
103 return dojox.uuid.isValid(this);
106 dojox.uuid.Uuid.prototype.getVariant = function(){
108 // Returns a variant code that indicates what type of UUID this is.
109 // Returns one of the enumerated dojox.uuid.variant values.
112 // var uuid = new dojox.uuid.Uuid("3b12f1df-5232-4804-897e-917bf397618a");
113 // var variant = uuid.getVariant();
114 // dojox.uuid.assert(variant == dojox.uuid.variant.DCE);
116 // "3b12f1df-5232-4804-897e-917bf397618a"
119 // (variant "10__" == DCE)
120 return dojox.uuid.getVariant(this);
123 dojox.uuid.Uuid.prototype.getVersion = function(){
125 // Returns a version number that indicates what type of UUID this is.
126 // Returns one of the enumerated dojox.uuid.version values.
128 // var uuid = new dojox.uuid.Uuid("b4308fb0-86cd-11da-a72b-0800200c9a66");
129 // var version = uuid.getVersion();
130 // dojox.uuid.assert(version == dojox.uuid.version.TIME_BASED);
132 // Throws an Error if this is not a DCE Variant UUID.
133 if(!this._versionNumber){
134 this._versionNumber = dojox.uuid.getVersion(this);
136 return this._versionNumber; // dojox.uuid.version
139 dojox.uuid.Uuid.prototype.getNode = function(){
141 // If this is a version 1 UUID (a time-based UUID), getNode() returns a
142 // 12-character string with the "node" or "pseudonode" portion of the UUID,
143 // which is the rightmost 12 characters.
145 // Throws an Error if this is not a version 1 UUID.
146 if (!this._nodeString) {
147 this._nodeString = dojox.uuid.getNode(this);
149 return this._nodeString; // String (a 12-character string, which will look something like "917bf397618a")
152 dojox.uuid.Uuid.prototype.getTimestamp = function(/*String?*/ returnType){
154 // If this is a version 1 UUID (a time-based UUID), this method returns
155 // the timestamp value encoded in the UUID. The caller can ask for the
156 // timestamp to be returned either as a JavaScript Date object or as a
157 // 15-character string of hex digits.
158 // returnType: Any of these five values: "string", String, "hex", "date", Date
160 // Returns the timestamp value as a JavaScript Date object or a 15-character string of hex digits.
162 // var uuid = new dojox.uuid.Uuid("b4308fb0-86cd-11da-a72b-0800200c9a66");
163 // var date, string, hexString;
164 // date = uuid.getTimestamp(); // returns a JavaScript Date
165 // date = uuid.getTimestamp(Date); //
166 // string = uuid.getTimestamp(String); // "Mon, 16 Jan 2006 20:21:41 GMT"
167 // hexString = uuid.getTimestamp("hex"); // "1da86cdb4308fb0"
169 // Throws an Error if this is not a version 1 UUID.
170 if(!returnType){returnType = null};
174 return this.getTimestamp(Date).toUTCString(); // String (e.g. "Mon, 16 Jan 2006 20:21:41 GMT")
177 // Return a 15-character string of hex digits containing the
178 // timestamp for this UUID, with the high-order bits first.
179 if (!this._timestampAsHexString) {
180 this._timestampAsHexString = dojox.uuid.getTimestamp(this, "hex");
182 return this._timestampAsHexString; // String (e.g. "1da86cdb4308fb0")
184 case null: // no returnType was specified, so default to Date
187 // Return a JavaScript Date object.
188 if (!this._timestampAsDate) {
189 this._timestampAsDate = dojox.uuid.getTimestamp(this, Date);
191 return this._timestampAsDate; // Date
194 // we got passed something other than a valid returnType
195 dojox.uuid.assert(false, "The getTimestamp() method dojox.uuid.Uuid was passed a bogus returnType: " + returnType);