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.computation.data
38 import gr.grnet.aquarium.logic.accounting.dsl.Timeslot
39 import scala.collection.immutable.{TreeMap, SortedMap}
40 import scala.collection.mutable.ListBuffer
41 import scala.annotation.tailrec
45 * @author Christos KK Loverdos <loverdos@gmail.com>
48 case class RoleHistory(
50 * The head role is the most recent. The same rule applies for the tail.
52 roles: List[RoleHistoryItem]) {
54 def roleNamesByTimeslot: SortedMap[Timeslot, String] = {
55 TreeMap(roles.map(role ⇒ (role.timeslot, role.name)): _*)
58 def rolesByTimeslot: SortedMap[Timeslot, RoleHistoryItem] = {
59 TreeMap(roles.map(role ⇒ (role.timeslot, role)): _*)
62 def updateWithRole(role: String, validFrom: Long) = {
63 def fixValidTo(validFrom: Long, validTo: Long): Long = {
64 if(validTo == validFrom) {
65 // Since validTo is exclusive, make at least 1ms gap
72 val newItems = roles match {
74 RoleHistoryItem(role, validFrom) :: Nil
77 if(head.startsStrictlyAfter(validFrom)) {
78 // must search history items to find where this fits in
80 def check(allChecked: ListBuffer[RoleHistoryItem],
81 lastCheck: RoleHistoryItem,
82 toCheck: List[RoleHistoryItem]): List[RoleHistoryItem] = {
86 allChecked.append(RoleHistoryItem(role, validFrom, fixValidTo(validFrom, lastCheck.validFrom)))
89 case toCheckHead :: toCheckTail ⇒
90 if(toCheckHead.startsStrictlyAfter(validFrom)) {
91 allChecked.append(toCheckHead)
93 check(allChecked, toCheckHead, toCheckTail)
95 allChecked.append(RoleHistoryItem(role, validFrom, fixValidTo(validFrom, lastCheck.validFrom)))
101 val buffer = new ListBuffer[RoleHistoryItem]
103 check(buffer, head, tail)
105 // assume head.validTo goes to infinity,
106 RoleHistoryItem(role, validFrom) :: head.copyWithValidTo(fixValidTo(head.validFrom, validFrom)) :: tail
110 RoleHistory(newItems)
114 * Returns the first, chronologically, role.
116 def firstRole: Option[RoleHistoryItem] = {
117 rolesByTimeslot.valuesIterator.toList.lastOption
121 * Returns the name of the first, chronologically, role.
123 def firstRoleName: Option[String] = {
124 roleNamesByTimeslot.valuesIterator.toList.lastOption
128 * Returns the last, chronologically, role.
130 def lastRole: Option[RoleHistoryItem] = {
131 rolesByTimeslot.valuesIterator.toList.headOption
135 * Returns the name of the last, chronologically, role.
137 def lastRoleName: Option[String] = {
138 roleNamesByTimeslot.valuesIterator.toList.headOption
143 final val Empty = RoleHistory(Nil)
145 def initial(role: String, validFrom: Long): RoleHistory = {
146 RoleHistory(RoleHistoryItem(role, validFrom) :: Nil)