All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.directwebremoting.datasync.DefaultComparatorFactory Maven / Gradle / Ivy

package org.directwebremoting.datasync;

import java.util.Comparator;

/**
 * A ComparatorFactory that assumes that
 * @author Joe Walker [joe at getahead dot ltd dot uk]
 */
public class DefaultComparatorFactory implements ComparatorFactory
{
    /* (non-Javadoc)
     * @see org.directwebremoting.datasync.ComparatorFactory#getComparator(java.lang.String, boolean)
     */
    public Comparator getComparator(final String property, final boolean ascending)
    {
        final AttributeValueExtractor extractor = new PojoAttributeValueExtractor();

        return new Comparator()
        {
            /* (non-Javadoc)
             * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
             */
            @SuppressWarnings("unchecked")
            public int compare(T o1, T o2)
            {
                Object value1 = extractor.getValue(o1, property);
                Object value2 = extractor.getValue(o2, property);

                if (value1 == null)
                {
                    if (value2 == null)
                    {
                        return 0;
                    }
                    else
                    {
                        return -1;
                    }
                }
                else
                {
                    if (value2 == null)
                    {
                        return 1;
                    }
                }

                Comparable comp1 = (Comparable) value1;
                Comparable comp2 = (Comparable) value2;

                if (!ascending)
                {
                    return comp2.compareTo(comp1);
                }
                else
                {
                    return comp1.compareTo(comp2);
                }
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy