package class2_3_Sorting_041_inClass_start; import java.util.ArrayList; public class Schedule { // We are getting a generics warning here: We are not using generics on this Getter. // So it is just compiled as the type-erased Getter with Comparable get(Course) instead // of K get(Course) where >. In other words, // if there are two getters, they might return different types of objects that can't be compared // with each other. // // But since we only have one getter in our program, this will not be a problem in practice. // // It is unfortunate that the generic system in the Java compiler cannot confirm this reasoning // for us. private Getter getter = new NameGetter(); private ArrayList courses = new ArrayList<>();; public void add(Course course) { courses.add(course); } public void sort() { ArrayList newList = new ArrayList<>(); for(int oldIndex = 0; oldIndex < courses.size(); oldIndex++) { int newIndex = 0; Course newCourse = courses.get(oldIndex); // As described above, since both getters point to the same object, they should return // the same type at runtime. while (newIndex < newList.size() && getter.get(newList.get(newIndex)) .compareTo(getter.get(newCourse)) <= 0) { newIndex++; } newList.add(newIndex, newCourse); } courses = newList; } public void printAll() { for(Course course: courses) { System.out.println("Course: "+course); } } public > void setGetter(Getter getter) { this.getter = getter; } }