diff options
Diffstat (limited to 'src/main/java/com/p4square/grow/model')
-rw-r--r-- | src/main/java/com/p4square/grow/model/Chapter.java | 10 | ||||
-rw-r--r-- | src/main/java/com/p4square/grow/model/Chapters.java | 46 | ||||
-rw-r--r-- | src/main/java/com/p4square/grow/model/Playlist.java | 31 | ||||
-rw-r--r-- | src/main/java/com/p4square/grow/model/Score.java | 4 |
4 files changed, 73 insertions, 18 deletions
diff --git a/src/main/java/com/p4square/grow/model/Chapter.java b/src/main/java/com/p4square/grow/model/Chapter.java index ac27de6..3306bf0 100644 --- a/src/main/java/com/p4square/grow/model/Chapter.java +++ b/src/main/java/com/p4square/grow/model/Chapter.java @@ -18,12 +18,12 @@ import com.fasterxml.jackson.annotation.JsonIgnore; * @author Jesse Morgan <jesse@jesterpm.net> */ public class Chapter implements Cloneable { - private String mName; + private Chapters mName; private Map<String, VideoRecord> mVideos; - public Chapter(String name) { + public Chapter(Chapters name) { mName = name; - mVideos = new HashMap<String, VideoRecord>(); + mVideos = new HashMap<>(); } /** @@ -36,7 +36,7 @@ public class Chapter implements Cloneable { /** * @return The Chapter name. */ - public String getName() { + public Chapters getName() { return mName; } @@ -45,7 +45,7 @@ public class Chapter implements Cloneable { * * @param name The name of the chapter. */ - public void setName(final String name) { + public void setName(final Chapters name) { mName = name; } diff --git a/src/main/java/com/p4square/grow/model/Chapters.java b/src/main/java/com/p4square/grow/model/Chapters.java new file mode 100644 index 0000000..175b6fb --- /dev/null +++ b/src/main/java/com/p4square/grow/model/Chapters.java @@ -0,0 +1,46 @@ +package com.p4square.grow.model; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +import java.util.Optional; + +/** + * The chapters of the training section. + */ +public enum Chapters { + INTRODUCTION, + SEEKER, + BELIEVER, + DISCIPLE, + TEACHER, + LEADER; + + /** + * A case-insensitive version of Chapters.valueOf(). + */ + @JsonCreator + public static Chapters fromString(String s) { + return valueOf(s.toUpperCase()); + } + + @JsonValue + public String identifier() { + return toString().toLowerCase(); + } + + /** + * Convert the Chapter to a score, if possible. + */ + public Optional<Double> toScore() { + switch (this) { + case SEEKER: + case BELIEVER: + case DISCIPLE: + case TEACHER: + return Optional.of(Score.numericScore(this.toString())); + default: + return Optional.empty(); + } + } +} diff --git a/src/main/java/com/p4square/grow/model/Playlist.java b/src/main/java/com/p4square/grow/model/Playlist.java index 3e77ada..201b3e2 100644 --- a/src/main/java/com/p4square/grow/model/Playlist.java +++ b/src/main/java/com/p4square/grow/model/Playlist.java @@ -21,7 +21,7 @@ public class Playlist { /** * Map of Chapter ID to map of Video ID to VideoRecord. */ - private Map<String, Chapter> mPlaylist; + private Map<Chapters, Chapter> mPlaylist; private Date mLastUpdated; @@ -29,7 +29,7 @@ public class Playlist { * Construct an empty playlist. */ public Playlist() { - mPlaylist = new HashMap<String, Chapter>(); + mPlaylist = new HashMap<>(); mLastUpdated = new Date(0); // Default to a prehistoric date if we don't have one. } @@ -82,7 +82,7 @@ public class Playlist { /** * Add a video to the playlist. */ - public VideoRecord add(String chapterId, String videoId) { + public VideoRecord add(Chapters chapterId, String videoId) { Chapter chapter = mPlaylist.get(chapterId); if (chapter == null) { @@ -100,17 +100,24 @@ public class Playlist { * @param chapterId The name of the chapter. * @param chapter The Chapter object to add. */ - @JsonAnySetter - public void addChapter(String chapterId, Chapter chapter) { + public void addChapter(Chapters chapterId, Chapter chapter) { chapter.setName(chapterId); mPlaylist.put(chapterId, chapter); } /** + * Variation of addChapter() with a String key for Jackson. + */ + @JsonAnySetter + private void addChapter(String chapterName, Chapter chapter) { + addChapter(Chapters.fromString(chapterName), chapter); + } + + /** * @return a map of chapter id to chapter. */ @JsonAnyGetter - public Map<String, Chapter> getChaptersMap() { + public Map<Chapters, Chapter> getChaptersMap() { return mPlaylist; } @@ -118,10 +125,10 @@ public class Playlist { * @return The last chapter to be completed. */ @JsonIgnore - public Map<String, Boolean> getChapterStatuses() { - Map<String, Boolean> completed = new HashMap<String, Boolean>(); + public Map<Chapters, Boolean> getChapterStatuses() { + Map<Chapters, Boolean> completed = new HashMap<>(); - for (Map.Entry<String, Chapter> entry : mPlaylist.entrySet()) { + for (Map.Entry<Chapters, Chapter> entry : mPlaylist.entrySet()) { completed.put(entry.getKey(), entry.getValue().isComplete()); } @@ -131,7 +138,7 @@ public class Playlist { /** * @return true if all required videos in the chapter have been watched. */ - public boolean isChapterComplete(String chapterId) { + public boolean isChapterComplete(Chapters chapterId) { Chapter chapter = mPlaylist.get(chapterId); if (chapter != null) { return chapter.isComplete(); @@ -152,8 +159,8 @@ public class Playlist { return; } - for (Map.Entry<String, Chapter> entry : source.getChaptersMap().entrySet()) { - String chapterName = entry.getKey(); + for (Map.Entry<Chapters, Chapter> entry : source.getChaptersMap().entrySet()) { + Chapters chapterName = entry.getKey(); Chapter theirChapter = entry.getValue(); Chapter myChapter = mPlaylist.get(entry.getKey()); diff --git a/src/main/java/com/p4square/grow/model/Score.java b/src/main/java/com/p4square/grow/model/Score.java index 031c309..c9bda83 100644 --- a/src/main/java/com/p4square/grow/model/Score.java +++ b/src/main/java/com/p4square/grow/model/Score.java @@ -15,6 +15,8 @@ public class Score { * * This method satisfies the invariant for Score x: * numericScore(x.toString()) <= x.getScore() + * + * @throws IllegalArgumentException if the string is not a score name. */ public static double numericScore(String score) { score = score.toLowerCase(); @@ -28,7 +30,7 @@ public class Score { } else if ("seeker".equals(score)) { return 0; } else { - return Integer.MAX_VALUE; + throw new IllegalArgumentException("Invalid score " + score); } } |