001package org.cpsolver.exam.model;
002
003/**
004 * Representation of a period placement of an exam. It contains a period
005 * {@link ExamPeriod} and a penalty associated with a placement of an exam into
006 * the given period. <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 ExamPeriodPlacement implements Comparable<ExamPeriodPlacement> {
029    private ExamPeriod iPeriod;
030    private int iPenalty;
031
032    /**
033     * Constructor
034     * 
035     * @param period
036     *            examination period that is available for an exam and that is
037     *            of enough length
038     * @param penalty
039     *            period penalty for given exam
040     */
041    public ExamPeriodPlacement(ExamPeriod period, int penalty) {
042        iPeriod = period;
043        iPenalty = penalty;
044    }
045
046    /** Examination period 
047     * @return period
048     **/
049    public ExamPeriod getPeriod() {
050        return iPeriod;
051    }
052
053    /** Examination period id 
054     * @return period unique id
055     **/
056    public Long getId() {
057        return getPeriod().getId();
058    }
059
060    /** Examination period index 
061     * @return period index
062     **/
063    public int getIndex() {
064        return getPeriod().getIndex();
065    }
066
067    /**
068     * Examination period penalty (for an assignment of this period to the given
069     * exam {@link Exam#getPeriodPlacements()})
070     * 
071     * @return given penalty plus global period penalty
072     *         {@link ExamPeriod#getPenalty()}
073     */
074    public int getPenalty() {
075        return 2 * iPenalty + iPeriod.getPenalty();
076    }
077    
078    /**
079     * Period penalty for given exam
080     * @return period penalty
081     */
082    public int getExamPenalty() {
083        return iPenalty;
084    }
085
086    /**
087     * Hash code
088     */
089    @Override
090    public int hashCode() {
091        return getPeriod().hashCode();
092    }
093
094    @Override
095    public String toString() {
096        return getPeriod().toString() + (getPenalty() == 0 ? "" : "/" + getPenalty());
097    }
098
099    /** Compare two room placements for equality */
100    @Override
101    public boolean equals(Object o) {
102        if (o == null)
103            return false;
104        if (o instanceof ExamPeriodPlacement) {
105            return getPeriod().equals(((ExamPeriodPlacement) o).getPeriod());
106        } else if (o instanceof ExamPeriod) {
107            return getPeriod().equals(o);
108        }
109        return false;
110    }
111
112    /** Compare two period placements */
113    @Override
114    public int compareTo(ExamPeriodPlacement o) {
115        return getPeriod().compareTo(o.getPeriod());
116    }
117}