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.env.event.EventListenerSource; 19 import org.as2lib.util.ArrayUtil; 20 21 /** 22 * {@code SimpleEventListenerSource} manages listeners in the simplest way possible. 23 * 24 * @author Simon Wacker 25 */ 26 class org.as2lib.env.event.SimpleEventListenerSource extends BasicClass implements EventListenerSource { 27 28 /** All added listeners. */ 29 private var l:Array; 30 31 /** 32 * Constructs a new {@code SimpleEventListenerSource} instance. 33 * 34 * @param listeners (optional) a collection of listeners to populate this listener 35 * source with 36 */ 37 public function SimpleEventListenerSource(listeners:Array) { 38 this.l = new Array(); 39 if (listeners) { 40 addAllListeners(listeners); 41 } 42 } 43 44 /** 45 * Adds the passed-in {@code listener}. 46 * 47 * <p>The listener will only be added if it is neither {@code null} nor 48 * {@code undefined} and if it has not already been added to this listener source. 49 * 50 * @param listener the listener to add 51 */ 52 public function addListener(listener):Void { 53 if (listener) { 54 if (!hasListener(listener)) { 55 this.l.push(listener); 56 } 57 } 58 } 59 60 /** 61 * Adds all listeners contained in the passed-in {@code listeners} array. 62 * 63 * <p>If the passed-in {@code listeners} array is {@code null} or {@code undefined} 64 * it will be ignored. If an individual listener is {@code null} or 65 * {@code undefined} it will be ignored too. 66 * 67 * <p>Note that the order of the listeners contained in the passed-in 68 * {@code listeners} array is preserved. 69 * 70 * @param listeners the listeners to add 71 * @see #addListener 72 */ 73 public function addAllListeners(listeners:Array):Void { 74 if (listeners) { 75 var h:Number = listeners.length; 76 // the original order of the passed-in 'listeners' is preserved 77 for (var i:Number = 0; i < h; i++) { 78 addListener(listeners[i]); 79 } 80 } 81 } 82 83 /** 84 * Removes the passed-in {@code listener}. 85 * 86 * <p>The removal will be ignored if the passed-in {@code listener} is {@code null} 87 * or {@code undefined}. 88 * 89 * @param listener the listener to remove 90 */ 91 public function removeListener(listener):Void { 92 if (listener) { 93 var i:Number = this.l.length; 94 while (--i > -1) { 95 if (this.l[i] == listener) { 96 this.l.splice(i, 1); 97 return; 98 } 99 } 100 } 101 } 102 103 /** 104 * Removes all added listeners. 105 */ 106 public function removeAllListeners(Void):Void { 107 this.l = new Array(); 108 } 109 110 /** 111 * Returns all added listeners. 112 * 113 * @return all added listeners 114 */ 115 public function getAllListeners(Void):Array { 116 return this.l.concat(); 117 } 118 119 /** 120 * Returns {@code true} if passed-in {@code listener} has been added. 121 * 122 * @param listener the listener to check whether it has been added 123 * @return {@code true} if the {@code listener} has been added 124 */ 125 public function hasListener(listener):Boolean { 126 return ArrayUtil.contains(this.l, listener); 127 } 128 129 }