| 1 |
/* |
|---|
| 2 |
* Copyright(c) 2006-2007 the Spark project. |
|---|
| 3 |
* |
|---|
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 5 |
* you may not use this file except in compliance with the License. |
|---|
| 6 |
* You may obtain a copy of the License at |
|---|
| 7 |
* |
|---|
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 9 |
* |
|---|
| 10 |
* Unless required by applicable law or agreed to in writing, software |
|---|
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
|---|
| 13 |
* either express or implied. See the License for the specific language |
|---|
| 14 |
* governing permissions and limitations under the License. |
|---|
| 15 |
*/ |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
package org.libspark.utils |
|---|
| 19 |
{ |
|---|
| 20 |
import flash.utils.Dictionary; |
|---|
| 21 |
import flash.utils.getDefinitionByName; |
|---|
| 22 |
|
|---|
| 23 |
/** |
|---|
| 24 |
* Class のためのユーティリティクラスです |
|---|
| 25 |
*/ |
|---|
| 26 |
public class ClassUtil |
|---|
| 27 |
{ |
|---|
| 28 |
|
|---|
| 29 |
/** |
|---|
| 30 |
* 指定したオブジェクトを型指定無しで返します。 |
|---|
| 31 |
* |
|---|
| 32 |
* @param instance キャスト対象オブジェクト |
|---|
| 33 |
* @return 指定したオブジェクトが型指定無しで返されます。 |
|---|
| 34 |
* @author michi at seyself.com |
|---|
| 35 |
*/ |
|---|
| 36 |
public static function cast(instance:Object):* |
|---|
| 37 |
{ |
|---|
| 38 |
return instance; |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
/** |
|---|
| 42 |
* インスタンスオブジェクトからクラス名を表す文字列を返します。 |
|---|
| 43 |
* |
|---|
| 44 |
* @param instance クラス名を取得するオブジェクト |
|---|
| 45 |
* @return クラス名を表す文字列 |
|---|
| 46 |
* @author michi at seyself.com |
|---|
| 47 |
*/ |
|---|
| 48 |
public static function getClassName(instance:Object):String |
|---|
| 49 |
{ |
|---|
| 50 |
var classObject:Class = instance.constructor; |
|---|
| 51 |
return String(classObject).replace(/\[class ([^\]]+)\]/, |
|---|
| 52 |
function():String { return arguments[1]; } ); |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
/** |
|---|
| 56 |
* クラスのコンストラクタ内で呼び出すことで、そのクラスを抽象クラスのように振舞わせます. |
|---|
| 57 |
* このメソッドは、必ずクラスのコンストラクタ内で使用してください。 |
|---|
| 58 |
* このメソッドを使用すると、そのクラスは直接インスタンスを作成できなくなります。 |
|---|
| 59 |
* インスタンスを作成するためには、このクラスを継承したサブクラスを作る必要があります。 |
|---|
| 60 |
* |
|---|
| 61 |
* @param instance 必ず this を指定します |
|---|
| 62 |
* @param klass 現在のクラスを指定します。 |
|---|
| 63 |
* @example 以下のコードでは AbstractTest クラスと、これを継承した AbstractSubClass を作成します。 |
|---|
| 64 |
* AbstractTest クラスからインスタンスを作成しようとすると ArgumentError が throw されますが、 |
|---|
| 65 |
* AbstractTest クラスを継承した AbstractSubClass は、正常にインスタンスを生成することができます。 |
|---|
| 66 |
* <listing> |
|---|
| 67 |
* import org.libspark.utils.ClassUtil; |
|---|
| 68 |
* public class AbstractTest |
|---|
| 69 |
* { |
|---|
| 70 |
* public function AbstractTest() |
|---|
| 71 |
* { |
|---|
| 72 |
* ClassUtil.abstraction(this, AbstractTest); |
|---|
| 73 |
* } |
|---|
| 74 |
* |
|---|
| 75 |
* public function testCall():void |
|---|
| 76 |
* { |
|---|
| 77 |
* trace("create instance ok!"); |
|---|
| 78 |
* } |
|---|
| 79 |
* } |
|---|
| 80 |
* |
|---|
| 81 |
* public class AbstractSubClass extends AbstractTest |
|---|
| 82 |
* { |
|---|
| 83 |
* public function AbstractSubClass() |
|---|
| 84 |
* { |
|---|
| 85 |
* testCall(); |
|---|
| 86 |
* } |
|---|
| 87 |
* } |
|---|
| 88 |
* </listing> |
|---|
| 89 |
*/ |
|---|
| 90 |
public static function abstraction(instance:Object, klass:Class):void |
|---|
| 91 |
{ |
|---|
| 92 |
if (instance.constructor == klass) throw new ArgumentError |
|---|
| 93 |
("Error #2012: "+getClassName(instance)+"$ クラスをインスタンス化することはできません。"); |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
/** |
|---|
| 97 |
* クラスのコンストラクタ内で呼び出すことで、そのクラスのインスタンス数を制限します。 |
|---|
| 98 |
* このメソッドは、必ずクラスのコンストラクタ内で使用してください。 |
|---|
| 99 |
* このメソッドを使用すると、コンストラクタは 1 度だけ呼び出すことができるようになります。 |
|---|
| 100 |
* |
|---|
| 101 |
* @param instance 必ず this を指定します |
|---|
| 102 |
* @example 以下のコードでは SingletonTest のコンストラクタは 1 度しか呼び出せなくなります。 |
|---|
| 103 |
* 通常 getInstance() メソッド等を作成して併用しますが、サンプルでは省略しています。 |
|---|
| 104 |
* <listing> |
|---|
| 105 |
* import org.libspark.utils.ClassUtil; |
|---|
| 106 |
* public class SingletonTest |
|---|
| 107 |
* { |
|---|
| 108 |
* public function SingletonTest() |
|---|
| 109 |
* { |
|---|
| 110 |
* ClassUtil.singleton(this); |
|---|
| 111 |
* } |
|---|
| 112 |
* } |
|---|
| 113 |
* </listing> |
|---|
| 114 |
*/ |
|---|
| 115 |
public static function singleton(instance:Object):void |
|---|
| 116 |
{ |
|---|
| 117 |
if ( _SingletonContainer[instance.constructor] ) throw new ArgumentError |
|---|
| 118 |
("Error #2012: "+getClassName(instance)+" クラスをインスタンス化することはできません。"); |
|---|
| 119 |
else _SingletonContainer[instance.constructor] = true; |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
/** |
|---|
| 123 |
* クラスのコンストラクタ内で呼び出すことで、そのクラスのインスタンス数を制限します。 |
|---|
| 124 |
* このメソッドは、必ずクラスのコンストラクタ内で使用してください。 |
|---|
| 125 |
* このメソッドを使用すると、コンストラクタは指定回数まで呼び出すことができるようになります。 |
|---|
| 126 |
* |
|---|
| 127 |
* @param instance 必ず this を指定します |
|---|
| 128 |
* @param maxCount コンストラクタの呼び出し可能数 |
|---|
| 129 |
* @example 以下のコードでは MultiSingletonTest のコンストラクタは 3 度だけ呼び出すことができます。 |
|---|
| 130 |
* 通常 getInstance() メソッド等を作成して併用しますが、サンプルでは省略しています。 |
|---|
| 131 |
* <listing> |
|---|
| 132 |
* import org.libspark.utils.ClassUtil; |
|---|
| 133 |
* public class MultiSingletonTest |
|---|
| 134 |
* { |
|---|
| 135 |
* public function MultiSingletonTest() |
|---|
| 136 |
* { |
|---|
| 137 |
* ClassUtil.multiSingleton(this, 3); |
|---|
| 138 |
* } |
|---|
| 139 |
* } |
|---|
| 140 |
* </listing> |
|---|
| 141 |
*/ |
|---|
| 142 |
public static function multiSingleton(instance:Object, maxCount:uint=1):void |
|---|
| 143 |
{ |
|---|
| 144 |
var cnst:Object = instance.constructor; |
|---|
| 145 |
if ( _SingletonContainer[cnst] ) |
|---|
| 146 |
if (_SingletonContainer[cnst].length < maxCount) |
|---|
| 147 |
_SingletonContainer[cnst].push([instance]); |
|---|
| 148 |
else throw new ArgumentError |
|---|
| 149 |
("Error #2012: "+getClassName(instance)+" クラスをインスタンス化することはできません。"); |
|---|
| 150 |
else _SingletonContainer[cnst] = [instance]; |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
//---------------------------------------------------- |
|---|
| 156 |
// PRIVATE PROPERTIES |
|---|
| 157 |
//---------------------------------------------------- |
|---|
| 158 |
|
|---|
| 159 |
private static var _SingletonContainer:Dictionary = new Dictionary(true); |
|---|
| 160 |
private static var _MultiSingletonContainer:Dictionary = new Dictionary(false); |
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
} |
|---|
| 164 |
|
|---|
| 165 |
} |
|---|