Coverage Report - org.kuali.rice.kns.util.BeanPropertyComparator
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanPropertyComparator
0%
0/43
0%
0/18
5.2
BeanPropertyComparator$1
0%
0/9
0%
0/4
5.2
BeanPropertyComparator$BeanComparisonException
0%
0/2
N/A
5.2
 
 1  
 /*
 2  
  * Copyright 2006-2007 The Kuali Foundation
 3  
  * 
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kns.util;
 17  
 
 18  
 import java.beans.PropertyDescriptor;
 19  
 import java.io.Serializable;
 20  
 import java.lang.reflect.InvocationTargetException;
 21  
 import java.util.Collections;
 22  
 import java.util.Comparator;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 
 26  
 import org.apache.commons.beanutils.PropertyUtils;
 27  
 import org.apache.commons.collections.comparators.ComparableComparator;
 28  
 import org.kuali.rice.core.api.exception.KualiException;
 29  
 import org.kuali.rice.core.util.type.TypeUtils;
 30  
 
 31  
 /**
 32  
  * This class compares the two beans using multiple property names.
 33  
  * 
 34  
  * 
 35  
  */
 36  
 public class BeanPropertyComparator implements Comparator, Serializable {
 37  
     private static final long serialVersionUID = -2675700473766186018L;
 38  
     boolean ignoreCase;
 39  
     private List propertyNames;
 40  
     private Comparator stringComparator;
 41  
     private Comparator booleanComparator;
 42  
     private Comparator genericComparator;
 43  
 
 44  
     /**
 45  
      * Constructs a PropertyComparator for comparing beans using the properties named in the given List; if the List is null, the
 46  
      * beans will be compared directly (by Properties will be compared in the order in which they are listed. Case will be ignored
 47  
      * in String comparisons.
 48  
      * 
 49  
      * @param propertyNames List of property names (as Strings) used to compare beans
 50  
      */
 51  
     public BeanPropertyComparator(List propertyNames) {
 52  0
         this(propertyNames, true);
 53  0
     }
 54  
 
 55  
     /**
 56  
      * Constructs a PropertyComparator for comparing beans using the properties named in the given List. Properties will be compared
 57  
      * in the order in which they are listed. Case will be ignored if ignoreCase is true.
 58  
      * 
 59  
      * @param propertyNames List of property names (as Strings) used to compare beans
 60  
      * @param ignoreCase if true, case will be ignored during String comparisons
 61  
      */
 62  0
     public BeanPropertyComparator(List propertyNames, boolean ignoreCase) {
 63  0
         if (propertyNames == null) {
 64  0
             throw new IllegalArgumentException("invalid (null) propertyNames list");
 65  
         }
 66  0
         if (propertyNames.size() == 0) {
 67  0
             throw new IllegalArgumentException("invalid (empty) propertyNames list");
 68  
         }
 69  0
         this.propertyNames = Collections.unmodifiableList(propertyNames);
 70  0
         this.ignoreCase = ignoreCase;
 71  
 
 72  0
         if (ignoreCase) {
 73  0
             this.stringComparator = String.CASE_INSENSITIVE_ORDER;
 74  
         }
 75  
         else {
 76  0
             this.stringComparator = ComparableComparator.getInstance();
 77  
         }
 78  0
         this.booleanComparator = new Comparator() {
 79  
             public int compare(Object o1, Object o2) {
 80  0
                 int compared = 0;
 81  
 
 82  0
                 Boolean b1 = (Boolean) o1;
 83  0
                 Boolean b2 = (Boolean) o2;
 84  
 
 85  0
                 if (!b1.equals(b2)) {
 86  0
                     if (b1.equals(Boolean.FALSE)) {
 87  0
                         compared = -1;
 88  
                     }
 89  
                     else {
 90  0
                         compared = 1;
 91  
                     }
 92  
                 }
 93  
 
 94  0
                 return compared;
 95  
             }
 96  
 
 97  
         };
 98  0
         this.genericComparator = ComparableComparator.getInstance();
 99  0
     }
 100  
 
 101  
 
 102  
     /**
 103  
      * Compare two JavaBeans by the properties given to the constructor. If no propertues
 104  
      * 
 105  
      * @param o1 Object The first bean to get data from to compare against
 106  
      * @param o2 Object The second bean to get data from to compare
 107  
      * @return int negative or positive based on order
 108  
      */
 109  
     public int compare(Object o1, Object o2) {
 110  0
         int compared = 0;
 111  
 
 112  
         try {
 113  0
             for (Iterator i = propertyNames.iterator(); (compared == 0) && i.hasNext();) {
 114  0
                 String currentProperty = i.next().toString();
 115  
 
 116  
                 // choose appropriate comparator
 117  0
                 Comparator currentComparator = null;
 118  
                 try {
 119  0
                     PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(o1, currentProperty);
 120  0
                     Class propertyClass = propertyDescriptor.getPropertyType();
 121  0
                     if (propertyClass.equals(String.class)) {
 122  0
                         currentComparator = this.stringComparator;
 123  
                     }
 124  0
                     else if (TypeUtils.isBooleanClass(propertyClass)) {
 125  0
                         currentComparator = this.booleanComparator;
 126  
                     }
 127  
                     else {
 128  0
                         currentComparator = this.genericComparator;
 129  
                     }
 130  
                 }
 131  0
                 catch (NullPointerException e) {
 132  0
                     throw new BeanComparisonException("unable to find property '" + o1.getClass().getName() + "." + currentProperty + "'", e);
 133  0
                 }
 134  
 
 135  
                 // compare the values
 136  0
                 Object value1 = PropertyUtils.getProperty(o1, currentProperty);
 137  0
                 Object value2 = PropertyUtils.getProperty(o2, currentProperty);
 138  0
                 if ( value1 == null ) value1 = "";
 139  0
                 if ( value2 == null ) value2 = "";
 140  0
                 compared = currentComparator.compare(value1, value2);
 141  0
             }
 142  
         }
 143  0
         catch (IllegalAccessException e) {
 144  0
             throw new BeanComparisonException("unable to compare property values", e);
 145  
         }
 146  0
         catch (NoSuchMethodException e) {
 147  0
             throw new BeanComparisonException("unable to compare property values", e);
 148  
         }
 149  0
         catch (InvocationTargetException e) {
 150  0
             throw new BeanComparisonException("unable to compare property values", e);
 151  0
         }
 152  
 
 153  0
         return compared;
 154  
     }
 155  
     
 156  
     public static class BeanComparisonException extends KualiException {
 157  
         private static final long serialVersionUID = 2622379680100640029L;
 158  
 
 159  
         /**
 160  
          * @param message
 161  
          * @param t
 162  
          */
 163  
         public BeanComparisonException(String message, Throwable t) {
 164  0
             super(message, t);
 165  0
         }
 166  
     }
 167  
 }