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 (iPenalty != 0 ? iPenalty : iRoom.getPenalty(period));
134        }
135    
136        /**
137         * Room size
138         * 
139         * @param altSeating
140         *            examination seeting (pass {@link Exam#hasAltSeating()})
141         * @return room size or room alternative size, based on given seating
142         */
143        public int getSize(boolean altSeating) {
144            return (altSeating ? getRoom().getAltSize() : getRoom().getSize());
145        }
146    
147        /**
148         * Room distance
149         * 
150         * @return appropriate {@link ExamRoom#getDistanceInMeters(ExamRoom)}
151         */
152        public double getDistanceInMeters(ExamRoomPlacement other) {
153            return getRoom().getDistanceInMeters(other.getRoom());
154        }
155    
156        /**
157         * Hash code
158         */
159        @Override
160        public int hashCode() {
161            return getRoom().hashCode();
162        }
163    
164        @Override
165        public String toString() {
166            return getRoom().toString() + (getPenalty() == 0 ? "" : "/" + getPenalty());
167        }
168    
169        /** Compare two room placements for equality */
170        @Override
171        public boolean equals(Object o) {
172            if (o == null)
173                return false;
174            if (o instanceof ExamRoomPlacement) {
175                return getRoom().equals(((ExamRoomPlacement) o).getRoom());
176            } else if (o instanceof ExamRoom) {
177                return getRoom().equals(o);
178            }
179            return false;
180        }
181    
182        /** Compare two room placements */
183        @Override
184        public int compareTo(ExamRoomPlacement o) {
185            return getRoom().compareTo(o.getRoom());
186        }
187    }