summaryrefslogtreecommitdiff
path: root/src/main/java/design/model/course_search/CurrentSearchQuery.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/design/model/course_search/CurrentSearchQuery.java')
-rw-r--r--src/main/java/design/model/course_search/CurrentSearchQuery.java138
1 files changed, 138 insertions, 0 deletions
diff --git a/src/main/java/design/model/course_search/CurrentSearchQuery.java b/src/main/java/design/model/course_search/CurrentSearchQuery.java
new file mode 100644
index 0000000..999dfcb
--- /dev/null
+++ b/src/main/java/design/model/course_search/CurrentSearchQuery.java
@@ -0,0 +1,138 @@
+package design.model.course_search;
+
+import design.persistence.MasterDatabase;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/*
+ * Represents the state of our current search.
+ */
+public class CurrentSearchQuery {
+ public static final CurrentSearchQuery INSTANCE = new CurrentSearchQuery();
+
+ // initialize our search with the master db data
+ MasterDatabase db = MasterDatabase.INSTANCE;
+ private CourseList query = db.getCourseList();
+ private final List<CourseSorter> filters = new ArrayList<CourseSorter>();
+
+ // reset the query
+ public void reset() {
+ query = db.getCourseList();
+ }
+
+ // add a new filter
+ public void addFilter(CourseSorter filter) {
+ filters.add(filter);
+ }
+
+ // clear the filters
+ public void clearFilters() {
+ filters.clear();
+ }
+
+ // print out the filters we're currently using and the order
+ public String printFilters() {
+ String filterResult = "";
+
+ // no filters? let the user know.
+ if (filters.size() == 0) {
+ return "nothing";
+ }
+
+ for (CourseSorter f : filters) {
+ filterResult += f.toString() + " --> ";
+ }
+
+ // remove last arrow and add padding
+ filterResult = filterResult.substring(0, filterResult.length() - 5);
+ filterResult += "\n";
+
+ return filterResult;
+ }
+
+ // get all the filters
+ public List<CourseSorter> getFilters() {
+ return filters;
+ }
+
+ // get our current query.
+ public CourseList getQueryResult() {
+ return query;
+ }
+
+ public void search(String searchQuery) {
+ // only grab courses which fit our search
+ List<ICourse> courses = db.getCourseList().getCourses().stream()
+ .filter(s -> s.toString().toLowerCase().contains(searchQuery.toLowerCase()))
+ .collect(Collectors.toList());
+
+ // and now, we filter it!
+ CourseList filtered = applyFilters(filters, courses);
+ query.setCourses(filtered.getCourses());
+ }
+
+ // to apply our filters we need to traverse the tree and make new grouping as we go.
+ public CourseList applyFilters(List<CourseSorter> filters, List<ICourse> coursesToFilter) {
+ CourseList root = new CourseList();
+ root.setCourses(applyFiltersRecursive(coursesToFilter, filters, 0));
+ return root;
+ }
+
+ // the actual recursive part. Level is how many filters deep we are.
+ private List<ICourse> applyFiltersRecursive(List<ICourse> courses, List<CourseSorter> filters, int level) {
+ // base case. we have gone past all filters or theres only one course in this list. already sorted!
+ if (level >= filters.size() || courses.size() <= 1) {
+ return courses;
+ }
+
+ // grab out current sorting strategy for this level, and sort the courselist.
+ CourseSorter sorter = filters.get(level);
+ sorter.sortCourses(courses);
+
+ // the resulting sorted list, with new groupings
+ List<ICourse> result = new ArrayList<>();
+
+ // courses with an equal value.
+ List<ICourse> currentGroup = new ArrayList<>();
+
+ ICourse prev = null;
+
+ // run through the courses, if
+ for (ICourse c : courses) {
+ /* always add the first course to a new group. when iterating through courses, if we have to values that are equal, we need to add them into a group together.
+ * think about it this way. If we have [ 1, 2, 2, 2, 3, 4 ]. 1 is first, so its in it's own group. 2 /= 1, so 2 gets its own group.
+ * now do 2 again. we add it to the existing group 2. Same with the next 2. Now 3. 3 /= 2, so we put it in it's own group.
+ */
+ if (prev == null || !sorter.isEqual(prev, c)) {
+ // already a course in that group? we now have two equal values and so
+ if (!currentGroup.isEmpty()) {
+ result.add(makeGroup(currentGroup, filters, level));
+ currentGroup = new ArrayList<>();
+ }
+ }
+
+ currentGroup.add(c);
+ prev = c;
+ }
+
+ // handle the last group.
+ if (!currentGroup.isEmpty()) {
+ result.add(makeGroup(currentGroup, filters, level));
+ }
+
+ return result;
+ }
+
+ // make a CourseList group, a sublist of a group, and filter it.
+ private ICourse makeGroup(List<ICourse> group, List<CourseSorter> filters, int level) {
+ // base case, group only has one course in it (already sorted)
+ if (group.size() == 1) return group.get(0);
+
+ // group has more than 1 course in it, it needs to be sorted more if possible.
+ CourseList subList = new CourseList();
+ subList.setCourses(applyFiltersRecursive(group, filters, level + 1));
+ return subList;
+ }
+}