Update of /cvsroot/displaytag/table-ben/src/com/tablelib/core/util
In directory sc8-pr-cvs1:/tmp/cvs-serv11620

Added Files:
        BeanSorter.java 
Log Message:


--- NEW FILE: BeanSorter.java ---
/**
 * $Id: BeanSorter.java,v 1.1 2003/07/18 13:23:49 javabencom Exp $
 *
 * Status: Under Development
 *
 * Todo
 *   - implementation
 *   - documentation (javadoc, examples, etc...)
 *   - junit test cases
 **/

package com.tablelib.core.util;

import org.apache.commons.beanutils.PropertyUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.Comparator;

import com.tablelib.tags.Decorator;

/**
 * This utility class is used to sort the list that the table is viewing
 * based on arbitrary properties of objects contained in that list.  The
 * only assumption made is that the object returned by the property is
 * either a native type, or implements the Comparable interface.
 *
 * If the property does not implement Comparable, then this little sorter
 * object will just assume that all objects are the same, and quietly do
 * nothing (so you should check for Comparable objecttypes elsewhere as
 * this object won't complain).
 **/

public class BeanSorter implements Comparator {
    private String property;
    private Decorator dec;


    /**
     * BeanSorter is a decorator of sorts, you need to initialize it with
     * the property name of the object that is to be sorted (getXXX method
     * name).  This property should return a Comparable object.
     */

    public static Comparator asComparator(String property, Decorator dec) {
        BeanSorter ret = new BeanSorter();
        ret.property = property;
        ret.dec = dec;
        return ret;
    }


    /**
     * Compares two objects by first fetching a property from each object
     * and then comparing that value.  If there are any errors produced while
     * trying to compare these objects then a RunTimeException will be
     * thrown as any error found here will most likely be a programming
     * error that needs to be quickly addressed (like trying to compare
     * objects that are not comparable, or trying to read a property from a
     * bean that is invalid, etc...)
     *
     * @throws java.lang.RuntimeException if there are any problems making a comparison
     *    of the object properties.
     */

    public int compare(Object o1, Object o2) throws RuntimeException {
        if (property == null) {
            throw new NullPointerException("Null property provided which " +
                    "prevents BeanSorter sort");
        }

        try {
            Object p1 = PropertyUtils.getProperty(o1, property);
            Object p2 = PropertyUtils.getProperty(o2, property);

            if (p1 instanceof Comparable && p2 instanceof Comparable) {
                Comparable c1 = (Comparable) p1;
                Comparable c2 = (Comparable) p2;
                return c1.compareTo(c2);
            } else {
                throw new RuntimeException("Object returned by property \"" +
                        property + "\" is not a Comparable object");
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException("IllegalAccessException thrown while " +
                    "trying to fetch property \"" + property + "\" during sort");
        } catch (InvocationTargetException e) {
            throw new RuntimeException("InvocationTargetException thrown while " +
                    "trying to fetch property \"" + property + "\" during sort");
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("NoSuchMethodException thrown while " +
                    "trying to fetch property \"" + property + "\" during sort");
        }
    }


    /**
     * Is this Comparator the same as another one...
     */

    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (obj instanceof BeanSorter == false) return false;
        if (this.property == null) return false;
        return this.property.equals(((BeanSorter) obj).property);

    }
}




-------------------------------------------------------
This SF.net email is sponsored by: VM Ware
With VMware you can run multiple operating systems on a single machine.
WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the
same time. Free trial click here: http://www.vmware.com/wl/offer/345/0
_______________________________________________
displaytag-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/displaytag-devel

Reply via email to