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 * @author  Tomáš Müller
011 * @version StudentSct 1.3 (Student Sectioning)<br>
012 *          Copyright (C) 2007 - 2016 Tomáš Müller<br>
013 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
014 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
015 * <br>
016 *          This library is free software; you can redistribute it and/or modify
017 *          it under the terms of the GNU Lesser General Public License as
018 *          published by the Free Software Foundation; either version 3 of the
019 *          License, or (at your option) any later version. <br>
020 * <br>
021 *          This library is distributed in the hope that it will be useful, but
022 *          WITHOUT ANY WARRANTY; without even the implied warranty of
023 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
024 *          Lesser General Public License for more details. <br>
025 * <br>
026 *          You should have received a copy of the GNU Lesser General Public
027 *          License along with this library; if not see
028 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
029 */
030public class Instructor {
031    private long iId;
032    private String iExternalId = null, iName = null;
033    private String iEmail;
034    
035    /**
036     * Constructor
037     * @param id instructor unique id
038     */
039    public Instructor(long id) {
040        this(id, null, null, null);
041    }
042    
043    /**
044     * Constructor
045     * @param id instructor unique id
046     * @param externalId instructor external id
047     * @param name instructor name
048     * @param email instructor email
049     */
050    public Instructor(long id, String externalId, String name, String email) {
051        iId = id; iName = name; iExternalId = externalId; iEmail = email;
052    }
053    
054    /** Instructor unique id 
055     * @return instructor unique id
056     **/
057    public long getId() {
058        return iId;
059    }
060
061    /** Set instructor unique id 
062     * @param id instructor unique id
063     **/
064    public void setId(long id) {
065        iId = id;
066    }
067    
068    @Override
069    public String toString() {
070        return getName() == null ? "I" + getId() : getName();
071    }
072    
073    /**
074     * Compare two instructors for equality. Two instructors are considered equal if
075     * they have the same id.
076     */
077    @Override
078    public boolean equals(Object object) {
079        if (object == null || !(object instanceof Instructor))
080            return false;
081        return getId() == ((Instructor) object).getId();
082    }
083
084    /**
085     * Hash code (based only on instructor id)
086     */
087    @Override
088    public int hashCode() {
089        return (int) (iId ^ (iId >>> 32));
090    }
091    
092    /**
093     * Get instructor external id
094     * @return instructor external unique id
095     */
096    public String getExternalId() { return iExternalId; }
097
098    /**
099     * Set instructor external id
100     * @param externalId instructor external id
101     */
102    public void setExternalId(String externalId) { iExternalId = externalId; }
103
104    /**
105     * Get instructor name
106     * @return instructor name
107     */
108    public String getName() { return iName; }
109
110    /**
111     * Set instructor name
112     * @param name instructor name
113     */
114    public void setName(String name) { iName = name; }
115    
116    /**
117     * Get instructor email
118     * @return instructor email
119     */
120    public String getEmail() { return iEmail; }
121
122    /**
123     * Set instructor email
124     * @param email instructor email
125     */
126    public void setEmail(String email) { iEmail = email; }
127    
128    @Deprecated
129    public static List<Instructor> toInstructors(String instructorIds, String instructorNames) {
130        if (instructorIds == null || instructorIds.isEmpty()) return null;
131        String[] names = (instructorNames == null ? new String[] {}: instructorNames.split(":"));
132        List<Instructor> instructors = new ArrayList<Instructor>();
133        for (String id: instructorIds.split(":")) {
134            Instructor instructor = new Instructor(Long.parseLong(id));
135            if (instructors.size() < names.length) {
136                String name = names[instructors.size()];
137                if (name.indexOf('|') >= 0) {
138                    instructor.setName(name.substring(0, name.indexOf('|')));
139                    instructor.setEmail(name.substring(name.indexOf('|') + 1));
140                } else {
141                    instructor.setName(name);
142                }
143            }
144            instructors.add(instructor);
145        }
146        return instructors;
147    }
148}