001package org.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.3 (Examination Timetabling)<br>
010 *          Copyright (C) 2008 - 2014 Tomáš Müller<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 */
028public 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     * @return examination room
075     **/
076    public ExamRoom getRoom() {
077        return iRoom;
078    }
079
080    /** Examination room id 
081     * @return examination room unique id
082     **/
083    public long getId() {
084        return getRoom().getId();
085    }
086
087    /** Examination room name 
088     * @return examination room name
089     **/
090    public String getName() {
091        return getRoom().getName();
092    }
093
094    /** Examination room availability 
095     * @param period given period
096     * @return true if the given period is available in this room
097     **/
098    public boolean isAvailable(ExamPeriod period) {
099        return iRoom.isAvailable(period) && iRoom.getPenalty(period) <= iMaxPenalty;
100    }
101
102    /**
103     * Penalty for assignment of an exam into this room
104     * {@link Exam#getRoomPlacements()}
105     * @return room assignment penalty
106     */
107    public int getPenalty() {
108        return iPenalty;
109    }
110
111    /**
112     * Maximal penalty imposed of {@link ExamRoom#getPenalty(ExamPeriod)}, i.e.,
113     * a placement with greater penalty is not allowed to be made
114     * @return maximal penalty
115     */
116    public int getMaxPenalty() {
117        return iMaxPenalty;
118    }
119
120    /**
121     * Penalty for assignment of an exam into this room
122     * {@link Exam#getRoomPlacements()}
123     * @param penalty room assignment penalty
124     */
125    public void setPenalty(int penalty) {
126        iPenalty = penalty;
127    }
128
129    /**
130     * Maximal penalty imposed of {@link ExamRoom#getPenalty(ExamPeriod)}, i.e.,
131     * a placement with greater penalty is not allowed to be made
132     * @param maxPenalty maximal penalty
133     */
134    public void setMaxPenalty(int maxPenalty) {
135        iMaxPenalty = maxPenalty;
136    }
137
138    /**
139     * Penalty for assignment of an exam into this room
140     * {@link Exam#getRoomPlacements()} and the given examination period
141
142     * @param period given period 
143     * @return {@link ExamRoomPlacement#getPenalty()} +
144     *         {@link ExamRoom#getPenalty(ExamPeriod)}
145     */
146    public int getPenalty(ExamPeriod period) {
147        return 2 * iPenalty + iRoom.getPenalty(period);
148        // return (iPenalty != 0 ? iPenalty : iRoom.getPenalty(period));
149    }
150
151    /**
152     * Room size
153     * 
154     * @param altSeating
155     *            examination seating (pass {@link Exam#hasAltSeating()})
156     * @return room size or room alternative size, based on given seating
157     */
158    public int getSize(boolean altSeating) {
159        return (altSeating ? getRoom().getAltSize() : getRoom().getSize());
160    }
161
162    /**
163     * Room distance
164
165     * @param other another placement
166     * @return appropriate {@link ExamRoom#getDistanceInMeters(ExamRoom)}
167     */
168    public double getDistanceInMeters(ExamRoomPlacement other) {
169        return getRoom().getDistanceInMeters(other.getRoom());
170    }
171
172    /**
173     * Hash code
174     */
175    @Override
176    public int hashCode() {
177        return getRoom().hashCode();
178    }
179
180    @Override
181    public String toString() {
182        return getRoom().toString() + (getPenalty() == 0 ? "" : "/" + getPenalty());
183    }
184
185    /** Compare two room placements for equality */
186    @Override
187    public boolean equals(Object o) {
188        if (o == null)
189            return false;
190        if (o instanceof ExamRoomPlacement) {
191            return getRoom().equals(((ExamRoomPlacement) o).getRoom());
192        } else if (o instanceof ExamRoom) {
193            return getRoom().equals(o);
194        }
195        return false;
196    }
197
198    /** Compare two room placements */
199    @Override
200    public int compareTo(ExamRoomPlacement o) {
201        return getRoom().compareTo(o.getRoom());
202    }
203}