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