    001package net.sf.cpsolver.studentsct.heuristics.studentord;
    002
    003import java.util.ArrayList;
    004import java.util.Collections;
    005import java.util.Comparator;
    006import java.util.List;
    007
    008import net.sf.cpsolver.ifs.util.DataProperties;
    009import net.sf.cpsolver.studentsct.model.AcademicAreaCode;
    010import net.sf.cpsolver.studentsct.model.Student;
    011
    012/**
    013 * Return the given set of students ordered by their majors
    014 *
    015 * @version StudentSct 1.2 (Student Sectioning)<br>
    016 *          Copyright (C) 2007 - 2010 Tomáš Müller<br>
    017 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    018 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    019 * <br>
    020 *          This library is free software; you can redistribute it and/or modify
    021 *          it under the terms of the GNU Lesser General Public License as
    022 *          published by the Free Software Foundation; either version 3 of the
    023 *          License, or (at your option) any later version. <br>
    024 * <br>
    025 *          This library is distributed in the hope that it will be useful, but
    026 *          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. <br>
    029 * <br>
    030 *          You should have received a copy of the GNU Lesser General Public
    031 *          License along with this library; if not see
    032 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    033 */
    034public class StudentMajorOrder implements StudentOrder, Comparator<Student> {
    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    @Override
    043    public List<Student> order(List<Student> students) {
    044        List<Student> ret = new ArrayList<Student>(students);
    045        Collections.sort(ret, this);
    046        return ret;
    047    }
    048
    049    @Override
    050    public int compare(Student s1, Student s2) {
    051        int cmp = compareMajors(s1.getMajors(), s2.getMajors());
    052        if (cmp != 0)
    053            return (iReverse ? -1 : 1) * cmp;
    054        return (iReverse ? -1 : 1) * Double.compare(s1.getId(), s2.getId());
    055    }
    056
    057    public int compareMajors(List<AcademicAreaCode> m1, List<AcademicAreaCode> m2) {
    058        if (m1.isEmpty()) {
    059            return m2.isEmpty() ? 0 : -1;
    060        } else if (m2.isEmpty())
    061            return 1;
    062        return compareMajors(m1.get(0), m2.get(0));
    063    }
    064
    065    public int compareMajors(AcademicAreaCode m1, AcademicAreaCode m2) {
    066        int cmp = (m1.getArea() == null ? "" : m1.getArea()).compareTo(m2.getArea() == null ? "" : m2.getArea());
    067        if (cmp != 0)
    068            return cmp;
    069        return (m1.getCode() == null ? "" : m1.getCode()).compareTo(m2.getCode() == null ? "" : m2.getCode());
    070    }
    071}