Created TimelineEventProcessor. Tested existing implementation.

This commit is contained in:
Jonathan Bernard
2011-01-10 18:03:19 -06:00
parent 68b51640af
commit 6de924927a
11 changed files with 290 additions and 50 deletions

View File

@ -7,9 +7,9 @@ import org.joda.time.Duration
*/
public abstract class Category implements Comparable<Category> {
List<Entry> entries
List<Category> categories
String description
public List<Entry> entries
public List<Category> categories
public String description
public Category() {
entries = []
@ -23,7 +23,7 @@ public abstract class Category implements Comparable<Category> {
Entry entry
// see if we have a subcategory that will hold this event
entry = addToSubCategory(e)
entry = addToSubcategory(e)
if (!entry) {
// if not, do we have another entry that could be grouped with
@ -34,15 +34,16 @@ public abstract class Category implements Comparable<Category> {
// yes
if (existingEntry) {
// create the new category
category = new DescriptionBasedCategory(e.description)
def category = new DescriptionBasedCategory(e.description)
// add the new event to the category
entry = category.addEvent(e)
// 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)
existingEntry.category = category
// add the category to our list of subcategories
categories << category
@ -69,11 +70,14 @@ public abstract class Category implements Comparable<Category> {
}
public Duration getDuration() {
return categories.sum { it.duration } + entries.sum { it.duration }
return categories.sum(new Duration(0)) { it.duration } +
entries.sum(new Duration(0)) { it.duration }
}
public int compareTo(Category other) {
return this.getDuration().compareTo(other.getDuration())
}
public String toString() { return description }
}

View File

@ -26,7 +26,10 @@ public class Entry {
this.notes = notes;
this.start = start;
this.duration = duration;
this.category = category;
this.category = c;
}
public String toString() {
return category.description + ": " + description;
}
}

View File

@ -2,6 +2,8 @@ package com.jdbernard.timeanalyzer;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.format.PeriodFormat;
import org.joda.time.format.PeriodFormatter;
public class Event {
@ -10,4 +12,11 @@ public class Event {
public DateTime start;
public Duration duration;
public static PeriodFormatter periodFormatter = PeriodFormat.getDefault();
public String toString() {
return description;
}
}

View File

@ -1,6 +1,6 @@
package com.jdbernard.timeanalyzer
public class GeneralCategory {
public class GeneralCategory extends Category {
public GeneralCategory() {
super()

View File

@ -0,0 +1,46 @@
package com.jdbernard.timeanalyzer
import com.jdbernard.timestamper.core.Timeline
import org.joda.time.DateTime
import org.joda.time.Duration
public class TimelineEventProcessor {
/** Events whose description matches one of these regex strings or
* patterns are ignored. */
List exclusions = []
public TimelineEventProcessor() {}
public TimelineEventProcessor(List exclusions) {
this.exclusions = exclusions
}
public List<Event> process(Timeline timeline) {
List<Event> events = []
timeline.each { marker ->
Event e = new Event()
e.description = marker.mark
e.notes = marker.notes
e.start = new DateTime(marker.timestamp)
e.duration = new Duration(0)
// if this is not the first event, then we need to update the
// duration of the previous event
if (events.size > 0) {
Event lastEvent = events[-1]
lastEvent.duration = new Duration(lastEvent.start, e.start)
}
events << e
}
def excluded = events.findAll { event ->
exclusions.any { exclusion -> event.description ==~ exclusion }
}
return events - excluded
}
}