Coverage Report - org.simpleframework.xml.core.OverrideValue
 
Classes in this File Line Coverage Branch Coverage Complexity
OverrideValue
80%
8/10
N/A
1
 
 1  
 /*
 2  
  * OverrideValue.java January 2010
 3  
  *
 4  
  * Copyright (C) 2010, 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>OverrideValue</code> is used to represent a value that
 25  
  * contains an override type. Providing a value in this way ensures
 26  
  * that should an XML element not contain any data representing
 27  
  * the type of object then the type data can be provided.
 28  
  * 
 29  
  * @author Niall Gallagher
 30  
  */
 31  
 class OverrideValue implements Value {
 32  
    
 33  
    /**
 34  
     * This is the value that is used internally for this value.
 35  
     */
 36  
    private final Value value;
 37  
    
 38  
    /**
 39  
     * This is the type that is used to represent the value.
 40  
     */
 41  
    private final Class type;
 42  
    
 43  
    /**
 44  
     * Constructor for the <code>OverrideValue</code> object. This
 45  
     * will delegate to an internal value instance but will provide
 46  
     * the declared type when requested. 
 47  
     * 
 48  
     * @param value this is the value that this will delegate to
 49  
     * @param type this is the override type for this value
 50  
     */
 51  26
    public OverrideValue(Value value, Class type){
 52  26
       this.value = value;
 53  26
       this.type = type;
 54  26
    }
 55  
 
 56  
    /**
 57  
     * This method is used to acquire an instance of the type that
 58  
     * is defined by this object. If the value has not been set
 59  
     * then this method will return null if this is not a reference.
 60  
     * 
 61  
     * @return an instance of the type this object represents
 62  
     */
 63  
    public Object getValue() {
 64  0
       return value.getValue();
 65  
    }
 66  
 
 67  
    /**
 68  
     * This method is used set the value within this object. Once
 69  
     * this is set then the <code>getValue</code> method will return
 70  
     * the object that has been provided for consistency. 
 71  
     * 
 72  
     * @param instance this is the value to insert as the type
 73  
     */
 74  
    public void setValue(Object instance) {
 75  56
       value.setValue(instance);
 76  56
    }
 77  
 
 78  
    /**
 79  
     * This is the type of the object instance this represents. The
 80  
     * type returned by this is used to instantiate an object which
 81  
     * will be set on this value and the internal graph maintained.
 82  
     * 
 83  
     * @return the type of the object that must be instantiated
 84  
     */
 85  
    public Class getType() {
 86  52
       return type;
 87  
    }
 88  
 
 89  
    /**
 90  
     * This returns the length of the array that is to be allocated.
 91  
     * If this value does not represent an array then this should
 92  
     * return zero to indicate that it is not an array object.
 93  
     * 
 94  
     * @return this returns the number of elements for the array
 95  
     */
 96  
    public int getLength() {
 97  0
       return value.getLength();
 98  
    }
 99  
 
 100  
    /**
 101  
     * This will return true if the object represents a reference.
 102  
     * A reference will provide a valid instance when this objects 
 103  
     * getter is invoked. A valid instance can be a null.
 104  
     * 
 105  
     * @return this returns true if this represents a reference
 106  
     */
 107  
    public boolean isReference() {
 108  41
       return value.isReference();
 109  
    }
 110  
 }