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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2006-2010 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.Comparator;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import java.util.concurrent.ConcurrentSkipListSet;
  28. /**
  29. * Represents a list of event listeners, similar to EventListenerList, but
  30. * not swing specific.
  31. *
  32. * @author chris
  33. */
  34. public class ListenerList {
  35. /** The comparator to use. */
  36. protected static EqualComparator COMPARATOR = new EqualComparator();
  37. /** The map of class->listener or string->listener that we're using. */
  38. private final Map<Object, Collection<Object>> listeners
  39. = new HashMap<Object, Collection<Object>>();
  40. /**
  41. * Creates a new instance of ListenerList.
  42. */
  43. public ListenerList() {
  44. // Do nothing
  45. }
  46. /**
  47. * Adds a new listener of the specified type to this listener list.
  48. *
  49. * @param listenerType The type of listener to be added
  50. * @param listener The listener to be added
  51. */
  52. public <T> void add(final Class<T> listenerType, final T listener) {
  53. if (!listeners.containsKey(listenerType)) {
  54. listeners.put(listenerType, new ConcurrentSkipListSet<Object>(COMPARATOR));
  55. }
  56. listeners.get(listenerType).add(listener);
  57. }
  58. /**
  59. * Adds a new listener of the specified type to this listener list.
  60. *
  61. * @param listenerType The name of the type of listener that's being added
  62. * @param listener The listener to be added
  63. */
  64. public void add(final String listenerType, final Object listener) {
  65. if (!listeners.containsKey(listenerType)) {
  66. listeners.put(listenerType, new ConcurrentSkipListSet<Object>(COMPARATOR));
  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 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. listeners.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. listeners.get(listenerType).remove(listener);
  90. }
  91. /**
  92. * Retrieves the list of listeners for the specified type.
  93. *
  94. * @param listenerType The type of listener that's being retrieved
  95. * @return A list of listeners for the specified type
  96. */
  97. @SuppressWarnings("unchecked")
  98. public <T> Collection<T> get(final Class<T> listenerType) {
  99. if (listeners.containsKey(listenerType)) {
  100. return (Collection<T>) listeners.get(listenerType);
  101. } else {
  102. return new ConcurrentSkipListSet<T>(COMPARATOR);
  103. }
  104. }
  105. /**
  106. * Retrieves the list of listeners for the specified type.
  107. *
  108. * @param listenerType The type of listener to be retrieved
  109. * @return A list of listeners for the specified type
  110. */
  111. public Collection<Object> get(final String listenerType) {
  112. if (listeners.containsKey(listenerType)) {
  113. return listeners.get(listenerType);
  114. } else {
  115. return new ConcurrentSkipListSet<Object>(COMPARATOR);
  116. }
  117. }
  118. /**
  119. * A comparator that says any two objects are equal.
  120. *
  121. * @since 0.6.4
  122. */
  123. protected static class EqualComparator implements Comparator<Object> {
  124. /** {@inheritDoc} */
  125. @Override
  126. public int compare(final Object o1, final Object o2) {
  127. return 0;
  128. }
  129. }
  130. }