001package org.cpsolver.coursett.model;
002
003import java.util.ArrayList;
004import java.util.HashSet;
005import java.util.List;
006import java.util.Set;
007
008/**
009 * Student group.
010 * 
011 * @version CourseTT 1.3 (University Course Timetabling)<br>
012 *          Copyright (C) 2006 - 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 StudentGroup implements Comparable<StudentGroup> {
031    private long iId;
032    private String iName;
033    private double iWeight;
034    private List<Student> iStudents = new ArrayList<Student>();
035    
036    /**
037     * Create a student group
038     * @param id group unique id
039     * @param weight group weight
040     * @param name group name
041     */
042    public StudentGroup(long id, double weight, String name) {
043        iId = id; iName = name; iWeight = weight;
044    }
045    
046    /**
047     * Returns student group id
048     */
049    public long getId() { return iId; }
050    
051    /**
052     * Returns student group name
053     */
054    public String getName() { return iName; }
055    
056    /**
057     * Returns student group weight
058     */
059    public double getWeight() { return iWeight; }
060    
061    /**
062     * Return students of this group
063     */
064    public List<Student> getStudents() {
065        return iStudents;
066    }
067    
068    /**
069     * Count students of this group that are requesting the given offering.
070     * @param offeringId offering id
071     * @return students with {@link Student#hasOffering(Long)} true
072     */
073    public int countStudents(Long offeringId) {
074        int ret = 0;
075        for (Student student: iStudents)
076            if (student.hasOffering(offeringId)) ret++;
077        return ret;
078    }
079    
080    /**
081     * Add student to this group
082     * @param student a student to add
083     */
084    public void addStudent(Student student) {
085        iStudents.add(student);
086    }
087    
088    @Override
089    public int hashCode() { return (int)(iId ^ (iId >>> 32)); }
090    
091    @Override
092    public boolean equals(Object o) {
093        return (o != null && o instanceof StudentGroup && getId() == ((StudentGroup)o).getId());
094    }
095    
096    @Override
097    public String toString() {
098        return getName();
099    }
100
101    @Override
102    public int compareTo(StudentGroup g) {
103        int cmp = getName().compareToIgnoreCase(g.getName());
104        if (cmp != 0) return cmp;
105        return (getId() < g.getId() ? -1 : getId() == g.getId() ? 0 : 1);
106    }
107    
108    /**
109     * Average enrollment weight of students of this group in the given offering
110     */
111    public double getAverageEnrollmentWeight(Long offeringId) {
112        double total = 0.0; int count = 0;
113        for (Student student: iStudents)
114            if (student.hasOffering(offeringId)) {
115                total += student.getOfferingWeight(offeringId);
116                count ++;
117            }
118        return count == 0 ? 0.0 : total / count;
119    }
120    
121    /**
122     * Count offerings that students of this group have
123     */
124    public int countOfferings() {
125        Set<Long> offeringIds = new HashSet<Long>();
126        for (Student student: iStudents)
127            offeringIds.addAll(student.getOfferings());
128        return offeringIds.size();
129    }
130}