package roart.common.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.math3.util.Precision;

public class MathUtil {
    public static double round(Double d, int n) {
        if (d.isInfinite()) {
            int jj = 0;
        }
        String hashes = StringUtils.repeat("#", n);
        DecimalFormat df = new DecimalFormat("#." + hashes);
        //df.setRoundingMode(RoundingMode.HALF_UP);
        return Double.valueOf(df.format(d));
    }

    public static double round2(Double d, int n) {
        return Precision.round(d, n);
    }

    public static double round3(Double d, int n) {
        BigDecimal bd = new BigDecimal(Double.toString(d));
        bd = bd.setScale(n, RoundingMode.HALF_UP);
        return bd.doubleValue();
    }

    public static Object[] round(Object[] o, int n) {
        String hashes = StringUtils.repeat("#", n);
        DecimalFormat df = new DecimalFormat("#." + hashes);
        Object[] newo = new Object[o.length];
        for (int i = 0; i < o.length; i++) {
            if (o[i] instanceof Double) {
                //df.setRoundingMode(RoundingMode.HALF_UP);
                newo[i] = Double.valueOf(df.format(o[i]));
            } else {
                newo[i] = o[i];
            }
        }
        return newo;
    }

    public static Object[] round2(Object[] o, int n) {
        Object[] newo = new Object[o.length];
        for (int i = 0; i < o.length; i++) {
            if (o[i] instanceof Double) {
                newo[i] = Precision.round((double) o[i], n);
            } else {
                newo[i] = o[i];
            }
        }
        return newo;
    }

    public static Object[] round3(Object[] o, int n) {
        Object[] newo = new Object[o.length];
        for (int i = 0; i < o.length; i++) {
            if (o[i] instanceof Double) {
                BigDecimal bd = new BigDecimal(Double.toString((double) o[i]));
                bd = bd.setScale(n, RoundingMode.HALF_UP);
                newo[i] = bd.doubleValue();
            } else {
                newo[i] = o[i];
            }
        }
        return newo;
    }    
    
    private Object[] round4(Object[] objs, int places) {
        if (objs != null) {
            for (int i = 0; i < objs.length; i++) {
                Object obj = objs[i];
                if (obj.getClass() == Double.class) {
                    obj = MathUtil.round2((Double) obj, 3);
                }
            }
        }
        return objs;
    }

    public static double[] getGeoSeq(double[] cap) {
        double[] geom = new double[cap.length];
        Double first = cap[0];
        Double last = cap[cap.length - 1];        
        for (int i = 0; i < cap.length - 1; i++) {
            geom[i] = first * Math.pow((double)last/first, (double) i/(cap.length - 1));
        }
        return geom;
    }

}

@@ -2632,15 +2632,103 @@
 e%5B%5D 
-cap
+array
 ) %7B%0A
+        if (array == null %7C%7C array.length == 1) %7B%0A            return array;%0A        %7D%0A
     
@@ -2758,19 +2758,21 @@
  double%5B
-cap
+array
 .length%5D
@@ -2796,19 +2796,21 @@
 first = 
-cap
+array
 %5B0%5D;%0A   
@@ -2832,15 +2832,19 @@
 t = 
-cap%5Bcap
+array%5Barray
 .len
@@ -2889,19 +2889,21 @@
  0; i %3C 
-cap
+array
 .length 
@@ -2989,11 +2989,13 @@
  i/(
-cap
+array
 .len

Fix for correlation for processing for simulate invest action (I220).