Clover coverage report - PicoContainer - 1.0
Coverage timestamp: Sat Jun 5 2004 21:34:14 EDT
file stats: LOC: 121   Methods: 6
NCLOC: 98   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
GenericCollectionComponentAdapter.java 15.4% 32% 50% 28%
coverage coverage
 1   
 package org.picocontainer.defaults;
 2   
 
 3   
 import org.picocontainer.ComponentAdapter;
 4   
 import org.picocontainer.PicoInitializationException;
 5   
 import org.picocontainer.PicoIntrospectionException;
 6   
 
 7   
 import java.lang.reflect.Array;
 8   
 import java.util.ArrayList;
 9   
 import java.util.Collection;
 10   
 import java.util.HashMap;
 11   
 import java.util.HashSet;
 12   
 import java.util.Iterator;
 13   
 import java.util.List;
 14   
 import java.util.Map;
 15   
 import java.util.Set;
 16   
 import java.util.SortedMap;
 17   
 import java.util.SortedSet;
 18   
 import java.util.TreeMap;
 19   
 import java.util.TreeSet;
 20   
 
 21   
 /**
 22   
  * This component adapter is capable of instantiating Arrays, Maps and Collections of certain types.
 23   
  * The contents of these instances depends on the keyType and valueType arguments.
 24   
  * <p/>
 25   
  * This makes it possible to support depenencies on arrays and generic collections.
 26   
  * <p/>
 27   
  *
 28   
  * @author Aslak Helles&oslash;y
 29   
  * @version $Revision: 1.4 $
 30   
  */
 31   
 class GenericCollectionComponentAdapter extends AbstractComponentAdapter {
 32   
     private final Class keyType;
 33   
     private final Class valueType;
 34   
     private final Class collectionType;
 35   
     private final Class collectionClass;
 36   
 
 37  9
     public GenericCollectionComponentAdapter(Object componentKey, Class keyType, Class valueType, Class collectionType) {
 38  9
         super(componentKey, HashMap.class);
 39  9
         this.keyType = keyType;
 40  9
         this.valueType = valueType;
 41  9
         this.collectionType = collectionType;
 42   
 
 43   
         // The order of tests are significant. The least generic types last.
 44  9
         if (Array.class.isAssignableFrom(collectionType)) {
 45  9
             collectionClass = Array.class;
 46  0
         } else if (List.class.isAssignableFrom(collectionType)) {
 47  0
             collectionClass = ArrayList.class;
 48  0
         } else if (SortedSet.class.isAssignableFrom(collectionType)) {
 49  0
             collectionClass = TreeSet.class;
 50  0
         } else if (Set.class.isAssignableFrom(collectionType)) {
 51  0
             collectionClass = HashSet.class;
 52  0
         } else if (Collection.class.isAssignableFrom(collectionType)) {
 53  0
             collectionClass = ArrayList.class;
 54  0
         } else if (SortedMap.class.isAssignableFrom(collectionType)) {
 55  0
             collectionClass = TreeMap.class;
 56  0
         } else if (Map.class.isAssignableFrom(collectionType)) {
 57  0
             collectionClass = HashMap.class;
 58   
         } else {
 59  0
             throw new PicoIntrospectionException("Unsupported collection type: " + collectionType.getName());
 60   
         }
 61   
     }
 62   
 
 63  7
     public Object getComponentInstance() throws PicoInitializationException, PicoIntrospectionException {
 64  7
         List adaptersOfType = getContainer().getComponentAdaptersOfType(valueType);
 65  7
         if (Array.class.isAssignableFrom(collectionType)) {
 66  7
             return getArrayInstance(adaptersOfType);
 67  0
         } else if (Map.class.isAssignableFrom(collectionType)) {
 68  0
             return getMapInstance(adaptersOfType);
 69   
         } else {
 70  0
             return getCollectionInstance(adaptersOfType);
 71   
         }
 72   
     }
 73   
 
 74  7
     private Object[] getArrayInstance(List adaptersOfType) {
 75  7
         Object[] result = (Object[]) Array.newInstance(valueType, adaptersOfType.size());
 76  7
         int i = 0;
 77  7
         for (Iterator iterator = adaptersOfType.iterator(); iterator.hasNext();) {
 78  11
             ComponentAdapter componentAdapter = (ComponentAdapter) iterator.next();
 79  11
             result[i] = componentAdapter.getComponentInstance();
 80  11
             i++;
 81   
         }
 82  7
         return result;
 83   
     }
 84   
 
 85  0
     private Collection getCollectionInstance(List adaptersOfType) {
 86  0
         try {
 87  0
             Collection result = (Collection) collectionClass.newInstance();
 88  0
             for (Iterator iterator = adaptersOfType.iterator(); iterator.hasNext();) {
 89  0
                 ComponentAdapter componentAdapter = (ComponentAdapter) iterator.next();
 90  0
                 result.add(componentAdapter.getComponentInstance());
 91   
             }
 92  0
             return result;
 93   
         } catch (InstantiationException e) {
 94  0
             throw new PicoInitializationException(e);
 95   
         } catch (IllegalAccessException e) {
 96  0
             throw new PicoInitializationException(e);
 97   
         }
 98   
     }
 99   
 
 100  0
     private Map getMapInstance(List adaptersOfType) {
 101  0
         try {
 102  0
             Map result = (Map) collectionClass.newInstance();
 103  0
             for (Iterator iterator = adaptersOfType.iterator(); iterator.hasNext();) {
 104  0
                 ComponentAdapter componentAdapter = (ComponentAdapter) iterator.next();
 105  0
                 Object componentKey = componentAdapter.getComponentKey();
 106  0
                 if (keyType.isAssignableFrom(componentKey.getClass())) {
 107  0
                     result.put(componentKey, componentAdapter.getComponentInstance());
 108   
                 }
 109   
             }
 110  0
             return result;
 111   
         } catch (InstantiationException e) {
 112  0
             throw new PicoInitializationException(e);
 113   
         } catch (IllegalAccessException e) {
 114  0
             throw new PicoInitializationException(e);
 115   
         }
 116   
     }
 117   
 
 118  0
     public void verify() throws UnsatisfiableDependenciesException {
 119   
 
 120   
     }
 121   
 }