001    package net.sf.cpsolver.exam.model;
002    
003    /**
004     * Representation of a room placement of an exam. It contains a room
005     * {@link ExamRoom} and a penalty associated with a placement of an exam into
006     * the given room. <br>
007     * <br>
008     * 
009     * @version ExamTT 1.2 (Examination Timetabling)<br>
010     *          Copyright (C) 2008 - 2010 Tomas Muller<br>
011     *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
012     *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
013     * <br>
014     *          This library is free software; you can redistribute it and/or modify
015     *          it under the terms of the GNU Lesser General Public License as
016     *          published by the Free Software Foundation; either version 3 of the
017     *          License, or (at your option) any later version. <br>
018     * <br>
019     *          This library is distributed in the hope that it will be useful, but
020     *          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. <br>
023     * <br>
024     *          You should have received a copy of the GNU Lesser General Public
025     *          License along with this library; if not see
026     *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
027     */
028    public class ExamRoomPlacement implements Comparable<ExamRoomPlacement> {
029        private ExamRoom iRoom;
030        private int iPenalty = 0;
031        private int iMaxPenalty = 100;
032    
033        /**
034         * Constructor
035         * 
036         * @param room
037         *            examination room
038         */
039        public ExamRoomPlacement(ExamRoom room) {
040            iRoom = room;
041        }
042    
043        /**
044         * Constructor
045         * 
046         * @param room
047         *            examination room
048         * @param penalty
049         *            penalty for using this room
050         */
051        public ExamRoomPlacement(ExamRoom room, int penalty) {
052            this(room);
053            iPenalty = penalty;
054        }
055    
056        /**
057         * Constructor
058         * 
059         * @param room
060         *            examination room
061         * @param penalty
062         *            penalty for using this room
063         * @param maxPenalty
064         *            maximal penalty imposed of
065         *            {@link ExamRoom#getPenalty(ExamPeriod)}, i.e., a placement
066         *            with greater penalty is not allowed to be made
067         */
068        public ExamRoomPlacement(ExamRoom room, int penalty, int maxPenalty) {
069            this(room, penalty);
070            iMaxPenalty = maxPenalty;
071        }
072    
073        /** Examination room */
074        public ExamRoom getRoom() {
075            return iRoom;
076        }
077    
078        /** Examination room id */
079        public long getId() {
080            return getRoom().getId();
081        }
082    
083        /** Examination room name */
084        public String getName() {
085            return getRoom().getName();
086        }
087    
088        /** Examination room availability */
089        public boolean isAvailable(ExamPeriod period) {
090            return iRoom.isAvailable(period) && iRoom.getPenalty(period) <= iMaxPenalty;
091        }
092    
093        /**
094         * Penalty for assignment of an exam into this room
095         * {@link Exam#getRoomPlacements()}
096         */
097        public int getPenalty() {
098            return iPenalty;
099        }
100    
101        /**
102         * Maximal penalty imposed of {@link ExamRoom#getPenalty(ExamPeriod)}, i.e.,
103         * a placement with greater penalty is not allowed to be made
104         */
105        public int getMaxPenalty() {
106            return iMaxPenalty;
107        }
108    
109        /**
110         * Penalty for assignment of an exam into this room
111         * {@link Exam#getRoomPlacements()}
112         */
113        public void setPenalty(int penalty) {
114            iPenalty = penalty;
115        }
116    
117        /**
118         * Maximal penalty imposed of {@link ExamRoom#getPenalty(ExamPeriod)}, i.e.,
119         * a placement with greater penalty is not allowed to be made
120         */
121        public void setMaxPenalty(int maxPenalty) {
122            iMaxPenalty = maxPenalty;
123        }
124    
125        /**
126         * Penalty for assignment of an exam into this room
127         * {@link Exam#getRoomPlacements()} and the given examination period
128         * 
129         * @return {@link ExamRoomPlacement#getPenalty()} +
130         *         {@link ExamRoom#getPenalty(ExamPeriod)}
131         */
132        public int getPenalty(ExamPeriod period) {
133            return 2 * iPenalty + iRoom.getPenalty(period);
134            // return (iPenalty != 0 ? iPenalty : iRoom.getPenalty(period));
135        }
136    
137        /**
138         * Room size
139         * 
140         * @param altSeating
141         *            examination seeting (pass {@link Exam#hasAltSeating()})
142         * @return room size or room alternative size, based on given seating
143         */
144        public int getSize(boolean altSeating) {
145            return (altSeating ? getRoom().getAltSize() : getRoom().getSize());
146        }
147    
148        /**
149         * Room distance
150         * 
151         * @return appropriate {@link ExamRoom#getDistanceInMeters(ExamRoom)}
152         */
153        public double getDistanceInMeters(ExamRoomPlacement other) {
154            return getRoom().getDistanceInMeters(other.getRoom());
155        }
156    
157        /**
158         * Hash code
159         */
160        @Override
161        public int hashCode() {
162            return getRoom().hashCode();
163        }
164    
165        @Override
166        public String toString() {
167            return getRoom().toString() + (getPenalty() == 0 ? "" : "/" + getPenalty());
168        }
169    
170        /** Compare two room placements for equality */
171        @Override
172        public boolean equals(Object o) {
173            if (o == null)
174                return false;
175            if (o instanceof ExamRoomPlacement) {
176                return getRoom().equals(((ExamRoomPlacement) o).getRoom());
177            } else if (o instanceof ExamRoom) {
178                return getRoom().equals(o);
179            }
180            return false;
181        }
182    
183        /** Compare two room placements */
184        @Override
185        public int compareTo(ExamRoomPlacement o) {
186            return getRoom().compareTo(o.getRoom());
187        }
188    }