Coverage Report - org.simpleframework.xml.core.MapFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
MapFactory
80%
24/30
61%
11/18
3.667
 
 1  
 /*
 2  
  * MapFactory.java July 2007
 3  
  *
 4  
  * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 15  
  * implied. See the License for the specific language governing 
 16  
  * permissions and limitations under the License.
 17  
  */
 18  
 
 19  
 package org.simpleframework.xml.core;
 20  
 
 21  
 import java.util.HashMap;
 22  
 import java.util.Map;
 23  
 import java.util.TreeMap;
 24  
 
 25  
 import org.simpleframework.xml.strategy.Type;
 26  
 import org.simpleframework.xml.strategy.Value;
 27  
 import org.simpleframework.xml.stream.InputNode;
 28  
 
 29  
 /**
 30  
  * The <code>MapFactory</code> is used to create map instances that
 31  
  * are compatible with the field type. This performs resolution of 
 32  
  * the map class by consulting the specified <code>Strategy</code> 
 33  
  * implementation. If the strategy cannot resolve the map class 
 34  
  * then this will select a type from the Java Collections framework, 
 35  
  * if a compatible one exists.
 36  
  * 
 37  
  * @author Niall Gallagher
 38  
  */ 
 39  
 class MapFactory extends Factory {
 40  
    
 41  
    /**
 42  
     * Constructor for the <code>MapFactory</code> object. This is 
 43  
     * given the field type as taken from the owning object. The
 44  
     * given type is used to determine the map instance created.
 45  
     * 
 46  
     * @param context this is the context object for this factory
 47  
     * @param type this is the class for the owning object
 48  
     */
 49  
    public MapFactory(Context context, Type type) {
 50  860
       super(context, type);           
 51  860
    }
 52  
    
 53  
    /**
 54  
     * Creates a map object that is determined from the field type. 
 55  
     * This is used for the <code>ElementMap</code> to get a map 
 56  
     * that does not have any overrides. This must be done as the 
 57  
     * inline list does not contain an outer element.
 58  
     * 
 59  
     * @return a type which is used to instantiate the map     
 60  
     */
 61  
    public Object getInstance() throws Exception {
 62  35
       Class expect = getType();
 63  35
       Class real = expect;
 64  
 
 65  35
       if(!isInstantiable(real)) {
 66  35
          real = getConversion(expect);   
 67  
       }
 68  35
       if(!isMap(real)) {
 69  0
          throw new InstantiationException("Invalid map %s for %s", expect, type);
 70  
       }
 71  35
       return real.newInstance();
 72  
    }
 73  
    
 74  
    /**
 75  
     * Creates the map object to use. The <code>Strategy</code> object
 76  
     * is consulted for the map object class, if one is not resolved
 77  
     * by the strategy implementation or if the collection resolved is
 78  
     * abstract then the Java Collections framework is consulted.
 79  
     * 
 80  
     * @param node this is the input node representing the list
 81  
     * 
 82  
     * @return this is the map object instantiated for the field
 83  
     */       
 84  
    public Instance getInstance(InputNode node) throws Exception {
 85  166
       Value value = getOverride(node);
 86  166
       Class expect = getType();
 87  
      
 88  166
       if(value != null) {              
 89  149
          return getInstance(value);
 90  
       }
 91  17
       if(!isInstantiable(expect)) {
 92  13
          expect = getConversion(expect);
 93  
       }
 94  17
       if(!isMap(expect)) {
 95  0
          throw new InstantiationException("Invalid map %s for %s", expect, type);
 96  
       }
 97  17
       return context.getInstance(expect);         
 98  
    }  
 99  
    
 100  
    /**
 101  
     * This creates a <code>Map</code> object instance from the type
 102  
     * provided. If the type provided is abstract or an interface then
 103  
     * this can promote the type to a map object type that can be 
 104  
     * instantiated. This is done by asking the type to convert itself.
 105  
     * 
 106  
     * @param value the type used to instantiate the map object
 107  
     * 
 108  
     * @return this returns a compatible map object instance 
 109  
     */
 110  
    public Instance getInstance(Value value) throws Exception {
 111  149
       Class expect = value.getType();
 112  
 
 113  149
       if(!isInstantiable(expect)) {
 114  3
          expect = getConversion(expect);
 115  
       }
 116  149
       if(!isMap(expect)) {
 117  0
          throw new InstantiationException("Invalid map %s for %s", expect, type);              
 118  
       }
 119  149
       return new ConversionInstance(context, value, expect);         
 120  
    } 
 121  
    
 122  
    /**
 123  
     * This is used to convert the provided type to a map object type
 124  
     * from the Java Collections framework. This will check to see if
 125  
     * the type is a <code>Map</code> or <code>SortedMap</code> and 
 126  
     * return a <code>HashMap</code> or <code>TreeSet</code> type. If 
 127  
     * no suitable match can be found this throws an exception.
 128  
     * 
 129  
     * @param require this is the type that is to be converted
 130  
     * 
 131  
     * @return a collection that is assignable to the provided type
 132  
     */ 
 133  
    public Class getConversion(Class require) throws Exception {
 134  51
       if(require.isAssignableFrom(HashMap.class)) {
 135  51
          return HashMap.class;
 136  
       }
 137  0
       if(require.isAssignableFrom(TreeMap.class)) {
 138  0
          return TreeMap.class;                 
 139  
       }      
 140  0
       throw new InstantiationException("Cannot instantiate %s for %s", require, type);
 141  
    }
 142  
    
 143  
    /**
 144  
     * This determines whether the type provided is a object map type.
 145  
     * If the type is assignable to a <code> Map</code> object then 
 146  
     * this returns true, otherwise this returns false.
 147  
     * 
 148  
     * @param type given to determine whether it is a map type  
 149  
     * 
 150  
     * @return true if the provided type is a map object type
 151  
     */
 152  
    private boolean isMap(Class type) {
 153  201
       return Map.class.isAssignableFrom(type);           
 154  
    }
 155  
 }