/*
 * UniTime 3.1 (University Timetabling Application)
 * Copyright (C) 2008, UniTime LLC, and individual contributors
 * as indicated by the @authors tag.
 * 
 * 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.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.unitime.timetable.model.comparators;

import java.util.Comparator;

import org.unitime.timetable.model.CourseOffering;


/**
 * Compares course offerings based on Subject Area and Course Number
 * 
 * @author Heston Fernandes
 */
public class CourseOfferingComparator implements Comparator {
    
    /** Compare 2 offerings on Subject Area and Course Number - Default **/
    public static final short COMPARE_BY_SUBJ_CRS = 1;
    
    /** Compare 2 offerings on Controlling Course Flag **/
    public static final short COMPARE_BY_CTRL_CRS = 2;
    
    // Decides method to compare 
    private short compareBy;
    
    public CourseOfferingComparator () {
		super();
		this.compareBy = COMPARE_BY_SUBJ_CRS;
	}
    
    public CourseOfferingComparator (short compareBy) {
		super();
		this.compareBy = compareBy;
	}
    
    public int compare (Object o1, Object o2){
        // Check if objects are of class Instructional Offering
        if (! (o1 instanceof CourseOffering)){
            throw new ClassCastException("o1 Class must be of type CourseOffering");
        }
        if (! (o2 instanceof CourseOffering)){
            throw new ClassCastException("o2 Class must be of type CourseOffering");
        }
        
        CourseOffering co1 = (CourseOffering) o1;
        CourseOffering co2 = (CourseOffering) o2;

        // Same Course Offering 
        if (co1.getUniqueId().equals(co2.getUniqueId())){
            return 0;
        }
        
        // One of the offerings is a Controlling Course
        if(compareBy==COMPARE_BY_CTRL_CRS) {
            if(co1.isIsControl().booleanValue())
                return -1;
            if(co2.isIsControl().booleanValue())
                return 1;
        }
        
        // Compare by course name (also used if neither is controlling)
        if (co1.getSubjectAreaAbbv().equals(co2.getSubjectAreaAbbv())){
        	if (co1.getCourseNbr().equals(co2.getCourseNbr())){
        		return(co1.getTitle().compareTo(co2.getTitle()));
        	} else {
        		return(co1.getCourseNbr().compareTo(co2.getCourseNbr()));
        	}
        } else {
            return(co1.getSubjectAreaAbbv().compareTo(co2.getSubjectAreaAbbv()));
        }                  
    } 
}

@@ -2821,24 +2821,192 @@
 rseNbr()))%7B%0A
+        %09%09if (co1.getTitle() == null && co2.getTitle() == null)%7B%0A        %09%09%09return(0);%0A        %09%09%7D else if (co1.getTitle() == null)%7B%0A        %09%09%09return(-1);%0A        %09%09%7D%0A
         %09%09re

Update course offering compare to handle course offerings with null titles.