Coverage Report - org.simpleframework.xml.load.MapFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
MapFactory
79%
22/28
89%
8/9
3.667
 
 1  
 /*
 2  
  * MapFactory.java July 2007
 3  
  *
 4  
  * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * This library is free software; you can redistribute it and/or
 7  
  * modify it under the terms of the GNU Lesser General Public
 8  
  * License as published by the Free Software Foundation.
 9  
  *
 10  
  * This library is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  
  * GNU Lesser General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU Lesser General 
 16  
  * Public License along with this library; if not, write to the 
 17  
  * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 18  
  * Boston, MA  02111-1307  USA
 19  
  */
 20  
 
 21  
 package org.simpleframework.xml.load;
 22  
 
 23  
 import org.simpleframework.xml.stream.InputNode;
 24  
 import java.util.TreeMap;
 25  
 import java.util.HashMap;
 26  
 import java.util.Map;
 27  
 
 28  
 /**
 29  
  * The <code>MapFactory</code> is used to create map instances that
 30  
  * are compatible with the field type. This performs resolution of 
 31  
  * the map class by consulting the specified <code>Strategy</code> 
 32  
  * implementation. If the strategy cannot resolve the map class 
 33  
  * then this will select a type from the Java Collections framework, 
 34  
  * if a compatible one exists.
 35  
  * 
 36  
  * @author Niall Gallagher
 37  
  */ 
 38  
 class MapFactory extends Factory {
 39  
    
 40  
    /**
 41  
     * Constructor for the <code>MapFactory</code> object. This is 
 42  
     * given the field type as taken from the owning object. The
 43  
     * given type is used to determine the map instance created.
 44  
     * 
 45  
     * @param field this is the class for the owning object
 46  
     */
 47  
    public MapFactory(Source root, Class field) {
 48  96
       super(root, field);           
 49  96
    }
 50  
    
 51  
    /**
 52  
     * Creates a map object that is determined from the field type. 
 53  
     * This is used for the <code>ElementMap</code> to get a map 
 54  
     * that does not have any overrides. This must be done as the 
 55  
     * inline list does not contain an outer element.
 56  
     * 
 57  
     * @return a type which is used to instantiate the map     
 58  
     */
 59  
    public Object getInstance() throws Exception {
 60  1
       Class type = field;
 61  
 
 62  1
       if(!isInstantiable(type)) {
 63  1
          type = getConversion(field);   
 64  
       }
 65  1
       if(!isMap(type)) {
 66  0
          throw new InstantiationException("Type is not a collection %s", field);
 67  
       }
 68  1
       return type.newInstance();
 69  
    }
 70  
    
 71  
    /**
 72  
     * Creates the map object to use. The <code>Strategy</code> object
 73  
     * is consulted for the map object class, if one is not resolved
 74  
     * by the strategy implementation or if the collection resolved is
 75  
     * abstract then the Java Collections framework is consulted.
 76  
     * 
 77  
     * @param node this is the input node representing the list
 78  
     * 
 79  
     * @return this is the map object instantiated for the field
 80  
     */       
 81  
    public Type getInstance(InputNode node) throws Exception {
 82  54
       Type type = getOverride(node);
 83  
      
 84  54
       if(type != null) {              
 85  45
          return getInstance(type);
 86  
       }
 87  9
       if(!isInstantiable(field)) {
 88  7
          field = getConversion(field);
 89  
       }
 90  9
       if(!isMap(field)) {
 91  0
          throw new InstantiationException("Type is not a map %s", field);
 92  
       }
 93  9
       return new ClassType(field);         
 94  
    }  
 95  
    
 96  
    /**
 97  
     * This creates a <code>Map</code> object instance from the type
 98  
     * provided. If the type provided is abstract or an interface then
 99  
     * this can promote the type to a map object type that can be 
 100  
     * instantiated. This is done by asking the type to convert itself.
 101  
     * 
 102  
     * @param type the type used to instantiate the map object
 103  
     * 
 104  
     * @return this returns a compatible map object instance 
 105  
     */
 106  
    public Type getInstance(Type type) throws Exception {
 107  45
       Class real = type.getType();
 108  
 
 109  45
       if(!isInstantiable(real)) {
 110  3
          real = getConversion(real);
 111  
       }
 112  45
       if(!isMap(real)) {
 113  0
          throw new InstantiationException("Type is not a map %s", real);              
 114  
       }
 115  45
       return new ConversionType(type, real);         
 116  
    } 
 117  
    
 118  
    /**
 119  
     * This is used to convert the provided type to a map object type
 120  
     * from the Java Collections framework. This will check to see if
 121  
     * the type is a <code>Map</code> or <code>SortedMap</code> and 
 122  
     * return a <code>HashMapt</code> or <code>TreeSet</code> type. If 
 123  
     * no suitable match can be found this throws an exception.
 124  
     * 
 125  
     * @param type this is the type that is to be converted
 126  
     * 
 127  
     * @return a collection that is assignable to the provided type
 128  
     */ 
 129  
    public Class getConversion(Class type) throws Exception {
 130  11
       if(type.isAssignableFrom(HashMap.class)) {
 131  11
          return HashMap.class;
 132  
       }
 133  0
       if(type.isAssignableFrom(TreeMap.class)) {
 134  0
          return TreeMap.class;                 
 135  
       }      
 136  0
       throw new InstantiationException("Cannot instantiate %s", type);
 137  
    }
 138  
    
 139  
    /**
 140  
     * This determines whether the type provided is a object map type.
 141  
     * If the type is assignable to a <code> Map</code> object then 
 142  
     * this returns true, otherwise this returns false.
 143  
     * 
 144  
     * @param type given to determine whether it is a map type  
 145  
     * 
 146  
     * @return true if the provided type is a map object type
 147  
     */
 148  
    private boolean isMap(Class type) {
 149  55
       return Map.class.isAssignableFrom(type);           
 150  
    }
 151  
 }