001    package net.sf.cpsolver.studentsct.heuristics.studentord;
002    
003    import java.util.Collections;
004    import java.util.Comparator;
005    import java.util.Vector;
006    
007    import net.sf.cpsolver.ifs.util.DataProperties;
008    import net.sf.cpsolver.studentsct.model.AcademicAreaCode;
009    import net.sf.cpsolver.studentsct.model.Student;
010    
011    /** 
012     * Return the given set of students ordered by their majors 
013     * 
014     * @version
015     * StudentSct 1.1 (Student Sectioning)<br>
016     * Copyright (C) 2007 Tomáš Müller<br>
017     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
018     * Lazenska 391, 76314 Zlin, Czech Republic<br>
019     * <br>
020     * This library is free software; you can redistribute it and/or
021     * modify it under the terms of the GNU Lesser General Public
022     * License as published by the Free Software Foundation; either
023     * version 2.1 of the License, or (at your option) any later version.
024     * <br><br>
025     * This library is distributed in the hope that it will be useful,
026     * but WITHOUT ANY WARRANTY; without even the implied warranty of
027     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
028     * Lesser General Public License for more details.
029     * <br><br>
030     * You should have received a copy of the GNU Lesser General Public
031     * License along with this library; if not, write to the Free Software
032     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
033    */
034    public class StudentMajorOrder implements StudentOrder, Comparator {
035        private boolean iReverse = false;
036    
037        public StudentMajorOrder(DataProperties config) {
038            iReverse = config.getPropertyBoolean("StudentMajorOrder.Reverse", iReverse);
039        }
040    
041        /** Order the given list of students */
042        public Vector order(Vector students) {
043            Vector ret = new Vector(students);
044            Collections.sort(ret, this);
045            return ret;
046        }
047        
048        public int compare(Object o1, Object o2) {
049            Student s1 = (Student)o1;
050            Student s2 = (Student)o2;
051            int cmp = compareMajors(s1.getMajors(), s2.getMajors());
052            if (cmp!=0) return (iReverse?-1:1)*cmp;
053            return (iReverse?-1:1)*Double.compare(s1.getId(), s2.getId());
054        }
055        
056        public int compareMajors(Vector m1, Vector m2) {
057            if (m1.isEmpty()) {
058                return m2.isEmpty()?0:-1; 
059            } else if (m2.isEmpty()) return 1;
060            return compareMajors((AcademicAreaCode)m1.firstElement(), (AcademicAreaCode)m2.firstElement());
061        }
062        
063        public int compareMajors(AcademicAreaCode m1, AcademicAreaCode m2) {
064            int cmp = (m1.getArea()==null?"":m1.getArea()).compareTo(m2.getArea()==null?"":m2.getArea());
065            if (cmp!=0) return cmp;
066            return (m1.getCode()==null?"":m1.getCode()).compareTo(m2.getCode()==null?"":m2.getCode());
067        }
068    }