Coverage Report - org.kuali.rice.kns.util.BeanPropertyComparator
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanPropertyComparator
0%
0/43
0%
0/18
6.25
BeanPropertyComparator$1
0%
0/9
0%
0/4
6.25
 
 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.kns.exception.BeanComparisonException;
 29  
 
 30  
 /**
 31  
  * This class compares the two beans using multiple property names.
 32  
  * 
 33  
  * 
 34  
  */
 35  
 public class BeanPropertyComparator implements Comparator, Serializable {
 36  
     private static final long serialVersionUID = -2675700473766186018L;
 37  
     boolean ignoreCase;
 38  
     private List propertyNames;
 39  
     private Comparator stringComparator;
 40  
     private Comparator booleanComparator;
 41  
     private Comparator genericComparator;
 42  
 
 43  
     /**
 44  
      * Constructs a PropertyComparator for comparing beans using the properties named in the given List; if the List is null, the
 45  
      * beans will be compared directly (by Properties will be compared in the order in which they are listed. Case will be ignored
 46  
      * in String comparisons.
 47  
      * 
 48  
      * @param propertyNames List of property names (as Strings) used to compare beans
 49  
      */
 50  
     public BeanPropertyComparator(List propertyNames) {
 51  0
         this(propertyNames, true);
 52  0
     }
 53  
 
 54  
     /**
 55  
      * Constructs a PropertyComparator for comparing beans using the properties named in the given List. Properties will be compared
 56  
      * in the order in which they are listed. Case will be ignored if ignoreCase is true.
 57  
      * 
 58  
      * @param propertyNames List of property names (as Strings) used to compare beans
 59  
      * @param ignoreCase if true, case will be ignored during String comparisons
 60  
      */
 61  0
     public BeanPropertyComparator(List propertyNames, boolean ignoreCase) {
 62  0
         if (propertyNames == null) {
 63  0
             throw new IllegalArgumentException("invalid (null) propertyNames list");
 64  
         }
 65  0
         if (propertyNames.size() == 0) {
 66  0
             throw new IllegalArgumentException("invalid (empty) propertyNames list");
 67  
         }
 68  0
         this.propertyNames = Collections.unmodifiableList(propertyNames);
 69  0
         this.ignoreCase = ignoreCase;
 70  
 
 71  0
         if (ignoreCase) {
 72  0
             this.stringComparator = String.CASE_INSENSITIVE_ORDER;
 73  
         }
 74  
         else {
 75  0
             this.stringComparator = ComparableComparator.getInstance();
 76  
         }
 77  0
         this.booleanComparator = new Comparator() {
 78  
             public int compare(Object o1, Object o2) {
 79  0
                 int compared = 0;
 80  
 
 81  0
                 Boolean b1 = (Boolean) o1;
 82  0
                 Boolean b2 = (Boolean) o2;
 83  
 
 84  0
                 if (!b1.equals(b2)) {
 85  0
                     if (b1.equals(Boolean.FALSE)) {
 86  0
                         compared = -1;
 87  
                     }
 88  
                     else {
 89  0
                         compared = 1;
 90  
                     }
 91  
                 }
 92  
 
 93  0
                 return compared;
 94  
             }
 95  
 
 96  
         };
 97  0
         this.genericComparator = ComparableComparator.getInstance();
 98  0
     }
 99  
 
 100  
 
 101  
     /**
 102  
      * Compare two JavaBeans by the properties given to the constructor. If no propertues
 103  
      * 
 104  
      * @param o1 Object The first bean to get data from to compare against
 105  
      * @param o2 Object The second bean to get data from to compare
 106  
      * @return int negative or positive based on order
 107  
      */
 108  
     public int compare(Object o1, Object o2) {
 109  0
         int compared = 0;
 110  
 
 111  
         try {
 112  0
             for (Iterator i = propertyNames.iterator(); (compared == 0) && i.hasNext();) {
 113  0
                 String currentProperty = i.next().toString();
 114  
 
 115  
                 // choose appropriate comparator
 116  0
                 Comparator currentComparator = null;
 117  
                 try {
 118  0
                     PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(o1, currentProperty);
 119  0
                     Class propertyClass = propertyDescriptor.getPropertyType();
 120  0
                     if (propertyClass.equals(String.class)) {
 121  0
                         currentComparator = this.stringComparator;
 122  
                     }
 123  0
                     else if (TypeUtils.isBooleanClass(propertyClass)) {
 124  0
                         currentComparator = this.booleanComparator;
 125  
                     }
 126  
                     else {
 127  0
                         currentComparator = this.genericComparator;
 128  
                     }
 129  
                 }
 130  0
                 catch (NullPointerException e) {
 131  0
                     throw new BeanComparisonException("unable to find property '" + o1.getClass().getName() + "." + currentProperty + "'", e);
 132  0
                 }
 133  
 
 134  
                 // compare the values
 135  0
                 Object value1 = PropertyUtils.getProperty(o1, currentProperty);
 136  0
                 Object value2 = PropertyUtils.getProperty(o2, currentProperty);
 137  0
                 if ( value1 == null ) value1 = "";
 138  0
                 if ( value2 == null ) value2 = "";
 139  0
                 compared = currentComparator.compare(value1, value2);
 140  0
             }
 141  
         }
 142  0
         catch (IllegalAccessException e) {
 143  0
             throw new BeanComparisonException("unable to compare property values", e);
 144  
         }
 145  0
         catch (NoSuchMethodException e) {
 146  0
             throw new BeanComparisonException("unable to compare property values", e);
 147  
         }
 148  0
         catch (InvocationTargetException e) {
 149  0
             throw new BeanComparisonException("unable to compare property values", e);
 150  0
         }
 151  
 
 152  0
         return compared;
 153  
     }
 154  
 }