Subversion Repositories Applications.papyrus

Rev

Rev 1372 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.uuid.TimeBasedGenerator");
14
dojo.require("dojo.lang.common");
15
dojo.require("dojo.lang.type");
16
dojo.require("dojo.lang.assert");
17
dojo.uuid.TimeBasedGenerator = new function () {
18
	this.GREGORIAN_CHANGE_OFFSET_IN_HOURS = 3394248;
19
	var _uuidPseudoNodeString = null;
20
	var _uuidClockSeqString = null;
21
	var _dateValueOfPreviousUuid = null;
22
	var _nextIntraMillisecondIncrement = 0;
23
	var _cachedMillisecondsBetween1582and1970 = null;
24
	var _cachedHundredNanosecondIntervalsPerMillisecond = null;
25
	var _uniformNode = null;
26
	var HEX_RADIX = 16;
27
	function _carry(arrayA) {
28
		arrayA[2] += arrayA[3] >>> 16;
29
		arrayA[3] &= 65535;
30
		arrayA[1] += arrayA[2] >>> 16;
31
		arrayA[2] &= 65535;
32
		arrayA[0] += arrayA[1] >>> 16;
33
		arrayA[1] &= 65535;
34
		dojo.lang.assert((arrayA[0] >>> 16) === 0);
35
	}
36
	function _get64bitArrayFromFloat(x) {
37
		var result = new Array(0, 0, 0, 0);
38
		result[3] = x % 65536;
39
		x -= result[3];
40
		x /= 65536;
41
		result[2] = x % 65536;
42
		x -= result[2];
43
		x /= 65536;
44
		result[1] = x % 65536;
45
		x -= result[1];
46
		x /= 65536;
47
		result[0] = x;
48
		return result;
49
	}
50
	function _addTwo64bitArrays(arrayA, arrayB) {
51
		dojo.lang.assertType(arrayA, Array);
52
		dojo.lang.assertType(arrayB, Array);
53
		dojo.lang.assert(arrayA.length == 4);
54
		dojo.lang.assert(arrayB.length == 4);
55
		var result = new Array(0, 0, 0, 0);
56
		result[3] = arrayA[3] + arrayB[3];
57
		result[2] = arrayA[2] + arrayB[2];
58
		result[1] = arrayA[1] + arrayB[1];
59
		result[0] = arrayA[0] + arrayB[0];
60
		_carry(result);
61
		return result;
62
	}
63
	function _multiplyTwo64bitArrays(arrayA, arrayB) {
64
		dojo.lang.assertType(arrayA, Array);
65
		dojo.lang.assertType(arrayB, Array);
66
		dojo.lang.assert(arrayA.length == 4);
67
		dojo.lang.assert(arrayB.length == 4);
68
		var overflow = false;
69
		if (arrayA[0] * arrayB[0] !== 0) {
70
			overflow = true;
71
		}
72
		if (arrayA[0] * arrayB[1] !== 0) {
73
			overflow = true;
74
		}
75
		if (arrayA[0] * arrayB[2] !== 0) {
76
			overflow = true;
77
		}
78
		if (arrayA[1] * arrayB[0] !== 0) {
79
			overflow = true;
80
		}
81
		if (arrayA[1] * arrayB[1] !== 0) {
82
			overflow = true;
83
		}
84
		if (arrayA[2] * arrayB[0] !== 0) {
85
			overflow = true;
86
		}
87
		dojo.lang.assert(!overflow);
88
		var result = new Array(0, 0, 0, 0);
89
		result[0] += arrayA[0] * arrayB[3];
90
		_carry(result);
91
		result[0] += arrayA[1] * arrayB[2];
92
		_carry(result);
93
		result[0] += arrayA[2] * arrayB[1];
94
		_carry(result);
95
		result[0] += arrayA[3] * arrayB[0];
96
		_carry(result);
97
		result[1] += arrayA[1] * arrayB[3];
98
		_carry(result);
99
		result[1] += arrayA[2] * arrayB[2];
100
		_carry(result);
101
		result[1] += arrayA[3] * arrayB[1];
102
		_carry(result);
103
		result[2] += arrayA[2] * arrayB[3];
104
		_carry(result);
105
		result[2] += arrayA[3] * arrayB[2];
106
		_carry(result);
107
		result[3] += arrayA[3] * arrayB[3];
108
		_carry(result);
109
		return result;
110
	}
111
	function _padWithLeadingZeros(string, desiredLength) {
112
		while (string.length < desiredLength) {
113
			string = "0" + string;
114
		}
115
		return string;
116
	}
117
	function _generateRandomEightCharacterHexString() {
118
		var random32bitNumber = Math.floor((Math.random() % 1) * Math.pow(2, 32));
119
		var eightCharacterString = random32bitNumber.toString(HEX_RADIX);
120
		while (eightCharacterString.length < 8) {
121
			eightCharacterString = "0" + eightCharacterString;
122
		}
123
		return eightCharacterString;
124
	}
125
	function _generateUuidString(node) {
126
		dojo.lang.assertType(node, String, {optional:true});
127
		if (node) {
128
			dojo.lang.assert(node.length == 12);
129
		} else {
130
			if (_uniformNode) {
131
				node = _uniformNode;
132
			} else {
133
				if (!_uuidPseudoNodeString) {
134
					var pseudoNodeIndicatorBit = 32768;
135
					var random15bitNumber = Math.floor((Math.random() % 1) * Math.pow(2, 15));
136
					var leftmost4HexCharacters = (pseudoNodeIndicatorBit | random15bitNumber).toString(HEX_RADIX);
137
					_uuidPseudoNodeString = leftmost4HexCharacters + _generateRandomEightCharacterHexString();
138
				}
139
				node = _uuidPseudoNodeString;
140
			}
141
		}
142
		if (!_uuidClockSeqString) {
143
			var variantCodeForDCEUuids = 32768;
144
			var random14bitNumber = Math.floor((Math.random() % 1) * Math.pow(2, 14));
145
			_uuidClockSeqString = (variantCodeForDCEUuids | random14bitNumber).toString(HEX_RADIX);
146
		}
147
		var now = new Date();
148
		var millisecondsSince1970 = now.valueOf();
149
		var nowArray = _get64bitArrayFromFloat(millisecondsSince1970);
150
		if (!_cachedMillisecondsBetween1582and1970) {
151
			var arraySecondsPerHour = _get64bitArrayFromFloat(60 * 60);
152
			var arrayHoursBetween1582and1970 = _get64bitArrayFromFloat(dojo.uuid.TimeBasedGenerator.GREGORIAN_CHANGE_OFFSET_IN_HOURS);
153
			var arraySecondsBetween1582and1970 = _multiplyTwo64bitArrays(arrayHoursBetween1582and1970, arraySecondsPerHour);
154
			var arrayMillisecondsPerSecond = _get64bitArrayFromFloat(1000);
155
			_cachedMillisecondsBetween1582and1970 = _multiplyTwo64bitArrays(arraySecondsBetween1582and1970, arrayMillisecondsPerSecond);
156
			_cachedHundredNanosecondIntervalsPerMillisecond = _get64bitArrayFromFloat(10000);
157
		}
158
		var arrayMillisecondsSince1970 = nowArray;
159
		var arrayMillisecondsSince1582 = _addTwo64bitArrays(_cachedMillisecondsBetween1582and1970, arrayMillisecondsSince1970);
160
		var arrayHundredNanosecondIntervalsSince1582 = _multiplyTwo64bitArrays(arrayMillisecondsSince1582, _cachedHundredNanosecondIntervalsPerMillisecond);
161
		if (now.valueOf() == _dateValueOfPreviousUuid) {
162
			arrayHundredNanosecondIntervalsSince1582[3] += _nextIntraMillisecondIncrement;
163
			_carry(arrayHundredNanosecondIntervalsSince1582);
164
			_nextIntraMillisecondIncrement += 1;
165
			if (_nextIntraMillisecondIncrement == 10000) {
166
				while (now.valueOf() == _dateValueOfPreviousUuid) {
167
					now = new Date();
168
				}
169
			}
170
		} else {
171
			_dateValueOfPreviousUuid = now.valueOf();
172
			_nextIntraMillisecondIncrement = 1;
173
		}
174
		var hexTimeLowLeftHalf = arrayHundredNanosecondIntervalsSince1582[2].toString(HEX_RADIX);
175
		var hexTimeLowRightHalf = arrayHundredNanosecondIntervalsSince1582[3].toString(HEX_RADIX);
176
		var hexTimeLow = _padWithLeadingZeros(hexTimeLowLeftHalf, 4) + _padWithLeadingZeros(hexTimeLowRightHalf, 4);
177
		var hexTimeMid = arrayHundredNanosecondIntervalsSince1582[1].toString(HEX_RADIX);
178
		hexTimeMid = _padWithLeadingZeros(hexTimeMid, 4);
179
		var hexTimeHigh = arrayHundredNanosecondIntervalsSince1582[0].toString(HEX_RADIX);
180
		hexTimeHigh = _padWithLeadingZeros(hexTimeHigh, 3);
181
		var hyphen = "-";
182
		var versionCodeForTimeBasedUuids = "1";
183
		var resultUuid = hexTimeLow + hyphen + hexTimeMid + hyphen + versionCodeForTimeBasedUuids + hexTimeHigh + hyphen + _uuidClockSeqString + hyphen + node;
184
		resultUuid = resultUuid.toLowerCase();
185
		return resultUuid;
186
	}
187
	this.setNode = function (node) {
188
		dojo.lang.assert((node === null) || (node.length == 12));
189
		_uniformNode = node;
190
	};
191
	this.getNode = function () {
192
		return _uniformNode;
193
	};
194
	this.generate = function (input) {
195
		var nodeString = null;
196
		var returnType = null;
197
		if (input) {
198
			if (dojo.lang.isObject(input) && !dojo.lang.isBuiltIn(input)) {
199
				var namedParameters = input;
200
				dojo.lang.assertValidKeywords(namedParameters, ["node", "hardwareNode", "pseudoNode", "returnType"]);
201
				var node = namedParameters["node"];
202
				var hardwareNode = namedParameters["hardwareNode"];
203
				var pseudoNode = namedParameters["pseudoNode"];
204
				nodeString = (node || pseudoNode || hardwareNode);
205
				if (nodeString) {
206
					var firstCharacter = nodeString.charAt(0);
207
					var firstDigit = parseInt(firstCharacter, HEX_RADIX);
208
					if (hardwareNode) {
209
						dojo.lang.assert((firstDigit >= 0) && (firstDigit <= 7));
210
					}
211
					if (pseudoNode) {
212
						dojo.lang.assert((firstDigit >= 8) && (firstDigit <= 15));
213
					}
214
				}
215
				returnType = namedParameters["returnType"];
216
				dojo.lang.assertType(returnType, Function, {optional:true});
217
			} else {
218
				if (dojo.lang.isString(input)) {
219
					nodeString = input;
220
					returnType = null;
221
				} else {
222
					if (dojo.lang.isFunction(input)) {
223
						nodeString = null;
224
						returnType = input;
225
					}
226
				}
227
			}
228
			if (nodeString) {
229
				dojo.lang.assert(nodeString.length == 12);
230
				var integer = parseInt(nodeString, HEX_RADIX);
231
				dojo.lang.assert(isFinite(integer));
232
			}
233
			dojo.lang.assertType(returnType, Function, {optional:true});
234
		}
235
		var uuidString = _generateUuidString(nodeString);
236
		var returnValue;
237
		if (returnType && (returnType != String)) {
238
			returnValue = new returnType(uuidString);
239
		} else {
240
			returnValue = uuidString;
241
		}
242
		return returnValue;
243
	};
244
}();
245