|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ListenerMapImpl.java | 100% | 100% | 100% | 100% |
|
1 | // Copyright 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | package org.apache.tapestry.listener; | |
16 | ||
17 | import java.util.Collection; | |
18 | import java.util.Collections; | |
19 | import java.util.HashMap; | |
20 | import java.util.Map; | |
21 | ||
22 | import org.apache.hivemind.ApplicationRuntimeException; | |
23 | import org.apache.hivemind.util.Defense; | |
24 | import org.apache.tapestry.IActionListener; | |
25 | ||
26 | /** | |
27 | * @author Howard M. Lewis Ship | |
28 | * @since 4.0 | |
29 | */ | |
30 | public class ListenerMapImpl implements ListenerMap | |
31 | { | |
32 | private final Object _target; | |
33 | ||
34 | /** | |
35 | * Keyed on String method name, value is | |
36 | * {@link org.apache.tapestry.listener.ListenerMethodInvoker}. | |
37 | */ | |
38 | ||
39 | private final Map _invokers; | |
40 | ||
41 | private final Map _listeners = new HashMap(); | |
42 | ||
43 | 38 | public ListenerMapImpl(Object target, Map invokers) |
44 | { | |
45 | 38 | Defense.notNull(target, "target"); |
46 | 38 | Defense.notNull(invokers, "invokers"); |
47 | ||
48 | 38 | _target = target; |
49 | 38 | _invokers = invokers; |
50 | } | |
51 | ||
52 | 7 | public boolean canProvideListener(String name) |
53 | { | |
54 | 7 | return _invokers.containsKey(name); |
55 | } | |
56 | ||
57 | 41 | public synchronized IActionListener getListener(String name) |
58 | { | |
59 | 41 | IActionListener result = (IActionListener) _listeners.get(name); |
60 | ||
61 | 41 | if (result == null) |
62 | { | |
63 | 38 | result = createListener(name); |
64 | 37 | _listeners.put(name, result); |
65 | } | |
66 | ||
67 | 40 | return result; |
68 | } | |
69 | ||
70 | 38 | private IActionListener createListener(String name) |
71 | { | |
72 | 38 | ListenerMethodInvoker invoker = (ListenerMethodInvoker) _invokers.get(name); |
73 | ||
74 | 38 | if (invoker == null) |
75 | 1 | throw new ApplicationRuntimeException(ListenerMessages.objectMissingMethod( |
76 | _target, | |
77 | name), _target, null, null); | |
78 | ||
79 | 37 | return new SyntheticListener(_target, invoker); |
80 | } | |
81 | ||
82 | 2 | public Collection getListenerNames() |
83 | { | |
84 | 2 | return Collections.unmodifiableCollection(_invokers.keySet()); |
85 | } | |
86 | } |
|