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