/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.livedata.normalization;

import org.fudgemsg.MutableFudgeMsg;

import com.opengamma.livedata.server.FieldHistoryStore;
import com.opengamma.util.ArgumentChecker;

/**
 * Multiplies the value of a {@code Double} field by a constant.
 */
public class UnitChange implements NormalizationRule {
  
  private final String _field;
  private final double _multiplier;
  
  public UnitChange(String field, double multiplier) {
    ArgumentChecker.notNull(field, "Field name");
    _field = field;
    _multiplier = multiplier;        
  }
  
  @Override
  public MutableFudgeMsg apply(MutableFudgeMsg msg, String securityUniqueId, FieldHistoryStore fieldHistory) {
    return multiplyField(msg, _field, _multiplier);
  }

  /*package*/ static MutableFudgeMsg multiplyField(MutableFudgeMsg msg, String field, double multiplier) {
    Double value = msg.getDouble(field);
    if (value != null) {
      double newValue = value * multiplier;
      msg.remove(field);
      msg.add(field, newValue);
    }
    return msg;
  }
  
}

@@ -178,16 +178,39 @@
 ation;%0A%0A
+import java.util.Set;%0A%0A
 import o
@@ -239,16 +239,63 @@
 geMsg;%0A%0A
+import com.google.common.collect.ImmutableSet;%0A
 import c
@@ -537,21 +537,27 @@
 nal 
+Set%3C
 String
+%3E
  _field
+s
 ;%0A  
@@ -712,16 +712,34 @@
 ield
+s
  = 
+ImmutableSet.of(
 field
+)
 ;%0A  
@@ -781,16 +781,374 @@
 %0A  %7D%0A  %0A
+  public UnitChange(Set%3CString%3E fields, double multiplier) %7B%0A    ArgumentChecker.notNull(fields, %22Field names%22);%0A    _fields = fields;%0A    _multiplier = multiplier;%0A  %7D%0A  %0A  public UnitChange(double multiplier, String... fields) %7B%0A    ArgumentChecker.notNull(fields, %22fields%22);%0A    _fields = ImmutableSet.copyOf(fields);%0A    _multiplier = multiplier;%0A  %7D%0A  %0A
   @Overr
@@ -1286,16 +1286,17 @@
 plyField
+s
 (msg, _f
@@ -1299,16 +1299,17 @@
 , _field
+s
 , _multi
@@ -1327,19 +1327,15 @@
 %0A%0A  
-/*package*/
+private
  sta
@@ -1367,16 +1367,17 @@
 plyField
+s
 (Mutable
@@ -1390,28 +1390,34 @@
 sg msg, 
+Set%3C
 String
+%3E
  field
+s
 , double
@@ -1427,24 +1427,60 @@
 ltiplier) %7B%0A
+    for (String field : fields) %7B%0A  
     Double v
@@ -1512,16 +1512,18 @@
 d);%0A    
+  
 if (valu
@@ -1541,16 +1541,18 @@
 %7B%0A      
+ 
+ 
 double n
@@ -1583,24 +1583,26 @@
 lier;%0A      
+  
 msg.remove(f
@@ -1610,24 +1610,26 @@
 eld);%0A      
+ 
+ 
 msg.add(fiel
@@ -1642,16 +1642,24 @@
 Value);%0A
+      %7D%0A
     %7D%0A  

Support adjustment of multiple fields