2 * Copyright 2011-2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
36 package gr.grnet.aquarium.logic.accounting.dsl
39 import scala.collection.mutable
40 import annotation.tailrec
41 import gr.grnet.aquarium.util.date.MutableDateCalc
44 * A representation of a timeslot with a start and end date.
46 * @author Georgios Gousios <gousiosg@gmail.com>
48 final case class Timeslot(from: Date, to: Date)
49 extends DSLItem with Ordered[Timeslot] {
51 /* Preconditions to ensure correct object creations */
54 assert(start <= end, "from = %s, to = %s".format(new MutableDateCalc(from), new MutableDateCalc(to)))
56 def startsBefore(t: Timeslot) : Boolean = start < t.start
58 def startsAfter(t: Timeslot) : Boolean = start > t.start
60 def endsBefore(t: Timeslot) : Boolean = end < t.end
62 def endsAfter(t: Timeslot) : Boolean = end > t.end
64 def after(t: Timeslot): Boolean = start > t.end
66 def before(t: Timeslot): Boolean = end < t.start
68 def start : Long = this.from.getTime
70 def end : Long = this.to.getTime
73 * Check whether this time slot fully contains the provided one.
75 def contains(t: Timeslot) : Boolean = this.start <= t.start && this.end >= t.end
78 def containsTimeInMillis(millis: Long) = start <= millis && millis <= end
82 * Check whether this timeslot contains the provided time instant.
84 private[dsl] def includes(t: Date) : Boolean = start <= t.getTime && t.getTime <= end
88 * Check whether this timeslot overlaps with the provided one.
90 def overlaps(t: Timeslot) : Boolean =
91 contains(t) || t.contains(this) || this.includes(t.from) || this.includes(t.to)
95 * Merges this timeslot with the provided one. If the timeslots overlap,
96 * a list with the resulting merge is returned. If the timeslots do not
97 * overlap, the returned list contains both timeslots in increasing start
100 def merge(t: Timeslot) : Timeslot = {
101 assert(overlaps(t),this +" has no overlap with " + t)
102 val nfrom = if (start < t.start) from else t.from
103 val nto = if (end > t.end) to else t.to
108 * Split the timeslot in two parts at the provided timestamp, if the
109 * timestamp falls within the timeslot boundaries.
111 def slice(d: Date) : List[Timeslot] =
112 if (includes(d) && d.getTime != start && d.getTime != end)
113 List(Timeslot(from, d), Timeslot(d,to))
119 * Find and return the timeslots within which this Timeslot overrides
120 * with the provided list of timeslots. For example if:
122 * - `this == Timeslot(7,20)`
123 * - `list == List(Timeslot(1,3), Timeslot(6,8), Timeslot(11,15))`
125 * the result will be: `List(Timeslot(7,8), Timeslot(11,15))`
127 def overlappingTimeslots(list: List[Timeslot]) : List[Timeslot] =
128 list.foldLeft(List[Timeslot]()) { (ret,t) =>
129 if (t.contains(this)) this :: ret
130 else if (this.contains(t)) t :: ret
131 else if (t.overlaps(this) && t.startsBefore(this)) slice(t.to).head :: ret
132 else if (t.overlaps(this) && t.startsAfter(this)) slice(t.from).last :: ret
138 * Find and return the timeslots whithin which this Timeslot does not
139 * override with the provided list of timeslots. For example if:
141 * - `this == Timeslot(7,20)`
142 * - `list == List(Timeslot(1,3), Timeslot(6,8), Timeslot(11,15))`
144 * the result will be: `List(Timeslot(9,10), Timeslot(15,20))`
146 def nonOverlappingTimeslots(list: List[Timeslot]): List[Timeslot] =
147 overlappingTimeslots(list) sortWith {_.start < _.start} match {
148 case Nil => List(this)
150 val (head,last) = (over.head,over.last)
151 val hd = if (head.start > this.start) List(Timeslot(this.from, head.from)) else List()
152 val tl = if (last.end < this.end) List(Timeslot(last.to, this.to)) else List()
153 hd ++ over.tail.foldLeft((List[Timeslot](),over.head)) {
154 case ((l,x),y) => (l ++ List(Timeslot(x.to, y.from)),y)
159 * Align a list of consecutive timeslots to the boundaries
160 * defined by this timeslot. Elements that do not overlap
161 * with this timeslot are rejected, while elements not
162 * contained in the timeslot are trimmed to this timeslot's
163 * start and end time.
165 def align(l: List[Timeslot]): List[Timeslot] = {
166 if (l.isEmpty) return List()
168 val result : Option[Timeslot] =
169 if (!this.overlaps(l.head)) None
170 else if (l.head.contains(this)) Some(this)
171 else if (l.head.startsBefore(this)) Some(Timeslot(this.from, l.head.to))
172 else if (l.head.endsAfter(this)) Some(Timeslot(l.head.from, this.to))
176 case Some(x) => x :: align(l.tail)
177 case None => align(l.tail)
181 /* align a time slot in "bound_size" boundaries so that
182 * start0 <= start and end0 >= end */
183 def align(bound_size : Long) : Timeslot = {
184 val start0 = (start / bound_size) * bound_size
185 val add_one = if (end % bound_size == 0) 0 else 1
186 val end0 = (end / bound_size + add_one) * bound_size
187 Timeslot(start0,end0)
190 /* returns true when the start and end address are
191 * multiples of bound_size*/
192 def isAligned(bound_size : Long) : Boolean =
193 start % bound_size == 0 && end % bound_size == 0
197 * Compares the starting times of two timeslots.
199 def compare(that: Timeslot): Int = {
200 if (this.startsBefore(that)) -1
201 else if (this.startsAfter(that)) 1
206 * Converts the timeslot to the amount of hours it represents
208 def hours: Double = (to.getTime - from.getTime).toDouble / 1000.0 / 60.0 / 60.0
210 def deltaMillis = to.getTime - from.getTime
213 def myString : String = "Timeslot(" + this.start + "," + this.end + ")"
214 //override def toString() = myString
215 override def toString() =
218 def toDateString = "Timeslot(%s, %s)".format(new MutableDateCalc(from), new MutableDateCalc(to))
219 def toISODateString = "Timeslot(%s, %s)".format(new MutableDateCalc(from).toISOString, new MutableDateCalc(to).toISOString)
223 def apply(x: Long, y: Long): Timeslot =
224 new Timeslot(new Date(x), new Date(y))
226 def apply(x: Int, y: Int): Timeslot =
227 new Timeslot(new Date(x), new Date(y))
229 def mergeOverlaps(list: List[Timeslot]): List[Timeslot] = {
230 def sorter(x: Timeslot, y: Timeslot) : Boolean = y.from after x.from
231 (list sortWith sorter).foldLeft(List[Timeslot]()) {
235 if (hd overlaps b) (hd merge b)::Nil
237 case (a @ hd::tl,b) =>
238 if(hd overlaps b) (hd merge b)::tl