/**
* Created by Labunsky on 24.03.2017.
*/
public class MultiListEntry<S, C> {
private MultiListEntry<S, C> nextStudentEntry = null;
private MultiListEntry<S, C> nextCourseEntry = null;
private S student;
private C course;
MultiListEntry(S student, C course) {
this.student = student;
this.course = course;
}
public S getStudent() {
return student;
}
public C getCourse() {
return course;
}
public void setNextStudentEntry(MultiListEntry<S, C> next) {
nextStudentEntry = next;
}
public void setNextCourseEntry(MultiListEntry<S, C> next) {
this.nextCourseEntry = next;
}
public MultiListEntry<S, C> getNextStudentEntry() {
return nextStudentEntry;
}
public MultiListEntry<S, C> getNextCourseEntry() {
return nextCourseEntry;
}
}
/**
* Created by Labunsky on 24.03.2017.
*/
public class MultiList<S, C> {
private Map<S, MultiListEntry<S, C>> studentsIndex = new HashMap<>();
private Map<C, MultiListEntry<S, C>> coursesIndex = new HashMap<>();
public void add(S student, C course) {
MultiListEntry<S, C> newEntry = new MultiListEntry<>(student, course);
MultiListEntry<S, C> lastEntry;
if (studentsIndex.containsKey(student)) {
lastEntry = studentsIndex.get(student);
while (lastEntry.getNextStudentEntry() != null)
lastEntry = lastEntry.getNextStudentEntry();
lastEntry.setNextStudentEntry(newEntry);
} studentsIndex.put(student, newEntry);
if (coursesIndex.containsKey(course)) {
lastEntry = coursesIndex.get(course);
while (lastEntry.getNextCourseEntry() != null)
lastEntry = lastEntry.getNextCourseEntry();
lastEntry.setNextCourseEntry(newEntry);
} else coursesIndex.put(course, newEntry);
}
public MultiListEntry<S, C> getStudentEntries(S student) {
return studentsIndex.get(student);
}
public MultiListEntry<S, C> getCourseEntries(C course) {
return coursesIndex.get(course);
}
}