001    package net.sf.cpsolver.exam.reports;
002    
003    import java.util.Comparator;
004    
005    import net.sf.cpsolver.exam.model.Exam;
006    import net.sf.cpsolver.exam.model.ExamRoomPlacement;
007    
008    /**
009     * Compare two rooms by size. Either normal seating size or 
010     * alternative sating size is used, based on the given exam (see
011     * {@link Exam#hasAltSeating()}. 
012     * <br><br>
013     * 
014     * @version
015     * ExamTT 1.1 (Examination Timetabling)<br>
016     * Copyright (C) 2008 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 ExamRoomComparator implements Comparator {
035        private boolean iAsc;
036        private Exam iExam;
037        
038        /**
039         * Constructor
040         * @param exam exam for which rooms are to be compared
041         */
042        public ExamRoomComparator(Exam exam, boolean asc) {
043            iExam = exam;
044            iAsc = asc;
045        }
046        
047        /**
048         * Compare two rooms based on their normal/alternative seating size
049         */
050        public int compare(Object o1, Object o2) {
051            ExamRoomPlacement r1 = (ExamRoomPlacement)o1;
052            ExamRoomPlacement r2 = (ExamRoomPlacement)o2;
053            int cmp = (iAsc?1:-1)*Double.compare(r1.getSize(iExam.hasAltSeating()),r2.getSize(iExam.hasAltSeating()));
054            if (cmp!=0) return cmp;
055            return r1.getRoom().compareTo(r2.getRoom());
056        }
057    }