Coverage Report - org.simpleframework.xml.core.LabelKey
 
Classes in this File Line Coverage Branch Coverage Complexity
LabelKey
75%
15/20
60%
6/10
3
 
 1  
 /*
 2  
  * LabelKey.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>LabelKey</code> object is used to create a key that will
 25  
  * uniquely identify an annotated method within a class. Creation of 
 26  
  * a key in this way enables annotated methods and fields to be cached
 27  
  * and looked up using a key.
 28  
  * 
 29  
  * @author Niall Gallagher
 30  
  */
 31  
 class LabelKey {
 32  
 
 33  
    /**
 34  
     * This is the annotation type that this is represented in the key.
 35  
     */
 36  
    private final Class label;
 37  
    
 38  
    /**
 39  
     * This is the declaring class where the method or field is defined.
 40  
     */
 41  
    private final Class owner;
 42  
    
 43  
    /**
 44  
     * This is the type that is represented by the annotated contact.
 45  
     */
 46  
    private final Class type;
 47  
    
 48  
    /**
 49  
     * This is the name of the field or method that is represented.
 50  
     */
 51  
    private final String name;
 52  
    
 53  
    /**
 54  
     * Constructor for the <code>LabelKey</code> object. This is used
 55  
     * to create an object using the contact and associated annotation
 56  
     * that can uniquely identity the label.
 57  
     * 
 58  
     * @param contact this is the contact that has been annotated
 59  
     * @param label this is the primary annotation associated with this
 60  
     */
 61  3637
    public LabelKey(Contact contact, Annotation label) {
 62  3637
       this.owner = contact.getDeclaringClass();
 63  3637
       this.label = label.annotationType();
 64  3637
       this.name = contact.getName();
 65  3637
       this.type = contact.getType();    
 66  3637
    }
 67  
    
 68  
    /**
 69  
     * This returns the unique has code used for this key. The hash 
 70  
     * code is created by combining the hash code of the method or field
 71  
     * name with the hash code of the declaring class.
 72  
     * 
 73  
     * @return this returns the hash code associated with this key
 74  
     */
 75  
    public int hashCode() {
 76  7126
       return name.hashCode() ^ owner.hashCode();
 77  
    }
 78  
    
 79  
    /**
 80  
     * This is used to determine if two keys are the same. Ultimately 
 81  
     * two keys are equal if they represent the same contact and
 82  
     * annotation from that contact. If everything is equal by
 83  
     * identity then this will be true.
 84  
     * 
 85  
     * @param value this is the value to compare to this key
 86  
     * 
 87  
     * @return this returns true if both keys have the same data
 88  
     */
 89  
    public boolean equals(Object value) {
 90  180
       if(value instanceof LabelKey) {
 91  180
          return equals((LabelKey)value);
 92  
       }
 93  0
       return false;
 94  
    }
 95  
    
 96  
    /**
 97  
     * This is used to determine if two keys are the same. Ultimately 
 98  
     * two keys are equal if they represent the same contact and
 99  
     * annotation from that contact. If everything is equal by
 100  
     * identity then this will be true.
 101  
     * 
 102  
     * @param key this is the value to compare to this key
 103  
     * 
 104  
     * @return this returns true if both keys have the same data
 105  
     */
 106  
    private boolean equals(LabelKey key) {
 107  180
       if(key == this) {
 108  0
          return true;
 109  
       }
 110  180
       if(key.label != label) {
 111  6
          return false;
 112  
       }
 113  174
       if(key.owner != owner) {
 114  0
          return false;
 115  
       }
 116  174
       if(key.type != type) {
 117  0
          return false;
 118  
       }
 119  174
       return key.name.equals(name);
 120  
    }
 121  
    
 122  
    /**
 123  
     * This returns a string representation of this key. It contains
 124  
     * the name and the declaring class for the method or field.
 125  
     * This is primarily used for debugging purposes.
 126  
     * 
 127  
     * @return this returns a string representation of this key
 128  
     */
 129  
    public String toString() {
 130  0
       return String.format("key '%s' for %s", name, owner);
 131  
    }
 132  
 }