Simplify and generalize cost policy var calculation
[aquarium] / src / main / scala / gr / grnet / aquarium / logic / accounting / dsl / DSLCostPolicy.scala
index d25d416..09912df 100644 (file)
@@ -75,7 +75,19 @@ abstract class DSLCostPolicy(val name: String, val vars: Set[DSLCostPolicyVar])
                    timeDelta: Double,
                    previousValue: Double,
                    currentValue: Double,
-                   unitPrice: Double): Map[DSLCostPolicyVar, Any]
+                   unitPrice: Double): Map[DSLCostPolicyVar, Any] = {
+
+    DSLCostPolicy.makeValueMapFor(
+      this,
+      costPolicyName,
+      totalCredits,
+      oldTotalAmount,
+      newTotalAmount,
+      timeDelta,
+      previousValue,
+      currentValue,
+      unitPrice)
+  }
 
   def isOnOff: Boolean = isNamed(DSLCostPolicyNames.onoff)
 
@@ -189,6 +201,30 @@ object DSLCostPolicy {
       }
     }
   }
+
+  def makeValueMapFor(costPolicy: DSLCostPolicy,
+                      costPolicyName: String,
+                      totalCredits: Double,
+                      oldTotalAmount: Double,
+                      newTotalAmount: Double,
+                      timeDelta: Double,
+                      previousValue: Double,
+                      currentValue: Double,
+                      unitPrice: Double): Map[DSLCostPolicyVar, Any] = {
+    val vars = costPolicy.vars
+    val map = scala.collection.mutable.Map[DSLCostPolicyVar, Any]()
+
+    if(vars.contains(DSLCostPolicyNameVar)) map += DSLCostPolicyNameVar -> costPolicyName
+    if(vars.contains(DSLTotalCreditsVar))   map += DSLTotalCreditsVar   -> totalCredits
+    if(vars.contains(DSLOldTotalAmountVar)) map += DSLOldTotalAmountVar -> oldTotalAmount
+    if(vars.contains(DSLNewTotalAmountVar)) map += DSLNewTotalAmountVar -> newTotalAmount
+    if(vars.contains(DSLTimeDeltaVar))      map += DSLTimeDeltaVar      -> timeDelta
+    if(vars.contains(DSLPreviousValueVar))  map += DSLPreviousValueVar  -> previousValue
+    if(vars.contains(DSLCurrentValueVar))   map += DSLCurrentValueVar   -> currentValue
+    if(vars.contains(DSLUnitPriceVar))      map += DSLUnitPriceVar      -> unitPrice
+
+    map.toMap
+  }
 }
 
 /**
@@ -200,19 +236,6 @@ object DSLCostPolicy {
 case object OnceCostPolicy
   extends DSLCostPolicy(DSLCostPolicyNames.once, Set(DSLCostPolicyNameVar, DSLCurrentValueVar)) {
 
-  def makeValueMap(costPolicyName: String,
-                   totalCredits: Double,
-                   oldTotalAmount: Double,
-                   newTotalAmount: Double,
-                   timeDelta: Double,
-                   previousValue: Double,
-                   currentValue: Double,
-                   unitPrice: Double): Map[DSLCostPolicyVar, Any] = {
-
-    Map(DSLCostPolicyNameVar -> costPolicyName,
-        DSLCurrentValueVar   -> currentValue)
-  }
-
   def needsImplicitCompanionAtEndOfBillingPeriod(eventValue: Double) = false
 
   def isBillableFirstEventBasedOnValue(eventValue: Double) = true
@@ -239,21 +262,6 @@ case object ContinuousCostPolicy
   extends DSLCostPolicy(DSLCostPolicyNames.continuous,
                         Set(DSLCostPolicyNameVar, DSLUnitPriceVar, DSLOldTotalAmountVar, DSLTimeDeltaVar)) {
 
-  def makeValueMap(costPolicyName: String,
-                   totalCredits: Double,
-                   oldTotalAmount: Double,
-                   newTotalAmount: Double,
-                   timeDelta: Double,
-                   previousValue: Double,
-                   currentValue: Double,
-                   unitPrice: Double): Map[DSLCostPolicyVar, Any] = {
-
-    Map(DSLCostPolicyNameVar -> costPolicyName,
-        DSLUnitPriceVar      -> unitPrice,
-        DSLOldTotalAmountVar -> oldTotalAmount,
-        DSLTimeDeltaVar      -> timeDelta)
-  }
-
   def computeNewAccumulatingAmount(oldAmount: Double, newEventValue: Double): Double = {
     oldAmount + newEventValue
   }
@@ -295,20 +303,6 @@ case object OnOffCostPolicy
   extends DSLCostPolicy(DSLCostPolicyNames.onoff,
                         Set(DSLUnitPriceVar, DSLTimeDeltaVar)) {
 
-  def makeValueMap(costPolicyName: String,
-                   totalCredits: Double,
-                   oldTotalAmount: Double,
-                   newTotalAmount: Double,
-                   timeDelta: Double,
-                   previousValue: Double,
-                   currentValue: Double,
-                   unitPrice: Double): Map[DSLCostPolicyVar, Any] = {
-
-    Map(DSLCostPolicyNameVar -> costPolicyName,
-        DSLUnitPriceVar -> unitPrice,
-        DSLTimeDeltaVar -> timeDelta)
-  }
-
   /**
    *
    * @param oldAmount is ignored
@@ -399,20 +393,6 @@ object OnOffCostPolicyValues {
 case object DiscreteCostPolicy extends DSLCostPolicy(DSLCostPolicyNames.discrete,
                                                      Set(DSLCostPolicyNameVar, DSLCurrentValueVar)) {
 
-  def makeValueMap(costPolicyName: String,
-                   totalCredits: Double,
-                   oldTotalAmount: Double,
-                   newTotalAmount: Double,
-                   timeDelta: Double,
-                   previousValue: Double,
-                   currentValue: Double,
-                   unitPrice: Double): Map[DSLCostPolicyVar, Any] = {
-
-    Map(DSLCostPolicyNameVar -> costPolicyName,
-        DSLUnitPriceVar      -> unitPrice,
-        DSLCurrentValueVar   -> currentValue)
-  }
-
   def computeNewAccumulatingAmount(oldAmount: Double, newEventValue: Double): Double = {
     oldAmount + newEventValue
   }