Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ListenerList.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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;
  23. import java.util.Collection;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import java.util.concurrent.CopyOnWriteArrayList;
  27. /**
  28. * Represents a list of event listeners, similar to EventListenerList, but
  29. * not swing specific.
  30. */
  31. public class ListenerList {
  32. /** The map of class->listener or string->listener that we're using. */
  33. private final Map<Object, Collection<Object>> listeners
  34. = new HashMap<Object, Collection<Object>>();
  35. /**
  36. * Adds a new listener of the specified type to this listener list.
  37. *
  38. * @param <T> The type of listener to be added
  39. * @param listenerType The type of listener to be added
  40. * @param listener The listener to be added
  41. */
  42. public <T> void add(final Class<T> listenerType, final T listener) {
  43. if (!listeners.containsKey(listenerType)) {
  44. listeners.put(listenerType, new CopyOnWriteArrayList<Object>());
  45. }
  46. listeners.get(listenerType).add(listener);
  47. }
  48. /**
  49. * Adds a new listener of the specified type to this listener list.
  50. *
  51. * @param listenerType The name of the type of listener that's being added
  52. * @param listener The listener to be added
  53. */
  54. public void add(final String listenerType, final Object listener) {
  55. if (!listeners.containsKey(listenerType)) {
  56. listeners.put(listenerType, new CopyOnWriteArrayList<Object>());
  57. }
  58. listeners.get(listenerType).add(listener);
  59. }
  60. /**
  61. * Removes the specified listener from the list of listeners for the
  62. * specified type.
  63. *
  64. * @param <T> The type of listener to be removed
  65. * @param listenerType The type that the listener should be removed from
  66. * @param listener The listener to be removed
  67. */
  68. public <T> void remove(final Class<T> listenerType, final T listener) {
  69. get(listenerType).remove(listener);
  70. }
  71. /**
  72. * Removes the specified listener from the list of listeners for the
  73. * specified type.
  74. *
  75. * @param listenerType The name of the type that the listener should be
  76. * removed from
  77. * @param listener The listener to be removed
  78. */
  79. public void remove(final String listenerType, final Object listener) {
  80. get(listenerType).remove(listener);
  81. }
  82. /**
  83. * Retrieves the list of listeners for the specified type.
  84. *
  85. * @param <T> The type of listener to be retrieved
  86. * @param listenerType The type of listener that's being retrieved
  87. * @return A list of listeners for the specified type
  88. */
  89. @SuppressWarnings("unchecked")
  90. public <T> Collection<T> get(final Class<T> listenerType) {
  91. if (listeners.containsKey(listenerType)) {
  92. return (Collection<T>) listeners.get(listenerType);
  93. } else {
  94. return new CopyOnWriteArrayList<T>();
  95. }
  96. }
  97. /**
  98. * Retrieves the list of listeners for the specified type.
  99. *
  100. * @param listenerType The type of listener to be retrieved
  101. * @return A list of listeners for the specified type
  102. */
  103. public Collection<Object> get(final String listenerType) {
  104. if (listeners.containsKey(listenerType)) {
  105. return listeners.get(listenerType);
  106. } else {
  107. return new CopyOnWriteArrayList<Object>();
  108. }
  109. }
  110. }