| 1 |
/** |
|---|
| 2 |
* Copyright (c) 2008 michiyasu wada |
|---|
| 3 |
* http://www.seyself.com/ |
|---|
| 4 |
* |
|---|
| 5 |
* Distributed under The MIT License. |
|---|
| 6 |
* [http://www.opensource.org/licenses/mit-license.php] |
|---|
| 7 |
*/ |
|---|
| 8 |
|
|---|
| 9 |
package |
|---|
| 10 |
{ |
|---|
| 11 |
import flash.utils.Proxy; |
|---|
| 12 |
import flash.utils.flash_proxy; |
|---|
| 13 |
|
|---|
| 14 |
/** |
|---|
| 15 |
* 指定された型のみで構成される配列を作ります。 |
|---|
| 16 |
* Array型として抜き出す場合は toArray() メソッドを用います。 |
|---|
| 17 |
*/ |
|---|
| 18 |
dynamic public class ArrayOf extends Proxy |
|---|
| 19 |
{ |
|---|
| 20 |
|
|---|
| 21 |
private var __type:Class; |
|---|
| 22 |
public function get type():Class |
|---|
| 23 |
{ |
|---|
| 24 |
return __type; |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
private var __container:Array; |
|---|
| 28 |
|
|---|
| 29 |
public function get length():uint |
|---|
| 30 |
{ |
|---|
| 31 |
return __container.length; |
|---|
| 32 |
} |
|---|
| 33 |
|
|---|
| 34 |
public function ArrayOf( type:Class ) |
|---|
| 35 |
{ |
|---|
| 36 |
this.__container = []; |
|---|
| 37 |
this.__type = type; |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
public function toArray():Array |
|---|
| 41 |
{ |
|---|
| 42 |
return __container.concat(); |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
override flash_proxy function callProperty(methodName:*, ... args):* |
|---|
| 46 |
{ |
|---|
| 47 |
var res:*; |
|---|
| 48 |
var f:Boolean = true; |
|---|
| 49 |
var len:uint, i:uint; |
|---|
| 50 |
switch (methodName.toString()) { |
|---|
| 51 |
case "push" : { |
|---|
| 52 |
f = true; |
|---|
| 53 |
len = args.length; |
|---|
| 54 |
for (i = 0; i < len;i++ ) { |
|---|
| 55 |
if (!(args[i] is __type)) { |
|---|
| 56 |
f = false; |
|---|
| 57 |
break; |
|---|
| 58 |
} |
|---|
| 59 |
} |
|---|
| 60 |
if (f) res = __container.push.apply( __container, args ); |
|---|
| 61 |
else __errorMessage(); |
|---|
| 62 |
break; |
|---|
| 63 |
} |
|---|
| 64 |
case "unshift" : { |
|---|
| 65 |
f = true; |
|---|
| 66 |
len = args.length; |
|---|
| 67 |
for (i = 0; i < len;i++ ) { |
|---|
| 68 |
if (!(args[i] is __type)) { |
|---|
| 69 |
f = false; |
|---|
| 70 |
break; |
|---|
| 71 |
} |
|---|
| 72 |
} |
|---|
| 73 |
if (f) res = __container.unshift.apply( __container, args ); |
|---|
| 74 |
else __errorMessage(); |
|---|
| 75 |
break; |
|---|
| 76 |
} |
|---|
| 77 |
case "splice" : { |
|---|
| 78 |
f = true; |
|---|
| 79 |
len = args.length; |
|---|
| 80 |
for ( i = 2; i < len;i++ ) { |
|---|
| 81 |
if (!(args[i] is __type)) { |
|---|
| 82 |
f = false; |
|---|
| 83 |
break; |
|---|
| 84 |
} |
|---|
| 85 |
} |
|---|
| 86 |
if (f) res = __container.splice.apply( __container, args ); |
|---|
| 87 |
else __errorMessage(); |
|---|
| 88 |
break; |
|---|
| 89 |
} |
|---|
| 90 |
case "concat" : { |
|---|
| 91 |
f = true; |
|---|
| 92 |
len = args.length; |
|---|
| 93 |
var tmp:Array = []; |
|---|
| 94 |
for ( i = 0; i < len; i++ ) { |
|---|
| 95 |
if (args[i] is __type) { |
|---|
| 96 |
tmp.push(args[i]); |
|---|
| 97 |
} else { |
|---|
| 98 |
if ( args[i] is ArrayOf ) { |
|---|
| 99 |
if (args[i].type != __type) { |
|---|
| 100 |
f = false; |
|---|
| 101 |
break; |
|---|
| 102 |
} else { |
|---|
| 103 |
tmp.push( args[i].toArray() ); |
|---|
| 104 |
} |
|---|
| 105 |
} else { |
|---|
| 106 |
f = false; |
|---|
| 107 |
break; |
|---|
| 108 |
} |
|---|
| 109 |
} |
|---|
| 110 |
} |
|---|
| 111 |
if ( f ) { |
|---|
| 112 |
var r:Array = __container.concat.apply( __container, tmp ); |
|---|
| 113 |
var n:uint = r.length; |
|---|
| 114 |
res = new ArrayOf(__type); |
|---|
| 115 |
for (i = 0; i < n;i++ ) { |
|---|
| 116 |
res[i] = r[i]; |
|---|
| 117 |
} |
|---|
| 118 |
} |
|---|
| 119 |
else { |
|---|
| 120 |
__errorMessage(); |
|---|
| 121 |
} |
|---|
| 122 |
break; |
|---|
| 123 |
} |
|---|
| 124 |
default: { |
|---|
| 125 |
res = __container[methodName].apply(__container, args); |
|---|
| 126 |
break; |
|---|
| 127 |
} |
|---|
| 128 |
} |
|---|
| 129 |
return res; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
override flash_proxy function getProperty(name:*):* |
|---|
| 133 |
{ |
|---|
| 134 |
return __container[name]; |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
override flash_proxy function setProperty(name:*, value:*):void |
|---|
| 138 |
{ |
|---|
| 139 |
if( value is __type ){ |
|---|
| 140 |
__container[name] = value; |
|---|
| 141 |
} else { |
|---|
| 142 |
__errorMessage(); |
|---|
| 143 |
} |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
final override flash_proxy function nextNameIndex (index:int):int |
|---|
| 147 |
{ |
|---|
| 148 |
if (index < __container.length) { |
|---|
| 149 |
return index+1; |
|---|
| 150 |
} else { |
|---|
| 151 |
return 0; |
|---|
| 152 |
} |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
final override flash_proxy function nextValue(index:int):* |
|---|
| 156 |
{ |
|---|
| 157 |
return __container[index-1]; |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
final override flash_proxy function nextName(index:int):String |
|---|
| 161 |
{ |
|---|
| 162 |
return String(index-1); |
|---|
| 163 |
} |
|---|
| 164 |
|
|---|
| 165 |
private function __errorMessage():void |
|---|
| 166 |
{ |
|---|
| 167 |
var cName:String = ("" + __type).replace("[class ", "").replace("]",""); |
|---|
| 168 |
throw new ArgumentError(cName + " と関連しない型の値は格納できません。"); |
|---|
| 169 |
} |
|---|
| 170 |
|
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
} |
|---|