Coverage Report - org.simpleframework.xml.convert.Reference
 
Classes in this File Line Coverage Branch Coverage Complexity
Reference
58%
7/12
0%
0/2
1.167
 
 1  
 /*
 2  
  * Reference.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.convert;
 20  
 
 21  
 import org.simpleframework.xml.strategy.Value;
 22  
 
 23  
 /**
 24  
  * The <code>Reference</code> object represents a value that holds
 25  
  * an object instance. If an object instance is to be provided from
 26  
  * a <code>Strategy</code> implementation it must be wrapped in a 
 27  
  * value object. The value object can then provide the details of
 28  
  * the instance and the actual object instance to the serializer.
 29  
  * 
 30  
  * @author Niall Gallagher
 31  
  */
 32  
 class Reference implements Value {
 33  
    
 34  
    /**
 35  
     * This represents the original value returned from a strategy.
 36  
     */
 37  
    private Value value;
 38  
    
 39  
    /**
 40  
     * This represents the object instance that this represents.
 41  
     */
 42  
    private Object data;
 43  
    
 44  
    /**
 45  
     * Constructor for a <code>Reference</code> object. To create
 46  
     * this a value and an object instance is required. The value
 47  
     * provided may be null, but the instance should be a valid
 48  
     * object instance to be used by the serializer.
 49  
     * 
 50  
     * @param value this is the original value from a strategy
 51  
     * @param data this is the object instance that is wrapped
 52  
     */
 53  27
    public Reference(Value value, Object data){
 54  27
       this.value = value;
 55  27
       this.data = data;
 56  27
    }
 57  
    
 58  
    /**
 59  
     * This will return the length of an array reference. Because
 60  
     * the value will represent the value itself the length is 
 61  
     * never used, as no instance needs to be created.
 62  
     * 
 63  
     * @return this will always return zero for a reference
 64  
     */
 65  
    public int getLength() {
 66  0
       return 0;
 67  
    }
 68  
    
 69  
    public Class getType() {
 70  54
       return data.getClass();
 71  
    }
 72  
    
 73  
    /**
 74  
     * This returns the actual object instance that is held by this
 75  
     * reference object.
 76  
     */
 77  
    public Object getValue() {
 78  27
       return data;
 79  
    }
 80  
    
 81  
    /**
 82  
     * This will always return true as this <code>Value</code> object
 83  
     * will always contain an object instance. Returning true from 
 84  
     * this method tells the serializer that there is no need to
 85  
     * actually perform any further deserialization.
 86  
     * 
 87  
     * @return this always returns true as this will be a reference
 88  
     */
 89  
    public boolean isReference() {
 90  54
       return true;
 91  
    }
 92  
    
 93  
    /**
 94  
     * This is used to set the value of the object. If the internal
 95  
     * <code>Value</code> is not null then the internal value will 
 96  
     * have the instance set also. 
 97  
     * 
 98  
     * @param data this is the object instance that is to be set
 99  
     */
 100  
    public void setValue(Object data) {
 101  0
       if(value != null) {
 102  0
          value.setValue(data);
 103  
       }
 104  0
       this.data = data;
 105  0
    }
 106  
 }