}
/**
+ * Merges overlapping timeslots. The merge is exhaustive only if the
+ * provided list is sorted in increasing timeslot from order.
+ */
+ def mergeOverlaps(list: List[Timeslot]): List[Timeslot] = {
+ list.foldLeft(List[Timeslot]()) {
+ (a, b) =>
+ if (a.isEmpty)
+ List(b)
+ else if (a.tail.isEmpty)
+ a.head.merge(b)
+ else {
+ val merged = a.tail.head.merge(b)
+ a ++ (if (merged.size == 1) merged else List(b))
+ }
+ }
+ }
+
+ /**
* Get a list of all timeslots within which the provided time frame
* is effective.
*/
def allEffectiveTimeslots(spec: DSLTimeFrame):
List[Timeslot] = {
- spec.repeat.flatMap {
+ val l = spec.repeat.flatMap {
r => effectiveTimeslots(r, spec.from, spec.to)
} sortWith sorter
+ mergeOverlaps(l)
}
/**
def allEffectiveTimeslots(spec: DSLTimeFrame, from: Date, to: Date):
List[Timeslot] = {
- spec.repeat.flatMap {
+ val l = spec.repeat.flatMap {
r => effectiveTimeslots(r, from, Some(to))
} sortWith sorter
+ mergeOverlaps(l)
}
/**
}
@Test
+ def testMergeOverlaps = {
+ var l = List(Timeslot(new Date(12345000), new Date(13345000)),
+ Timeslot(new Date(12845000), new Date(13845000)))
+
+ var result = mergeOverlaps(l)
+ assertEquals(1, result.size)
+ assertEquals(Timeslot(new Date(12345000), new Date(13845000)), result.head)
+
+ l = l ++ List(Timeslot(new Date(13645000), new Date(14845000)))
+ result = mergeOverlaps(l)
+ assertEquals(1, result.size)
+ assertEquals(Timeslot(new Date(12345000), new Date(14845000)), result.head)
+
+ l = l ++ List(Timeslot(new Date(15845000), new Date(16845000)))
+ result = mergeOverlaps(l)
+ assertEquals(2, result.size)
+ assertEquals(Timeslot(new Date(12345000), new Date(14845000)), result.head)
+ assertEquals(Timeslot(new Date(15845000), new Date(16845000)), result.tail.head)
+ }
+
+ @Test
def testEffectiveTimeslots = {
val from = new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011
val to = new Date(1324214719000L) //Sun Dec 18 15:25:19 +0200 2011
testSuccessiveTimeslots(result.tail)
}
- private def printTimeslots(result: List[(Date, Date)]) = {
- result.foreach(p => print("from:%s to:%s\n".format(p._1, p._2)))
+ private def printTimeslots(result: List[Timeslot]) = {
+ result.foreach(p => print("from:%s to:%s\n".format(p.from, p.to)))
}
}
\ No newline at end of file