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.java75
1 files changed, 29 insertions, 46 deletions
diff --git a/src/main/java/design/model/course_search/CurrentSearchQuery.java b/src/main/java/design/model/course_search/CurrentSearchQuery.java
index e1f52ce..999dfcb 100644
--- a/src/main/java/design/model/course_search/CurrentSearchQuery.java
+++ b/src/main/java/design/model/course_search/CurrentSearchQuery.java
@@ -1,81 +1,72 @@
package design.model.course_search;
+import design.persistence.MasterDatabase;
+
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
-import design.persistence.MasterDatabase;
-
-/*
+/*
* 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()
- {
+ public void reset() {
query = db.getCourseList();
}
// add a new filter
- public void addFilter(CourseSorter filter)
- {
+ public void addFilter(CourseSorter filter) {
filters.add(filter);
}
// clear the filters
- public void clearFilters()
- {
+ public void clearFilters() {
filters.clear();
}
// print out the filters we're currently using and the order
- public String printFilters()
- {
+ public String printFilters() {
String filterResult = "";
// no filters? let the user know.
- if(filters.size() == 0)
- {
+ if (filters.size() == 0) {
return "nothing";
}
- for( CourseSorter f : filters)
- {
+ for (CourseSorter f : filters) {
filterResult += f.toString() + " --> ";
}
// remove last arrow and add padding
- filterResult = filterResult.substring(0, filterResult.length() - 5);
+ filterResult = filterResult.substring(0, filterResult.length() - 5);
filterResult += "\n";
return filterResult;
}
// get all the filters
- public List<CourseSorter> getFilters()
- {
+ public List<CourseSorter> getFilters() {
return filters;
}
// get our current query.
- public CourseList getQueryResult()
- {
+ public CourseList getQueryResult() {
return query;
}
- public void search(String searchQuery)
- {
+ 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());
+ .filter(s -> s.toString().toLowerCase().contains(searchQuery.toLowerCase()))
+ .collect(Collectors.toList());
// and now, we filter it!
CourseList filtered = applyFilters(filters, courses);
@@ -83,19 +74,16 @@ public class CurrentSearchQuery {
}
// 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)
- {
+ 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)
- {
+ 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;
}
@@ -112,17 +100,14 @@ public class CurrentSearchQuery {
ICourse prev = null;
// run through the courses, if
- for (ICourse c : courses)
- {
+ 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))
- {
+ * 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())
- {
+ if (!currentGroup.isEmpty()) {
result.add(makeGroup(currentGroup, filters, level));
currentGroup = new ArrayList<>();
}
@@ -133,8 +118,7 @@ public class CurrentSearchQuery {
}
// handle the last group.
- if (!currentGroup.isEmpty())
- {
+ if (!currentGroup.isEmpty()) {
result.add(makeGroup(currentGroup, filters, level));
}
@@ -142,8 +126,7 @@ public class CurrentSearchQuery {
}
// make a CourseList group, a sublist of a group, and filter it.
- private ICourse makeGroup(List<ICourse> group, List<CourseSorter> filters, int level)
- {
+ 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);
@@ -151,5 +134,5 @@ public class CurrentSearchQuery {
CourseList subList = new CourseList();
subList.setCourses(applyFiltersRecursive(group, filters, level + 1));
return subList;
- }
+ }
}