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