Skip to content

Commit 5d22de8

Browse files
committed
add skeleton for CourseCategory
Courses will be able to describe any special categories of courses they belong to, such as being in the category of language courses.
1 parent fa5b71c commit 5d22de8

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ucst-core/src/main/java/com/dvf/ucst/core/courseutils/Course.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.dvf.ucst.core.Student.CompletedCourse;
66
import com.dvf.ucst.core.StudentCoreQualities;
77
import com.dvf.ucst.core.UbcLocalFiles;
8+
import com.dvf.ucst.core.courseutils.categorymatchers.CourseCategory;
89
import com.dvf.ucst.core.faculties.CampusNotFoundException;
910
import com.dvf.ucst.core.faculties.FacultyTreeNode;
1011
import com.dvf.ucst.core.faculties.UbcCampuses;
@@ -36,6 +37,8 @@ public final class Course implements CreditValued, HyperlinkBookIf, SectionIdStr
3637
private final String courseIdToken;
3738
private final int creditValue;
3839
private final String descriptionString;
40+
private final Set<CourseCategory> categories =
41+
Collections.unmodifiableSet(EnumSet.noneOf(CourseCategory.class)); // unmodifiable. // TODO
3942

4043
// reqs are non-null:
4144
private final Requirement<StudentCoreQualities> studentReqs;
@@ -111,6 +114,10 @@ public final String getCourseDescription() {
111114
return descriptionString;
112115
}
113116

117+
public Set<CourseCategory> getCategories() {
118+
return categories;
119+
}
120+
114121
@Override
115122
public final String getSystemFullSectionIdString() {
116123
return facultyTreeNode.getRootCampus().getAbbreviation()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.dvf.ucst.core.courseutils.categorymatchers;
2+
3+
/**
4+
*
5+
*/
6+
class CategoryLanguage {
7+
8+
/*
9+
@ naomi. delete this when you don't need it anymore.
10+
A [Predicate] is a function that takes an object and returns a boolean value.
11+
in java, the "::" operator is used to refer to a function/method as an object.
12+
so I could have written this as:
13+
14+
static Predicate<String> CHECK_OBJECT = new Predicate<String>() {
15+
return false;
16+
}
17+
18+
or also in lambda-style shorthand as:
19+
20+
static Predicate<String> CHECK_OBJECT = (courseCode) -> {
21+
return false;
22+
}
23+
24+
and in either of these cases, in CourseCategory.java, it would instead say
25+
26+
LANGUAGE (CategoryLanguage.CHECK_OBJECT)
27+
28+
*/
29+
static boolean CHECK(final String courseCode) {
30+
return false; // TODO:
31+
}
32+
33+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.dvf.ucst.core.courseutils.categorymatchers;
2+
3+
import com.dvf.ucst.utils.xml.MalformedXmlDataException;
4+
import com.dvf.ucst.utils.xml.XmlUtils;
5+
import org.w3c.dom.Attr;
6+
7+
import java.util.function.Predicate;
8+
9+
/**
10+
* the [isCourseInCategoryCheckerFunction] will be defined in its own file in the
11+
* [categorymatchers] package.
12+
*/
13+
public enum CourseCategory implements XmlUtils.XmlConstant {
14+
LANGUAGE (CategoryLanguage::CHECK),
15+
// more things will go here as we find them.
16+
;
17+
private final Predicate<String> isCourseInCategoryCheckerFunction;
18+
19+
CourseCategory(final Predicate<String> isCourseInCategoryCheckerFunction) {
20+
this.isCourseInCategoryCheckerFunction = isCourseInCategoryCheckerFunction;
21+
}
22+
23+
public boolean isCourseInThisCategory(final String courseCode) {
24+
return isCourseInCategoryCheckerFunction.test(courseCode);
25+
}
26+
27+
@Override
28+
public String getXmlConstantValue() {
29+
/*
30+
@ naomi, delete this comment after reading.
31+
this is the enum's name in the source code.
32+
for example, the above enum "LANGUAGE" will return "LANGUAGE".
33+
we could define names for each one but there's not much point:
34+
this string will only be seen in xml files and this works well
35+
because we'll be sure that no names are the same (would mess up
36+
decoding the xml) (enums of the same type must have different
37+
names in the source code).
38+
*/
39+
return name();
40+
}
41+
42+
/**
43+
*
44+
* @param attr Must not be [null].
45+
* @return The [CourseCategory] whose [::getXmlConstantValue] equals [xmlAttrValue.getValue()].
46+
* @throws MalformedXmlDataException If no matching [CourseCategory] could be found.
47+
*/
48+
public static CourseCategory decodeXmlAttr(final Attr attr) throws MalformedXmlDataException {
49+
// there may be ways to make this perform better (using a static map from
50+
// enum names to themselves). for now this is ok!
51+
for (final CourseCategory category : CourseCategory.values()) {
52+
if (category.getXmlConstantValue().equals(attr.getValue())) {
53+
return category;
54+
}
55+
}
56+
throw MalformedXmlDataException.invalidAttrVal(attr);
57+
}
58+
59+
}

0 commit comments

Comments
 (0)