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