Coverage Report - org.simpleframework.xml.strategy.Allocate
 
Classes in this File Line Coverage Branch Coverage Complexity
Allocate
100%
13/13
50%
1/2
1.167
 
 1  
 /*
 2  
  * Allocate.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.strategy;
 20  
 
 21  
 import java.util.Map;
 22  
 
 23  
 /**
 24  
  * The <code>Allocate</code> object is used to represent an entity 
 25  
  * that has not yet been created and needs to be allocated to the
 26  
  * the object graph. This is given a map that contains each node 
 27  
  * in the graph keyed via a unique identifier. When an instance is 
 28  
  * created and set then it is added to the object graph.
 29  
  * 
 30  
  * @author Niall Gallagher
 31  
  */
 32  
 class Allocate implements Value {
 33  
    
 34  
    /**
 35  
     * This is used to create an instance of the specified type.
 36  
     */
 37  
    private Value value;
 38  
    
 39  
    /**
 40  
     * This is the unique key that is used to store the value.
 41  
     */
 42  
    private String key;
 43  
    
 44  
    /**
 45  
     * This is used to store each instance in the object graph.
 46  
     */
 47  
    private Map map;
 48  
    
 49  
    /**
 50  
     * Constructor for the <code>Allocate</code> object. This is used
 51  
     * to create a value that can be used to set any object in to the
 52  
     * internal object graph so references can be discovered.
 53  
     * 
 54  
     * @param value this is the value used to describe the instance
 55  
     * @param map this contains each instance mapped with a key
 56  
     * @param key this is the unique key representing this instance
 57  
     */
 58  22270
    public Allocate(Value value, Map map, String key) {
 59  22270
       this.value = value;
 60  22270
       this.map = map;
 61  22270
       this.key = key;
 62  22270
    }
 63  
    
 64  
    /**
 65  
     * This method is used to acquire an instance of the type that
 66  
     * is defined by this object. If the object is not set in the
 67  
     * graph then this will return null.
 68  
     * 
 69  
     * @return an instance of the type this object represents
 70  
     */
 71  
    public Object getValue() {
 72  2
       return map.get(key);
 73  
    }
 74  
    
 75  
    /**
 76  
     * This method is used to set the provided object in to the graph
 77  
     * so that it can later be retrieved. If the key for this value
 78  
     * is null then no object is set in the object graph.
 79  
     * 
 80  
     * @param object this is the value to insert to the graph
 81  
     */
 82  
    public void setValue(Object object) {
 83  39674
       if(key != null) {
 84  39674
          map.put(key, object);
 85  
       }     
 86  39674
       value.setValue(object);
 87  39674
    }
 88  
    
 89  
    /**
 90  
     * This is the type of the object instance that will be created
 91  
     * and set on this value. If this represents an array then this
 92  
     * is the component type for the array to be created.
 93  
     * 
 94  
     * @return the type of the object that will be instantiated
 95  
     */
 96  
    public Class getType() {
 97  44593
       return value.getType();
 98  
    }
 99  
    
 100  
    /**
 101  
     * This returns the length of an array if this value represents
 102  
     * an array. If this does not represent an array then this will
 103  
     * return zero. It is up to the deserialization process to 
 104  
     * determine if the annotated field or method is an array.
 105  
     * 
 106  
     * @return this returns the length of the array object
 107  
     */
 108  
    public int getLength() {
 109  79
       return value.getLength();
 110  
    }
 111  
    
 112  
    /**
 113  
     * This method always returns false for the default type. This
 114  
     * is because by default all elements encountered within the 
 115  
     * XML are to be deserialized based on there XML annotations.
 116  
     * 
 117  
     * @return this returns false for each type encountered     
 118  
     */  
 119  
    public boolean isReference() {
 120  31108
       return false;
 121  
    }
 122  
 }