blackboard.base
Class GenericFieldFilter

java.lang.Object
  |
  +--blackboard.base.ListFilter
        |
        +--blackboard.base.GenericFieldFilter

public class GenericFieldFilter
extends ListFilter

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 thier
     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: 5 $ $Date: 12/07/01 2:59p $

Nested Class Summary
static class GenericFieldFilter.Comparison
           
 
Field Summary
 
Fields inherited from class blackboard.base.ListFilter
NOOP
 
Constructor Summary
GenericFieldFilter(java.lang.String fieldAccessorMethod, java.lang.Class targetClass, java.lang.Object targetForComparison, GenericFieldFilter.Comparison comparison)
          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  boolean passesFilter(java.lang.Object listElement)
          Returns true if the given element passes this filter's tests for inclusion in the sublist.
 
Methods inherited from class blackboard.base.ListFilter
addFilter, passesFilters
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericFieldFilter

public GenericFieldFilter(java.lang.String fieldAccessorMethod,
                          java.lang.Class targetClass,
                          java.lang.Object targetForComparison,
                          GenericFieldFilter.Comparison comparison)
                   throws java.lang.IllegalArgumentException,
                          java.lang.SecurityException
Constructor.

Parameters:
targetClass - a Class for the object which contains the accessor method. This can be a base class of the objects in a list or an interface that they all implement.
targetForComparison - an Object
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.
Method Detail

passesFilter

protected boolean passesFilter(java.lang.Object listElement)
Returns true if the given element passes this filter's tests for inclusion in the sublist.

Specified by:
passesFilter in class ListFilter

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 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.

java.lang.IllegalArgumentException


Copyright © 2003 Blackboard, Inc. All Rights Reserved.