@Test public void testCompare_differentDates() throws ParseException { List propertiesD = Arrays.asList(new String[] { "d" }); DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT); BeanPropertyComparator comparator = new BeanPropertyComparator(propertiesD); D lesser = new D(dateFormat.parse("01/01/1990")); D greater = new D(dateFormat.parse("01/02/1990")); int result = comparator.compare(greater, lesser); Assert.assertEquals(1, result); result = comparator.compare(lesser, greater); Assert.assertEquals(-1, result); result = comparator.compare(lesser, lesser); Assert.assertEquals(0, result); result = comparator.compare(greater, greater); Assert.assertEquals(0, result); }
public int compare(Object o1, Object o2) { int compared = 0; try { for (Iterator i = propertyNames.iterator(); (compared == 0) && i.hasNext();) { String currentProperty = i.next().toString(); Comparator currentComparator = null; try { PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(o1, currentProperty); Class propertyClass = propertyDescriptor.getPropertyType(); if (propertyClass.equals(String.class)) { currentComparator = this.stringComparator; } else if (TypeUtils.isBooleanClass(propertyClass)) { currentComparator = this.booleanComparator; } else { currentComparator = this.genericComparator; } } catch (NullPointerException e) { throw new BeanComparisonException("unable to find property '" + o1.getClass().getName() + "." + currentProperty + "'", e); } Object value1 = PropertyUtils.getProperty(o1, currentProperty); Object value2 = PropertyUtils.getProperty(o2, currentProperty); if ( value1 == null && value2 == null) return 0; else if ( value1 == null) return -1; else if ( value2 == null ) return 1; compared = currentComparator.compare(value1, value2); } } catch (IllegalAccessException e) { throw new BeanComparisonException("unable to compare property values", e); } catch (NoSuchMethodException e) { throw new BeanComparisonException("unable to compare property values", e); } catch (InvocationTargetException e) { throw new BeanComparisonException("unable to compare property values", e); } return compared; }
BeanPropertyComparator implements Comparator, Serializable { public int compare(Object o1, Object o2) { int compared = 0; try { for (Iterator i = propertyNames.iterator(); (compared == 0) && i.hasNext();) { String currentProperty = i.next().toString(); Comparator currentComparator = null; try { PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(o1, currentProperty); Class propertyClass = propertyDescriptor.getPropertyType(); if (propertyClass.equals(String.class)) { currentComparator = this.stringComparator; } else if (TypeUtils.isBooleanClass(propertyClass)) { currentComparator = this.booleanComparator; } else { currentComparator = this.genericComparator; } } catch (NullPointerException e) { throw new BeanComparisonException("unable to find property '" + o1.getClass().getName() + "." + currentProperty + "'", e); } Object value1 = PropertyUtils.getProperty(o1, currentProperty); Object value2 = PropertyUtils.getProperty(o2, currentProperty); if ( value1 == null && value2 == null) return 0; else if ( value1 == null) return -1; else if ( value2 == null ) return 1; compared = currentComparator.compare(value1, value2); } } catch (IllegalAccessException e) { throw new BeanComparisonException("unable to compare property values", e); } catch (NoSuchMethodException e) { throw new BeanComparisonException("unable to compare property values", e); } catch (InvocationTargetException e) { throw new BeanComparisonException("unable to compare property values", e); } return compared; } }
BeanPropertyComparator implements Comparator, Serializable { public int compare(Object o1, Object o2) { int compared = 0; try { for (Iterator i = propertyNames.iterator(); (compared == 0) && i.hasNext();) { String currentProperty = i.next().toString(); Comparator currentComparator = null; try { PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(o1, currentProperty); Class propertyClass = propertyDescriptor.getPropertyType(); if (propertyClass.equals(String.class)) { currentComparator = this.stringComparator; } else if (TypeUtils.isBooleanClass(propertyClass)) { currentComparator = this.booleanComparator; } else { currentComparator = this.genericComparator; } } catch (NullPointerException e) { throw new BeanComparisonException("unable to find property '" + o1.getClass().getName() + "." + currentProperty + "'", e); } Object value1 = PropertyUtils.getProperty(o1, currentProperty); Object value2 = PropertyUtils.getProperty(o2, currentProperty); if ( value1 == null && value2 == null) return 0; else if ( value1 == null) return -1; else if ( value2 == null ) return 1; compared = currentComparator.compare(value1, value2); } } catch (IllegalAccessException e) { throw new BeanComparisonException("unable to compare property values", e); } catch (NoSuchMethodException e) { throw new BeanComparisonException("unable to compare property values", e); } catch (InvocationTargetException e) { throw new BeanComparisonException("unable to compare property values", e); } return compared; } BeanPropertyComparator(List propertyNames);  BeanPropertyComparator(List propertyNames, boolean ignoreCase); }
BeanPropertyComparator implements Comparator, Serializable { public int compare(Object o1, Object o2) { int compared = 0; try { for (Iterator i = propertyNames.iterator(); (compared == 0) && i.hasNext();) { String currentProperty = i.next().toString(); Comparator currentComparator = null; try { PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(o1, currentProperty); Class propertyClass = propertyDescriptor.getPropertyType(); if (propertyClass.equals(String.class)) { currentComparator = this.stringComparator; } else if (TypeUtils.isBooleanClass(propertyClass)) { currentComparator = this.booleanComparator; } else { currentComparator = this.genericComparator; } } catch (NullPointerException e) { throw new BeanComparisonException("unable to find property '" + o1.getClass().getName() + "." + currentProperty + "'", e); } Object value1 = PropertyUtils.getProperty(o1, currentProperty); Object value2 = PropertyUtils.getProperty(o2, currentProperty); if ( value1 == null && value2 == null) return 0; else if ( value1 == null) return -1; else if ( value2 == null ) return 1; compared = currentComparator.compare(value1, value2); } } catch (IllegalAccessException e) { throw new BeanComparisonException("unable to compare property values", e); } catch (NoSuchMethodException e) { throw new BeanComparisonException("unable to compare property values", e); } catch (InvocationTargetException e) { throw new BeanComparisonException("unable to compare property values", e); } return compared; } BeanPropertyComparator(List propertyNames);  BeanPropertyComparator(List propertyNames, boolean ignoreCase); int compare(Object o1, Object o2); }
BeanPropertyComparator implements Comparator, Serializable { public int compare(Object o1, Object o2) { int compared = 0; try { for (Iterator i = propertyNames.iterator(); (compared == 0) && i.hasNext();) { String currentProperty = i.next().toString(); Comparator currentComparator = null; try { PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(o1, currentProperty); Class propertyClass = propertyDescriptor.getPropertyType(); if (propertyClass.equals(String.class)) { currentComparator = this.stringComparator; } else if (TypeUtils.isBooleanClass(propertyClass)) { currentComparator = this.booleanComparator; } else { currentComparator = this.genericComparator; } } catch (NullPointerException e) { throw new BeanComparisonException("unable to find property '" + o1.getClass().getName() + "." + currentProperty + "'", e); } Object value1 = PropertyUtils.getProperty(o1, currentProperty); Object value2 = PropertyUtils.getProperty(o2, currentProperty); if ( value1 == null && value2 == null) return 0; else if ( value1 == null) return -1; else if ( value2 == null ) return 1; compared = currentComparator.compare(value1, value2); } } catch (IllegalAccessException e) { throw new BeanComparisonException("unable to compare property values", e); } catch (NoSuchMethodException e) { throw new BeanComparisonException("unable to compare property values", e); } catch (InvocationTargetException e) { throw new BeanComparisonException("unable to compare property values", e); } return compared; } BeanPropertyComparator(List propertyNames);  BeanPropertyComparator(List propertyNames, boolean ignoreCase); int compare(Object o1, Object o2);  }