001package org.cpsolver.studentsct.model;
002
003import java.util.ArrayList;
004import java.util.List;
005
006/**
007 * Representation of an instructor. Each instructor contains id, and a name. <br>
008 * <br>
009 * 
010 * @version StudentSct 1.3 (Student Sectioning)<br>
011 *          Copyright (C) 2007 - 2016 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 Instructor {
030    private long iId;
031    private String iExternalId = null, iName = null;
032    private String iEmail;
033    
034    /**
035     * Constructor
036     * @param id instructor unique id
037     */
038    public Instructor(long id) {
039        this(id, null, null, null);
040    }
041    
042    /**
043     * Constructor
044     * @param id instructor unique id
045     * @param externalId instructor external id
046     * @param name instructor name
047     * @param email instructor email
048     */
049    public Instructor(long id, String externalId, String name, String email) {
050        iId = id; iName = name; iExternalId = externalId; iEmail = email;
051    }
052    
053    /** Instructor unique id 
054     * @return instructor unique id
055     **/
056    public long getId() {
057        return iId;
058    }
059
060    /** Set instructor unique id 
061     * @param id instructor unique id
062     **/
063    public void setId(long id) {
064        iId = id;
065    }
066    
067    @Override
068    public String toString() {
069        return getName() == null ? "I" + getId() : getName();
070    }
071    
072    /**
073     * Compare two instructors for equality. Two instructors are considered equal if
074     * they have the same id.
075     */
076    @Override
077    public boolean equals(Object object) {
078        if (object == null || !(object instanceof Instructor))
079            return false;
080        return getId() == ((Instructor) object).getId();
081    }
082
083    /**
084     * Hash code (based only on instructor id)
085     */
086    @Override
087    public int hashCode() {
088        return (int) (iId ^ (iId >>> 32));
089    }
090    
091    /**
092     * Get instructor external id
093     * @return instructor external unique id
094     */
095    public String getExternalId() { return iExternalId; }
096
097    /**
098     * Set instructor external id
099     * @param externalId instructor external id
100     */
101    public void setExternalId(String externalId) { iExternalId = externalId; }
102
103    /**
104     * Get instructor name
105     * @return instructor name
106     */
107    public String getName() { return iName; }
108
109    /**
110     * Set instructor name
111     * @param name instructor name
112     */
113    public void setName(String name) { iName = name; }
114    
115    /**
116     * Get instructor email
117     * @return instructor email
118     */
119    public String getEmail() { return iEmail; }
120
121    /**
122     * Set instructor email
123     * @param email instructor email
124     */
125    public void setEmail(String email) { iEmail = email; }
126    
127    @Deprecated
128    public static List<Instructor> toInstructors(String instructorIds, String instructorNames) {
129        if (instructorIds == null || instructorIds.isEmpty()) return null;
130        String[] names = (instructorNames == null ? new String[] {}: instructorNames.split(":"));
131        List<Instructor> instructors = new ArrayList<Instructor>();
132        for (String id: instructorIds.split(":")) {
133            Instructor instructor = new Instructor(Long.parseLong(id));
134            if (instructors.size() < names.length) {
135                String name = names[instructors.size()];
136                if (name.indexOf('|') >= 0) {
137                    instructor.setName(name.substring(0, name.indexOf('|')));
138                    instructor.setEmail(name.substring(name.indexOf('|') + 1));
139                } else {
140                    instructor.setName(name);
141                }
142            }
143            instructors.add(instructor);
144        }
145        return instructors;
146    }
147}