001    package net.sf.cpsolver.exam.model;
002    
003    import java.util.HashSet;
004    import java.util.Set;
005    
006    /**
007     * Representation of a course or a section (or any other group of students that
008     * is associated with an exam). This entity is not used for examination
009     * timetabling, but it may be important for reports since students are usually
010     * enrolled to sections and/or courses and an exam can be offered for a set of
011     * courses/sections. <br>
012     * <br>
013     * The relations between course/section and exams, students and instructors are
014     * bidirectional, see {@link Exam#getOwners()}, {@link ExamStudent#getOwners()},
015     * and {@link ExamInstructor#getOwners()}. <br>
016     * <br>
017     * 
018     * @version ExamTT 1.2 (Examination Timetabling)<br>
019     *          Copyright (C) 2008 - 2010 Tomas Muller<br>
020     *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
021     *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
022     * <br>
023     *          This library is free software; you can redistribute it and/or modify
024     *          it under the terms of the GNU Lesser General Public License as
025     *          published by the Free Software Foundation; either version 3 of the
026     *          License, or (at your option) any later version. <br>
027     * <br>
028     *          This library is distributed in the hope that it will be useful, but
029     *          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. <br>
032     * <br>
033     *          You should have received a copy of the GNU Lesser General Public
034     *          License along with this library; if not see
035     *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
036     */
037    public class ExamOwner implements Comparable<ExamOwner> {
038        private long iId;
039        private String iName;
040        private Exam iExam;
041        private Set<ExamStudent> iStudents = new HashSet<ExamStudent>();
042        private Set<ExamInstructor> iInstructors = new HashSet<ExamInstructor>();
043    
044        /**
045         * Constructor.
046         * 
047         * @param exam
048         *            an exam for this course/section
049         * @param id
050         *            unique id
051         * @param name
052         *            course/section name
053         */
054        public ExamOwner(Exam exam, long id, String name) {
055            iExam = exam;
056            iId = id;
057            iName = name;
058        }
059    
060        /**
061         * Unique identifier
062         */
063        public long getId() {
064            return iId;
065        }
066    
067        /**
068         * Course/section name
069         */
070        public String getName() {
071            return iName;
072        }
073    
074        /**
075         * An exam for this course/section
076         */
077        public Exam getExam() {
078            return iExam;
079        }
080    
081        /**
082         * List of students that are enrolled into this section/course
083         * 
084         * @return set of {@link ExamStudent}
085         */
086        public Set<ExamStudent> getStudents() {
087            return iStudents;
088        }
089    
090        /**
091         * List of instructors that are enrolled into this section/course
092         * 
093         * @return set of {@link ExamInstructor}
094         */
095        public Set<ExamInstructor> getIntructors() {
096            return iInstructors;
097        }
098    
099        /**
100         * String representation -- course/section name
101         */
102        @Override
103        public String toString() {
104            return iName;
105        }
106    
107        /** Hash code */
108        @Override
109        public int hashCode() {
110            return (int) (iId ^ (iId >>> 32));
111        }
112    
113        /** Compare two exam owners for equality */
114        @Override
115        public boolean equals(Object o) {
116            if (o == null || !(o instanceof ExamOwner))
117                return false;
118            return getId() == ((ExamOwner) o).getId();
119        }
120    
121        /** Compare two exam owners by name */
122        @Override
123        public int compareTo(ExamOwner owner) {
124            if (!getName().equals(owner.getName()))
125                return getName().compareTo(owner.getName());
126            return Double.compare(getId(), owner.getId());
127        }
128    }