001    package net.sf.cpsolver.exam.model;
002    
003    /**
004     * Representation of an examination period.
005     * Examination timetabling model contains a list of non-overlapping examination periods.
006     * Each period has a day, starting time and a length (in minutes) defined. Each exam
007     * is to be assigned to one period that is available for the exam and that is of the same
008     * of greater length than the exam.
009     * <br><br>
010     * A penalty weight ({@link ExamPeriod#getPenalty()}) can be assigned to each period. It is used 
011     * to penalize unpopular examination times (e.g., evening or last-day).   
012     * <br><br>
013     * A list of periods is to be defined using {@link ExamModel#addPeriod(Long, String, String, int, int)}, inserting
014     * periods in the order of increasing days and times.
015     * <br><br>
016     * 
017     * @version
018     * ExamTT 1.1 (Examination Timetabling)<br>
019     * Copyright (C) 2007 Tomáš Müller<br>
020     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
021     * Lazenska 391, 76314 Zlin, Czech Republic<br>
022     * <br>
023     * This library is free software; you can redistribute it and/or
024     * modify it under the terms of the GNU Lesser General Public
025     * License as published by the Free Software Foundation; either
026     * version 2.1 of the License, or (at your option) any later version.
027     * <br><br>
028     * This library is distributed in the hope that it will be useful,
029     * but WITHOUT ANY WARRANTY; without even the implied warranty of
030     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
031     * Lesser General Public License for more details.
032     * <br><br>
033     * You should have received a copy of the GNU Lesser General Public
034     * License along with this library; if not, write to the Free Software
035     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
036     */
037    public class ExamPeriod implements Comparable {
038        private int iIndex = -1;
039        private Long iId = null;
040        private String iTimeStr;
041        private String iDayStr;
042        private int iLength;
043        private int iDay, iTime;
044        private int iPenalty;
045        private ExamPeriod iPrev, iNext;
046        
047        /**
048         * Constructor
049         * @param id period unique identifier
050         * @param day day (e.g., 07/12/10)
051         * @param time (e.g., 8:00am-10:00am)
052         * @param length length of period in minutes
053         * @param penalty penalization of using this period
054         */
055        public ExamPeriod(Long id, String day, String time, int length, int penalty) {
056            iId = id;
057            iDayStr = day;
058            iTimeStr = time;
059            iLength = length;
060            iPenalty = penalty;
061        }
062        
063        /** Period unique identifier */
064        public Long getId() {
065            return iId;
066        }
067        
068        /** Period unique identifier */
069        public void setId(Long id) {
070            iId = id;
071        }
072    
073        /**
074         * Day string, e.g., 07/12/10
075         */
076        public String getDayStr() {
077            return iDayStr;
078        }
079        /**
080         * Day index
081         * @return index of the day within all days that are used for examination
082         */
083        public int getDay() {
084            return iDay;
085        }
086        /**
087         * Time string, e.g., 8:00am-10:00am 
088         */
089        public String getTimeStr() {
090            return iTimeStr;
091        }
092        /**
093         * Time index
094         * @return index of the time within all time that are used for examination on the same day
095         */
096        public int getTime() {
097            return iTime;
098        }
099        /**
100         * Length of period in minutes
101         * @return period length
102         */
103        public int getLength() {
104            return iLength;
105        }
106        /**
107         * Period index
108         * @return index of the period within all examination periods 
109         */
110        public int getIndex() {
111            return iIndex;
112        }
113        /**
114         * Period weight to be used to penalize unpopular periods
115         * @return period weight
116         */
117        public int getPenalty() {
118            return iPenalty;
119        }
120        /**
121         * Previous period 
122         * @return period with index equal to index-1, null if this is the first period 
123         */
124        public ExamPeriod prev() { return iPrev; }
125        /**
126         * Next period 
127         * @return period with index equal to index+1, null if this is the last period 
128         */
129        public ExamPeriod next() { return iNext; }
130        /**
131         * Set priod indexes (only to be used by {@link ExamModel#addPeriod(Long, String, String, int, int)})
132         * @param index period index
133         * @param day day index
134         * @param time time index
135         */
136        public void setIndex(int index, int day, int time) {
137            iIndex = index;
138            iDay = day;
139            iTime = time;
140        }
141        /**
142         * Set previous period (only to be used by {@link ExamModel#addPeriod(Long, String, String, int, int)})
143         * @param prev previous period
144         */
145        public void setPrev(ExamPeriod prev) { iPrev = prev;}
146        /**
147         * Set next period (only to be used by {@link ExamModel#addPeriod(Long, String, String, int, int)})
148         * @param next next period
149         */
150        public void setNext(ExamPeriod next) { iNext = next;}
151        /**
152         * String representation
153         * @return day string time string
154         */
155        public String toString() {
156            return getDayStr()+" "+getTimeStr();
157        }
158        /**
159         * String representation for debuging purposes
160         * @return day string time string (idx: index, day: day index, time: time index, weight: period penalty, prev: previous period, next: next period)
161         */
162        public String toDebugString() {
163            return getDayStr()+" "+getTimeStr()+
164            " (idx:"+getIndex()+", day:"+getDay()+", time:"+getTime()+", penalty:"+getPenalty()+
165            (prev()==null?"":", prev:"+prev().getDayStr()+" "+prev().getTimeStr()+")")+
166            (next()==null?"":", next:"+next().getDayStr()+" "+next().getTimeStr()+")");
167        }
168        public int hashCode() {
169            return iIndex;
170        }
171        public boolean equals(Object o) {
172            if (o==null || !(o instanceof ExamPeriod)) return false;
173            return getIndex()==((ExamPeriod)o).getIndex();
174        }
175        public int compareTo(Object o) {
176            return Double.compare(getIndex(), ((ExamPeriod)o).getIndex());
177        }
178    }