Coverage Report - org.kuali.rice.core.util.EqualsAndHashCodeUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
EqualsAndHashCodeUtils
96%
30/31
87%
21/24
9
 
 1  
 /*
 2  
  * Copyright 2006-2011 The Kuali Foundation
 3  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 4  
  * you may not use this file except in compliance with the License.
 5  
  * You may obtain a copy of the License at
 6  
  *
 7  
  * http://www.opensource.org/licenses/ecl2.php
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing, software
 10  
  * distributed under the License is distributed on an "AS IS" BASIS,
 11  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
  * See the License for the specific language governing permissions and
 13  
  * limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.rice.core.util;
 17  
 
 18  
 import java.lang.reflect.Field;
 19  
 import java.util.Calendar;
 20  
 
 21  
 import org.apache.commons.lang.ArrayUtils;
 22  
 import org.apache.commons.lang.builder.HashCodeBuilder;
 23  
 
 24  
 /**
 25  
  * Class of static utility methods used to aid in the generation of hashcode values and equals comparisons of objects
 26  
  * for corner cases that EqualsBuilder and HashCodeBuilder of commons-lang cannot cover.
 27  
  */
 28  0
 public class EqualsAndHashCodeUtils {
 29  
 
 30  
     /**
 31  
      * This method provides an equals comparison of two objects by evaluating the results of compareTo across specified
 32  
      * internal fields of the class of the two objects being compared.
 33  
      * <p/>
 34  
      * This method should be used where evaluating equality on fields of two instances of type T using .equals() yields
 35  
      * false, but for the purposes of determining equality of the two instances of type T, should be true.  An example
 36  
      * is where a class has internal fields of type Calendar that need equality determined using only its time value
 37  
      * and not other internal fields of Calendar.
 38  
      *
 39  
      * @param o1         The first object used in an equality operation using compareTo
 40  
      * @param o2         The second object used in an equality operation using compareTo
 41  
      * @param fieldNames All field names within type T that should be determined equal or not using compareTo
 42  
      * @param <T>        Type of both o1 and o2 parameters.  Guarantees both o1 and o2 are the same reference type.
 43  
      * @return true if (o1.field.compareTo(o2.field) == 0) is true for all passed in fieldNames.  Otherwise false
 44  
      *         is returned.  False is also returned if any fields specified in fieldNames are not of type Comparable or if one
 45  
      *         (but not both) of the passed in objects are null references.
 46  
      */
 47  
     public static <T> boolean equalsUsingCompareToOnFields(T o1, T o2, String... fieldNames) {
 48  30
         if (o1 == o2) {
 49  2
             return true;
 50  
         }
 51  28
         if (o1 == null || o2 == null) {
 52  1
             return false;
 53  
         }
 54  
 
 55  27
         boolean isEqual = true;
 56  27
         Class<?> targetClass = o1.getClass();
 57  
         try {
 58  54
             for (String fieldName : fieldNames) {
 59  31
                 Field field = targetClass.getDeclaredField(fieldName);
 60  30
                 field.setAccessible(true);
 61  30
                 Class<?> fieldClass = field.getType();
 62  
 
 63  30
                 if (ArrayUtils.contains(fieldClass.getInterfaces(), Comparable.class)) {
 64  29
                     @SuppressWarnings("unchecked") Comparable<Object> c1 = (Comparable<Object>) field.get(o1);
 65  29
                     @SuppressWarnings("unchecked") Comparable<Object> c2 = (Comparable<Object>) field.get(o2);
 66  29
                     if (c1 == c2) {
 67  6
                         continue;
 68  
                     }
 69  23
                     if (c1 == null || c2 == null) {
 70  1
                         isEqual = false;
 71  
                     } else {
 72  22
                         isEqual = (c1.compareTo(c2) == 0);
 73  
                     }
 74  23
                 } else {
 75  1
                     isEqual = false;
 76  
                 }
 77  
 
 78  24
                 if (!isEqual) {
 79  3
                     break;
 80  
                 }
 81  
             }
 82  
 
 83  26
             return isEqual;
 84  1
         } catch (Exception e) {
 85  1
             throw new RuntimeException(e);
 86  
         }
 87  
     }
 88  
 
 89  
     /**
 90  
      * Generates an int hashcode from all calendars passed in.  This is a convenience method for hashcode methods
 91  
      * to call if they have to generate hashcodes from fields of type Calendar when those Calendar fields
 92  
      * have equality evaluated using compareTo and not equals within the equals method of the container class.
 93  
      *
 94  
      * @param calendars
 95  
      * @return int hashcode value generated by using the long value returned from each Calendar.getTimeInMillis()
 96  
      */
 97  
     public static int hashCodeForCalendars(Calendar... calendars) {
 98  76
         HashCodeBuilder hcb = new HashCodeBuilder();
 99  152
         for (Calendar calendar : calendars) {
 100  76
             if (calendar != null) {
 101  76
                 hcb.append(calendar.getTimeInMillis());
 102  
             }
 103  
         }
 104  76
         return hcb.toHashCode();
 105  
     }
 106  
 }