diff --git a/src/main/com/jdbernard/timeanalyzer/CategorizationPlan.java b/src/main/com/jdbernard/timeanalyzer/CategorizationPlan.java new file mode 100644 index 0000000..36184d1 --- /dev/null +++ b/src/main/com/jdbernard/timeanalyzer/CategorizationPlan.java @@ -0,0 +1,9 @@ +package com.jdbernard.timeanalyzer; + +import java.util.List; +import java.util.Map; + +public interface CategorizationPlan { + boolean deservesNewCategory(Event event, List existingEntries); + Map createNewCategory(Event event, List existingEntries); +} diff --git a/src/main/com/jdbernard/timeanalyzer/Category.groovy b/src/main/com/jdbernard/timeanalyzer/Category.groovy index d2d3832..5d2d2cb 100644 --- a/src/main/com/jdbernard/timeanalyzer/Category.groovy +++ b/src/main/com/jdbernard/timeanalyzer/Category.groovy @@ -9,11 +9,13 @@ public abstract class Category implements Comparable { public List entries public List categories + public List categorizationPlans public String description public Category() { entries = [] categories = [] + categorizationPlans = [] description = "Unnamed Category" } @@ -26,6 +28,8 @@ public abstract class Category implements Comparable { entry = addToSubcategory(e) if (!entry) { + // if not, do we have a plan for categorizing this event? + // if not, do we have another entry that could be grouped with // this one to create a new category, based on the description? def existingEntry = entries.find @@ -61,14 +65,33 @@ public abstract class Category implements Comparable { public Entry addToSubcategory(Event e) { - // find all matching subcategories - def matchingCategories = categories.findAll { it.matchesEvent(e) } + // find all matching subcategories + def matchingCategories = categories.findAll { it.matchesEvent(e) } - if (matchingCategories) + if (matchingCategories) return matchingCategories[0].addEvent(e) - return null + + // no matching subcategories, can we create a new one based on one + // of our plans? + def matchingPlans = categorizationPlans.findAll + { it.deservesNewCategory(e, entries) } + + if (matchingPlans) { + // create the new category + def result = matchingPlans.createNewCategory(e, entries) + + // add it to our list of cateogries + categories << result.category + + // return new entry + return result.entry + } + + return null } + public Entry + public Duration getDuration() { return categories.sum(new Duration(0)) { it.duration } + entries.sum(new Duration(0)) { it.duration }