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