source: Dev/trunk/src/client/dojox/uuid/Uuid.js @ 529

Last change on this file since 529 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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