Finised initial implementation. Need to test.

This commit is contained in:
Jonathan Bernard
2011-01-10 00:24:08 -06:00
parent 9f4008a775
commit 68b51640af
12 changed files with 242 additions and 78 deletions

View File

@ -1,15 +1,79 @@
package com.jdbernard.timeanalyzer
public abstract class Category implements Comparable<T extends Category> {
import org.joda.time.Duration
/**
* A category represents a collection of like events.
*/
public abstract class Category implements Comparable<Category> {
List<Entry> entries
List<Category> categories
String description
public Category() {
entries = []
categories = []
description = "Unnamed Category"
}
public abstract boolean matchesEvent(Event e)
public abstract Entry addEvent(Event e)
public Category() { entries = [] }
public Entry addEvent(Event e) {
Entry entry
public int compareTo(C other) {
// 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
}
// no, let's create a generic entry and add it
else {
entry = new Entry(this, e)
entries << entry
}
}
return entry
}
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())
}
}

View File

@ -0,0 +1,16 @@
package com.jdbernard.timeanalyzer
import org.joda.time.Duration
public class DescriptionBasedCategory extends Category {
public DescriptionBasedCategory(String description) {
super()
this.description = description
}
public boolean matchesEvent(Event e) {
return e.description == description
}
}

View File

@ -1,6 +1,32 @@
package com.jdbernard.timanalyzer;
package com.jdbernard.timeanalyzer;
public class Entry extends Event {
import org.joda.time.DateTime;
import org.joda.time.Duration;
public class Entry {
public String description;
public String notes;
public DateTime start;
public Duration duration;
public Category category;
public Entry(Category c, String description, DateTime start,
Duration duration) {
this(c, description, "", start, duration);
}
public Entry(Category c, Event e) {
this(c, e.description, e.notes, e.start, e.duration);
}
public Entry(Category c, String description, String notes, DateTime start,
Duration duration) {
this.description = description;
this.notes = notes;
this.start = start;
this.duration = duration;
this.category = category;
}
}

View File

@ -1,25 +0,0 @@
package com.jdbernard.timeanalyzer;
public abstract class EventTransformation {
private Category category;
public EventTransformation(Category cat) {
this.category = cat;
}
/**
* Determine if this transformation is applicable for a given event.
* @param e An event to match.
* @return *true* if this transformation is applicable to the given event,
* *false* otherwise.
*/
public abstract boolean matches(Event e);
/**
* Transform an entry.
* @param e The Event to transform.
* @return A new Entry.
*/
public abstract Entry transform(Event e);
}

View File

@ -1,21 +0,0 @@
package com.jdbernard.timeanalyzer
public class EventTransformer {
List transformations
public EventTransformer(List transformations) {
this.transformations = transformations
}
/**
* Transform an event. The first matching transformation is used.
* @param e The event to transform.
* @return The resulting Entry.
*/
public Entry transform(Event e) {
for (t : transformations)
if (t.matches(e))
return t.transform(t)
}
}

View File

@ -0,0 +1,12 @@
package com.jdbernard.timeanalyzer
public class GeneralCategory {
public GeneralCategory() {
super()
description = "General"
}
public boolean matchesEvent(Event e) { true }
}