001    package net.sf.cpsolver.exam.model;
002    
003    /**
004     * Representation of a room placement of an exam. It contains a room {@link ExamRoom} and a penalty
005     * associated with a placement of an exam into the given room.  
006     * <br><br>
007     * 
008     * @version
009     * ExamTT 1.1 (Examination Timetabling)<br>
010     * Copyright (C) 2008 Tomáš Müller<br>
011     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
012     * Lazenska 391, 76314 Zlin, Czech Republic<br>
013     * <br>
014     * This library is free software; you can redistribute it and/or
015     * modify it under the terms of the GNU Lesser General Public
016     * License as published by the Free Software Foundation; either
017     * version 2.1 of the License, or (at your option) any later version.
018     * <br><br>
019     * This library is distributed in the hope that it will be useful,
020     * but WITHOUT ANY WARRANTY; without even the implied warranty of
021     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
022     * Lesser General Public License for more details.
023     * <br><br>
024     * You should have received a copy of the GNU Lesser General Public
025     * License along with this library; if not, write to the Free Software
026     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
027     */
028    public class ExamRoomPlacement implements Comparable {
029        private ExamRoom iRoom;
030        private int iPenalty = 0;
031        private int iMaxPenalty = 100;
032    
033        /**
034         * Constructor 
035         * @param room examination room
036         */
037        public ExamRoomPlacement(ExamRoom room) {
038            iRoom = room;
039        }
040        
041        /**
042         * Constructor 
043         * @param room examination room
044         * @param penalty penalty for using this room
045         */
046        public ExamRoomPlacement(ExamRoom room, int penalty) {
047            this(room);
048            iPenalty = penalty;
049        }
050    
051        /**
052         * Constructor 
053         * @param room examination room
054         * @param penalty penalty for using this room
055         * @param maxPenalty maximal penalty imposed of {@link ExamRoom#getPenalty(ExamPeriod)}, i.e., a placement with greater penalty is not allowed to be made
056         */
057        public ExamRoomPlacement(ExamRoom room, int penalty, int maxPenalty) {
058            this(room, penalty);
059            iMaxPenalty = maxPenalty;
060        }
061        
062        /** Examination room */
063        public ExamRoom getRoom() {
064            return iRoom;
065        }
066        
067        /** Examination room id */
068        public long getId() {
069            return getRoom().getId();
070        }
071        
072        /** Examination room name */
073        public String getName() {
074            return getRoom().getName();
075        }
076        
077        /** Examination room availability */
078        public boolean isAvailable(ExamPeriod period) {
079            return iRoom.isAvailable(period) && iRoom.getPenalty(period)<=iMaxPenalty;
080        }
081        
082        /** Penalty for assignment of an exam into this room {@link Exam#getRoomPlacements()} */
083        public int getPenalty() {
084            return iPenalty;
085        }
086        
087        /** Maximal penalty imposed of {@link ExamRoom#getPenalty(ExamPeriod)}, i.e., a placement with greater penalty is not allowed to be made */
088        public int getMaxPenalty() {
089            return iMaxPenalty;
090        }
091    
092        /** Penalty for assignment of an exam into this room {@link Exam#getRoomPlacements()} */
093        public void setPenalty(int penalty) { iPenalty = penalty; }
094        
095        /** Maximal penalty imposed of {@link ExamRoom#getPenalty(ExamPeriod)}, i.e., a placement with greater penalty is not allowed to be made */
096        public void setMaxPenalty(int maxPenalty) { iMaxPenalty = maxPenalty; }
097    
098        /** Penalty for assignment of an exam into this room {@link Exam#getRoomPlacements()} and the given examination period
099         * @return {@link ExamRoomPlacement#getPenalty()} + {@link ExamRoom#getPenalty(ExamPeriod)}
100         */
101        public int getPenalty(ExamPeriod period) {
102            return (iPenalty!=0?iPenalty:iRoom.getPenalty(period));
103        }
104    
105        /**
106         * Room size 
107         * @param altSeating examination seeting (pass {@link Exam#hasAltSeating()})
108         * @return room size or room alternative size, based on given seating
109         */
110        public int getSize(boolean altSeating) {
111            return (altSeating?getRoom().getAltSize():getRoom().getSize());
112        }
113        
114        /**
115         * Room distance
116         * @return appropriate {@link ExamRoom#getDistance(ExamRoom)}
117         */
118        public int getDistance(ExamRoomPlacement other) {
119            return getRoom().getDistance(other.getRoom());
120        }
121        
122        /**
123         * Hash code
124         */
125        public int hashCode() {
126            return getRoom().hashCode();
127        }
128        
129        public String toString() {
130            return getRoom().toString()+(getPenalty()==0?"":"/"+getPenalty());
131        }
132        
133        /** Compare two room placements for equality */
134        public boolean equals(Object o) {
135            if (o==null) return false;
136            if (o instanceof ExamRoomPlacement) {
137                return getRoom().equals(((ExamRoomPlacement)o).getRoom());
138            } else if (o instanceof ExamRoom) {
139                return getRoom().equals(o);
140            }
141            return false;
142        }
143        
144        /** Compare two room placements */
145        public int compareTo(Object o) {
146            if (o==null || !(o instanceof ExamRoomPlacement)) return -1;
147            return getRoom().compareTo(((ExamRoomPlacement)o).getRoom());
148        }
149    }