1  /*
     2   * Copyright the original author or authors.
     3   * 
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (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.mozilla.org/MPL/MPL-1.1.html
     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, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import org.as2lib.core.BasicClass;
    18  import org.as2lib.util.ClassUtil;
    19  import org.as2lib.env.reflect.ReflectUtil;
    20  import org.as2lib.env.except.IllegalArgumentException;
    21  import org.as2lib.env.except.IllegalStateException;
    22  import org.as2lib.env.event.broadcaster.EventBroadcasterFactory;
    23  import org.as2lib.env.event.broadcaster.EventBroadcaster;
    24  
    25  /**
    26   * {@code DynamicEventBroadcasterFactory} creates and returns any kind of event
    27   * broadcaster. The specific event broadcaster class can be specified on construction
    28   * or with the {@link #setEventBroadcasterClass} method.
    29   * 
    30   * @author Martin Heidegger
    31   */
    32  class org.as2lib.env.event.broadcaster.DynamicEventBroadcasterFactory extends BasicClass implements EventBroadcasterFactory {
    33  	
    34  	/** The event broadcaster class to return instances of. */
    35  	private var clazz:Function;
    36  	
    37  	/**
    38  	 * Constructs a new {@code DynamicEventBroadcasterFactory} instance.
    39  	 *
    40  	 * @param clazz the {@link EventBroadcaster} implementation to return instances of
    41  	 */
    42  	public function DynamicEventBroadcasterFactory(clazz:Function) {
    43  		setEventBroadcasterClass(clazz);
    44  	}
    45  	
    46  	/**
    47  	 * Sets the {@link EventBroadcaster} implementation to return instances of.
    48  	 * 
    49  	 * @param clazz the {@link EventBroadcaster} implementation to return instances of
    50  	 * @throws IllegalArgumentException if the passed-in {@code clazz} is not an
    51  	 * implementation of the {@code EventBroadcaster} interface
    52  	 */
    53  	public function setEventBroadcasterClass(clazz:Function) {
    54  		if (!ClassUtil.isImplementationOf(clazz, EventBroadcaster)){
    55  			var className:String = ReflectUtil.getTypeNameForType(clazz);
    56  			if (!className) className = "unknown";
    57  			throw new IllegalArgumentException("Argument 'clazz' [" + clazz + ":" + className + "] is not an implementation of the 'org.as2lib.env.event.EventBroadcaster' interface.", this, arguments);
    58  		}
    59  		this.clazz = clazz;
    60  	}
    61  	
    62  	/**
    63  	 * Creates and returns a new instance of a the specified class.
    64  	 * 
    65  	 * @return a new instance of the specified class
    66  	 * @throws IllegalStateException if no class to return instances of has been set
    67  	 * yet
    68  	 */
    69  	public function createEventBroadcaster(Void):EventBroadcaster {
    70  		if (!clazz) {
    71  			throw new IllegalStateException("A class to instantiate must be set before invoking this method.", this, arguments);
    72  		}
    73  		return new clazz();
    74  	}
    75  	
    76  }