| /trunk/src/org/as3lib/utils/AbstractEnforcer.as r3 | /trunk/src/org/as3lib/utils/AbstractEnforcer.as r18 | ||
| 1 | package org.as3lib.utils | 1 | package org.as3lib.utils |
|---|---|---|---|
| 2 | { | 2 | { |
| 3 | import org.as3lib.utils.strictIs; | 3 | import org.as3lib.utils.strictIs; |
| 4 | import org.as3lib.errors.AbstractError; | 4 | import org.as3lib.errors.AbstractError; |
| 5 | 5 | ||
| 6 | /** | ||
| 7 | * A utility class for helping to enforce runtime Abstract class and | ||
| 8 | * method checking. | ||
| 9 | * | ||
| 10 | * @author Mims Wright | ||
| 11 | */ | ||
| 6 | public class AbstractEnforcer | 12 | public class AbstractEnforcer |
| 7 | { | 13 | { |
| 8 | public static function enforceConstructor(instance:Object, className:Class):void { | 14 | /** |
| 9 | if (strictIs(instance, className)) { | 15 | * When called within a constructor, ensures that the constructor |
| 16 | * cannot be instantiated unless it is subclassed. | ||
| 17 | * | ||
| 18 | * @param self A reference to the object whose constructor is being called. | ||
| 19 | * @param abstractClass The class to make abstract. The class of the constructed object. | ||
| 20 | * @throws org.as3lib.errors.AbstractError If self is an instance of abstractClass (and not a subclass). | ||
| 21 | * | ||
| 22 | * @use <code> | ||
| 23 | * package { | ||
| 24 | * public class AbstractClass { | ||
| 25 | * // this constructor cannot be called (without throwing an error) unless AbstractClass is subclassed. | ||
| 26 | * public function AbstractClass() { | ||
| 27 | * AbstractEnforcer.enforceConstructor(this, AbstractClass); | ||
| 28 | * // other constructor code here. | ||
| 29 | * } | ||
| 30 | * } | ||
| 31 | * } | ||
| 32 | * </code> | ||
| 33 | */ | ||
| 34 | public static function enforceConstructor(self:Object, abstractClass:Class):void { | ||
| 35 | if (strictIs(self, abstractClass)) { | ||
| 10 | throw (new AbstractError(AbstractError.CONSTRUCTOR_ERROR)); | 36 | throw (new AbstractError(AbstractError.CONSTRUCTOR_ERROR)); |
| 11 | } | 37 | } |
| 12 | } | 38 | } |
| 13 | 39 | ||
| 40 | /** | ||
| 41 | * When called within a method, will throw an error if the method is | ||
| 42 | * called, thus forcing it to be overridden in a subclass to be used. | ||
| 43 | * Note, there can be no default implementation of an abstract method so | ||
| 44 | * using super.methodName() will still throw the error. | ||
| 45 | * | ||
| 46 | * @throws org.as3lib.errors.AbstractError If the method is called without being overridden. | ||
| 47 | * | ||
| 48 | * @use <code> | ||
| 49 | * package { | ||
| 50 | * public class AbstractClass { | ||
| 51 | * // this method must be overridden. | ||
| 52 | * public function abstractMethod() { | ||
| 53 | * AbstractEnforcer.enforceMethod(); | ||
| 54 | * } | ||
| 55 | * } | ||
| 56 | * } | ||
| 57 | * </code> | ||
| 58 | */ | ||
| 14 | public static function enforceMethod ():void { | 59 | public static function enforceMethod ():void { |
| 15 | throw (new AbstractError(AbstractError.METHOD_ERROR)); | 60 | throw (new AbstractError(AbstractError.METHOD_ERROR)); |
| 16 | } | 61 | } |
| 17 | } | 62 | } |
| 18 | } | 63 | } |