package net.toodarkpark.util.comparison; import java.util.*; import org.apache.struts.util.*; // PropertyUtils // sick of multiple comparators when you just want to compare key values against one another? // good, coz i was too. public class PropertyComparator implements Comparator { protected String sourceKey; protected String destKey; public PropertyComparator() { super(); this.sourceKey = null; this.destKey = null; } // use the same key for the source and destination objects public PropertyComparator( String aKey ) { this( aKey, aKey ); } // use a different ( possibly null/no key ) for the source and destination objects public PropertyComparator( String aSourceKey, String aDestKey ) { this(); this.sourceKey = aSourceKey; this.destKey = aDestKey; } private Object objectForProperty( Object obj, String property ) { Object value = null; try { value = PropertyUtils.getProperty( obj, property ); } catch( Exception e ) { } return value; } public int compare( Object source, Object dest ) { Object sourceValue = null; Object destValue = null; if( sourceKey != null ) { sourceValue = this.objectForProperty( source, sourceKey ); } else { sourceValue = source; } if( destKey != null ) { destValue = this.objectForProperty( dest, destKey ); } else { destValue = dest; } return evaluateComparison( sourceValue, destValue ); } public boolean equals( Object source, Object dest ) { return( this.compare( source, dest ) == 0 ); } // from ComparisonQualifier private int evaluateComparison( Object compare, Object value ) throws ClassCastException { if( compare == null && value == null ) { // do two nulls equal one another? im saying yes... but ill probably regret it. return 0; } else if( compare == null ) { // the getter on 'comparison' returned null, so this isnt a match. //return -1; throw new NullPointerException( "PropertyComparator requires a non-null comparison" ); } else if( value == null ) { System.out.println( "compare == " + compare + " | value == null!" ); throw new NullPointerException( "PropertyComparator requires a non-null value comparison." ); // eek! //return 1; } else if( compare instanceof Comparable && value instanceof Comparable ) { return( ((Comparable)compare).compareTo( value ) ); } else if( compare instanceof Calendar && value instanceof Calendar ) { Calendar compareCal = (Calendar)compare; Calendar valueCal = (Calendar)value; if( compareCal.after( valueCal ) == true ) { return -1; } else if( compareCal.equals( valueCal ) == true ) { return 0; } else { return 1; } } else { // try to find a class called [.getClass()]Comparator, eg // GregorianCalendarComparator, and use that. // if no class can be found, continue up the class chain ( superclass -> superclass ) // until one can be found OR we reach java.lang.Object. /* Class compareClass = compare.getClass(); String comparatorName = compareClass + "Comparator"; Comparator compareObj; try { compareObj = (Comparator)Class.forName( comparatorName ).newInstance(); return( compareObj.compare( compare, value ) ); } catch( ClassNotFoundException cnfe ) { throw new ClassCastException( "PropertyComparator requires that both objects implement the Comparable interface or have a Comparator object named the same as the class name." ); } */ throw new ClassCastException( "PropertyComparator requires that both objects implement the Comparable interface." ); } } }