001package net.sf.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.2 (Examination Timetabling)<br>
010 *          Copyright (C) 2008 - 2010 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    public ExamPeriod getPeriod() {
048        return iPeriod;
049    }
050
051    /** Examination period id */
052    public Long getId() {
053        return getPeriod().getId();
054    }
055
056    /** Examination period index */
057    public int getIndex() {
058        return getPeriod().getIndex();
059    }
060
061    /**
062     * Examination period penalty (for an assignment of this period to the given
063     * exam {@link Exam#getPeriodPlacements()})
064     * 
065     * @return given penalty plus global period penalty
066     *         {@link ExamPeriod#getPenalty()}
067     */
068    public int getPenalty() {
069        return 2 * iPenalty + iPeriod.getPenalty();
070    }
071    
072    /**
073     * Period penalty for given exam
074     */
075    public int getExamPenalty() {
076        return iPenalty;
077    }
078
079    /**
080     * Hash code
081     */
082    @Override
083    public int hashCode() {
084        return getPeriod().hashCode();
085    }
086
087    @Override
088    public String toString() {
089        return getPeriod().toString() + (getPenalty() == 0 ? "" : "/" + getPenalty());
090    }
091
092    /** Compare two room placements for equality */
093    @Override
094    public boolean equals(Object o) {
095        if (o == null)
096            return false;
097        if (o instanceof ExamPeriodPlacement) {
098            return getPeriod().equals(((ExamPeriodPlacement) o).getPeriod());
099        } else if (o instanceof ExamPeriod) {
100            return getPeriod().equals(o);
101        }
102        return false;
103    }
104
105    /** Compare two period placements */
106    @Override
107    public int compareTo(ExamPeriodPlacement o) {
108        return getPeriod().compareTo(o.getPeriod());
109    }
110}