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