Coverage Report - org.simpleframework.xml.core.Comparer
 
Classes in this File Line Coverage Branch Coverage Complexity
Comparer
95%
22/23
85%
12/14
3.5
 
 1  
 /*
 2  
  * Comparer.java December 2009
 3  
  *
 4  
  * Copyright (C) 2009, 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  
 import java.lang.reflect.Method;
 23  
 
 24  
 /**
 25  
  * The <code>Comparer</code> is used to compare annotations on the
 26  
  * attributes of that annotation. Unlike the <code>equals</code>
 27  
  * method, this can ignore some attributes based on the name of the
 28  
  * attributes. This is useful if some annotations have overridden
 29  
  * values, such as the field or method name.
 30  
  * 
 31  
  * @author Niall Gallagher
 32  
  */
 33  
 class Comparer {
 34  
    
 35  
    /**
 36  
     * This is the default attribute to ignore for the comparer.
 37  
     */
 38  
    private static final String NAME = "name";
 39  
    
 40  
    /**
 41  
     * This is the list of names to ignore for this instance.
 42  
     */
 43  
    private final String[] ignore;
 44  
    
 45  
    /**
 46  
     * Constructor for the <code>Comparer</code> object. This is
 47  
     * used to create a comparer that has a default set of names
 48  
     * to be ignored during the comparison of annotations.
 49  
     */
 50  
    public Comparer() {
 51  2853
       this(NAME);
 52  2853
    }
 53  
 
 54  
    /**
 55  
     * Constructor for the <code>Comparer</code> object. This is
 56  
     * used to create a comparer that has a default set of names
 57  
     * to be ignored during the comparison of annotations.
 58  
     * 
 59  
     * @param ignore this is the set of attributes to be ignored
 60  
     */
 61  2853
    public Comparer(String... ignore) {
 62  2853
       this.ignore = ignore;
 63  2853
    }
 64  
    
 65  
    /**
 66  
     * This is used to determine if two annotations are equals based
 67  
     * on the attributes of the annotation. The comparison done can
 68  
     * ignore specific attributes, for instance the name attribute.
 69  
     * 
 70  
     * @param left this is the left side of the comparison done
 71  
     * @param right this is the right side of the comparison done
 72  
     * 
 73  
     * @return this returns true if the annotations are equal
 74  
     */
 75  
    public boolean equals(Annotation left, Annotation right) throws Exception {
 76  1008
       Class type = left.annotationType();
 77  1008
       Class expect = right.annotationType();
 78  1008
       Method[] list = type.getDeclaredMethods();
 79  
 
 80  1008
       if(type.equals(expect)) {
 81  4609
          for(Method method : list) {
 82  3751
             if(!isIgnore(method)) {
 83  2775
                Object value = method.invoke(left);
 84  2775
                Object other = method.invoke(right);
 85  
                
 86  2775
                if(!value.equals(other)) {
 87  150
                   return false;
 88  
                }
 89  
             }
 90  
          }
 91  858
          return true;
 92  
       }
 93  0
       return false;
 94  
    }
 95  
    
 96  
    /**
 97  
     * This is used to determine if the method for an attribute is 
 98  
     * to be ignore. To determine if it should be ignore the method
 99  
     * name is compared against the list of attributes to ignore.
 100  
     * 
 101  
     * @param method this is the method to be evaluated
 102  
     * 
 103  
     * @return this returns true if the method should be ignored
 104  
     */
 105  
    private boolean isIgnore(Method method) {
 106  3751
       String name = method.getName();
 107  
       
 108  3751
       if(ignore != null) {
 109  6526
          for(String value : ignore) {
 110  3751
             if(name.equals(value)) {
 111  976
                return true;
 112  
             }
 113  
          }
 114  
       }
 115  2775
       return false;
 116  
    }
 117  
 }