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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2006-2015 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 = new HashMap<>();
  38. /**
  39. * Adds a new listener of the specified type to this listener list.
  40. *
  41. * @param <T> The type of listener to be added
  42. * @param listenerType The type of listener to be added
  43. * @param listener The listener to be added
  44. */
  45. public <T> void add(final Class<T> listenerType, final T listener) {
  46. if (listener == null) {
  47. throw new IllegalArgumentException("Listener cannot be null");
  48. }
  49. if (!listeners.containsKey(listenerType)) {
  50. listeners.put(listenerType, new CopyOnWriteArrayList<>());
  51. }
  52. listeners.get(listenerType).add(listener);
  53. }
  54. /**
  55. * Adds a new listener of the specified type to this listener list.
  56. *
  57. * @param listenerType The name of the type of listener that's being added
  58. * @param listener The listener to be added
  59. */
  60. public void add(final String listenerType, final Object listener) {
  61. if (listener == null) {
  62. throw new IllegalArgumentException("Listener cannot be null");
  63. }
  64. if (!listeners.containsKey(listenerType)) {
  65. listeners.put(listenerType, new CopyOnWriteArrayList<>());
  66. }
  67. listeners.get(listenerType).add(listener);
  68. }
  69. /**
  70. * Removes the specified listener from the list of listeners for the
  71. * specified type.
  72. *
  73. * @param <T> The type of listener to be removed
  74. * @param listenerType The type that the listener should be removed from
  75. * @param listener The listener to be removed
  76. */
  77. public <T> void remove(final Class<T> listenerType, final T listener) {
  78. get(listenerType).remove(listener);
  79. }
  80. /**
  81. * Removes the specified listener from the list of listeners for the
  82. * specified type.
  83. *
  84. * @param listenerType The name of the type that the listener should be
  85. * removed from
  86. * @param listener The listener to be removed
  87. */
  88. public void remove(final String listenerType, final Object listener) {
  89. get(listenerType).remove(listener);
  90. }
  91. /**
  92. * Retrieves the list of listeners for the specified type.
  93. *
  94. * @param <T> The type of listener to be retrieved
  95. * @param listenerType The type of listener that's being retrieved
  96. * @return A list of listeners for the specified type
  97. */
  98. @SuppressWarnings("unchecked")
  99. public <T> Collection<T> get(final Class<T> listenerType) {
  100. if (listeners.containsKey(listenerType)) {
  101. return (Collection<T>) listeners.get(listenerType);
  102. } else {
  103. return new CopyOnWriteArrayList<>();
  104. }
  105. }
  106. /**
  107. * Retrieves the list of listeners for the specified type.
  108. *
  109. * @param listenerType The type of listener to be retrieved
  110. * @return A list of listeners for the specified type
  111. */
  112. public Collection<Object> get(final String listenerType) {
  113. if (listeners.containsKey(listenerType)) {
  114. return listeners.get(listenerType);
  115. } else {
  116. return new CopyOnWriteArrayList<>();
  117. }
  118. }
  119. /**
  120. * Returns a callable proxy of the specified type. Methods called on the
  121. * returned proxy will be proxied to all of the registered listeners.
  122. *
  123. * @param <T> The type of listener to be called
  124. * @param listenerType The type of listener to be called
  125. * @return A proxy instance that can be used to call methods
  126. */
  127. @SuppressWarnings("unchecked")
  128. public <T> T getCallable(final Class<T> listenerType) {
  129. return (T) Proxy.newProxyInstance(listenerType.getClassLoader(),
  130. new Class<?>[] { listenerType }, new CallHandler<>(listenerType));
  131. }
  132. /**
  133. * Utility class to handle calls to a "callable" interface as returned by
  134. * {@link #getCallable(Class)}.
  135. *
  136. * @param <T> The type of listener being called
  137. */
  138. private class CallHandler<T> implements InvocationHandler {
  139. /** The type of listener this handler is handling. */
  140. private final Class<T> listenerType;
  141. /**
  142. * Creates a new call handler for the specified type of listener.
  143. *
  144. * @param listenerType The type of listener to handle
  145. */
  146. CallHandler(final Class<T> listenerType) {
  147. this.listenerType = listenerType;
  148. }
  149. @Override
  150. public Object invoke(final Object proxy, final Method method,
  151. final Object[] args) throws Throwable {
  152. for (T target : get(listenerType)) {
  153. try {
  154. method.invoke(target, args);
  155. } catch (IllegalAccessException | IllegalArgumentException ex) {
  156. // Ignore, not possible
  157. } catch (InvocationTargetException ex) {
  158. throw ex.getCause();
  159. }
  160. }
  161. return null;
  162. }
  163. }
  164. }