2011-01-09 15:11:36 -06:00
|
|
|
package com.jdbernard.timeanalyzer
|
|
|
|
|
2011-01-10 00:24:08 -06:00
|
|
|
import org.joda.time.Duration
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A category represents a collection of like events.
|
|
|
|
*/
|
|
|
|
public abstract class Category implements Comparable<Category> {
|
2011-01-09 15:11:36 -06:00
|
|
|
|
2011-01-10 18:03:19 -06:00
|
|
|
public List<Entry> entries
|
|
|
|
public List<Category> categories
|
2011-01-12 06:54:03 -06:00
|
|
|
public List<CategorizationPlan> categorizationPlans
|
2011-01-10 18:03:19 -06:00
|
|
|
public String description
|
2011-01-10 00:24:08 -06:00
|
|
|
|
|
|
|
public Category() {
|
|
|
|
entries = []
|
|
|
|
categories = []
|
2011-01-12 06:54:03 -06:00
|
|
|
categorizationPlans = []
|
2011-01-10 00:24:08 -06:00
|
|
|
description = "Unnamed Category"
|
|
|
|
}
|
2011-01-09 15:11:36 -06:00
|
|
|
|
2011-01-12 17:21:39 -06:00
|
|
|
public Category(String description) {
|
|
|
|
entries = []
|
|
|
|
categories = []
|
|
|
|
categorizationPlans = []
|
|
|
|
this.description = description
|
|
|
|
}
|
|
|
|
|
2011-01-09 15:11:36 -06:00
|
|
|
public abstract boolean matchesEvent(Event e)
|
|
|
|
|
2011-01-10 00:24:08 -06:00
|
|
|
public Entry addEvent(Event e) {
|
|
|
|
Entry entry
|
|
|
|
|
2011-01-12 17:21:39 -06:00
|
|
|
// see if we have or can create a subcategory that will hold this event
|
2011-01-10 18:03:19 -06:00
|
|
|
entry = addToSubcategory(e)
|
2011-01-10 00:24:08 -06:00
|
|
|
|
2011-01-12 17:21:39 -06:00
|
|
|
// no, let's create a generic entry and add it
|
2011-01-10 00:24:08 -06:00
|
|
|
if (!entry) {
|
2011-01-12 17:21:39 -06:00
|
|
|
entry = new Entry(this, e)
|
|
|
|
entries << entry
|
2011-01-10 00:24:08 -06:00
|
|
|
}
|
2011-01-09 15:11:36 -06:00
|
|
|
|
2011-01-10 00:24:08 -06:00
|
|
|
return entry
|
2011-01-09 15:11:36 -06:00
|
|
|
}
|
2011-01-10 00:24:08 -06:00
|
|
|
|
|
|
|
public Entry addToSubcategory(Event e) {
|
|
|
|
|
2011-01-12 06:54:03 -06:00
|
|
|
// find all matching subcategories
|
|
|
|
def matchingCategories = categories.findAll { it.matchesEvent(e) }
|
2011-01-10 00:24:08 -06:00
|
|
|
|
2011-01-12 06:54:03 -06:00
|
|
|
if (matchingCategories)
|
2011-01-10 00:24:08 -06:00
|
|
|
return matchingCategories[0].addEvent(e)
|
2011-01-12 06:54:03 -06:00
|
|
|
|
|
|
|
// 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
|
2011-01-12 17:21:39 -06:00
|
|
|
def newCategory = matchingPlans[0].newCategory(e, entries)
|
2011-01-12 06:54:03 -06:00
|
|
|
|
|
|
|
// add it to our list of cateogries
|
2011-01-12 17:21:39 -06:00
|
|
|
categories << newCategory
|
|
|
|
|
|
|
|
// add the new event to the category
|
|
|
|
def entry = newCategory.addEvent(e)
|
|
|
|
|
|
|
|
// move all the entries that match the new category over
|
|
|
|
def existingEntries = matchingPlans[0].findEntriesToRecategorize(e, entries)
|
|
|
|
entries -= existingEntries
|
|
|
|
newCategory.entries.addAll(existingEntries)
|
|
|
|
existingEntries.each { it.category = newCategory }
|
|
|
|
|
|
|
|
// return the new entry
|
|
|
|
return entry
|
2011-01-12 06:54:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
2011-01-10 00:24:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public Duration getDuration() {
|
2011-01-10 18:03:19 -06:00
|
|
|
return categories.sum(new Duration(0)) { it.duration } +
|
|
|
|
entries.sum(new Duration(0)) { it.duration }
|
2011-01-10 00:24:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public int compareTo(Category other) {
|
|
|
|
return this.getDuration().compareTo(other.getDuration())
|
|
|
|
}
|
|
|
|
|
2011-01-10 18:03:19 -06:00
|
|
|
public String toString() { return description }
|
|
|
|
|
2011-01-09 15:11:36 -06:00
|
|
|
}
|