blackboard.base
Class GenericFieldComparator
java.lang.Object
blackboard.base.BaseComparator
blackboard.base.GenericFieldComparator
- All Implemented Interfaces:
- java.util.Comparator
public class GenericFieldComparator
- extends BaseComparator
- implements java.util.Comparator
Comparator implementation that allows objects in a list to be compared based
on the value of an attribute that they have in common. For example, a list
of announcements could be ordered by their creation dates, returned by
announcement.getCreationDate()
.
Note that this class is implemented using reflection.
Example Use:
The following code separates an announcement list into two sections. First
the permanent announcements, and then the non-permanent announcements.
Each of these sections is sorted in order of date created. Finally, those
announcements with the same isPermanent value and same created date are
sorted in alphabetical order by their title.
AnnouncementList list = loadAnnouncementList();
GenericFieldComparator comparator = new GenericFieldComparator( BaseComparator.DESCENDING, "getIsPermanent", Announcement.class );
comparator.appendSecondaryComparator( new GenericFieldComparator( BaseComparator.ASCENDING, "getCreatedDate", Announcement.class ) );
comparator.appendSecondaryComparator( new GenericFieldComparator( BaseComparator.ASCENDING, "getTitle", Announcement.class ) );
Collections.sort( list, comparator );
- Since:
- Bb 6.0
- Version:
- $Revision: #1 $ $Date: 2005/06/27 $
Constructor Summary |
GenericFieldComparator(boolean ascending,
java.lang.String fieldAccessorMethod,
java.lang.Class targetClass)
Constructor. |
GenericFieldComparator(java.lang.String fieldAccessorMethod,
java.lang.Class targetClass)
Constructor. |
Method Summary |
protected int |
compareFields(java.lang.Object field1,
java.lang.Object field2)
Compare method allowing explicit support for comparing objects that don't
implement Comparable. |
protected int |
doCompare(java.lang.Object o1,
java.lang.Object o2)
Implementation of BaseComparator.doCompare(). |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface java.util.Comparator |
compare, equals |
GenericFieldComparator
public GenericFieldComparator(java.lang.String fieldAccessorMethod,
java.lang.Class targetClass)
throws java.lang.IllegalArgumentException,
java.lang.SecurityException
- Constructor. Default to ASCENDING order.
- Parameters:
fieldAccessMethod
- the case-sensitive name of the accessor method
defined in the target class that will be used to get the attribute
used to compare objects.targetClass
- the class defining the requested accessor method. This
can be a base class of the objects in a list of an interface that
they all implement.
- Throws:
java.lang.IllegalArgumentException
- if the target class does not contain
a method called fieldAccessorMethod
.
java.lang.SecurityException
- if the caller does not have access to the
specified method in the specified class.
GenericFieldComparator
public GenericFieldComparator(boolean ascending,
java.lang.String fieldAccessorMethod,
java.lang.Class targetClass)
throws java.lang.IllegalArgumentException,
java.lang.SecurityException
- Constructor.
- Parameters:
ascending
- the ascending/descending mode for this comparator as defined
in BaseComparator
.fieldAccessMethod
- the case-sensitive name of the accessor method
defined in the target class that will be used to get the attribute
used to compare objects.targetClass
- the class defining the requested accessor method. This
can be a base class of the objects in a list of an interface that
they all implement.
- Throws:
java.lang.IllegalArgumentException
- if the target class does not contain a
method called fieldAccessorMethod
.
java.lang.SecurityException
- if the caller does not have access to the
specified method in the specified class.
doCompare
protected int doCompare(java.lang.Object o1,
java.lang.Object o2)
throws java.lang.ClassCastException,
java.lang.IllegalArgumentException
- Implementation of BaseComparator.doCompare().
- Specified by:
doCompare
in class BaseComparator
- Parameters:
o1
- First object to compareo2
- Second object to compare
- Returns:
- Comparison result, as defined by Comparable.compareTo()
- Throws:
java.lang.ClassCastException
- if the fields are not of the same type
java.lang.IllegalArgumentException
- if the objects' fields are not a supported type, do not
implement comparable, or the objects themselves do not match types
compareFields
protected int compareFields(java.lang.Object field1,
java.lang.Object field2)
throws java.lang.IllegalArgumentException
- Compare method allowing explicit support for comparing objects that don't
implement Comparable. Base implementation provides support for String,
Comparable, Boolean, Calendar. Note that blackboard.persist.Id and
blackboard.base.BbEnum already implement Comparable.
Subclasses can override this method to add support for additional non-
comparable types, but be sure to call super.compareFields() if the objects
are not of one of the target types being supported.
Note that ascending order for booleans is defined as false before true.
Calendars are compared by the Dates returned by their getTime() methods.
BbEnum elements are sorted by their external string representation in
alphabetical order.
- Parameters:
field1
- First field to comparefield2
- Second field to compare
- Returns:
- Comparison result, as defined by Comparable.compareTo()
- Throws:
java.lang.IllegalArgumentException
- if the fields is not a supported type, does not
implement comparable, or the fields do not match types
Copyright © 2003 Blackboard, Inc. All Rights Reserved.