package io.warp10.script.mapper;
  
  //
//Copyright 2016  Cityzen Data
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//


import io.warp10.script.NamedWarpScriptFunction;
import io.warp10.script.WarpScriptMapperFunction;
import io.warp10.script.WarpScriptStack;
import io.warp10.script.WarpScriptStackFunction;
import io.warp10.script.WarpScriptException;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import java.util.Map;

/**
* Mapper which returns the absolute value of the value passed as parameter
*/
public class MapperParseDouble extends NamedWarpScriptFunction implements WarpScriptMapperFunction {

private final NumberFormat format;
  
  public MapperParseDouble(String name) {
    super(name);
    this.format = null;
  }
  
  public MapperParseDouble(String name, Object language) throws WarpScriptException {   
    super(name);
    
    if (language instanceof String) {
      Locale locale = Locale.forLanguageTag((String) language);
      format = NumberFormat.getInstance(locale);
    } else {
      throw new WarpScriptException("Invalid value type for " + getName() + ", expects a String");
    }
  }
  
  /**
   * Builder for case user specify a language tag according to an
   * IETF BCP 47 language tag string of the Locale Java class
   */
  public static class Builder extends NamedWarpScriptFunction implements WarpScriptStackFunction {
    
    public Builder(String name) {
      super(name);
    }
    
    @Override
    public Object apply(WarpScriptStack stack) throws WarpScriptException {
      Object value = stack.pop();
      
      if (!(value instanceof String)) {
        throw new WarpScriptException("Invalid parameter for " + getName());
      }
      
      stack.push(new MapperParseDouble(getName(), value));
      return stack;
    }
  }
  
  @Override
  public Object apply(Object[] args) throws WarpScriptException {
    long tick = (long) args[0];
    String[] names = (String[]) args[1];
    Map<String,String>[] labels = (Map<String,String>[]) args[2];
    long[] ticks = (long[]) args[3];
    long[] locations = (long[]) args[4];
    long[] elevations = (long[]) args[5];
    Object[] values = (Object[]) args[6];

    if (1 != values.length) {
      throw new WarpScriptException(getName() + " can only be applied to a single value.");
    }
    
    Object value = null;
    long location = locations[0];
    long elevation = elevations[0];
    
    if (null != values[0]) {
      if (values[0] instanceof String) {
        try {
          if (null == this.format) {
            value = Double.parseDouble((String) values[0]);
          } else
          {           
            value = format.parse((String) values[0]).doubleValue();
          }
        } catch (NumberFormatException | ParseException e) {
          value = null;
        }
      }
    }
    
    return new Object[] { tick, location, elevation, value };
  }
}

@@ -29,18 +29,16 @@
 per;%0A  %0A
-  
 //%0A//Cop
@@ -47,17 +47,17 @@
 ight 201
-6
+7
   Cityze
@@ -1146,16 +1146,18 @@
 tion %7B%0A%0A
+  
 private 
@@ -1640,22 +1640,60 @@
 pects a 
-String
+language tag (a string) on top of the stack.
 %22);%0A    
@@ -2108,28 +2108,31 @@
      Object 
-valu
+languag
 e = stack.po
@@ -2155,20 +2155,23 @@
   if (!(
-valu
+languag
 e instan
@@ -2233,25 +2233,26 @@
 Invalid 
-parameter
+value type
  for %22 +
@@ -2261,16 +2261,77 @@
 etName()
+ + %22, expects a language tag (a string) on top of the stack.%22
 );%0A     
@@ -2390,20 +2390,23 @@
 Name(), 
-valu
+languag
 e));%0A   
@@ -3131,17 +3131,16 @@
    try %7B
-%0A
         
@@ -3145,137 +3145,9 @@
     
-if (null == this.format) %7B%0A            value = Double.parseDouble((String) values%5B0%5D);%0A          %7D else%0A          %7B           %0A  
+%0A
     
@@ -3212,20 +3212,8 @@
 ();%0A
-          %7D%0A
     

Update format mapper parse double