    001package org.cpsolver.exam.reports;
    002
    003import java.util.Comparator;
    004
    005import org.cpsolver.exam.model.Exam;
    006import org.cpsolver.exam.model.ExamRoomPlacement;
    007
    008
    009/**
    010 * Compare two rooms by size. Either normal seating size or alternative sating
    011 * size is used, based on the given exam (see {@link Exam#hasAltSeating()}. <br>
    012 * <br>
    013 *
    014 * @version ExamTT 1.3 (Examination Timetabling)<br>
    015 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
    016 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    017 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    018 * <br>
    019 *          This library is free software; you can redistribute it and/or modify
    020 *          it under the terms of the GNU Lesser General Public License as
    021 *          published by the Free Software Foundation; either version 3 of the
    022 *          License, or (at your option) any later version. <br>
    023 * <br>
    024 *          This library is distributed in the hope that it will be useful, but
    025 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    026 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    027 *          Lesser General Public License for more details. <br>
    028 * <br>
    029 *          You should have received a copy of the GNU Lesser General Public
    030 *          License along with this library; if not see
    031 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    032 */
    033public class ExamRoomComparator implements Comparator<ExamRoomPlacement> {
    034    private boolean iAsc;
    035    private Exam iExam;
    036
    037    /**
    038     * Constructor
    039     *
    040     * @param exam
    041     *            exam for which rooms are to be compared
    042     * @param asc room order
    043     */
    044    public ExamRoomComparator(Exam exam, boolean asc) {
    045        iExam = exam;
    046        iAsc = asc;
    047    }
    048
    049    /**
    050     * Compare two rooms based on their normal/alternative seating size
    051     */
    052    @Override
    053    public int compare(ExamRoomPlacement r1, ExamRoomPlacement r2) {
    054        int cmp = (iAsc ? 1 : -1)
    055                * Double.compare(r1.getSize(iExam.hasAltSeating()), r2.getSize(iExam.hasAltSeating()));
    056        if (cmp != 0)
    057            return cmp;
    058        return r1.getRoom().compareTo(r2.getRoom());
    059    }
    060}