summaryrefslogtreecommitdiff
path: root/src/main/java/design/model
diff options
context:
space:
mode:
authorWillemDalton <willemhdalton@gmail.com>2025-10-08 21:49:21 -0400
committerWillemDalton <willemhdalton@gmail.com>2025-10-08 21:49:21 -0400
commit2ef38d86d8b207bc0dad6e3c6b7780abc6f6e82b (patch)
tree5e81c9311165b98bc4c65b25c3b6a37456d23e82 /src/main/java/design/model
parentdb1ede10a6547b51ffed71dc86d73f3a6c8af129 (diff)
downloaddesignproject-design-6-2ef38d86d8b207bc0dad6e3c6b7780abc6f6e82b.tar.gz
designproject-design-6-2ef38d86d8b207bc0dad6e3c6b7780abc6f6e82b.tar.bz2
designproject-design-6-2ef38d86d8b207bc0dad6e3c6b7780abc6f6e82b.zip
finished multi filtering
Diffstat (limited to '')
-rw-r--r--src/main/java/design/model/course_search/CurrentSearchQuery.java77
1 files changed, 74 insertions, 3 deletions
diff --git a/src/main/java/design/model/course_search/CurrentSearchQuery.java b/src/main/java/design/model/course_search/CurrentSearchQuery.java
index c00eb86..e1f52ce 100644
--- a/src/main/java/design/model/course_search/CurrentSearchQuery.java
+++ b/src/main/java/design/model/course_search/CurrentSearchQuery.java
@@ -17,12 +17,10 @@ public class CurrentSearchQuery {
private CourseList query = db.getCourseList();
private final List<CourseSorter> filters = new ArrayList<CourseSorter>();
-
// reset the query
public void reset()
{
query = db.getCourseList();
- clearFilters();
}
// add a new filter
@@ -79,6 +77,79 @@ public class CurrentSearchQuery {
.filter(s -> s.toString().toLowerCase().contains(searchQuery.toLowerCase()))
.collect(Collectors.toList());
- query.setCourses(courses);
+ // 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;
+ }
}