1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/compact/src/main/java/java/beans/ChangeListenerMap.java Mon Jan 28 18:12:47 2013 +0100
1.3 @@ -0,0 +1,247 @@
1.4 +/*
1.5 + * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +package java.beans;
1.29 +
1.30 +import java.util.ArrayList;
1.31 +import java.util.Collections;
1.32 +import java.util.EventListener;
1.33 +import java.util.EventListenerProxy;
1.34 +import java.util.HashMap;
1.35 +import java.util.List;
1.36 +import java.util.Map;
1.37 +import java.util.Map.Entry;
1.38 +import java.util.Set;
1.39 +
1.40 +/**
1.41 + * This is an abstract class that provides base functionality
1.42 + * for the {@link PropertyChangeSupport PropertyChangeSupport} class
1.43 + * and the {@link VetoableChangeSupport VetoableChangeSupport} class.
1.44 + *
1.45 + * @see PropertyChangeListenerMap
1.46 + * @see VetoableChangeListenerMap
1.47 + *
1.48 + * @author Sergey A. Malenkov
1.49 + */
1.50 +abstract class ChangeListenerMap<L extends EventListener> {
1.51 + private Map<String, L[]> map;
1.52 +
1.53 + /**
1.54 + * Creates an array of listeners.
1.55 + * This method can be optimized by using
1.56 + * the same instance of the empty array
1.57 + * when {@code length} is equal to {@code 0}.
1.58 + *
1.59 + * @param length the array length
1.60 + * @return an array with specified length
1.61 + */
1.62 + protected abstract L[] newArray(int length);
1.63 +
1.64 + /**
1.65 + * Creates a proxy listener for the specified property.
1.66 + *
1.67 + * @param name the name of the property to listen on
1.68 + * @param listener the listener to process events
1.69 + * @return a proxy listener
1.70 + */
1.71 + protected abstract L newProxy(String name, L listener);
1.72 +
1.73 + /**
1.74 + * Adds a listener to the list of listeners for the specified property.
1.75 + * This listener is called as many times as it was added.
1.76 + *
1.77 + * @param name the name of the property to listen on
1.78 + * @param listener the listener to process events
1.79 + */
1.80 + public final synchronized void add(String name, L listener) {
1.81 + if (this.map == null) {
1.82 + this.map = new HashMap<String, L[]>();
1.83 + }
1.84 + L[] array = this.map.get(name);
1.85 + int size = (array != null)
1.86 + ? array.length
1.87 + : 0;
1.88 +
1.89 + L[] clone = newArray(size + 1);
1.90 + clone[size] = listener;
1.91 + if (array != null) {
1.92 + System.arraycopy(array, 0, clone, 0, size);
1.93 + }
1.94 + this.map.put(name, clone);
1.95 + }
1.96 +
1.97 + /**
1.98 + * Removes a listener from the list of listeners for the specified property.
1.99 + * If the listener was added more than once to the same event source,
1.100 + * this listener will be notified one less time after being removed.
1.101 + *
1.102 + * @param name the name of the property to listen on
1.103 + * @param listener the listener to process events
1.104 + */
1.105 + public final synchronized void remove(String name, L listener) {
1.106 + if (this.map != null) {
1.107 + L[] array = this.map.get(name);
1.108 + if (array != null) {
1.109 + for (int i = 0; i < array.length; i++) {
1.110 + if (listener.equals(array[i])) {
1.111 + int size = array.length - 1;
1.112 + if (size > 0) {
1.113 + L[] clone = newArray(size);
1.114 + System.arraycopy(array, 0, clone, 0, i);
1.115 + System.arraycopy(array, i + 1, clone, i, size - i);
1.116 + this.map.put(name, clone);
1.117 + }
1.118 + else {
1.119 + this.map.remove(name);
1.120 + if (this.map.isEmpty()) {
1.121 + this.map = null;
1.122 + }
1.123 + }
1.124 + break;
1.125 + }
1.126 + }
1.127 + }
1.128 + }
1.129 + }
1.130 +
1.131 + /**
1.132 + * Returns the list of listeners for the specified property.
1.133 + *
1.134 + * @param name the name of the property
1.135 + * @return the corresponding list of listeners
1.136 + */
1.137 + public final synchronized L[] get(String name) {
1.138 + return (this.map != null)
1.139 + ? this.map.get(name)
1.140 + : null;
1.141 + }
1.142 +
1.143 + /**
1.144 + * Sets new list of listeners for the specified property.
1.145 + *
1.146 + * @param name the name of the property
1.147 + * @param listeners new list of listeners
1.148 + */
1.149 + public final void set(String name, L[] listeners) {
1.150 + if (listeners != null) {
1.151 + if (this.map == null) {
1.152 + this.map = new HashMap<String, L[]>();
1.153 + }
1.154 + this.map.put(name, listeners);
1.155 + }
1.156 + else if (this.map != null) {
1.157 + this.map.remove(name);
1.158 + if (this.map.isEmpty()) {
1.159 + this.map = null;
1.160 + }
1.161 + }
1.162 + }
1.163 +
1.164 + /**
1.165 + * Returns all listeners in the map.
1.166 + *
1.167 + * @return an array of all listeners
1.168 + */
1.169 + public final synchronized L[] getListeners() {
1.170 + if (this.map == null) {
1.171 + return newArray(0);
1.172 + }
1.173 + List<L> list = new ArrayList<L>();
1.174 +
1.175 + L[] listeners = this.map.get(null);
1.176 + if (listeners != null) {
1.177 + for (L listener : listeners) {
1.178 + list.add(listener);
1.179 + }
1.180 + }
1.181 + for (Entry<String, L[]> entry : this.map.entrySet()) {
1.182 + String name = entry.getKey();
1.183 + if (name != null) {
1.184 + for (L listener : entry.getValue()) {
1.185 + list.add(newProxy(name, listener));
1.186 + }
1.187 + }
1.188 + }
1.189 + return list.toArray(newArray(list.size()));
1.190 + }
1.191 +
1.192 + /**
1.193 + * Returns listeners that have been associated with the named property.
1.194 + *
1.195 + * @param name the name of the property
1.196 + * @return an array of listeners for the named property
1.197 + */
1.198 + public final L[] getListeners(String name) {
1.199 + if (name != null) {
1.200 + L[] listeners = get(name);
1.201 + if (listeners != null) {
1.202 + return listeners.clone();
1.203 + }
1.204 + }
1.205 + return newArray(0);
1.206 + }
1.207 +
1.208 + /**
1.209 + * Indicates whether the map contains
1.210 + * at least one listener to be notified.
1.211 + *
1.212 + * @param name the name of the property
1.213 + * @return {@code true} if at least one listener exists or
1.214 + * {@code false} otherwise
1.215 + */
1.216 + public final synchronized boolean hasListeners(String name) {
1.217 + if (this.map == null) {
1.218 + return false;
1.219 + }
1.220 + L[] array = this.map.get(null);
1.221 + return (array != null) || ((name != null) && (null != this.map.get(name)));
1.222 + }
1.223 +
1.224 + /**
1.225 + * Returns a set of entries from the map.
1.226 + * Each entry is a pair consisted of the property name
1.227 + * and the corresponding list of listeners.
1.228 + *
1.229 + * @return a set of entries from the map
1.230 + */
1.231 + public final Set<Entry<String, L[]>> getEntries() {
1.232 + return (this.map != null)
1.233 + ? this.map.entrySet()
1.234 + : Collections.<Entry<String, L[]>>emptySet();
1.235 + }
1.236 +
1.237 + /**
1.238 + * Extracts a real listener from the proxy listener.
1.239 + * It is necessary because default proxy class is not serializable.
1.240 + *
1.241 + * @return a real listener
1.242 + */
1.243 + public final L extract(L listener) {
1.244 + while (listener instanceof EventListenerProxy) {
1.245 + EventListenerProxy<L> proxy = (EventListenerProxy<L>) listener;
1.246 + listener = proxy.getListener();
1.247 + }
1.248 + return listener;
1.249 + }
1.250 +}