Clover coverage report - PicoContainer - 1.0-beta-1
Coverage timestamp: Thu Aug 14 2003 23:16:27 BST
file stats: LOC: 167   Methods: 16
NCLOC: 108   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
DefaultComponentRegistry.java 90% 100% 100% 97.7%
coverage coverage
 1   
 /*****************************************************************************
 2   
  * Copyright (C) PicoContainer Organization. All rights reserved.            *
 3   
  * ------------------------------------------------------------------------- *
 4   
  * The software in this package is published under the terms of the BSD      *
 5   
  * style license a copy of which has been included with this distribution in *
 6   
  * the LICENSE.txt file.                                                     *
 7   
  *                                                                           *
 8   
  * Idea by Rachel Davies, Original code by various                           *
 9   
  *****************************************************************************/
 10   
 package org.picocontainer.defaults;
 11   
 
 12   
 import org.picocontainer.internals.ComponentRegistry;
 13   
 import org.picocontainer.PicoInitializationException;
 14   
 import org.picocontainer.PicoContainer;
 15   
 import org.picocontainer.internals.ComponentSpecification;
 16   
 
 17   
 import java.io.Serializable;
 18   
 import java.util.*;
 19   
 
 20   
 /**
 21   
  * The default component registry.
 22   
  *
 23   
  * @author Aslak Hellesoy, Paul Hammant, various
 24   
  * @version $Revision: 1.8 $
 25   
  */
 26   
 
 27   
 public class DefaultComponentRegistry implements ComponentRegistry, Serializable {
 28   
 
 29   
     protected final List registeredComponentSpecifications;
 30   
 
 31   
     // Keeps track of the instantiation order
 32   
     protected final List orderedComponents;
 33   
 
 34   
     protected final Map componentKeyToInstanceMap;
 35   
 
 36   
     protected final Map componentToSpec;
 37   
 
 38   
 
 39  116
     public DefaultComponentRegistry() {
 40  116
         registeredComponentSpecifications = new ArrayList();
 41  116
         orderedComponents = new ArrayList();
 42  116
         componentKeyToInstanceMap = new HashMap();
 43  116
         componentToSpec = new HashMap();
 44   
     }
 45   
 
 46  172
     public void registerComponent(ComponentSpecification compSpec) {
 47  172
         componentToSpec.put(compSpec.getComponentImplementation(), compSpec);
 48  172
         registeredComponentSpecifications.add(compSpec);
 49   
     }
 50   
     
 51  5
     public void unregisterComponent(Object componentKey) {
 52  5
         for (Iterator iterator = registeredComponentSpecifications.iterator(); iterator.hasNext();) {
 53  5
             ComponentSpecification currentCompSpec = (ComponentSpecification) iterator.next();
 54   
             
 55  5
             if (currentCompSpec.getComponentKey().equals(componentKey)) {
 56  5
                 registeredComponentSpecifications.remove(currentCompSpec);
 57  5
                 componentKeyToInstanceMap.remove(componentKey);
 58  5
                 break;
 59   
             }
 60   
         }
 61   
     }
 62   
 
 63  277
     public Collection getComponentSpecifications() {
 64  277
         return registeredComponentSpecifications;
 65   
     }
 66   
 
 67  58
     public List getOrderedComponents() {
 68  58
         return new ArrayList(orderedComponents);
 69   
     }
 70   
 
 71  140
     public void addOrderedComponent(Object component) {
 72  140
         orderedComponents.add(component);
 73   
     }
 74   
 
 75  141
     public void putComponent(Object componentKey, Object component) {
 76  141
         componentKeyToInstanceMap.put(componentKey, component);
 77   
     }
 78   
 
 79  160
     public boolean contains(Object componentKey) {
 80  160
         return componentKeyToInstanceMap.containsKey(componentKey);
 81   
     }
 82   
 
 83  253
     public Object getComponentInstance(Object componentKey) {
 84  253
         return componentKeyToInstanceMap.get(componentKey);
 85   
     }
 86   
 
 87  45
     public Set getComponentInstanceKeys() {
 88  45
         Set types = componentKeyToInstanceMap.keySet();
 89  45
         return Collections.unmodifiableSet(types);
 90   
     }
 91   
 
 92  19
     public Set getComponentInstances() {
 93   
 //        ArrayList list = new ArrayList();
 94   
 //        Set types = componentKeyToInstanceMap.entrySet();
 95   
 //        for (Iterator iterator = types.iterator(); iterator.hasNext();) {
 96   
 //            Map.Entry e = (Map.Entry) iterator.next();
 97   
 //            list.add(e.getValue());
 98   
 //        }
 99  19
         Set result = new HashSet();
 100  19
         result.addAll(componentKeyToInstanceMap.values());
 101  19
         return Collections.unmodifiableSet(result);
 102   
     }
 103   
 
 104  15
     public boolean hasComponentInstance(Object componentKey) {
 105  15
         return componentKeyToInstanceMap.containsKey(componentKey);
 106   
     }
 107   
 
 108  7
     public ComponentSpecification getComponentSpec(Object componentKey) {
 109  7
         return (ComponentSpecification) componentToSpec.get(componentKey);
 110   
     }
 111   
 
 112  33
     public Object findImplementingComponent(Class componentType) throws AmbiguousComponentResolutionException {
 113  33
         List found = new ArrayList();
 114   
 
 115  33
         for (Iterator iterator = getComponentInstanceKeys().iterator(); iterator.hasNext();) {
 116  28
             Object key = iterator.next();
 117  28
             Object component = getComponentInstance(key);
 118  28
             if (componentType.isInstance(component)) {
 119  13
                 found.add(key);
 120   
             }
 121   
         }
 122   
 
 123  33
         if (found.size() > 1) {
 124  1
             Object[] ambiguousKeys = found.toArray();
 125  1
             throw new AmbiguousComponentResolutionException(componentType, ambiguousKeys);
 126   
         }
 127   
 
 128  32
         return found.isEmpty() ? null : getComponentInstance(found.get(0));
 129   
     }
 130   
 
 131  24
     public ComponentSpecification findImplementingComponentSpecification(Class componentType) throws AmbiguousComponentResolutionException {
 132  24
         List found = new ArrayList();
 133  24
         for (Iterator iterator = getComponentSpecifications().iterator(); iterator.hasNext();) {
 134  49
             ComponentSpecification componentSpecification = (ComponentSpecification) iterator.next();
 135   
 
 136  49
             if (componentType.isAssignableFrom(componentSpecification.getComponentImplementation())) {
 137  20
                 found.add(componentSpecification);
 138   
             }
 139   
         }
 140   
 
 141  24
         if (found.size() > 1) {
 142  1
             Class[] foundClasses = new Class[found.size()];
 143  1
             for (int i = 0; i < foundClasses.length; i++) {
 144  2
                 foundClasses[i] = ((ComponentSpecification) found.get(i)).getComponentImplementation();
 145   
             }
 146  1
             throw new AmbiguousComponentResolutionException(componentType, foundClasses);
 147   
         }
 148   
 
 149  23
         return found.isEmpty() ? null : ((ComponentSpecification) found.get(0));
 150   
     }
 151   
 
 152   
 
 153  158
     public Object createComponent(ComponentSpecification componentSpecification) throws PicoInitializationException {
 154  158
         if (!contains(componentSpecification.getComponentKey())) {
 155  124
             Object component = componentSpecification.instantiateComponent(this);
 156  118
             addOrderedComponent(component);
 157   
 
 158  118
             putComponent(componentSpecification.getComponentKey(), component);
 159   
 
 160  118
             return component;
 161   
         } else {
 162  34
             return getComponentInstance(componentSpecification.getComponentKey());
 163   
         }
 164   
     }
 165   
 
 166   
 }
 167