File Source: ListenerList.java
/*
P/P * Method: com.dmdirc.util.ListenerList__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 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
23 package com.dmdirc.util;
24
25 import java.util.List;
26
27 /**
28 * Represents a list of event listeners, similar to EventListenerList, but
29 * not swing specific.
30 *
31 * @author chris
32 */
33 public class ListenerList {
34
35 /** The map of class->listener or string->listener that we're using. */
36 private final MapList<Object, Object> listeners
37 = new MapList<Object, Object>();
38
39 /**
40 * Creates a new instance of ListenerList.
41 */
/*
P/P * Method: void com.dmdirc.util.ListenerList()
*
* Postconditions:
* this.listeners == &new MapList(ListenerList#1)
* new HashMap(MapList#1) num objects == 1
* new MapList(ListenerList#1) num objects == 1
* this.listeners.map == &new HashMap(MapList#1)
*/
42 public ListenerList() {
43 // Do nothing
44 }
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) {
/*
P/P * Method: void add(Class, Object)
*
* Preconditions:
* this.listeners != null
* this.listeners.map != null
*/
53 listeners.add(listenerType, listener);
54 }
55
56 /**
57 * Adds a new listener of the specified type to this listener list.
58 *
59 * @param listenerType The name of the type of listener that's being added
60 * @param listener The listener to be added
61 */
62 public void add(final String listenerType, final Object listener) {
/*
P/P * Method: void add(String, Object)
*
* Preconditions:
* this.listeners != null
* this.listeners.map != null
*/
63 listeners.add(listenerType, listener);
64 }
65
66 /**
67 * Removes the specified listener from the list of listeners for the
68 * specified type.
69 *
70 * @param listenerType The type that the listener should be removed from
71 * @param listener The listener to be removed
72 */
73 public <T> void remove(final Class<T> listenerType, final T listener) {
/*
P/P * Method: void remove(Class, Object)
*
* Preconditions:
* this.listeners != null
* this.listeners.map != null
*/
74 listeners.remove(listenerType, listener);
75 }
76
77 /**
78 * Removes the specified listener from the list of listeners for the
79 * specified type.
80 *
81 * @param listenerType The name of the type that the listener should be
82 * removed from
83 * @param listener The listener to be removed
84 */
85 public void remove(final String listenerType, final Object listener) {
/*
P/P * Method: void remove(String, Object)
*
* Preconditions:
* this.listeners != null
* this.listeners.map != null
*/
86 listeners.remove(listenerType, listener);
87 }
88
89 /**
90 * Retrieves the list of listeners for the specified type.
91 *
92 * @param listenerType The type of listener that's being retrieved
93 * @return A list of listeners for the specified type
94 */
95 @SuppressWarnings("unchecked")
96 public <T> List<T> get(final Class<T> listenerType) {
/*
P/P * Method: List get(Class)
*
* Preconditions:
* this.listeners != null
* this.listeners.map != null
*
* Postconditions:
* init'ed(return_value)
* new ArrayList(WeakList#1) num objects <= 1
* new WeakList(get#1) num objects <= 1
* new WeakList(get#1).list == &new ArrayList(WeakList#1)
*
* Test Vectors:
* java.util.Map:containsKey(...)@84: {0}, {1}
*/
97 if (listeners.containsKey(listenerType)) {
98 return (List<T>) listeners.get(listenerType);
99 } else {
100 return new WeakList<T>();
101 }
102 }
103
104 /**
105 * Retrieves the list of listeners for the specified type.
106 *
107 * @param listenerType The type of listener to be retrieved
108 * @return A list of listeners for the specified type
109 */
110 public List<Object> get(final String listenerType) {
/*
P/P * Method: List get(String)
*
* Preconditions:
* this.listeners != null
* this.listeners.map != null
*
* Postconditions:
* init'ed(return_value)
* new ArrayList(WeakList#1) num objects <= 1
* new WeakList(get#1) num objects <= 1
* new WeakList(get#1).list == &new ArrayList(WeakList#1)
*
* Test Vectors:
* java.util.Map:containsKey(...)@84: {0}, {1}
*/
111 if (listeners.containsKey(listenerType)) {
112 return listeners.get(listenerType);
113 } else {
114 return new WeakList<Object>();
115 }
116 }
117
118 }
SofCheck Inspector Build Version : 2.17854
| ListenerList.java |
2009-Jun-25 01:54:24 |
| ListenerList.class |
2009-Sep-02 17:04:12 |