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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.List;
  24. /**
  25. * Represents a list of event listeners, similar to EventListenerList, but
  26. * not swing specific.
  27. *
  28. * @author chris
  29. */
  30. public class ListenerList {
  31. /** The map of class->listener or string->listener that we're using. */
  32. private final MapList<Object, Object> listeners
  33. = new MapList<Object, Object>();
  34. /**
  35. * Creates a new instance of ListenerList.
  36. */
  37. public ListenerList() {
  38. // Do nothing
  39. }
  40. /**
  41. * Adds a new listener of the specified type to this listener list.
  42. *
  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. listeners.add(listenerType, listener);
  48. }
  49. /**
  50. * Adds a new listener of the specified type to this listener list.
  51. *
  52. * @param listenerType The name of the type of listener that's being added
  53. * @param listener The listener to be added
  54. */
  55. public void add(final String listenerType, final Object listener) {
  56. listeners.add(listenerType, listener);
  57. }
  58. /**
  59. * Removes the specified listener from the list of listeners for the
  60. * specified type.
  61. *
  62. * @param listenerType The type that the listener should be removed from
  63. * @param listener The listener to be removed
  64. */
  65. public <T> void remove(final Class<T> listenerType, final T listener) {
  66. listeners.remove(listenerType, listener);
  67. }
  68. /**
  69. * Removes the specified listener from the list of listeners for the
  70. * specified type.
  71. *
  72. * @param listenerType The name of the type that the listener should be
  73. * removed from
  74. * @param listener The listener to be removed
  75. */
  76. public void remove(final String listenerType, final Object listener) {
  77. listeners.remove(listenerType, listener);
  78. }
  79. /**
  80. * Retrieves the list of listeners for the specified type.
  81. *
  82. * @param listenerType The type of listener that's being retrieved
  83. * @return A list of listeners for the specified type
  84. */
  85. @SuppressWarnings("unchecked")
  86. public <T> List<T> get(final Class<T> listenerType) {
  87. if (listeners.containsKey(listenerType)) {
  88. return (List<T>) listeners.get(listenerType);
  89. } else {
  90. return new WeakList<T>();
  91. }
  92. }
  93. /**
  94. * Retrieves the list of listeners for the specified type.
  95. *
  96. * @param listenerType The type of listener to be retrieved
  97. * @return A list of listeners for the specified type
  98. */
  99. public List<Object> get(final String listenerType) {
  100. if (listeners.containsKey(listenerType)) {
  101. return listeners.get(listenerType);
  102. } else {
  103. return new WeakList<Object>();
  104. }
  105. }
  106. }