no.abmu.common.jasperreports.SortingFieldSchemaComparator.java Source code

Java tutorial

Introduction

Here is the source code for no.abmu.common.jasperreports.SortingFieldSchemaComparator.java

Source

/*$Id: SortingFieldSchemaComparator.java 9604 2008-04-17 22:45:13Z jens $*/
/*
 ****************************************************************************
 *                                                                          *
 *                   (c) Copyright 2007 ABM-utvikling                       *
 *                                                                          *
 * This program is free software; you can redistribute it and/or modify it  *
 * under the terms of the GNU General Public License as published by the    *
 * Free Software Foundation; either version 2 of the License, or (at your   *
 * option) any later version.                                               *
 *                                                                          *
 * This program is distributed in the hope that it will be useful, but      *
 * WITHOUT ANY WARRANTY; without even the implied warranty of               *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
 * Public License for more details. http://www.gnu.org/licenses/gpl.html    *
 *                                                                          *
 ****************************************************************************
 */

package no.abmu.common.jasperreports;

import java.util.Comparator;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * SortingFieldSchemaComparator.
 * 
 * @author Jens Vindvad, Jens.Vindvad@abm-utvikling.no
 * @author $Author: jens $
 * @version $Rev: 9604 $
 * $Date: 2008-04-18 00:45:13 +0200 (Fri, 18 Apr 2008) $
 * copyright ABM-Utvikling
 * @since 2007-05-02
 */
public class SortingFieldSchemaComparator implements Comparator<Map<String, Object>> {

    private static final Log logger = (Log) LogFactory.getLog(SortingFieldSchemaComparator.class);

    private String sortingKey;

    public SortingFieldSchemaComparator() {
        this.sortingKey = "sortingField";
    }

    public SortingFieldSchemaComparator(String sortingKey) {
        this.sortingKey = sortingKey;
    }

    public int compare(Map<String, Object> o1, Map<String, Object> o2) {

        Map<String, Object> report1 = (Map<String, Object>) o1;
        Map<String, Object> report2 = (Map<String, Object>) o2;

        String sortingField1 = (String) report1.get(sortingKey);
        String sortingField2 = (String) report2.get(sortingKey);

        if (sortingField1 == null && sortingField2 == null) {
            logger.warn("sortingField1 == null and sortingField2 == null");
            return 0;
        }

        if (sortingField1 == null) {
            logger.warn("sortingField1 == null");
            return -1;
        }

        if (sortingField2 == null) {
            logger.warn("sortingField2 == null");
            return 1;
        }

        return sortingField1.compareTo(sortingField2);

    }

}