WIP Resource event handling
[aquarium] / src / main / scala / gr / grnet / aquarium / computation / data / RoleHistory.scala
1 /*
2  * Copyright 2011-2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
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.
16  *
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.
29  *
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.
34  */
35
36 package gr.grnet.aquarium.computation.data
37
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
42
43 /**
44  *
45  * @author Christos KK Loverdos <loverdos@gmail.com>
46  */
47
48 case class RoleHistory(
49                         /**
50                          * The head role is the most recent. The same rule applies for the tail.
51                          */
52                        roles: List[RoleHistoryItem]) {
53
54   def roleNamesByTimeslot: SortedMap[Timeslot, String] = {
55     TreeMap(roles.map(role ⇒ (role.timeslot, role.name)): _*)
56   }
57
58   def rolesByTimeslot: SortedMap[Timeslot, RoleHistoryItem] = {
59     TreeMap(roles.map(role ⇒ (role.timeslot, role)): _*)
60   }
61
62   def copyWithRole(role: String, validFrom: Long) = {
63     val newItems = roles match {
64       case Nil ⇒
65         RoleHistoryItem(role, validFrom) :: Nil
66
67       case head :: tail ⇒
68         if(head.isStrictlyAfter(validFrom)) {
69           // must search history items to find where this fits in
70           @tailrec
71           def check(allChecked: ListBuffer[RoleHistoryItem],
72                     lastCheck: RoleHistoryItem,
73                     toCheck: List[RoleHistoryItem]): List[RoleHistoryItem] = {
74
75             toCheck match {
76               case Nil ⇒
77                 allChecked.append(RoleHistoryItem(role, validFrom, lastCheck.validFrom))
78                 allChecked.toList
79
80               case toCheckHead :: toCheckTail ⇒
81                 if(toCheckHead.isStrictlyAfter(validFrom)) {
82                   allChecked.append(toCheckHead)
83
84                   check(allChecked, toCheckHead, toCheckTail)
85                 } else {
86                   allChecked.append(RoleHistoryItem(role, validFrom, lastCheck.validFrom))
87                   allChecked.toList
88                 }
89             }
90           }
91
92           val buffer = new ListBuffer[RoleHistoryItem]
93           buffer.append(head)
94           check(buffer, head, tail)
95         } else {
96           // assume head.validTo goes to infinity,
97           RoleHistoryItem(role, validFrom) :: head.copyWithValidTo(validFrom) :: tail
98         }
99     }
100
101     RoleHistory(newItems)
102   }
103
104   /**
105    * Returns the first, chronologically, role.
106    */
107   def firstRole: Option[RoleHistoryItem] = {
108     rolesByTimeslot.valuesIterator.toList.lastOption
109   }
110
111   /**
112    * Returns the name of the first, chronologically, role.
113    */
114   def firstRoleName: Option[String] = {
115     roleNamesByTimeslot.valuesIterator.toList.lastOption
116   }
117
118   /**
119    * Returns the last, chronologically, role.
120    */
121   def lastRole: Option[RoleHistoryItem] = {
122     rolesByTimeslot.valuesIterator.toList.headOption
123   }
124
125   /**
126    * Returns the name of the last, chronologically, role.
127    */
128   def lastRoleName: Option[String] = {
129     roleNamesByTimeslot.valuesIterator.toList.headOption
130   }
131 }
132
133 object RoleHistory {
134   final val Empty = RoleHistory(Nil)
135
136   def initial(role: String, validFrom: Long): RoleHistory = {
137     RoleHistory(RoleHistoryItem(role, validFrom) :: Nil)
138   }
139 }