From: Christos KK Loverdos Date: Thu, 24 May 2012 10:44:31 +0000 (+0300) Subject: Enhance IMStateSnapshot X-Git-Url: https://code.grnet.gr/git/aquarium/commitdiff_plain/4c6da548cb1d4d55b74098d6fe1c3d522c61fb31 Enhance IMStateSnapshot --- diff --git a/src/main/scala/gr/grnet/aquarium/computation/data/IMStateSnapshot.scala b/src/main/scala/gr/grnet/aquarium/computation/data/IMStateSnapshot.scala index 4954c83..1b5064c 100644 --- a/src/main/scala/gr/grnet/aquarium/computation/data/IMStateSnapshot.scala +++ b/src/main/scala/gr/grnet/aquarium/computation/data/IMStateSnapshot.scala @@ -42,4 +42,27 @@ import gr.grnet.aquarium.event.model.im.IMEventModel * @author Christos KK Loverdos */ -case class IMStateSnapshot(imEvent: IMEventModel) +case class IMStateSnapshot( + /** + * This is the latest processed IMEvent + */ + latestIMEvent: IMEventModel, + + /** + * This is the recorded role history + */ + roleHistory: RoleHistory) { + + def addMostRecentEvent(newEvent: IMEventModel) = { + copy( + latestIMEvent = newEvent, + roleHistory = roleHistory.addMostRecentRole(newEvent.role, newEvent.occurredMillis) + ) + } +} + +object IMStateSnapshot { + def initial(imEvent: IMEventModel): IMStateSnapshot = { + IMStateSnapshot(imEvent, RoleHistory.initial(imEvent.role, imEvent.occurredMillis)) + } +} diff --git a/src/main/scala/gr/grnet/aquarium/computation/data/RoleHistory.scala b/src/main/scala/gr/grnet/aquarium/computation/data/RoleHistory.scala new file mode 100644 index 0000000..5a710e6 --- /dev/null +++ b/src/main/scala/gr/grnet/aquarium/computation/data/RoleHistory.scala @@ -0,0 +1,78 @@ +/* + * Copyright 2011-2012 GRNET S.A. All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * 1. Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and + * documentation are those of the authors and should not be + * interpreted as representing official policies, either expressed + * or implied, of GRNET S.A. + */ + +package gr.grnet.aquarium.computation.data + +import gr.grnet.aquarium.logic.accounting.dsl.Timeslot +import scala.collection.immutable.{TreeMap, SortedMap} + +/** + * + * @author Christos KK Loverdos + */ + +case class RoleHistory( + /** + * The head role is the most recent. The same rule applies for the tail. + */ + roles: List[RoleHistoryItem]) { + def rolesByTimeslot: SortedMap[Timeslot, String] = { + TreeMap(roles.map(role => (role.timeslot, role.name)): _*) + } + + /** + * Insert the new role in front of the other ones and adjust the `validTo` limit of the previous role. + */ + def addMostRecentRole(role: String, validFrom: Long) = { + val newItem = RoleHistoryItem(role, validFrom) + val newRoles = roles match { + case head :: tail ⇒ + newItem :: head.withNewValidTo(validFrom) :: tail + + case Nil ⇒ + newItem :: Nil + } + + RoleHistory(newRoles) + } +} + +object RoleHistory { + final val Empty = RoleHistory(Nil) + + def initial(role: String, validFrom: Long): RoleHistory = { + RoleHistory(RoleHistoryItem(role, validFrom) :: Nil) + } +} diff --git a/src/main/scala/gr/grnet/aquarium/computation/data/RoleHistoryItem.scala b/src/main/scala/gr/grnet/aquarium/computation/data/RoleHistoryItem.scala new file mode 100644 index 0000000..c617fb4 --- /dev/null +++ b/src/main/scala/gr/grnet/aquarium/computation/data/RoleHistoryItem.scala @@ -0,0 +1,72 @@ +/* + * Copyright 2011-2012 GRNET S.A. All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * 1. Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and + * documentation are those of the authors and should not be + * interpreted as representing official policies, either expressed + * or implied, of GRNET S.A. + */ + +package gr.grnet.aquarium.computation.data + +import gr.grnet.aquarium.util.date.MutableDateCalc +import gr.grnet.aquarium.logic.accounting.dsl.Timeslot + +/** + * + * @author Christos KK Loverdos + */ + +case class RoleHistoryItem( + /** + * The role name. + */ + name: String, + + /** + * Validity start time for this role. The time is inclusive. + */ + validFrom: Long, + + /** + * Validity stop time for this role. The time is exclusive. + */ + validTo: Long = Long.MaxValue) { + + require(validTo > validFrom) + require(!name.isEmpty) + + def timeslot = Timeslot(validFrom, validTo) + + def withNewValidTo(newValidTo: Long) = copy(validTo = newValidTo) + + override def toString = + "RoleHistoryItem(%s, [%s, %s))". + format(name, new MutableDateCalc(validFrom), new MutableDateCalc(validTo)) +}