Coverage Report - org.simpleframework.xml.core.CacheParameter
 
Classes in this File Line Coverage Branch Coverage Complexity
CacheParameter
69%
18/26
N/A
1
 
 1  
 /*
 2  
  * CacheParameter.java April 2012
 3  
  *
 4  
  * Copyright (C) 2012, 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.annotation.Annotation;
 22  
 
 23  
 /**
 24  
  * The <code>CacheParameter</code> object represents a parameter 
 25  
  * which caches its values internally. As well as caching parameter
 26  
  * values this also caches a key from a <code>Label</code> object
 27  
  * which ties the parameter an label together making it possible
 28  
  * to reference each other from hash containers.
 29  
  * 
 30  
  * @author Niall Gallagher
 31  
  */
 32  
 class CacheParameter implements Parameter{
 33  
 
 34  
    /**
 35  
     * This is the annotation used to represent this parameter.
 36  
     */
 37  
    private final Annotation annotation;
 38  
    
 39  
    /**
 40  
     * This is the XPath expression used to represent the parameter.
 41  
     */
 42  
    private final Expression expression;
 43  
    
 44  
    /**
 45  
     * This is the name of the element or attribute for the parameter.
 46  
     */
 47  
    private final String name;
 48  
    
 49  
    /**
 50  
     * This is the path of the element or attribute for the parameter.
 51  
     */
 52  
    private final String path;
 53  
    
 54  
    /**
 55  
     * This is the string representation of this parameter object.
 56  
     */
 57  
    private final String string;
 58  
    
 59  
    /**
 60  
     * This is the type within the constructor of this parameter.
 61  
     */
 62  
    private final Class type;
 63  
    
 64  
    /**
 65  
     * This is the key that uniquely identifies this parameter.
 66  
     */
 67  
    private final Object key;
 68  
    
 69  
    /**
 70  
     * This is the index within the constructor for the parameter.
 71  
     */
 72  
    private final int index;
 73  
    
 74  
    /**
 75  
     * Determines if this parameter represents a primitive value.
 76  
     */
 77  
    private final boolean primitive;
 78  
    
 79  
    /**
 80  
     * This is true if this parameter is required to exist.
 81  
     */
 82  
    private final boolean required;
 83  
    
 84  
    /**
 85  
     * This is true if this parameter represents an attribute.
 86  
     */
 87  
    private final boolean attribute;
 88  
    
 89  
    /**
 90  
     * This is true if this parameter represents a text value.
 91  
     */
 92  
    private final boolean text;
 93  
    
 94  
    /**
 95  
     * Constructor for the <code>CacheParameter</code> object. This
 96  
     * is used to create a parameter that internally caches all of
 97  
     * the information of the provided parameter and also makes 
 98  
     * use of the label provided to generate a unique key.
 99  
     * 
 100  
     * @param value this is the parameter to cache values from
 101  
     * @param label this is the label to acquire the key from
 102  
     */
 103  554
    public CacheParameter(Parameter value, Label label) throws Exception {
 104  554
       this.annotation = value.getAnnotation();
 105  554
       this.expression = value.getExpression();
 106  554
       this.attribute = value.isAttribute();
 107  554
       this.primitive = value.isPrimitive();
 108  554
       this.required = label.isRequired();
 109  554
       this.string = value.toString();
 110  554
       this.text = value.isText();
 111  554
       this.index = value.getIndex();
 112  554
       this.name = value.getName();
 113  554
       this.path = value.getPath();
 114  554
       this.type = value.getType();
 115  554
       this.key = label.getKey();
 116  554
    }
 117  
    
 118  
    /**
 119  
     * This is the key used to represent the parameter. The key is
 120  
     * used to store the parameter in hash containers. Unlike the
 121  
     * path is not necessarily the path for the parameter.
 122  
     * 
 123  
     * @return this is the key used to represent the parameter
 124  
     */
 125  
    public Object getKey() {
 126  2326
       return key;
 127  
    }
 128  
 
 129  
    /**
 130  
     * This is used to acquire the annotated type class. The class
 131  
     * is the type that is to be deserialized from the XML. This
 132  
     * is used to validate against annotated fields and methods.
 133  
     * 
 134  
     * @return this returns the type used for the parameter
 135  
     */
 136  
    public Class getType() {
 137  602
       return type;
 138  
    }
 139  
 
 140  
    /**
 141  
     * This returns the index position of the parameter in the
 142  
     * constructor. This is used to determine the order of values
 143  
     * that are to be injected in to the constructor.
 144  
     * 
 145  
     * @return this returns the index for the parameter
 146  
     */
 147  
    public int getIndex() {
 148  0
       return index;
 149  
    }
 150  
 
 151  
    /**
 152  
     * This is used to acquire the annotation that is used for the
 153  
     * parameter. The annotation provided will be an XML annotation
 154  
     * such as the <code>Element</code> or <code>Attribute</code>
 155  
     * annotation.
 156  
     * 
 157  
     * @return this returns the annotation used on the parameter
 158  
     */
 159  
    public Annotation getAnnotation() {
 160  0
       return annotation;
 161  
    }
 162  
 
 163  
    /**
 164  
     * This method is used to return an XPath expression that is 
 165  
     * used to represent the position of this parameter. If there is 
 166  
     * no XPath expression associated with this then an empty path 
 167  
     * is returned. This will never return a null expression.
 168  
     * 
 169  
     * @return the XPath expression identifying the location
 170  
     */
 171  
    public Expression getExpression() {
 172  0
       return expression;
 173  
    }
 174  
 
 175  
    /**
 176  
     * This is used to acquire the name of the parameter that this
 177  
     * represents. The name is determined using annotation and 
 178  
     * the name attribute of that annotation, if one is provided.
 179  
     * 
 180  
     * @return this returns the name of the annotated parameter
 181  
     */
 182  
    public String getName() {
 183  0
       return name;
 184  
    }
 185  
 
 186  
    /**
 187  
     * This is used to acquire the path of the element or attribute
 188  
     * represented by this parameter. The path is determined by
 189  
     * acquiring the XPath expression and appending the name of the
 190  
     * label to form a fully qualified path.
 191  
     * 
 192  
     * @return returns the path that is used for this parameter
 193  
     */
 194  
    public String getPath() {
 195  0
       return path;
 196  
    }
 197  
 
 198  
    /**
 199  
     * This is used to determine if the parameter is required. If 
 200  
     * an attribute is not required then it can be null. Which 
 201  
     * means that we can inject a null value. Also, this means we
 202  
     * can match constructors in a more flexible manner.
 203  
     * 
 204  
     * @return this returns true if the parameter is required
 205  
     */
 206  
    public boolean isRequired() {
 207  20
       return required;
 208  
    }
 209  
 
 210  
    /**
 211  
     * This is used to determine if the parameter is primitive. A
 212  
     * primitive parameter must not be null. As there is no way to
 213  
     * provide the value to the constructor. A default value is 
 214  
     * not a good solution as it affects the constructor score.
 215  
     * 
 216  
     * @return this returns true if the parameter is primitive
 217  
     */
 218  
    public boolean isPrimitive() {
 219  20
       return primitive;
 220  
    }
 221  
 
 222  
    /**
 223  
     * This method is used to determine if the parameter represents 
 224  
     * an attribute. This is used to style the name so that elements
 225  
     * are styled as elements and attributes are styled as required.
 226  
     * 
 227  
     * @return this is used to determine if this is an attribute
 228  
     */
 229  
    public boolean isAttribute() {
 230  0
       return attribute;
 231  
    }
 232  
 
 233  
    /**
 234  
     * This is used to determine if the parameter represents text. 
 235  
     * If this represents text it typically does not have a name,
 236  
     * instead the empty string represents the name. Also text
 237  
     * parameters can not exist with other text parameters.
 238  
     * 
 239  
     * @return returns true if this parameter represents text
 240  
     */
 241  
    public boolean isText() {
 242  0
       return text;
 243  
    }
 244  
    
 245  
    /**
 246  
     * This is used to provide a textual representation of the 
 247  
     * parameter. Providing a string describing the parameter is
 248  
     * useful for debugging and for exception messages.
 249  
     * 
 250  
     * @return this returns the string representation for this
 251  
     */
 252  
    public String toString() {
 253  0
       return string;
 254  
    }  
 255  
 }