Coverage Report - org.simpleframework.xml.strategy.Reference
 
Classes in this File Line Coverage Branch Coverage Complexity
Reference
80%
8/10
N/A
1
 
 1  
 /*
 2  
  * Reference.java May 2006
 3  
  *
 4  
  * Copyright (C) 2006, 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.strategy;
 20  
 
 21  
 /**
 22  
  * The <code>Reference</code> object represents an object that 
 23  
  * is used to provide a reference to an already instantiated value.
 24  
  * This is what is used if there is a cycle in the object graph. 
 25  
  * The <code>getValue</code> method of this object will simply
 26  
  * return the object instance that was previously created.
 27  
  * 
 28  
  * @author Niall Gallagher
 29  
  */
 30  
 class Reference implements Value {
 31  
    
 32  
    /**
 33  
     * This is the object instance that has already be created.
 34  
     */
 35  
    private Object value;
 36  
    
 37  
    /**
 38  
     * This is the type of the object that this references.
 39  
     */
 40  
    private Class type;
 41  
    
 42  
    /**
 43  
     * Constructor for the <code>Reference</code> object. This 
 44  
     * is used to create a value that will produce the specified 
 45  
     * value when the <code>getValue</code> method is invoked.
 46  
     * 
 47  
     * @param value the value for the reference this represents
 48  
     * @param type this is the type value for the instance
 49  
     */
 50  4200
    public Reference(Object value, Class type) {      
 51  4200
       this.value = value;      
 52  4200
       this.type = type;
 53  4200
    }
 54  
    
 55  
    /**
 56  
     * This is used to acquire a reference to the instance that is
 57  
     * taken from the created object graph. This enables any cycles
 58  
     * in the graph to be reestablished from the persisted XML.
 59  
     * 
 60  
     * @return this returns a reference to the created instance
 61  
     */
 62  
    public Object getValue() {     
 63  4165
       return value;      
 64  
    }
 65  
    
 66  
    /**
 67  
     * This method is used set the value within this object. Once
 68  
     * this is set then the <code>getValue</code> method will return
 69  
     * the object that has been provided. Typically this will not 
 70  
     * be set as this represents a reference value.
 71  
     * 
 72  
     * @param value this is the value to insert as the type
 73  
     */
 74  
    public void setValue(Object value) {
 75  0
       this.value = value;
 76  0
    }
 77  
    
 78  
    /**
 79  
     * This returns the type for the object that this references.
 80  
     * This will basically return the <code>getClass</code> class
 81  
     * from the referenced instance. This is used to ensure that
 82  
     * the type this represents is compatible to the object field.
 83  
     * 
 84  
     * @return this returns the type for the referenced object
 85  
     */
 86  
    public Class getType() {
 87  8414
       return type;
 88  
    }
 89  
    
 90  
    /**
 91  
     * This returns zero as this is a reference and will typically
 92  
     * not be used to instantiate anything. If the reference is an
 93  
     * an array then this can not be used to instantiate it.
 94  
     * 
 95  
     * @return this returns zero regardless of the value type
 96  
     */
 97  
    public int getLength() {
 98  16
       return 0;
 99  
    }
 100  
    
 101  
    /**
 102  
     * This always returns true for this object. This indicates to
 103  
     * the deserialization process that there should be not further
 104  
     * deserialization of the object from the XML source stream.
 105  
     * 
 106  
     * @return because this is a reference this is always true 
 107  
     */
 108  
    public boolean isReference() {      
 109  8368
       return true;
 110  
    }
 111  
 }