You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ListenerList.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2006-2013 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util.collections;
  23. import java.lang.reflect.InvocationHandler;
  24. import java.lang.reflect.InvocationTargetException;
  25. import java.lang.reflect.Method;
  26. import java.lang.reflect.Proxy;
  27. import java.util.Collection;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. import java.util.concurrent.CopyOnWriteArrayList;
  31. /**
  32. * Represents a list of event listeners, similar to EventListenerList, but
  33. * not swing specific.
  34. */
  35. public class ListenerList {
  36. /** The map of class->listener or string->listener that we're using. */
  37. private final Map<Object, Collection<Object>> listeners
  38. = new HashMap<Object, Collection<Object>>();
  39. /**
  40. * Adds a new listener of the specified type to this listener list.
  41. *
  42. * @param <T> The type of listener to be added
  43. * @param listenerType The type of listener to be added
  44. * @param listener The listener to be added
  45. */
  46. public <T> void add(final Class<T> listenerType, final T listener) {
  47. if (listener == null) {
  48. throw new IllegalArgumentException("Listener cannot be null");
  49. }
  50. if (!listeners.containsKey(listenerType)) {
  51. listeners.put(listenerType, new CopyOnWriteArrayList<Object>());
  52. }
  53. listeners.get(listenerType).add(listener);
  54. }
  55. /**
  56. * Adds a new listener of the specified type to this listener list.
  57. *
  58. * @param listenerType The name of the type of listener that's being added
  59. * @param listener The listener to be added
  60. */
  61. public void add(final String listenerType, final Object listener) {
  62. if (listener == null) {
  63. throw new IllegalArgumentException("Listener cannot be null");
  64. }
  65. if (!listeners.containsKey(listenerType)) {
  66. listeners.put(listenerType, new CopyOnWriteArrayList<Object>());
  67. }
  68. listeners.get(listenerType).add(listener);
  69. }
  70. /**
  71. * Removes the specified listener from the list of listeners for the
  72. * specified type.
  73. *
  74. * @param <T> The type of listener to be removed
  75. * @param listenerType The type that the listener should be removed from
  76. * @param listener The listener to be removed
  77. */
  78. public <T> void remove(final Class<T> listenerType, final T listener) {
  79. get(listenerType).remove(listener);
  80. }
  81. /**
  82. * Removes the specified listener from the list of listeners for the
  83. * specified type.
  84. *
  85. * @param listenerType The name of the type that the listener should be
  86. * removed from
  87. * @param listener The listener to be removed
  88. */
  89. public void remove(final String listenerType, final Object listener) {
  90. get(listenerType).remove(listener);
  91. }
  92. /**
  93. * Retrieves the list of listeners for the specified type.
  94. *
  95. * @param <T> The type of listener to be retrieved
  96. * @param listenerType The type of listener that's being retrieved
  97. * @return A list of listeners for the specified type
  98. */
  99. @SuppressWarnings("unchecked")
  100. public <T> Collection<T> get(final Class<T> listenerType) {
  101. if (listeners.containsKey(listenerType)) {
  102. return (Collection<T>) listeners.get(listenerType);
  103. } else {
  104. return new CopyOnWriteArrayList<T>();
  105. }
  106. }
  107. /**
  108. * Retrieves the list of listeners for the specified type.
  109. *
  110. * @param listenerType The type of listener to be retrieved
  111. * @return A list of listeners for the specified type
  112. */
  113. public Collection<Object> get(final String listenerType) {
  114. if (listeners.containsKey(listenerType)) {
  115. return listeners.get(listenerType);
  116. } else {
  117. return new CopyOnWriteArrayList<Object>();
  118. }
  119. }
  120. /**
  121. * Returns a callable proxy of the specified type. Methods called on the
  122. * returned proxy will be proxied to all of the registered listeners.
  123. *
  124. * @param <T> The type of listener to be called
  125. * @param listenerType The type of listener to be called
  126. * @return A proxy instance that can be used to call methods
  127. */
  128. @SuppressWarnings({"unchecked", "rawtypes"})
  129. public <T> T getCallable(final Class<T> listenerType) {
  130. return (T) Proxy.newProxyInstance(listenerType.getClassLoader(),
  131. new Class[] { listenerType }, new CallHandler<T>(listenerType));
  132. }
  133. /**
  134. * Utility class to handle calls to a "callable" interface as returned by
  135. * {@link #getCallable(java.lang.Class)}.
  136. *
  137. * @param <T> The type of listener being called
  138. */
  139. private class CallHandler<T> implements InvocationHandler {
  140. /** The type of listener this handler is handling. */
  141. private final Class<T> listenerType;
  142. /**
  143. * Creates a new call handler for the specified type of listener.
  144. *
  145. * @param listenerType The type of listener to handle
  146. */
  147. public CallHandler(final Class<T> listenerType) {
  148. this.listenerType = listenerType;
  149. }
  150. /** {@inheritDoc} */
  151. @Override
  152. public Object invoke(final Object proxy, final Method method,
  153. final Object[] args) throws Throwable {
  154. for (T target : get(listenerType)) {
  155. try {
  156. method.invoke(target, args);
  157. } catch (IllegalAccessException ex) {
  158. // Ignore, not possible
  159. } catch (IllegalArgumentException ex) {
  160. // Ignore, not possible
  161. } catch (InvocationTargetException ex) {
  162. throw ex.getCause();
  163. }
  164. }
  165. return null;
  166. }
  167. }
  168. }