001package org.cpsolver.exam.model;
002
003import java.util.HashSet;
004import 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 * @author  Tomáš Müller
019 * @version ExamTT 1.3 (Examination Timetabling)<br>
020 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
021 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
022 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
023 * <br>
024 *          This library is free software; you can redistribute it and/or modify
025 *          it under the terms of the GNU Lesser General Public License as
026 *          published by the Free Software Foundation; either version 3 of the
027 *          License, or (at your option) any later version. <br>
028 * <br>
029 *          This library is distributed in the hope that it will be useful, but
030 *          WITHOUT ANY WARRANTY; without even the implied warranty of
031 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
032 *          Lesser General Public License for more details. <br>
033 * <br>
034 *          You should have received a copy of the GNU Lesser General Public
035 *          License along with this library; if not see
036 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
037 */
038public class ExamOwner implements Comparable<ExamOwner> {
039    private long iId;
040    private String iName;
041    private Exam iExam;
042    private Set<ExamStudent> iStudents = new HashSet<ExamStudent>();
043    private Set<ExamInstructor> iInstructors = new HashSet<ExamInstructor>();
044
045    /**
046     * Constructor.
047     * 
048     * @param exam
049     *            an exam for this course/section
050     * @param id
051     *            unique id
052     * @param name
053     *            course/section name
054     */
055    public ExamOwner(Exam exam, long id, String name) {
056        iExam = exam;
057        iId = id;
058        iName = name;
059    }
060
061    /**
062     * Unique identifier
063     * @return exam owner unique id
064     */
065    public long getId() {
066        return iId;
067    }
068
069    /**
070     * Course/section name
071     * @return owner name
072     */
073    public String getName() {
074        return iName;
075    }
076
077    /**
078     * An exam for this course/section
079     * @return exam associated with this owner
080     */
081    public Exam getExam() {
082        return iExam;
083    }
084
085    /**
086     * List of students that are enrolled into this section/course
087     * 
088     * @return set of {@link ExamStudent}
089     */
090    public Set<ExamStudent> getStudents() {
091        return iStudents;
092    }
093
094    /**
095     * List of instructors that are enrolled into this section/course
096     * 
097     * @return set of {@link ExamInstructor}
098     */
099    public Set<ExamInstructor> getIntructors() {
100        return iInstructors;
101    }
102
103    /**
104     * String representation -- course/section name
105     */
106    @Override
107    public String toString() {
108        return iName;
109    }
110
111    /** Hash code */
112    @Override
113    public int hashCode() {
114        return (int) (iId ^ (iId >>> 32));
115    }
116
117    /** Compare two exam owners for equality */
118    @Override
119    public boolean equals(Object o) {
120        if (o == null || !(o instanceof ExamOwner))
121            return false;
122        return getId() == ((ExamOwner) o).getId();
123    }
124
125    /** Compare two exam owners by name */
126    @Override
127    public int compareTo(ExamOwner owner) {
128        if (!getName().equals(owner.getName()))
129            return getName().compareTo(owner.getName());
130        return Double.compare(getId(), owner.getId());
131    }
132}