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
|
|
|
|
|
|
|
List<Entry> entries
|
2011-01-10 00:24:08 -06:00
|
|
|
List<Category> categories
|
|
|
|
String description
|
|
|
|
|
|
|
|
public Category() {
|
|
|
|
entries = []
|
|
|
|
categories = []
|
|
|
|
description = "Unnamed Category"
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
// see if we have a subcategory that will hold this event
|
|
|
|
entry = addToSubCategory(e)
|
|
|
|
|
|
|
|
if (!entry) {
|
|
|
|
// 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
|
|
|
|
{ it.description == e.description }
|
|
|
|
|
|
|
|
// yes
|
|
|
|
if (existingEntry) {
|
|
|
|
// create the new category
|
|
|
|
category = new DescriptionBasedCategory(e.description)
|
|
|
|
|
|
|
|
// remove the existing entry from this category and
|
|
|
|
// add it to the subcategory
|
|
|
|
this.entries -= existingEntry
|
|
|
|
category.entries << existingEntry
|
|
|
|
|
|
|
|
// add the new event to the category
|
|
|
|
entry = category.addEvent(e)
|
|
|
|
|
|
|
|
// add the category to our list of subcategories
|
|
|
|
categories << category
|
|
|
|
}
|
2011-01-09 15:11:36 -06:00
|
|
|
|
2011-01-10 00:24:08 -06:00
|
|
|
// no, let's create a generic entry and add it
|
|
|
|
else {
|
|
|
|
entry = new Entry(this, e)
|
|
|
|
entries << entry
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
|
|
|
|
// find all matching subcategories
|
|
|
|
def matchingCategories = categories.findAll { it.matchesEvent(e) }
|
|
|
|
|
|
|
|
if (matchingCategories)
|
|
|
|
return matchingCategories[0].addEvent(e)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
public Duration getDuration() {
|
|
|
|
return categories.sum { it.duration } + entries.sum { it.duration }
|
|
|
|
}
|
|
|
|
|
|
|
|
public int compareTo(Category other) {
|
|
|
|
return this.getDuration().compareTo(other.getDuration())
|
|
|
|
}
|
|
|
|
|
2011-01-09 15:11:36 -06:00
|
|
|
}
|