Coverage Report - org.simpleframework.xml.core.ConversionInstance
 
Classes in this File Line Coverage Branch Coverage Complexity
ConversionInstance
100%
19/19
66%
4/6
1.667
 
 1  
 /*
 2  
  * CoversionInstance.java April 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 org.simpleframework.xml.strategy.Value;
 22  
 
 23  
 /**
 24  
  * The <code>ConversionInstance</code> object is used to promote the
 25  
  * type to some more specialized type. For example if a field or 
 26  
  * method that represents a <code>List</code> is annotated then this
 27  
  * might create a specialized type such as a <code>Vector</code>. It
 28  
  * typically used to promote a type either because it is abstract
 29  
  * or because another type is required. 
 30  
  * <p>
 31  
  * This is used by the <code>CollectionFactory</code> to convert the
 32  
  * type of a collection field from an abstract type to a instantiable
 33  
  * type. This is used to simplify strategy implementations. 
 34  
  *
 35  
  * @author Niall Gallagher
 36  
  * 
 37  
  * @see org.simpleframework.xml.core.CollectionFactory
 38  
  */
 39  
 class ConversionInstance implements Instance {
 40  
    
 41  
    /**
 42  
     * This is the context that is used to create the instance.
 43  
     */
 44  
    private final Context context;
 45  
    
 46  
    /**
 47  
     * This is the new class that is used for the type conversion. 
 48  
     */
 49  
    private final Class convert;
 50  
    
 51  
    /**
 52  
     * This is the value object that will be wrapped by this.
 53  
     */
 54  
    private final Value value;
 55  
    
 56  
    /**
 57  
     * This is used to specify the creation of a conversion type that
 58  
     * can be used for creating an instance with a class other than 
 59  
     * the default class specified by the <code>Value</code> object.
 60  
     * 
 61  
     * @param context this is the context used for instantiation
 62  
     * @param value this is the type used to create the instance
 63  
     * @param convert this is the class the type is converted to
 64  
     */
 65  71404
    public ConversionInstance(Context context, Value value, Class convert) throws Exception {
 66  71404
       this.context = context;
 67  71404
       this.convert = convert;      
 68  71404
       this.value = value;
 69  71404
    }
 70  
    
 71  
    /**
 72  
     * This method is used to acquire an instance of the type that
 73  
     * is defined by this object. If for some reason the type can
 74  
     * not be instantiated an exception is thrown from this.
 75  
     * 
 76  
     * @return an instance of the type this object represents
 77  
     */
 78  
    public Object getInstance() throws Exception {
 79  70206
       if(value.isReference()) {
 80  7
          return value.getValue();
 81  
       }
 82  70199
       Object created = getInstance(convert);
 83  
       
 84  70199
       if(created != null) {
 85  70199
          setInstance(created);
 86  
       }
 87  70199
       return created;
 88  
    }
 89  
 
 90  
    /**
 91  
     * This method is used to acquire an instance of the type that
 92  
     * is defined by this object. If for some reason the type can
 93  
     * not be instantiated an exception is thrown from this.
 94  
     * 
 95  
     * @param type this is the type of the instance to create
 96  
     * 
 97  
     * @return an instance of the type this object represents
 98  
     */
 99  
    public Object getInstance(Class type) throws Exception {
 100  70199
       Instance value = context.getInstance(type);
 101  70199
       Object object = value.getInstance();
 102  
       
 103  70199
       return object;
 104  
    }
 105  
    
 106  
    /**
 107  
     * This method is used acquire the value from the type and if
 108  
     * possible replace the value for the type. If the value can
 109  
     * not be replaced then an exception should be thrown. This 
 110  
     * is used to allow primitives to be inserted into a graph.
 111  
     * 
 112  
     * @param object this is the object to insert as the value
 113  
     * 
 114  
     * @return an instance of the type this object represents
 115  
     */
 116  
    public Object setInstance(Object object) throws Exception {
 117  71395
       if(value != null) {
 118  71395
          value.setValue(object);
 119  
       }
 120  71395
       return object;
 121  
    }
 122  
    
 123  
    /**
 124  
     * This is the type of the object instance that will be created
 125  
     * by the <code>getInstance</code> method. This allows the 
 126  
     * deserialization process to perform checks against the field.
 127  
     * 
 128  
     * @return the type of the object that will be instantiated
 129  
     */
 130  
    public Class getType() {
 131  104
       return convert;
 132  
    }
 133  
    
 134  
    /**
 135  
     * This will return true if the <code>Value</code> object provided
 136  
     * is a reference type. Typically a reference type refers to a 
 137  
     * type that is substituted during the deserialization process 
 138  
     * and so constitutes an object that does not need initialization.
 139  
     * 
 140  
     * @return this returns true if the type is a reference type
 141  
     */
 142  
    public boolean isReference() {
 143  71404
       return value.isReference();
 144  
    }   
 145  
 }