Factoring out common code
authorChristos KK Loverdos <loverdos@gmail.com>
Mon, 6 Feb 2012 13:56:45 +0000 (15:56 +0200)
committerChristos KK Loverdos <loverdos@gmail.com>
Mon, 6 Feb 2012 13:56:45 +0000 (15:56 +0200)
src/main/scala/gr/grnet/aquarium/logic/events/ResourceEvent.scala

index 90ca8ef..de7e9b6 100644 (file)
@@ -126,18 +126,18 @@ case class ResourceEvent(
       )
     }
   }
-  /**
-   * Returns a beautiful string representation of the value.
-   *
-   * @param policy The policy to be asked for resources.
-   * @return A beautiful string representation of the value.
-   */
-  def beautifyValue(policy: DSLPolicy): String = {
-    policy.findResource(this.resource) match {
+
+  private[this]
+  def beatifyValue(resourceProvider: (String) ⇒ Option[DSLResource]): String = {
+    resourceProvider(this.resource) match {
       case Some(DSLComplexResource(_, _, OnOffCostPolicy, _)) ⇒
         OnOffPolicyResourceState(this.value).state.toUpperCase
+      case Some(rc @ DSLComplexResource(_, _, _, _)) ⇒
+        "%s [%s]".format(value, rc.unit)
       case Some(DSLSimpleResource(_, _, OnOffCostPolicy)) ⇒
         OnOffPolicyResourceState(this.value).state.toUpperCase
+      case Some(rc @ DSLSimpleResource(_, _, _)) ⇒
+        "%s [%s]".format(value, rc.unit)
       case _ ⇒
         value.toString
     }
@@ -146,22 +146,21 @@ case class ResourceEvent(
   /**
    * Returns a beautiful string representation of the value.
    *
+   * @param policy The policy to be asked for resources.
+   * @return A beautiful string representation of the value.
+   */
+  def beautifyValue(policy: DSLPolicy): String = {
+    beatifyValue(policy.findResource)
+  }
+
+  /**
+   * Returns a beautiful string representation of the value.
+   *
    * @param resourcesMap The resources map to be asked for resources.
    * @return A beautiful string representation of the value.
    */
   def beautifyValue(resourcesMap: DSLResourcesMap): String = {
-    resourcesMap.findResource(this.resource) match {
-      case Some(DSLComplexResource(_, _, OnOffCostPolicy, _)) ⇒
-        OnOffPolicyResourceState(this.value).state.toUpperCase
-      case Some(rc @ DSLComplexResource(_, _, _, _)) ⇒
-        "%s [%s]".format(value, rc.unit)
-      case Some(DSLSimpleResource(_, _, OnOffCostPolicy)) ⇒
-        OnOffPolicyResourceState(this.value).state.toUpperCase
-      case Some(rc @ DSLSimpleResource(_, _, _)) ⇒
-        "%s [%s]".format(value, rc.unit)
-      case _ ⇒
-        value.toString
-    }
+    beatifyValue(resourcesMap.findResource)
   }
 
   /**