Coverage Report - org.simpleframework.xml.core.SignatureCreator
 
Classes in this File Line Coverage Branch Coverage Complexity
SignatureCreator
92%
47/51
91%
22/24
2.8
 
 1  
 /*
 2  
  * Instantiator.java April 2009
 3  
  *
 4  
  * Copyright (C) 2009, 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 static org.simpleframework.xml.core.Support.isAssignable;
 22  
 
 23  
 import java.util.List;
 24  
 
 25  
 /**
 26  
  * The <code>Instantiator</code> object is used to represent an single
 27  
  * constructor within an object. It contains the actual constructor
 28  
  * as well as the list of parameters. Each instantiator will score its
 29  
  * weight when given a <code>Criteria</code> object. This allows
 30  
  * the deserialization process to find the most suitable one to
 31  
  * use when instantiating an object.
 32  
  * 
 33  
  * @author Niall Gallagher
 34  
  */
 35  
 class SignatureCreator implements Creator {   
 36  
    
 37  
    /**
 38  
     * This is the list of parameters in the order of declaration. 
 39  
     */
 40  
    private final List<Parameter> list;
 41  
    
 42  
    /**
 43  
     * This is the map that contains the parameters to be used.
 44  
     */
 45  
    private final Signature signature;
 46  
    
 47  
    /**
 48  
     * This is the type represented by the creator instance.
 49  
     */
 50  
    private final Class type;
 51  
    
 52  
    /**
 53  
     * Constructor for the <code>Instantiator</code> object. This is used
 54  
     * to create a factory like object used for instantiating objects.
 55  
     * Each instantiator will score its suitability using the parameters
 56  
     * it is provided.
 57  
     * 
 58  
     * @param signature this is the signature that contains parameters
 59  
     */
 60  3895
    public SignatureCreator(Signature signature) {
 61  3895
       this.type = signature.getType();
 62  3895
       this.list = signature.getAll();
 63  3895
       this.signature = signature;
 64  3895
    } 
 65  
    
 66  
    /**
 67  
     * This is the type associated with the <code>Creator</code> object.
 68  
     * All instances returned from this creator will be of this type.
 69  
     * 
 70  
     * @return this returns the type associated with this creator
 71  
     */
 72  
    public Class getType() {
 73  0
       return type;
 74  
    }
 75  
    
 76  
    /**
 77  
     * This is the signature associated with the creator. The signature
 78  
     * contains all the parameters associated with the creator as well
 79  
     * as the constructor that this represents. Exposing the signature
 80  
     * allows the creator to be validated.
 81  
     * 
 82  
     * @return this is the signature associated with the creator
 83  
     */
 84  
    public Signature getSignature() {
 85  4246
       return signature;
 86  
    }
 87  
    
 88  
    /**
 89  
     * This is used to instantiate the object using the default no
 90  
     * argument constructor. If for some reason the object can not be
 91  
     * instantiated then this will throw an exception with the reason.    
 92  
     * 
 93  
     * @return this returns the object that has been instantiated
 94  
     */
 95  
    public Object getInstance() throws Exception {
 96  0
       return signature.create();
 97  
    }
 98  
    
 99  
    /**
 100  
     * This is used to instantiate the object using a constructor that
 101  
     * takes deserialized objects as arguments. The object that have
 102  
     * been deserialized can be taken from the <code>Criteria</code>
 103  
     * object which contains the deserialized values.
 104  
     *     
 105  
     * @param criteria this contains the criteria to be used
 106  
     * 
 107  
     * @return this returns the object that has been instantiated
 108  
     */
 109  
    public Object getInstance(Criteria criteria) throws Exception {
 110  276
       Object[] values = list.toArray();
 111  
       
 112  820
       for(int i = 0; i < list.size(); i++) {
 113  544
          values[i] = getVariable(criteria, i);
 114  
       }
 115  276
       return signature.create(values);
 116  
    }
 117  
    
 118  
    /**
 119  
     * This is used to acquire a variable from the criteria provided. 
 120  
     * In order to match the constructor correctly this will check to
 121  
     * see if the if the parameter is required. If it is required then
 122  
     * there must be a non null value or an exception is thrown.
 123  
     * 
 124  
     * @param criteria this is used to acquire the parameter value
 125  
     * @param index this is the index to acquire the value for
 126  
     * 
 127  
     * @return the value associated with the specified parameter
 128  
     */
 129  
    private Object getVariable(Criteria criteria, int index) throws Exception {
 130  544
       Parameter parameter = list.get(index);
 131  544
       Object key = parameter.getKey();
 132  544
       Variable variable = criteria.remove(key);
 133  
       
 134  544
       if(variable != null) {
 135  535
          return variable.getValue();
 136  
       }
 137  9
       return null;
 138  
    }
 139  
   
 140  
    /**
 141  
     * This is used to score this <code>Instantiator</code> object so that
 142  
     * it can be weighed amongst other constructors. The instantiator that
 143  
     * scores the highest is the one that is used for instantiation.
 144  
     * <p>
 145  
     * If any read only element or attribute is not a parameter in
 146  
     * the constructor then the constructor is discounted. This is
 147  
     * because there is no way to set the read only entity without a
 148  
     * constructor injection in to the instantiated object.
 149  
     * 
 150  
     * @param criteria this contains the criteria to be used
 151  
     * 
 152  
     * @return this returns the score based on the criteria provided
 153  
     */
 154  
    public double getScore(Criteria criteria) throws Exception {
 155  332
       Signature match = signature.copy();
 156  
       
 157  332
       for(Object key : criteria) {
 158  651
          Parameter parameter = match.get(key);
 159  651
          Variable label = criteria.get(key);
 160  651
          Contact contact = label.getContact();
 161  
 
 162  651
          if(parameter != null) {
 163  602
             Object value = label.getValue();
 164  602
             Class expect = value.getClass();
 165  602
             Class actual = parameter.getType();
 166  
             
 167  602
             if(!isAssignable(expect, actual)) {
 168  18
                return -1.0;
 169  
             }
 170  
          }
 171  633
          if(contact.isReadOnly()) {
 172  419
             if(parameter == null) {
 173  12
                return -1.0;
 174  
             }               
 175  
          }
 176  621
       }
 177  302
       return getPercentage(criteria);
 178  
    }
 179  
    
 180  
    /**
 181  
     * This is used to determine what percentage of available values
 182  
     * can be injected in to a constructor. Calculating the percentage
 183  
     * in this manner ensures that the best possible fit will be used
 184  
     * to construct the object. This also allows the object to define
 185  
     * what defaults it wishes to set for the values.
 186  
     * <p>
 187  
     * This will use a slight adjustment to ensure that if there are
 188  
     * many constructors with a 100% match on parameters, the one 
 189  
     * with the most values to be injected wins. This ensures the
 190  
     * most desirable constructor is chosen each time.
 191  
     *     
 192  
     * @param criteria this is the criteria object containing values
 193  
     * 
 194  
     * @return this returns the percentage match for the values
 195  
     */
 196  
    private double getPercentage(Criteria criteria) throws Exception {
 197  302
       double score = 0.0;
 198  
       
 199  302
       for(Parameter value : list) {
 200  591
          Object key = value.getKey();
 201  591
          Label label = criteria.get(key);
 202  
 
 203  591
          if(label == null) {
 204  20
             if(value.isRequired()) {
 205  0
                return -1;
 206  
             }  
 207  20
             if(value.isPrimitive()) {
 208  2
                return -1;
 209  
             }
 210  
          } else {
 211  571
             score++;
 212  
          }
 213  589
       }
 214  300
       return getAdjustment(score);
 215  
    }
 216  
    
 217  
    /**
 218  
     * This will use a slight adjustment to ensure that if there are
 219  
     * many constructors with a 100% match on parameters, the one 
 220  
     * with the most values to be injected wins. This ensures the
 221  
     * most desirable constructor is chosen each time.
 222  
     *     
 223  
     * @param score this is the score from the parameter matching
 224  
     * 
 225  
     * @return an adjusted score to account for the signature size
 226  
     */
 227  
    private double getAdjustment(double score) {
 228  300
       double adjustment = list.size() / 1000.0;
 229  
       
 230  300
       if(score > 0) {
 231  300
          return adjustment + score / list.size();
 232  
       }
 233  0
       return score / list.size();
 234  
    }   
 235  
    
 236  
    /**
 237  
     * This is used to acquire a descriptive name for the instantiator.
 238  
     * Providing a name is useful in debugging and when exceptions are
 239  
     * thrown as it describes the constructor the instantiator represents.
 240  
     * 
 241  
     * @return this returns the name of the constructor to be used
 242  
     */
 243  
    public String toString() {
 244  3
       return signature.toString();
 245  
    }
 246  
 }