    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 * @author  Tomáš Müller
    012 * @version CourseTT 1.3 (University Course Timetabling)<br>
    013 *          Copyright (C) 2006 - 2016 Tomáš Müller<br>
    014 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    015 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    016 * <br>
    017 *          This library is free software; you can redistribute it and/or modify
    018 *          it under the terms of the GNU Lesser General Public License as
    019 *          published by the Free Software Foundation; either version 3 of the
    020 *          License, or (at your option) any later version. <br>
    021 * <br>
    022 *          This library is distributed in the hope that it will be useful, but
    023 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    024 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    025 *          Lesser General Public License for more details. <br>
    026 * <br>
    027 *          You should have received a copy of the GNU Lesser General Public
    028 *          License along with this library; if not see
    029 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    030 */
    031public class StudentGroup implements Comparable<StudentGroup> {
    032    private long iId;
    033    private String iName;
    034    private double iWeight;
    035    private List<Student> iStudents = new ArrayList<Student>();
    036
    037    /**
    038     * Create a student group
    039     * @param id group unique id
    040     * @param weight group weight
    041     * @param name group name
    042     */
    043    public StudentGroup(long id, double weight, String name) {
    044        iId = id; iName = name; iWeight = weight;
    045    }
    046
    047    /**
    048     * Returns student group id
    049     */
    050    public long getId() { return iId; }
    051
    052    /**
    053     * Returns student group name
    054     */
    055    public String getName() { return iName; }
    056
    057    /**
    058     * Returns student group weight
    059     */
    060    public double getWeight() { return iWeight; }
    061
    062    /**
    063     * Return students of this group
    064     */
    065    public List<Student> getStudents() {
    066        return iStudents;
    067    }
    068
    069    /**
    070     * Count students of this group that are requesting the given offering.
    071     * @param offeringId offering id
    072     * @return students with {@link Student#hasOffering(Long)} true
    073     */
    074    public int countStudents(Long offeringId) {
    075        int ret = 0;
    076        for (Student student: iStudents)
    077            if (student.hasOffering(offeringId)) ret++;
    078        return ret;
    079    }
    080
    081    /**
    082     * Add student to this group
    083     * @param student a student to add
    084     */
    085    public void addStudent(Student student) {
    086        iStudents.add(student);
    087    }
    088
    089    @Override
    090    public int hashCode() { return (int)(iId ^ (iId >>> 32)); }
    091
    092    @Override
    093    public boolean equals(Object o) {
    094        return (o != null && o instanceof StudentGroup && getId() == ((StudentGroup)o).getId());
    095    }
    096
    097    @Override
    098    public String toString() {
    099        return getName();
    100    }
    101
    102    @Override
    103    public int compareTo(StudentGroup g) {
    104        int cmp = getName().compareToIgnoreCase(g.getName());
    105        if (cmp != 0) return cmp;
    106        return (getId() < g.getId() ? -1 : getId() == g.getId() ? 0 : 1);
    107    }
    108
    109    /**
    110     * Average enrollment weight of students of this group in the given offering
    111     */
    112    public double getAverageEnrollmentWeight(Long offeringId) {
    113        double total = 0.0; int count = 0;
    114        for (Student student: iStudents)
    115            if (student.hasOffering(offeringId)) {
    116                total += student.getOfferingWeight(offeringId);
    117                count ++;
    118            }
    119        return count == 0 ? 0.0 : total / count;
    120    }
    121
    122    /**
    123     * Count offerings that students of this group have
    124     */
    125    public int countOfferings() {
    126        Set<Long> offeringIds = new HashSet<Long>();
    127        for (Student student: iStudents)
    128            offeringIds.addAll(student.getOfferings());
    129        return offeringIds.size();
    130    }
    131}