Coverage Report - org.apache.tapestry.listener.ListenerMapImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ListenerMapImpl
100% 
100% 
1.6
 
 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 org.apache.hivemind.ApplicationRuntimeException;
 18  
 import org.apache.hivemind.util.Defense;
 19  
 import org.apache.tapestry.IActionListener;
 20  
 
 21  
 import java.util.Collection;
 22  
 import java.util.Collections;
 23  
 import java.util.HashMap;
 24  
 import java.util.Map;
 25  
 
 26  
 /**
 27  
  * @author Howard M. Lewis Ship
 28  
  * @since 4.0
 29  
  */
 30  
 public class ListenerMapImpl implements ListenerMap
 31  
 {
 32  
 
 33  
     private final Object _target;
 34  
 
 35  
     /**
 36  
      * Keyed on String method name, value is
 37  
      * {@link org.apache.tapestry.listener.ListenerMethodInvoker}.
 38  
      */
 39  
 
 40  
     private final Map _invokers;
 41  
 
 42  16
     private final Map _listeners = new HashMap();
 43  
 
 44  
     public ListenerMapImpl(Object target, Map invokers)
 45  16
     {
 46  16
         Defense.notNull(target, "target");
 47  16
         Defense.notNull(invokers, "invokers");
 48  
 
 49  16
         _target = target;
 50  16
         _invokers = invokers;
 51  16
     }
 52  
 
 53  
     public boolean canProvideListener(String name)
 54  
     {
 55  2
         return _invokers.containsKey(name);
 56  
     }
 57  
 
 58  
     public synchronized IActionListener getListener(String name)
 59  
     {
 60  15
         IActionListener result = (IActionListener) _listeners.get(name);
 61  
 
 62  15
         if (result == null)
 63  
         {
 64  14
             result = createListener(name);
 65  12
             _listeners.put(name, result);
 66  
         }
 67  
 
 68  13
         return result;
 69  
     }
 70  
 
 71  
     private IActionListener createListener(String name)
 72  
     {
 73  14
         ListenerMethodInvoker invoker = (ListenerMethodInvoker) _invokers.get(name);
 74  
 
 75  14
         if (invoker == null)
 76  2
             throw new ApplicationRuntimeException(ListenerMessages.objectMissingMethod(_target, name),
 77  
                                                   _target, null, null);
 78  
 
 79  12
         return new SyntheticListener(_target, invoker);
 80  
     }
 81  
 
 82  
     public Collection getListenerNames()
 83  
     {
 84  2
         return Collections.unmodifiableCollection(_invokers.keySet());
 85  
     }
 86  
 }