Coverage Report - org.simpleframework.xml.core.ArrayInstance
 
Classes in this File Line Coverage Branch Coverage Complexity
ArrayInstance
100%
16/16
66%
4/6
1.8
 
 1  
 /*
 2  
  * ArrayInstance.java January 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.lang.reflect.Array;
 22  
 
 23  
 import org.simpleframework.xml.strategy.Value;
 24  
 
 25  
 /**
 26  
  * The <code>ArrayInstance</code> object is used for creating arrays
 27  
  * from a specified <code>Value</code> object. This allows primitive
 28  
  * and composite arrays to be acquired either by reference or by value 
 29  
  * from the given value object. This must be  given the length of the 
 30  
  * array so that it can be allocated correctly.
 31  
  * 
 32  
  * @author Niall Gallagher
 33  
  * 
 34  
  * @see org.simpleframework.xml.core.Instance
 35  
  */
 36  
 class ArrayInstance implements Instance {
 37  
  
 38  
    /**
 39  
     * This is the value object that contains the criteria.
 40  
     */
 41  
    private final Value value;
 42  
    
 43  
    /**
 44  
     * This is the array component type for the created array.
 45  
     */
 46  
    private final Class type;
 47  
    
 48  
    /**
 49  
     * This is the length of the array to be instantiated.
 50  
     */
 51  
    private final int length;
 52  
    
 53  
    /**
 54  
     * Constructor for the <code>ArrayInstance</code> object. This
 55  
     * is used to create an object that can create an array of the
 56  
     * given length and specified component type.
 57  
     * 
 58  
     * @param value this is the value object describing the instance
 59  
     */
 60  140
    public ArrayInstance(Value value) {
 61  140
       this.length = value.getLength();
 62  140
       this.type = value.getType();
 63  140
       this.value = value;
 64  140
    }
 65  
    
 66  
    /**
 67  
     * This method is used to acquire an instance of the type that
 68  
     * is defined by this object. If for some reason the type can
 69  
     * not be instantiated an exception is thrown from this.
 70  
     * 
 71  
     * @return an instance of the type this object represents
 72  
     */
 73  
    public Object getInstance() throws Exception {      
 74  102
       if(value.isReference()) {
 75  12
          return value.getValue();
 76  
       }
 77  90
       Object array = Array.newInstance(type, length);
 78  
       
 79  90
       if(value != null) {
 80  90
          value.setValue(array);
 81  
       }
 82  90
       return array;
 83  
    }
 84  
    
 85  
    /**
 86  
     * This method is used acquire the value from the type and if
 87  
     * possible replace the value for the type. If the value can
 88  
     * not be replaced then an exception should be thrown. This 
 89  
     * is used to allow primitives to be inserted into a graph.
 90  
     * 
 91  
     * @param array this is the array to insert as the value
 92  
     * 
 93  
     * @return an instance of the type this object represents
 94  
     */
 95  
    public Object setInstance(Object array) {
 96  35
       if(value != null) {
 97  35
          value.setValue(array);
 98  
       }
 99  35
       return array;
 100  
    }
 101  
    
 102  
    /**
 103  
     * This is the type of the object instance that will be created
 104  
     * by the <code>getInstance</code> method. This allows the 
 105  
     * deserialization process to perform checks against the field.
 106  
     * 
 107  
     * @return the type of the object that will be instantiated
 108  
     */
 109  
    public Class getType() {
 110  35
       return type;
 111  
    }
 112  
 
 113  
    /**
 114  
     * This is used to determine if the type is a reference type.
 115  
     * A reference type is a type that does not require any XML
 116  
     * deserialization based on its annotations. Values that are
 117  
     * references could be substitutes objects of existing ones. 
 118  
     * 
 119  
     * @return this returns true if the object is a reference
 120  
     */
 121  
    public boolean isReference() {
 122  140
       return value.isReference();
 123  
    }
 124  
 }