Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
if(!dojo._hasResource["dojox.uuid.generateTimeBasedUuid"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2
dojo._hasResource["dojox.uuid.generateTimeBasedUuid"] = true;
3
dojo.provide("dojox.uuid.generateTimeBasedUuid");
4
 
5
dojox.uuid.generateTimeBasedUuid = function(/*String?*/ node){
6
	// summary:
7
	//		This function generates time-based UUIDs, meaning "version 1" UUIDs.
8
	// description:
9
	// For more info, see
10
	//		http://www.webdav.org/specs/draft-leach-uuids-guids-01.txt
11
	//		http://www.infonuovo.com/dma/csdocs/sketch/instidid.htm
12
	//		http://kruithof.xs4all.nl/uuid/uuidgen
13
	//		http://www.opengroup.org/onlinepubs/009629399/apdxa.htm#tagcjh_20
14
	//		http://jakarta.apache.org/commons/sandbox/id/apidocs/org/apache/commons/id/uuid/clock/Clock.html
15
	// node:
16
	//		A 12-character hex string representing either a pseudo-node or
17
	//		hardware-node (an IEEE 802.3 network node).  A hardware-node
18
	//		will be something like "017bf397618a", always with the first bit
19
	//		being 0.  A pseudo-node will be something like "f17bf397618a",
20
	//		always with the first bit being 1.
21
	// examples:
22
	//		string = dojox.uuid.generateTimeBasedUuid();
23
	//		string = dojox.uuid.generateTimeBasedUuid("017bf397618a");
24
	//		dojox.uuid.generateTimeBasedUuid.setNode("017bf397618a");
25
	//		string = dojox.uuid.generateTimeBasedUuid(); // the generated UUID has node == "017bf397618a"
26
	var uuidString = dojox.uuid.generateTimeBasedUuid._generator.generateUuidString(node);
27
	return uuidString; // String
28
};
29
 
30
dojox.uuid.generateTimeBasedUuid.isValidNode = function(/*String?*/ node){
31
	var HEX_RADIX = 16;
32
	var integer = parseInt(node, HEX_RADIX);
33
	var valid = dojo.isString(node) && node.length == 12 && isFinite(integer);
34
	return valid; // Boolean
35
};
36
 
37
dojox.uuid.generateTimeBasedUuid.setNode = function(/*String?*/ node){
38
	// summary:
39
	//		Sets the 'node' value that will be included in generated UUIDs.
40
	// node: A 12-character hex string representing a pseudoNode or hardwareNode.
41
	dojox.uuid.assert((node === null) || this.isValidNode(node));
42
	this._uniformNode = node;
43
};
44
 
45
dojox.uuid.generateTimeBasedUuid.getNode = function(){
46
	// summary:
47
	//		Returns the 'node' value that will be included in generated UUIDs.
48
	return this._uniformNode; // String (a 12-character hex string representing a pseudoNode or hardwareNode)
49
};
50
 
51
 
52
dojox.uuid.generateTimeBasedUuid._generator = new function(){
53
	// Number of hours between October 15, 1582 and January 1, 1970:
54
	this.GREGORIAN_CHANGE_OFFSET_IN_HOURS = 3394248;
55
 
56
	// Number of seconds between October 15, 1582 and January 1, 1970:
57
	//   dojox.uuid.generateTimeBasedUuid.GREGORIAN_CHANGE_OFFSET_IN_SECONDS = 12219292800;
58
 
59
	// --------------------------------------------------
60
	// Private variables:
61
	var _uuidPseudoNodeString = null;
62
	var _uuidClockSeqString = null;
63
	var _dateValueOfPreviousUuid = null;
64
	var _nextIntraMillisecondIncrement = 0;
65
	var _cachedMillisecondsBetween1582and1970 = null;
66
	var _cachedHundredNanosecondIntervalsPerMillisecond = null;
67
 
68
	// --------------------------------------------------
69
	// Private constants:
70
	var HEX_RADIX = 16;
71
 
72
	function _carry(/* array */ arrayA){
73
		// summary:
74
		//		Given an array which holds a 64-bit number broken into 4 16-bit
75
		//		elements, this method carries any excess bits (greater than 16-bits)
76
		//		from each array element into the next.
77
		// arrayA: An array with 4 elements, each of which is a 16-bit number.
78
		arrayA[2] += arrayA[3] >>> 16;
79
		arrayA[3] &= 0xFFFF;
80
		arrayA[1] += arrayA[2] >>> 16;
81
		arrayA[2] &= 0xFFFF;
82
		arrayA[0] += arrayA[1] >>> 16;
83
		arrayA[1] &= 0xFFFF;
84
		dojox.uuid.assert((arrayA[0] >>> 16) === 0);
85
	}
86
 
87
	function _get64bitArrayFromFloat(/* float */ x){
88
		// summary:
89
		//		Given a floating point number, this method returns an array which
90
		//		holds a 64-bit number broken into 4 16-bit elements.
91
		var result = new Array(0, 0, 0, 0);
92
		result[3] = x % 0x10000;
93
		x -= result[3];
94
		x /= 0x10000;
95
		result[2] = x % 0x10000;
96
		x -= result[2];
97
		x /= 0x10000;
98
		result[1] = x % 0x10000;
99
		x -= result[1];
100
		x /= 0x10000;
101
		result[0] = x;
102
		return result; // Array with 4 elements, each of which is a 16-bit number.
103
	}
104
 
105
	function _addTwo64bitArrays(/* array */ arrayA, /* array */ arrayB){
106
		// summary:
107
		//		Takes two arrays, each of which holds a 64-bit number broken into 4
108
		//		16-bit elements, and returns a new array that holds a 64-bit number
109
		//		that is the sum of the two original numbers.
110
		// arrayA: An array with 4 elements, each of which is a 16-bit number.
111
		// arrayB: An array with 4 elements, each of which is a 16-bit number.
112
		dojox.uuid.assert(dojo.isArray(arrayA));
113
		dojox.uuid.assert(dojo.isArray(arrayB));
114
		dojox.uuid.assert(arrayA.length == 4);
115
		dojox.uuid.assert(arrayB.length == 4);
116
 
117
		var result = new Array(0, 0, 0, 0);
118
		result[3] = arrayA[3] + arrayB[3];
119
		result[2] = arrayA[2] + arrayB[2];
120
		result[1] = arrayA[1] + arrayB[1];
121
		result[0] = arrayA[0] + arrayB[0];
122
		_carry(result);
123
		return result; // Array with 4 elements, each of which is a 16-bit number.
124
	}
125
 
126
	function _multiplyTwo64bitArrays(/* array */ arrayA, /* array */ arrayB){
127
		// summary:
128
		//		Takes two arrays, each of which holds a 64-bit number broken into 4
129
		//		16-bit elements, and returns a new array that holds a 64-bit number
130
		//		that is the product of the two original numbers.
131
		// arrayA: An array with 4 elements, each of which is a 16-bit number.
132
		// arrayB: An array with 4 elements, each of which is a 16-bit number.
133
		dojox.uuid.assert(dojo.isArray(arrayA));
134
		dojox.uuid.assert(dojo.isArray(arrayB));
135
		dojox.uuid.assert(arrayA.length == 4);
136
		dojox.uuid.assert(arrayB.length == 4);
137
 
138
		var overflow = false;
139
		if(arrayA[0] * arrayB[0] !== 0){ overflow = true; }
140
		if(arrayA[0] * arrayB[1] !== 0){ overflow = true; }
141
		if(arrayA[0] * arrayB[2] !== 0){ overflow = true; }
142
		if(arrayA[1] * arrayB[0] !== 0){ overflow = true; }
143
		if(arrayA[1] * arrayB[1] !== 0){ overflow = true; }
144
		if(arrayA[2] * arrayB[0] !== 0){ overflow = true; }
145
		dojox.uuid.assert(!overflow);
146
 
147
		var result = new Array(0, 0, 0, 0);
148
		result[0] += arrayA[0] * arrayB[3];
149
		_carry(result);
150
		result[0] += arrayA[1] * arrayB[2];
151
		_carry(result);
152
		result[0] += arrayA[2] * arrayB[1];
153
		_carry(result);
154
		result[0] += arrayA[3] * arrayB[0];
155
		_carry(result);
156
		result[1] += arrayA[1] * arrayB[3];
157
		_carry(result);
158
		result[1] += arrayA[2] * arrayB[2];
159
		_carry(result);
160
		result[1] += arrayA[3] * arrayB[1];
161
		_carry(result);
162
		result[2] += arrayA[2] * arrayB[3];
163
		_carry(result);
164
		result[2] += arrayA[3] * arrayB[2];
165
		_carry(result);
166
		result[3] += arrayA[3] * arrayB[3];
167
		_carry(result);
168
		return result; // Array with 4 elements, each of which is a 16-bit number.
169
	}
170
 
171
	function _padWithLeadingZeros(/* string */ string, /* int */ desiredLength){
172
		// summary:
173
		//		Pads a string with leading zeros and returns the result.
174
		// string: A string to add padding to.
175
		// desiredLength: The number of characters the return string should have.
176
 
177
		// examples:
178
		//		result = _padWithLeadingZeros("abc", 6);
179
		//		dojox.uuid.assert(result == "000abc");
180
		while(string.length < desiredLength){
181
			string = "0" + string;
182
		}
183
		return string; // string
184
	}
185
 
186
	function _generateRandomEightCharacterHexString() {
187
		// summary:
188
		//		Returns a randomly generated 8-character string of hex digits.
189
 
190
		// FIXME: This probably isn't a very high quality random number.
191
 
192
		// Make random32bitNumber be a randomly generated floating point number
193
		// between 0 and (4,294,967,296 - 1), inclusive.
194
		var random32bitNumber = Math.floor( (Math.random() % 1) * Math.pow(2, 32) );
195
 
196
		var eightCharacterString = random32bitNumber.toString(HEX_RADIX);
197
		while(eightCharacterString.length < 8){
198
			eightCharacterString = "0" + eightCharacterString;
199
		}
200
		return eightCharacterString; // String (an 8-character hex string)
201
	}
202
 
203
	this.generateUuidString = function(/*String?*/ node){
204
		// summary:
205
		//		Generates a time-based UUID, meaning a version 1 UUID.
206
		// description:
207
		//		JavaScript code running in a browser doesn't have access to the
208
		//		IEEE 802.3 address of the computer, so if a node value isn't
209
		//		supplied, we generate a random pseudonode value instead.
210
		// node: An optional 12-character string to use as the node in the new UUID.
211
		if(node){
212
			dojox.uuid.assert(dojox.uuid.generateTimeBasedUuid.isValidNode(node));
213
		}else{
214
			if(dojox.uuid.generateTimeBasedUuid._uniformNode){
215
				node = dojox.uuid.generateTimeBasedUuid._uniformNode;
216
			}else{
217
				if(!_uuidPseudoNodeString){
218
					var pseudoNodeIndicatorBit = 0x8000;
219
					var random15bitNumber = Math.floor( (Math.random() % 1) * Math.pow(2, 15) );
220
					var leftmost4HexCharacters = (pseudoNodeIndicatorBit | random15bitNumber).toString(HEX_RADIX);
221
					_uuidPseudoNodeString = leftmost4HexCharacters + _generateRandomEightCharacterHexString();
222
				}
223
				node = _uuidPseudoNodeString;
224
			}
225
		}
226
		if(!_uuidClockSeqString){
227
			var variantCodeForDCEUuids = 0x8000; // 10--------------, i.e. uses only first two of 16 bits.
228
			var random14bitNumber = Math.floor( (Math.random() % 1) * Math.pow(2, 14) );
229
			_uuidClockSeqString = (variantCodeForDCEUuids | random14bitNumber).toString(HEX_RADIX);
230
		}
231
 
232
		// Maybe we should think about trying to make the code more readable to
233
		// newcomers by creating a class called "WholeNumber" that encapsulates
234
		// the methods and data structures for working with these arrays that
235
		// hold 4 16-bit numbers?  And then these variables below have names
236
		// like "wholeSecondsPerHour" rather than "arraySecondsPerHour"?
237
		var now = new Date();
238
		var millisecondsSince1970 = now.valueOf(); // milliseconds since midnight 01 January, 1970 UTC.
239
		var nowArray = _get64bitArrayFromFloat(millisecondsSince1970);
240
		if(!_cachedMillisecondsBetween1582and1970){
241
			var arraySecondsPerHour = _get64bitArrayFromFloat(60 * 60);
242
			var arrayHoursBetween1582and1970 = _get64bitArrayFromFloat(dojox.uuid.generateTimeBasedUuid._generator.GREGORIAN_CHANGE_OFFSET_IN_HOURS);
243
			var arraySecondsBetween1582and1970 = _multiplyTwo64bitArrays(arrayHoursBetween1582and1970, arraySecondsPerHour);
244
			var arrayMillisecondsPerSecond = _get64bitArrayFromFloat(1000);
245
			_cachedMillisecondsBetween1582and1970 = _multiplyTwo64bitArrays(arraySecondsBetween1582and1970, arrayMillisecondsPerSecond);
246
			_cachedHundredNanosecondIntervalsPerMillisecond = _get64bitArrayFromFloat(10000);
247
		}
248
		var arrayMillisecondsSince1970 = nowArray;
249
		var arrayMillisecondsSince1582 = _addTwo64bitArrays(_cachedMillisecondsBetween1582and1970, arrayMillisecondsSince1970);
250
		var arrayHundredNanosecondIntervalsSince1582 = _multiplyTwo64bitArrays(arrayMillisecondsSince1582, _cachedHundredNanosecondIntervalsPerMillisecond);
251
 
252
		if(now.valueOf() == _dateValueOfPreviousUuid){
253
			arrayHundredNanosecondIntervalsSince1582[3] += _nextIntraMillisecondIncrement;
254
			_carry(arrayHundredNanosecondIntervalsSince1582);
255
			_nextIntraMillisecondIncrement += 1;
256
			if (_nextIntraMillisecondIncrement == 10000) {
257
				// If we've gotten to here, it means we've already generated 10,000
258
				// UUIDs in this single millisecond, which is the most that the UUID
259
				// timestamp field allows for.  So now we'll just sit here and wait
260
				// for a fraction of a millisecond, so as to ensure that the next
261
				// time this method is called there will be a different millisecond
262
				// value in the timestamp field.
263
				while (now.valueOf() == _dateValueOfPreviousUuid) {
264
					now = new Date();
265
				}
266
			}
267
		}else{
268
			_dateValueOfPreviousUuid = now.valueOf();
269
			_nextIntraMillisecondIncrement = 1;
270
		}
271
 
272
		var hexTimeLowLeftHalf  = arrayHundredNanosecondIntervalsSince1582[2].toString(HEX_RADIX);
273
		var hexTimeLowRightHalf = arrayHundredNanosecondIntervalsSince1582[3].toString(HEX_RADIX);
274
		var hexTimeLow = _padWithLeadingZeros(hexTimeLowLeftHalf, 4) + _padWithLeadingZeros(hexTimeLowRightHalf, 4);
275
		var hexTimeMid = arrayHundredNanosecondIntervalsSince1582[1].toString(HEX_RADIX);
276
		hexTimeMid = _padWithLeadingZeros(hexTimeMid, 4);
277
		var hexTimeHigh = arrayHundredNanosecondIntervalsSince1582[0].toString(HEX_RADIX);
278
		hexTimeHigh = _padWithLeadingZeros(hexTimeHigh, 3);
279
		var hyphen = "-";
280
		var versionCodeForTimeBasedUuids = "1"; // binary2hex("0001")
281
		var resultUuid = hexTimeLow + hyphen + hexTimeMid + hyphen +
282
					versionCodeForTimeBasedUuids + hexTimeHigh + hyphen +
283
					_uuidClockSeqString + hyphen + node;
284
		resultUuid = resultUuid.toLowerCase();
285
		return resultUuid; // String (a 36 character string, which will look something like "b4308fb0-86cd-11da-a72b-0800200c9a66")
286
	}
287
 
288
}();
289
 
290
}