Coverage Report - org.simpleframework.xml.graph.ArrayInstance
 
Classes in this File Line Coverage Branch Coverage Complexity
ArrayInstance
100%
9/9
N/A
1
 
 1  
 /*
 2  
  * ArrayInstance.java April 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.graph;
 22  
 
 23  
 import org.simpleframework.xml.load.Type;
 24  
 import java.lang.reflect.Array;
 25  
 
 26  
 /**
 27  
  * The <code>ArrayInstance</code> object is a type used for creating
 28  
  * arrays from a specified component type or <code>Type</code> object.
 29  
  * This allows primitive and composite arrays to be acquired either 
 30  
  * by reference or by value from a converter object. This must be 
 31  
  * given the length of the array so that it can be allocated.
 32  
  * 
 33  
  * @author Niall Gallagher
 34  
  * 
 35  
  * @see org.simpleframework.xml.graph.Allocate
 36  
  */
 37  
 class ArrayInstance implements Type {
 38  
    
 39  
    /**
 40  
     * This is the optional field type for the array to be created. 
 41  
     */
 42  
    private Class type;
 43  
    
 44  
    /**
 45  
     * This is used to determine the size of the array to be created.
 46  
     */
 47  
    private int size;
 48  
    
 49  
    /**
 50  
     * This is used to specify the creation of an array type which
 51  
     * can be used for creating an array that can hold instances of 
 52  
     * the specified type. The type specified must be the component
 53  
     * type for the array that is to be created. 
 54  
     * 
 55  
     * @param type this is the component type for the array
 56  
     * @param size this is the size of the array to instantiate
 57  
     */
 58  25
    public ArrayInstance(Class type, int size) {
 59  25
       this.type = type;      
 60  25
       this.size = size;
 61  25
    }
 62  
    
 63  
    /**
 64  
     * This is the instance that is acquired from this type. This is
 65  
     * typically used if the <code>isReference</code> method is true.
 66  
     * If there was to type reference provided then this returns null
 67  
     * otherwise this will delegate to the <code>Type</code> given.
 68  
     * 
 69  
     * @return this returns a reference to an existing array
 70  
     */
 71  
    public Object getInstance() throws Exception {
 72  12
       return getInstance(type);
 73  
    }
 74  
    
 75  
    /**
 76  
     * This is the instance that is acquired from this type. This is
 77  
     * typically used if the <code>isReference</code> method is true.
 78  
     * If there was to type reference provided then this returns null
 79  
     * otherwise this will delegate to the <code>Type</code> given.
 80  
     * 
 81  
     * @param type the type to convert this array instance to
 82  
     * 
 83  
     * @return this returns a reference to an existing array
 84  
     */
 85  
    public Object getInstance(Class type) throws Exception {  
 86  12
       return Array.newInstance(type, size);
 87  
    }
 88  
    
 89  
    /**
 90  
     * This method is used acquire the value from the type and if
 91  
     * possible replace the value for the type. If the value can
 92  
     * not be replaced then an exception should be thrown. This 
 93  
     * is used to allow primitives to be inserted into a graph.
 94  
     * 
 95  
     * @param value this is the value to insert as the type
 96  
     * 
 97  
     * @return an instance of the type this object represents
 98  
     */
 99  
    public Object getInstance(Object value) throws Exception {
 100  12
       return value;
 101  
    }
 102  
    
 103  
    /**
 104  
     * This will return the component type for the array instance 
 105  
     * that is produced by this object. Depending on the constructor 
 106  
     * used this will either delegate to the <code>Type</code> object 
 107  
     * specified or will return the component type class provided. 
 108  
     * 
 109  
     * @return this returns the component type for the array
 110  
     */
 111  
    public Class getType() {
 112  57
       return type;
 113  
    }
 114  
    
 115  
    /**
 116  
     * This will return true if the <code>Type</code> object provided
 117  
     * is a reference type. Typically a reference type refers to a 
 118  
     * type that is substituted during the deserialization process 
 119  
     * and so constitutes an object that does not need initialization.
 120  
     * 
 121  
     * @return this returns true if the type is a reference type
 122  
     */
 123  
    public boolean isReference() {
 124  5
       return false;
 125  
    }   
 126  
 }