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

net.sf.jett.util.GroupOrderByComparator Maven / Gradle / Ivy

Go to download

JETT is a Java API that reads an Excel spreadsheet as a template, takes your data, and creates a new Excel spreadsheet that contains your data, formatted as in the template. It works with .xls and .xlsx template spreadsheets.

The newest version!
package net.sf.jett.util;

import java.util.Comparator;
import java.util.List;

import net.sf.jett.exception.ParseException;
import net.sf.jett.model.Group;

/**
 * A GroupOrderByComparator is an OrderByComparator
 * that operates on groups and understands that some of the "order by"
 * properties are group properties (these must all be before the other "order
 * by" properties that aren't group properties).
 *
 * @author Randy Gettman
 * @since 0.3.0
 */
public class GroupOrderByComparator implements Comparator
{
    private OrderByComparator myOrderByComparator;
    private List myGroupByProperties;

    /**
     * Constructs a GroupOrderByComparator that operates on the
     * given List of "order by" expressions, and assumes that the
     * Groups are grouped by the given List of "group
     * by" expressions.
     * @param orderByComparator An OrderByComparator.
     * @param groupByProperties A List of "group by" properties.
     * @throws ParseException If there is a problem parsing the expressions, or
     *    if any "group by" properties are preceded in the "order by" properties
     *    by other properties that are not "group by" properties.
     */
    public GroupOrderByComparator(OrderByComparator orderByComparator, List groupByProperties)
    {
        myOrderByComparator = orderByComparator;
        myGroupByProperties = groupByProperties;
        ensureOrderByGroupByLegal();
    }

    /**
     * Ensures that no "order by" properties are before any "group by"
     * properties in the expressions list.
     * @throws ParseException If any "group by" properties are preceded in the
     *    "order by" properties by other properties that are not "group by"
     *    properties.
     */
    private void ensureOrderByGroupByLegal()
    {
        boolean foundOrderByNotInGroupBy = false;
        boolean orderByInGroupBy;
        List orderByProperties = myOrderByComparator.getProperties();
        for (String orderBy : orderByProperties)
        {
            orderByInGroupBy = myGroupByProperties.contains(orderBy);
            if (foundOrderByNotInGroupBy)
            {
                if (!orderByInGroupBy)
                    throw new ParseException("The \"order by\" property \"" + orderBy +
                            "\" is in the \"group by\" properties (" + myGroupByProperties.toString() +
                            "), but it must not follow other \"order by\" properties (" +
                            orderByProperties.toString() + ") that aren't \"group by\" properties.");
            }
            else
            {
                // Set flag indicating that all further "order by" properties must
                // NOT be a "group by" property.
                if (!orderByInGroupBy)
                    foundOrderByNotInGroupBy = true;
            }
        }
    }

    /**
     * 

Compares the given Groups to determine order. Fulfills * the Comparator contract by returning a negative integer, 0, * or a positive integer if o1 is less than, equal to, or * greater than o2.

*

This compare method compares the group's representative objects.

* * @param g1 The left-hand-side Group to compare. * @param g2 The right-hand-side Group to compare. * @return A negative integer, 0, or a positive integer if g1 * is less than, equal to, or greater than g2. * @throws UnsupportedOperationException If any property specified in the * constructor doesn't correspond to a no-argument "get<Property>" * getter method in T, or if the property's type is not * Comparable. */ @Override public int compare(T g1, T g2) { return myOrderByComparator.compare(g1.getObj(), g2.getObj()); } /** * Returns the List of "group by" properties. * @return The List of "group by" properties. */ public List getGroupByProperties() { return myGroupByProperties; } }