Clover coverage report - PicoContainer - 1.0-beta-1
Coverage timestamp: Thu Aug 14 2003 23:16:27 BST
file stats: LOC: 151   Methods: 12
NCLOC: 103   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
CompositePicoContainer.java 100% 100% 100% 100%
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.PicoContainer;
 15   
 import org.picocontainer.defaults.DefaultComponentRegistry;
 16   
 
 17   
 import java.util.ArrayList;
 18   
 import java.util.HashSet;
 19   
 import java.util.Iterator;
 20   
 import java.util.List;
 21   
 import java.util.Set;
 22   
 import java.util.Collections;
 23   
 import java.util.Collection;
 24   
 import java.io.Serializable;
 25   
 
 26   
 /**
 27   
  * CompositePicoContainer aggregates the the contents of more
 28   
  * than one internals together for the sake of a single list of
 29   
  * components. This list may be used as the parent internals for
 30   
  * another PicoContainer. This will result in directive graphs of
 31   
  * containers/components rather than just trees.
 32   
  *
 33   
  * It is not in itself, a Pico component (the array in the
 34   
  * constructor puts paid to that).
 35   
  *
 36   
  */
 37   
 public class CompositePicoContainer implements PicoContainer, Serializable {
 38   
 
 39   
     private final List containers = new ArrayList();
 40   
     private final ComponentRegistry componentRegistry;
 41   
 
 42  17
     public CompositePicoContainer(final ComponentRegistry componentRegistry,
 43   
                                   final PicoContainer[] containers) {
 44  17
         this.componentRegistry = componentRegistry;
 45   
 
 46  17
         if (containers == null) {
 47  1
             throw new NullPointerException("containers can't be null");
 48   
         }
 49  16
         for (int i = 0; i < containers.length; i++) {
 50  15
             PicoContainer container = containers[i];
 51  15
             if (container == null) {
 52  1
                 throw new NullPointerException("PicoContainer at position " + i + " was null");
 53   
             }
 54  14
             this.containers.add(container);
 55   
         }
 56   
     }
 57   
 
 58   
     public static class WithContainerArray extends CompositePicoContainer {
 59  13
         public WithContainerArray(final PicoContainer[] containers) {
 60  13
             super(new DefaultComponentRegistry(), containers);
 61   
         }
 62   
     }
 63   
 
 64   
     public static class Default extends CompositePicoContainer {
 65  1
         public Default() {
 66  1
             super(new DefaultComponentRegistry(), new PicoContainer[]{new NullContainer()});
 67   
         }
 68   
     }
 69   
 
 70  8
     public Object getComponent(Object componentKey) {
 71  8
         Object answer = componentRegistry.getComponentInstance(componentKey);
 72  8
         if (answer == null) {
 73  7
             for (Iterator iter = containers.iterator(); iter.hasNext();) {
 74  8
                 PicoContainer container = (PicoContainer) iter.next();
 75  8
                 if (container.hasComponent(componentKey)) {
 76  6
                     return container.getComponent(componentKey);
 77   
                 }
 78   
             }
 79   
         }
 80  2
         return answer;
 81   
     }
 82   
 
 83  2
     public Collection getComponentKeys() {
 84  2
         Set componentTypes = new HashSet();
 85  2
         componentTypes.addAll(componentRegistry.getComponentInstanceKeys());
 86  2
         for (Iterator iter = containers.iterator(); iter.hasNext();) {
 87  3
             PicoContainer container = (PicoContainer) iter.next();
 88  3
             componentTypes.addAll(container.getComponentKeys());
 89   
         }
 90  2
         return Collections.unmodifiableSet(componentTypes);
 91   
     }
 92   
 
 93  1
     public void instantiateComponents() {
 94  1
         throw new UnsupportedOperationException();
 95   
     }
 96   
 
 97  7
     public boolean hasComponent(Object componentKey) {
 98  7
         if (componentRegistry.hasComponentInstance(componentKey)) {
 99  1
             return true;
 100   
         } else {
 101  6
             for (Iterator iter = containers.iterator(); iter.hasNext();) {
 102  7
                 PicoContainer container = (PicoContainer) iter.next();
 103  7
                 if (container.hasComponent(componentKey)) {
 104  4
                     return true;
 105   
                 }
 106   
             }
 107  2
             return false;
 108   
         }
 109   
     }
 110   
 
 111  3
     public Collection getComponents() {
 112  3
         Set componentTypes = new HashSet();
 113  3
         componentTypes.addAll(componentRegistry.getComponentInstanceKeys());
 114  3
         for (Iterator iter = containers.iterator(); iter.hasNext();) {
 115  3
             PicoContainer container = (PicoContainer) iter.next();
 116  3
             componentTypes.addAll(container.getComponentKeys());
 117   
         }
 118  3
         Set set = new HashSet();
 119  3
         for (Iterator iterator = componentTypes.iterator(); iterator.hasNext();) {
 120  3
             Object key = (Object) iterator.next();
 121  3
             set.add(getComponent(key));
 122   
         }
 123  3
         return Collections.unmodifiableSet(set);
 124   
     }
 125   
 
 126  1
     public Object getComponentMulticaster() {
 127  1
         throw new UnsupportedOperationException();
 128   
     }
 129   
 
 130  1
     public Object getComponentMulticaster(boolean callInInstantiationOrder, boolean callUnmanagedComponents) {
 131  1
         throw new UnsupportedOperationException();
 132   
     }
 133   
 
 134   
     /**
 135   
      * Adds a new Pico internals to this composite internals
 136   
      * @param container
 137   
      */
 138  1
     protected void addContainer(PicoContainer container) {
 139  1
         containers.add(container);
 140   
     }
 141   
 
 142   
     /**
 143   
      * Removes a Pico internals from this composite internals
 144   
      * @param container
 145   
      */
 146  1
     protected void removeContainer(PicoContainer container) {
 147  1
         containers.remove(container);
 148   
     }
 149   
 
 150   
 }
 151