Clover coverage report - PicoContainer - 1.0-beta-1
Coverage timestamp: Thu Aug 14 2003 23:16:27 BST
file stats: LOC: 179   Methods: 18
NCLOC: 112   Classes: 3
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
HierarchicalComponentRegistry.java 71.4% 87% 83.3% 83.3%
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 Aslak Hellesoy and Paul Hammant   *
 9   
  *****************************************************************************/
 10   
 
 11   
 package org.picocontainer.extras;
 12   
 
 13   
 import org.picocontainer.internals.ComponentRegistry;
 14   
 import org.picocontainer.PicoInitializationException;
 15   
 import org.picocontainer.defaults.AmbiguousComponentResolutionException;
 16   
 import org.picocontainer.internals.ComponentSpecification;
 17   
 import org.picocontainer.defaults.DefaultComponentRegistry;
 18   
 
 19   
 import java.io.Serializable;
 20   
 import java.util.Collection;
 21   
 import java.util.Collections;
 22   
 import java.util.HashSet;
 23   
 import java.util.List;
 24   
 import java.util.Set;
 25   
 import java.util.ArrayList;
 26   
 
 27   
 public class HierarchicalComponentRegistry implements ComponentRegistry, Serializable {
 28   
 
 29   
     protected final ComponentRegistry parentRegistry;
 30   
     protected final ComponentRegistry childRegistry;
 31   
 
 32  6
     protected HierarchicalComponentRegistry(ComponentRegistry parentRegistry, ComponentRegistry childRegistry) {
 33  6
         if (parentRegistry == null) {
 34  1
             throw new NullPointerException("parentRegistry cannot be null");
 35   
         }
 36  5
         if (childRegistry == null) {
 37  1
             throw new NullPointerException("childRegistry cannot be null");
 38   
         }
 39  4
         this.parentRegistry = parentRegistry;
 40  4
         this.childRegistry = childRegistry;
 41   
     }
 42   
 
 43   
     public static class Default extends HierarchicalComponentRegistry {
 44  4
         public Default(ComponentRegistry parentRegistry) {
 45  4
             super(parentRegistry, new DefaultComponentRegistry());
 46   
         }
 47   
     }
 48   
 
 49   
     public static class WithChildRegistry extends HierarchicalComponentRegistry {
 50  0
         public WithChildRegistry(ComponentRegistry parentRegistry, ComponentRegistry childRegistry) {
 51  0
             super(parentRegistry, childRegistry);
 52   
         }
 53   
     }
 54   
 
 55  2
     public void registerComponent(ComponentSpecification compSpec) {
 56  2
         childRegistry.registerComponent(compSpec);
 57   
     }
 58   
     
 59  0
     public void unregisterComponent(Object componentKey) {
 60  0
         childRegistry.unregisterComponent(componentKey);
 61   
     }
 62   
 
 63  5
     public Collection getComponentSpecifications() {
 64  5
         return childRegistry.getComponentSpecifications();
 65   
     }
 66   
 
 67  1
     public List getOrderedComponents() {
 68   
         // Get child types
 69  1
         List types = new ArrayList(childRegistry.getOrderedComponents());
 70   
 
 71   
         // Get those from parent.
 72  1
         types.addAll(parentRegistry.getOrderedComponents());
 73   
 
 74  1
         return Collections.unmodifiableList(types);
 75   
     }
 76   
 
 77  2
     public void addOrderedComponent(Object component) {
 78  2
         childRegistry.addOrderedComponent(component);
 79   
     }
 80   
 
 81  2
     public void putComponent(Object componentKey, Object component) {
 82  2
         childRegistry.putComponent(componentKey, component);
 83   
     }
 84   
 
 85  2
     public boolean contains(Object componentKey) {
 86  2
         return childRegistry.contains(componentKey);
 87   
     }
 88   
 
 89  6
     public Object getComponentInstance(Object componentKey) {
 90   
 
 91   
         // First look in child
 92  6
         Object result = childRegistry.getComponentInstance(componentKey);
 93   
 
 94   
         // Then look in parent if we had nothing
 95  6
         if (result == null) {
 96  4
             result = parentRegistry.getComponentInstance(componentKey);
 97   
         }
 98  6
         return result;
 99   
     }
 100   
 
 101  2
     public Set getComponentInstanceKeys() {
 102   
 
 103   
         // Get child types
 104  2
         Set types = new HashSet(childRegistry.getComponentInstanceKeys());
 105   
 
 106   
         // Get those from parent.
 107  2
         types.addAll(parentRegistry.getComponentInstanceKeys());
 108   
 
 109  2
         return Collections.unmodifiableSet(types);
 110   
 
 111   
     }
 112   
 
 113  2
     public Set getComponentInstances() {
 114   
         // Get child types
 115  2
         Set types = new HashSet(childRegistry.getComponentInstances());
 116   
 
 117   
         // Get those from parent.
 118  2
         types.addAll(parentRegistry.getComponentInstances());
 119   
 
 120  2
         return Collections.unmodifiableSet(types);
 121   
     }
 122   
 
 123  4
     public boolean hasComponentInstance(Object componentKey) {
 124  4
         return childRegistry.hasComponentInstance(componentKey)
 125   
                 | parentRegistry.hasComponentInstance(componentKey);
 126   
     }
 127   
 
 128  0
     public ComponentSpecification getComponentSpec(Object componentKey) {
 129   
         // First look in child
 130  0
         ComponentSpecification result = childRegistry.getComponentSpec(componentKey);
 131   
 
 132   
         // Then look in parent if we had nothing
 133  0
         if (result == null) {
 134  0
             result = parentRegistry.getComponentSpec(componentKey);
 135   
         }
 136  0
         return result;
 137   
 
 138   
     }
 139   
 
 140  2
     public Object findImplementingComponent(Class componentType) throws AmbiguousComponentResolutionException {
 141   
 
 142   
         // First look in child
 143  2
         Object result = childRegistry.findImplementingComponent(componentType);
 144   
 
 145   
         // Then look in parent if we had nothing
 146  2
         if (result == null) {
 147  2
             result = parentRegistry.findImplementingComponent(componentType);
 148   
         }
 149  2
         return result;
 150   
 
 151   
     }
 152   
 
 153  1
     public ComponentSpecification findImplementingComponentSpecification(Class componentType) throws AmbiguousComponentResolutionException {
 154   
 
 155   
         // First look in child
 156  1
         ComponentSpecification result = childRegistry.findImplementingComponentSpecification(componentType);
 157   
 
 158   
         // Then look in parent if we had nothing
 159  1
         if (result == null) {
 160  1
             result = parentRegistry.findImplementingComponentSpecification(componentType);
 161   
         }
 162  1
         return result;
 163   
     }
 164   
 
 165  2
     public Object createComponent(ComponentSpecification componentSpecification) throws PicoInitializationException {
 166  2
         if (!contains(componentSpecification.getComponentKey())) {
 167  1
             Object component = componentSpecification.instantiateComponent(this);
 168  1
             addOrderedComponent(component);
 169   
 
 170  1
             putComponent(componentSpecification.getComponentKey(), component);
 171   
 
 172  1
             return component;
 173   
         } else {
 174  1
             return getComponentInstance(componentSpecification.getComponentKey());
 175   
         }
 176   
     }
 177   
 
 178   
 }
 179