山水

May 5th, 2010

看山是山,看水是水——
知道的少,从这山到那水走这条道就可以了,只不过这道真难走

看山不是山,看水不是水——
知道的多了,道路数不胜数,不知道从这山到那水该走哪一条道

看山还是山,看水还是水,但是山更绿,水更清——
知道的非常多了,晓得从这山到那水这条道更好走一些


“Type(var)” vs “var as Type”

April 23rd, 2010

Description:
I have done an experiment to test the performance of data type conversions in actionscript 3. Create a new flex 4 based application with the code below, then compile and run it:


CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
			   xmlns:s="library://ns.adobe.com/flex/spark"
			   xmlns:mx="library://ns.adobe.com/flex/mx"
			   minWidth="955" minHeight="600"
			   xmlns:controls="com.wds.framework.controls.*"
			   initialize="application1_initializeHandler(event)">
	<fx:Script>
		<![CDATA[
			import mx.containers.Canvas;
			import mx.core.Container;
			import mx.core.FlexSprite;
			import mx.core.IContainer;
			import mx.core.IUIComponent;
			import mx.core.UIComponent;
			import mx.events.FlexEvent;
			import mx.utils.ObjectProxy;
 
			protected function application1_initializeHandler(event:FlexEvent):void
			{
				var n:int = 1000000;
				var i:int = 0;
				var timer:int = 0;
				var obj:*;
 
				trace("--------------------------");
				////////////////////////////////////////////////////
				trace("Basic Types");
				trace("-----------");
				obj = {};
				trace("obj = {}");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Object(obj);
				}
				trace("Object(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Object;
				}
				trace("obj as Object",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = "str";
				trace("obj = \"str\"");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					String(obj);
				}
				trace("String(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as String;
				}
				trace("obj as String",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = 0.5;
				trace("obj = 0.5");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Number(obj);
				}
				trace("Number(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Number;
				}
				trace("obj as Number",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = -12;
				trace("obj = -12");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					int(obj);
				}
				trace("int(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as int;
				}
				trace("obj as int",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = 12;
				trace("obj = 12");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					uint(obj);
				}
				trace("uint(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as uint;
				}
				trace("obj as uint",getTimer() - timer);
				trace("-----------");
 
				////////////////////////////////////////////////////
				trace("Dynamic Classes");
				trace("-----------");
				obj = new Array();
				trace("obj = new Array()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Array(obj);
				}
				trace("Array(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Array;
				}
				trace("obj as Array",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new ObjectProxy();
				trace("obj = new ObjectProxy()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					ObjectProxy(obj);
				}
				trace("ObjectProxy(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as ObjectProxy;
				}
				trace("obj as ObjectProxy",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new MyDynamicClass();
				trace("obj = new MyDynamicClass()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					MyDynamicClass(obj);
				}
				trace("MyDynamicClass(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as MyDynamicClass;
				}
				trace("obj as MyDynamicClass",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new MyProxyClass();
				trace("obj = new MyProxyClass()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					MyProxyClass(obj);
				}
				trace("MyProxyClass(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as MyProxyClass;
				}
				trace("obj as MyProxyClass",getTimer() - timer);
				trace("-----------");
				////////////////////////////////////////////////////
				trace("Typed Classes");
				trace("-----------");
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Canvas(obj);
				}
				trace("Canvas(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Canvas;
				}
				trace("obj as Canvas",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Container(obj);
				}
				trace("Container(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Container;
				}
				trace("obj as Container",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					UIComponent(obj);
				}
				trace("UIComponent(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as UIComponent;
				}
				trace("obj as UIComponent",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Sprite(obj);
				}
				trace("Sprite(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Sprite;
				}
				trace("obj as Sprite",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					DisplayObject(obj);
				}
				trace("DisplayObject(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as DisplayObject;
				}
				trace("obj as DisplayObject",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					EventDispatcher(obj);
				}
				trace("EventDispatcher(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as EventDispatcher;
				}
				trace("obj as EventDispatcher",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Object(obj);
				}
				trace("Object(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Object;
				}
				trace("obj as Object",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Container();
				trace("obj = new Container()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Container(obj);
				}
				trace("Container(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Container;
				}
				trace("obj as Container",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new UIComponent();
				trace("obj = new UIComponent()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					UIComponent(obj);
				}
				trace("UIComponent(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as UIComponent;
				}
				trace("obj as UIComponent",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new FlexSprite();
				trace("obj = new FlexSprite()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					FlexSprite(obj);
				}
				trace("FlexSprite(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as FlexSprite;
				}
				trace("obj as FlexSprite",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Sprite();
				trace("obj = new Sprite()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Sprite(obj);
				}
				trace("Sprite(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Sprite;
				}
				trace("obj as Sprite",getTimer() - timer);
				trace("-----------");
 
				////////////////////////////////////////////////////
				trace("Interfaces");
				trace("-----------");
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					IContainer(obj);
				}
				trace("IContainer(obj)",getTimer() - timer);				
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as IContainer;
				}
				trace("obj as IContainer",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					IUIComponent(obj);
				}
				trace("IUIComponent(obj)",getTimer() - timer);				
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as IUIComponent;
				}
				trace("obj as IUIComponent",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					IEventDispatcher(obj);
				}
				trace("IEventDispatcher(obj)",getTimer() - timer);				
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as IEventDispatcher;
				}
				trace("obj as IEventDispatcher",getTimer() - timer);
				trace();
			}
 
		]]>
	</fx:Script>
</s:Application>

RESULTS:

--------------------------
Basic Types
-----------
obj = {}
Object(obj) 150
obj as Object 109

obj = "str"
String(obj) 110
obj as String 110

obj = 0.5
Number(obj) 65
obj as Number 107

obj = -12
int(obj) 67
obj as int 110

obj = 12
uint(obj) 67
obj as uint 108
-----------
Dynamic Classes
-----------
obj = new Array()
Array(obj) 1563
obj as Array 113

obj = new ObjectProxy()
ObjectProxy(obj) 126
obj as ObjectProxy 134

obj = new MyDynamicClass()
MyDynamicClass(obj) 128
obj as MyDynamicClass 138

obj = new MyProxyClass()
MyProxyClass(obj) 133
obj as MyProxyClass 135
-----------
Typed Classes
-----------
obj = new Canvas()
Canvas(obj) 131
obj as Canvas 132

obj = new Canvas()
Container(obj) 136
obj as Container 144

obj = new Canvas()
UIComponent(obj) 141
obj as UIComponent 148

obj = new Canvas()
Sprite(obj) 176
obj as Sprite 124

obj = new Canvas()
DisplayObject(obj) 167
obj as DisplayObject 122

obj = new Canvas()
EventDispatcher(obj) 170
obj as EventDispatcher 125

obj = new Canvas()
Object(obj) 150
obj as Object 108

obj = new Container()
Container(obj) 132
obj as Container 138

obj = new UIComponent()
UIComponent(obj) 134
obj as UIComponent 138

obj = new FlexSprite()
FlexSprite(obj) 133
obj as FlexSprite 134

obj = new Sprite()
Sprite(obj) 170
obj as Sprite 120
-----------
Interfaces
-----------
obj = new Canvas()
IContainer(obj) 136
obj as IContainer 147

obj = new Canvas()
IUIComponent(obj) 139
obj as IUIComponent 148

obj = new Canvas()
IEventDispatcher(obj) 120
obj as IEventDispatcher 121

Conclusion

  • To convert an Object instance to String, uint, int or Number, “Type(var)” is faster than “var as Type”
  • To convert an Object  instance to Array,  ”Type(var)” is much slower than “var as Type”, you can receive a warning “Array(x) behaves the same as new Array(x). To cast a value to type Array use the expression x as Array instead of Array(x)” with Flash Builder 4.
  • To convert an Object  instance of a sub-class to its super class, “var as Type” is faster than “Type(var)” if the super class is not Sprite or its super classes.
  • To convert an Object instance of a class to that class instance, ”var as Type” is faster than “Type(var)” sometimes if the super class is not Sprite or its super classes.
  • To convert an Object instance of a class as an interface, ”Type(var)” is faster than “var as Type”.

as3 DateParser

April 23rd, 2010
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
////////////////////////////////////////////////////////////////////////////////
// DateParser.as
//
// An as3 implementation of the java SimpleDateFormat like class based on Flex 
// SDK which can be used to parse a String with specified format to as3 Date 
// object.
//
// This code is a modification version of Daniel Wabyick's as2 
// SimpleDateFormatter(http://osflash.org/simpledateformatter) which 
// is directly adapted from Matt Kruse's Javascript class implementation.
//
// @author edison.guo(http://www.hydra1983.com)
// @author Daniel Wabyick(http://www.fluid.com)
// @author Matt Kruse (http://www.JavascriptToolbox.com)
 
// The following notice is maintained from Matt Kruse's 
// original Javascript code. 
// 
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// USAGE
// ----------------------------------------------------------------------
// These functions use the same pattern letters used by the DateFormatter 
// class provided by Flex SDK. You can find the detailed description of 
// these letter in the Adobe Flex Language Reference.
// Examples:
//		var dateString:String = "9/10/2008 GMT+0800 08:30:41";
// 		var parser:DateParser = new DateParser();
//		parser.formatString = "M/D/Y HH:NN:SS";
//		trace(parser.parse(dateString));
//		//Wed Sep 10 08:30:41 GMT+0800 2008
//
// ----------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
 
package
{
	import mx.formatters.Formatter;
	import mx.utils.StringUtil;
 
	public class DateParser
	{
		private static const VALID_PATTERN_CHARS:String = "Y,M,D,A,E,H,J,K,L,N,S,Q";
 
		public var error:String;
 
		private var _formatString:String = "YYYY-MM-DD HH:NN";
 
		public function get formatString():String
		{
			return _formatString;
		}
 
		public function set formatString(value:String):void
		{
			if(value) _formatString = value;
		}
 
		private var _defaultInvalidFormatError:String = Formatter.defaultInvalidFormatError;
 
		public function get defaultInvalidFormatError():String
		{
			return _defaultInvalidFormatError;
		}
 
		public function set defaultInvalidFormatError(value:String):void
		{
			_defaultInvalidFormatError = value;
		}
 
		private var _defaultInvalidValueError:String = Formatter.defaultInvalidValueError;
 
		public function get defaultInvalidValueError():String
		{
			return _defaultInvalidValueError;
		}
 
		public function set defaultInvalidValueError(value:String):void
		{
			_defaultInvalidValueError = value;
		}
 
		public function parse( value:Object):Date 
		{
			if (error)
				error = null;
 
			if(!(value is String))
			{
				error = defaultInvalidValueError;
				return null;
			}
 
			if(!value || value == "")
			{
				error = defaultInvalidValueError;
				return null;
			}
 
			var timezoneRegEx:RegExp = /(GMT|UTC)((\+|-)\d\d\d\d(\s)?)?/ig;
			value = StringUtil.trim(String(value).replace(timezoneRegEx, ""));
 
			var letter:String;
			var nTokens:int = 0;
			var tokens:String = "";
 
			var n:int = formatString.length;
			for (var i:int = 0; i < n; i++)
			{
				letter = formatString.charAt(i);
				if (VALID_PATTERN_CHARS.indexOf(letter) != -1 && letter != ",")
				{
					nTokens++;
					if (tokens.indexOf(letter) == -1)
					{
						tokens += letter;
					}
					else
					{
						if (letter != formatString.charAt(Math.max(i - 1, 0)))
						{
							error = defaultInvalidFormatError;
							return null;
						}
					}
				}
			}
 
			if (nTokens < 1)
			{
				error = defaultInvalidFormatError;
				return null;
			}
 
			var dataParser:DateStringParser = new DateStringParser(
				formatString, VALID_PATTERN_CHARS);
 
			return dataParser.parseValue(value);	
		}
	}
}
import mx.formatters.DateBase;
 
class DateStringParser
{
	private static var digits:String = "1234567890";
 
	private static var dayNames:Array = DateBase.dayNamesLong.concat(DateBase.dayNamesShort);
 
	private static var monthNames:Array = DateBase.monthNamesLong.concat(DateBase.monthNamesShort);
 
	public function DateStringParser(format:String, tokens:String)
	{
		super();
 
		this.format = format;
		this.tokens = tokens;
	}
 
	private var format:String;
 
	private var tokens:String;
 
	public function parseValue(value:Object):Date
	{		
		var valueString:String = String(value);
 
		var i_value:int = 0;
		var i_format:int = 0;
		var char_token:String = "";
		var token:String = "";
		var start:int = 0;
		var end:int = 0;
 
		var now:Date = new Date();
		var years:Object = now.getFullYear();
		var months:Object = now.getMonth() + 1;
		var dates:Object = now.getDate();	
		var days:Object = now.getDay();
		var hours:Object = now.getHours();
		var minutes:Object = now.getMinutes();
		var seconds:Object = now.getSeconds();
		var milliseconds:Object = now.getMilliseconds();
		var ampm:String = "";
 
		var max:int = 0;
		var min:int = 0;
 
		while(i_format < format.length)
		{
			char_token = format.charAt(i_format);
			if(tokens.indexOf(char_token) > -1)
			{
				start = format.indexOf(char_token);
				end = format.lastIndexOf(char_token);
				end = end >= 0 ? end + 1 : start + 1;
				token = format.substring(start,end);			
				i_format = end;
 
				if(token == "YYYY" || token == "YY" || token == "Y")
				{					
					if(token == "YYYY") {max = min = 4;}
					if(token == "YY") {max = min = 2;}
					if(token == "Y"){max = 4;min = 2;}
 
					years = getIntegerValue(valueString,i_value,min,max);
					if(isNaN(Number(years))) return null;
					i_value += String(years).length;
					years = Number(years);
				}
				else if(token == "MM" || token == "M")
				{
					months = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(months)) || months < 1 || months > 12) return null;
					i_value += String(months).length;
					months = Number(months);
				}
				else if(token == "MMMM" || token == "MMM")
				{
					var monthName:String = getMonthName(valueString,i_value);
					if(!monthName || monthName.length == 0) return null;
					months = getMonths(monthName);
					if(isNaN(Number(months)) || months < 1 || months > 12) return null;
					i_value += monthName.length;
					months = Number(months);
				}
				else if(token == "EE" || token == "E")
				{
					days = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(days)) || days < 1 || days > 7) return null;
					i_value += String(days).length;
					days = Number(days);
				}
				else if(token == "EEEE" || token == "EEE")
				{
					var dayName:String = getDayName(valueString,i_value);
					if(!dayName || dayName.length == 0) return null;
					days = getDays(dayName);
					if(isNaN(Number(days)) || days < 1 || days > 7) return null;
					i_value += dayName.length;
					days = Number(days);
				}
				else if(token == "DD" || token == "D")
				{
					dates = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(dates)) || dates < 1 || dates > 31) return null;
					i_value += String(dates).length;
					dates = Number(dates);
				}
				else if(token == "AA" || token == "A")
				{
					ampm = valueString.substr(i_value,2).toUpperCase();
					if(ampm != "AM" && ampm != "PM") return null;
					i_value += 2;
				}
				else if(token == "JJ" || token == "J")
				{
					hours = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(hours)) || hours < 0 || hours > 23) return null;
					i_value += String(hours).length;
					hours = Number(hours);
				}
				else if(token == "HH" || token == "H")
				{
					hours = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(hours)) || hours < 1 || hours > 24) return null;
					i_value += String(hours).length;
					hours = Number(hours);
				}				
				else if(token == "KK" || token == "K")
				{
					hours = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(hours)) || hours < 0 || hours > 11) return null;
					i_value += String(hours).length;
					hours = Number(hours);
				}
				else if(token == "LL" || token == "L")
				{
					hours = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(hours)) || hours < 1 || hours > 12) return null;
					i_value += String(hours).length;
					hours = Number(hours);
				}
				else if(token == "NN" || token == "N")
				{
					minutes = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(minutes)) || minutes < 0 || minutes > 59) return null;
					i_value += String(minutes).length;
					minutes = Number(minutes);
				}
				else if(token == "SS" || token == "S")
				{
					seconds = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(seconds)) || seconds < 0 || seconds > 59) return null;
					i_value += String(seconds).length;
					seconds = Number(seconds);
				}
				else if(token == "QQQ" || token == "QQ" || token == "Q")
				{
					milliseconds = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(milliseconds)) || milliseconds < 0 || milliseconds > 999) return null;
					i_value += String(milliseconds).length;
					milliseconds = Number(milliseconds);
				}
			}
			else
			{
				i_format ++;
				i_value ++;
			}	
		}
 
		if (i_value != valueString.length) { return null; }
 
		if (months == 2) 
		{
			var isLeapYear:Boolean = isLeapYear(Number(years));
			if(isLeapYear && dates > 28) return null;
 
			if(!isLeapYear && dates > 29) return null;
		}
 
		if (((months == 4)||(months == 6)||(months == 9)||(months == 11)) && dates > 30) 
			return null;
 
		if(hours < 12 && ampm == "PM") {hours = (Number(hours)) + 12;}
		else if(hours > 11 && ampm == "AM") {hours = (Number(hours)) - 12};
 
		return new Date(years,(Number(months)) - 1,dates,hours,minutes,seconds,milliseconds);
	}
 
	public function getIntegerValue(dateString:String,index:int,min:int,max:int):Object
	{
		var valueString:String;
		for(var i:int = max;i >= min;i--)
		{
			valueString = dateString.substring(index,index + i);
			if(valueString.length < min) return NaN;
			if(isInteger(valueString)) return valueString;
		}
		return NaN;
	}
 
	private function isInteger(valueString:String):Boolean
	{
		var n:int = valueString.length;
		for(var i:int = 0;i < n;i++)
		{
			if(!isDigit(valueString.charAt(i)))
				return false;
		}
		return true;
	}
 
	private function isDigit(char:String):Boolean
	{
		return digits.indexOf(char) > -1;
	}
 
	private function getDayName(valueString:String,index:int):String
	{		
		var n:int = dayNames.length;
		var dayName:String;
		for(var i:int = 0;i < n;i++)
		{
			dayName = dayNames[i];
			if(valueString.substr(index,dayName.length) == dayName)
				return dayName;
		}	
		return "";
	}
 
	private function getDays(dayName:String):Number
	{		
		var day:Number = dayNames.indexOf(dayName) + 1;
		day = day > 12? day - 12:day;
		day = day == 0?NaN:day;
		return day;
	}
 
	private function getMonthName(valueString:String,index:int):String
	{
		var n:int = monthNames.length;
		var monthName:String;
		for(var i:int = 0;i < n;i++)
		{
			monthName = monthNames[i];
			if(valueString.substr(index,monthName.length) == monthName)
				return monthName;
		}	
		return "";
	}
 
	private function getMonths(monthName:String):Number
	{
		var month:Number = monthNames.indexOf(monthName) + 1;
		month = month > 12? month - 12:month;
		month = month == 0?NaN:month;
		return month;
	}
 
	private function isLeapYear(year:Number):Boolean
	{
		return ((year % 4 == 0) && (year % 100 != 0)) || ( year % 400 == 0);
	}
}

你是个软件架构师吗?

April 21st, 2010

The original copy comes from InfoQ


开发和架构的界限难以捉摸。有些人告诉你它根本不存在,架构只是开发者们所做的设计过程的简单扩展。 另外一些人认为这是一个鸿沟,它只能由那些做到高度抽象,而且不会陷入实现细节的开发者才能跨越。通常,在这两个极端的观点中间某处有个可操作的平衡点;不论如何,怎么从开发转换为架构师都是个有趣的问题。

经常被用来区分软件架构和软件设计开发的关键几点包括 伸缩性和抽象程度的增加以及作出正确设计决策意义的增强。软件架构是通过一个全局的观点,宏观的视角来理解软件系统作为一个整体如何工作。

即使这能够帮助区分软件开发和架构,它并不能帮助理解某人如何从开发提升到架构。 并且,它也不能帮助识别谁能够成为一个好的软件架构师,如果你想雇人的话你如何去寻找他们以及你是否是一个软件架构师。

经验可以判定但你需要更深入地了解

要成为一个软件架构师并不是一夜之间或者一个职位的提升就能简单达到的。 这是个职责,而不是头衔。这是个进化的过程,你将会逐步得到担当这个职责所需的经验和信心。

当你寻找架构师时,需要考虑各方面的素质,他们过去的经验往往是他们有能力担当这个职责很好的判断。由于软件架构师的职责是多种多样的,所以你需要再深入了解他们在不同领域的参与度,影响力,领导力和责任感。一般来说,在大多数项目中软件架构可分为两个阶段,架构的定义,然后是它的交付。

软件架构的定义

架构的定义过程看起来非常简单明了。 你需要做的是理解需求并设计一个系统来满足需求。 但实际上并没有那么简单,根据你不同的做法,软件架构的职责之间差距很大,以及如何认真看待自己的职责而定。如下图所示,这个职责的架构定义部分,可以进一步细分成不同的元素。

The role of a hands-on software architect from a definition perspective

  1. 管理非功能性需求:软件项目经常陷入问用户要求是什么,什么是他们想要的功能,但很少问他们需要什么非功能性需求(或系统质量)有时候,干系人会告诉我们,“这个系统必须很快”,但是这太主观了。非功能性需求如果要满足的话需要明确,可度量,可获得以及可测试。大多数非功能性需求本质上是技术层面的而且经常对软件架构有很大的影响。理解非功能性要求是架构师职责非常重要的一个部分,但假设这些需求是什么并不一定是对他们的挑战。你见过多少系统真正需要24×7的运行呢?
    Management of non-functional requirements
  2. 架构定义:捕捉到了非功能性需求后,下一步是开始思考你打算如何去解决干系人提出的这些问题并定义它的架构。 公平的说每个软件系统都有一个架构,但并不是每个软件系统都有一个定义好的架构。这正是问题的关键。架构定义过程让你想清楚你打算怎么在兼顾需求和限制的情况下把问题解决好。架构定义是将结构,方针,原则和领导力引入软件项目的技术层面。定义架构是作为软件架构师的工作,但是从头开始设计一个软件系统和对已存在的系统扩展是相当不同的。
    Architecture definition
  3. 技术选型:技术选型通常是一个有趣的练习,但它也有公平的挑战,因为你需要综合考虑成本、许可、供应商关系、技术策略、兼容性、协作性、支持、部署、升级的政策以及最终用户环境等各方面。综合这些因素,通常会导致简单选择类似富客户端技术而进入了完全的噩梦。接下来的问题就是这些技术是否能真正有用。技术选型是彻头彻尾的风险管理;复杂性或不确定性太高的时候要减轻风险,当有机会或利益的时候要引入风险。技术决策需要考虑多种因素,而且所有的技术决策需要被检查和评估。这包含软件项目的主要组成部分乃至开发中引入的类库和框架。如果定义一个架构,你还需要有信心认为选择这项技术是正确的。同样在技术评估中也还是存在开发新系统和向现有的系统增加新技术的不同点。
    Technology selection
  4. 架构评估:如果你设计软件,你需要问问自己你的架构是否有用。 对我来说,一个架构是成功的,如果它满足非功能性需求,而且为其他部分的代码提供必要的基础,并为解决和存在的业务问题提供足够的平台。软件的一个最大的问题就是它复杂而抽象,导致很难从UML图或代码本身去设想出运行时的特性。在软件开发周期中我们进行了很多不同类型的测试,这样我们能够有信心我们发布的系统在推出时能够正常运行。我们为什么不对架构也这样做呢? 如果能够测试你的架构,那你就可以证明它是有效的。如果你能尽早做到这一点,你就能减少项目失败的风险,而不是简单地希望一切都好。
    Architecture evaluation
  5. 架构协作:任何一个软件都不是与世隔绝的,需要很多人理解它。 包括从需要理解和切入架构的直接开发团队到其他对安全性、数据库、运营、维护、支持等有兴趣的干系人。要想让一个软件项目成功,你需要和所有的系统干系人紧密协作来保证架构和所在的环境很好的集成。不幸的是,现状是与开发团队的架构协作很少发生,更不要说外部干系人了。
    Architecture collaboration

软件架构的发布

对于架构的发布也是同样,对于成功的软件项目参与程度的不同,也决定了软件架构职责的不同。

The role of a hands-on software architect from a delivery perspective

  1. 拥有全局的视角:为了把一个架构成功地实现,我们需要具有全局的视角并把贯穿软件开发生命周期的愿景加以宣传与推广,必要的话在整个项目中展开和完善,并对成功发布负责。如果如果你定义了一个架构,参与并保持不断发展的架构才是有意义的,而不是选择把它传递给一个“执行小组”。
    Ownership of the bigger picture
  2. 领导力:拥有全局的视角是技术领导的一个方面,但是还有其他事情在软件项目发布阶段需要做。 这包括承担责任、提供技术指导、作出技术决策以及具有权力作出这些决定。作为架构师,你需要进行技术领导来确保每件事都被考虑到,而且团队在朝着正确的方向持续前进。软件架构师职位是需要内在领导力的,虽然这听起来很明显,但很多项目团队并没有获得他们所需要的技术领导,因为架构师认为一个成功的发布并不一定是他们所关注的问题。
    Leadership
  3. 教练和指导:在大多数软件开发项目中,教练和指导经常不被重视,团队成员得不到他们需要的支持。 虽然技术领导是引导整个项目,但个人也经常需要帮助。除此以外,教练和指导提供了一个强化技能的方式,并帮助提升职业生涯。这应该是软件架构师份内的事,而且指导团队架构和设计与帮他们解决代码问题是截然不同的。
    Architecture delivery
  4. 质量保证:即使是世界上最好的架构和领导,很糟糕的交付也足以让一个具备其他成功条件的项目失败。质量保证在架构师职责中占很大一部分,但这并不只是简单做代码检查。 比如,你需要一个基线来确保,这意味着引入新的标准和工作实践。从一个软件开发的角度来说,这可能包括代码标准、设计原则和源码分析工具甚至于使用持续集成,自动化单元测试以及代码覆盖工具。可以说大多数项目质量保证做的并不够,所以你需要搞清楚什么是重要的并给予它足够的保证。对于我来说,一个项目的重要部分包括架构上的重点,关键、复杂或高度可见的业务。你要关注实效并认识到你并不能保证一切,要知道做总比不做好。
    Quality assurance
  5. 设计、开发和测试:软件架构师的职责范围的最后一件事是设计、开发和测试。作为一个实际动手的架构师并不是需要你每天都要写代码,但是它的确意味着你一直在参与项目,而且积极帮助打造和交付它。说了这么多,为什么每天写代码不应该成为一个架构师职责的一部分呢?大多数架构师都有写代码的经验,因此让这些技能保鲜是有意义的。而且,架构师能体会到团队里其他人的痛苦和感受,这样能让他们更好地理解他们的架构从开发角度看是什么样的。很多公司有政策阻止软件架构师从事写代码,因为架构师“去做那些廉价的工作太贵了” ,这显然是个错误的态度…如果架构师已经花了那么多时间精力为项目做架构,何必从政策上不允许他们多走一步来帮助项目达到最终的成功呢?当然,有些情况下卷入代码级别并不现实。比如,一个大的项目通常意味有一个更大的“全局观” 来考虑它,而且可能有时候你就是没有时间。但一般来说,一个写代码的架构师比只在旁边观望要更高效和快乐。
    Design, development and testing

你是一个软件架构师吗?

不管你认为软件开发和架构之间的界限只是一个幻觉还是个巨大的鸿沟,以上强调了人们对整个软件架构中的经验水平往往有很大的差别,而这取决于他们怎么样工作以及他们如何认真地看待他们的职责。大多数开发人员不是在某一个星期一的早晨醒来就宣布自己成为一个软件架构师的。我当然也不是,我成为软件架构师的路线是一个渐进的过程。话虽如此,但很可能同样那些开发者已经做了一部分架构的工作,不论他们的职位名称是什么。

为软件系统的架构作出贡献和自己负责定义它有很大的区别,拥有持续的、跨不同领域的技能、知识和经验构成了软件架构的职责。跨越软件开发者和架构师的界限取决于你自己,但是首先你要明白你的经验水平,才能开始架构师之旅的第一站。

关于作者

你可以认为Simon Brown是一个写代码的软件架构师或者理解架构的软件开发者。当他没有用.NET或Java开发软件的时候,Simon通常在做咨询,指导或者培训。 Simon还写过关于Java的书,在行业活动做过演讲,并且整合了一个叫Software Architecture for Developers的培训课程, 该课程基于他在Coding the Architecture描 述的软件架构。你可以通过e-mail 或 Twitter 找到他。

查看英文原文Are You a Software Architect?


ICONS AND LOGOS ARE NOT THE SAME

April 16th, 2010

The original copy comes from Pixel Resort


I often stumple upon the confusion between icon and logo design. While logos may use the same visual vocabulary as icons, let there be no doubt; Icons and Logos are two completely separate design disciplines requiring different tools and different mindsets.

The gap between the designers vocabulary and the clients knowhow can cause some problematic confusions. To alleviate this lets look at what an icon is, what a logo is and how these two things could come to be confused.

WHAT’S AN ICON?
Apart from any religious denotations an icon is a graphical representation of a concept or operation. We use icons to bridge the understanding of abstract analogies and practical use. Icons can be used to illustrate an entire application or individual operations within that application. In short, icons help us understand and recognize concepts that might otherwise be pretty hard to grasp.

I could write a very long article about the whimsical nature of icon conventions and the semiotics that guide these, but in this case it’s more relevant to look at the technical differences that is so fundamental for icon design and how these differ from logo design.

ICONS ARE NOT SCALABLE
More than often, icons are not scalable. The very idea of icons are to best convey a given message within a predetermined confined visual space. In today’s iconcentric interfaces we allow for multiple variations of the same icon. The icons that are sitting in your dock most likely have atleast 5 different states embedded, making them appear crisp in all aspects of your interaction with them. List view in OSX gives you the 16×16 pixel version while the dock uses the 256×256 pixel adaptation. These are not scalable vector versions, they are handcrafted raster masterpieces. The creator must carefully select how to best take advantage of the canvas in any given size and more than often completely recreate the icon in those sizes.

My manilla mail icon in it’s various states. Note the different layout of the elements in the smaller sizes.

ICONS ARE QUADRATIC
Icons operate within a complete square canvas. How you choose to employ that canvas is up to you, but it’s restricted to that straight edged space.

Icons are created on a neatly defined and restricted canvas

So that’s it. Icons are not scalable, they’re handcrafted raster imagery born from the desire to objectify an operation or a concept within a confined visual space. How does this differ from a logo?

WHAT’S A LOGO?
A logo is a graphical element like an ideogram and/or a carefully arranged typeface that together forms a trademark or a brand. There’s an infinite amount of ways to think about logos and logo design. Again, the important thing here is to look at the technical differences from icon design.

LOGOS ARE SCALABLE
A logo should be completely scalable. A logo is the spearhead of a company’s commercial brand or any economic or non profit entity for that matter. Therefore a logo should be replicatable across many forms of media. This has great impact on the sort of mindset you need to bring when designing logos. We’re talking strictly vectorbased output and more than often, graceful degeneration of colours all the way down to uni colours.

Logos are supposed to be scalable.

LOGOS HAVE NO BOUNDARIES
Well in theory a logo could be anything. Other than the obvious benefits of working in a format that is easily scalable and replicatable there really is very little rules compared to icon design. Icon design is very influenced by technical dimensions and the restrictions of the systems that display them. Logo design is a completely different venue. A logo could be any shape, colour or dimension – it can be waved from a 100 feet banner or tattooed on a butt cheek. It’s only constraint is that of the physical media that will display it.

WHY ARE WE CONFUSED?
Icons have taken a very prominent role in modern interfaces. This has obviously spilled over to the realm of branding where many icons serve both as application icon and branding for that entity.

Panic creates excellent software and uses their application icons as product branding

This wave of iconism™ (yes, I just invented that for this purpose) has influenced many graphic designers and a lot of the appealing aspects of the cartoony and crafty iconized style has made it’s way to modern logo design trends. Infact this style has become the posterchild for the web 2.0 movement, and such many internetbased firms have logos that uses the same visual vocabulary as icons.

Logos inspired by an Iconistic style

And while logos can certainly employ an icon-like style, and even mimic the quadratic nature of icons. Let there be no doubt, Icons and Logos are two completely separate design disciplines. It’s important to know the difference between these two things, as they inheretly seek out to fulfil two very different goals, both technically and mentally.

Below I’ve included a PSD template that supplies you with the canvas in the correct dimensions for making your own icons. If you wanna talk icon or logo design throw me an email or just have a look at my services page.

If you liked this article, why not comment and/or tweet about it. You can also hit me up on that thing called twitter