/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package structures;

import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;
import utility.StringUtil;

/**
 *
 * @author baba
 */
public class HashEntryDoubleVector {

    public Vector vectorDouble = new Vector();
    public double average = -1;
    public double stdDeviation = -1;
    public DoubleEntry mostRecent = null;
    private double sum = 0;
    public static final String className = "structures.HashEntryDoubleVector";

    public HashEntryDoubleVector() {
    }

    public HashEntryDoubleVector(String vectorString) throws Exception {
        String[] tmp = StringUtil.split(vectorString, Constants.DELIMITER);
        for (int i = 0; i < tmp.length; i++) {
            String some = tmp[i];
            try {
                AddToVector(Double.parseDouble(some));
            } catch (Exception ex) {
                throw ex;
                //ex.printStackTrace();
            }
        }
    }


    public void AddToVector(double someDouble) {
        mostRecent = new DoubleEntry(someDouble);
        sum = sum + someDouble;
        int icount = vectorDouble.size();
        average = sum / (icount + 1);
        vectorDouble.addElement(mostRecent);

        double summ = 0;


        Enumeration en = vectorDouble.elements();
        while (en.hasMoreElements()) {
            DoubleEntry tmpEntry = (DoubleEntry)en.nextElement();
            double v = tmpEntry.valueDouble - average;
            summ += v * v;

        }
        // this is for the entire population i.e. n
        // if Required cahnge to "n-1".
        stdDeviation = Math.sqrt(summ / (icount + 1));

    }

    public double GetCustomAverage(int minutes) {
        double customAverage = -1;
        long milliSeconds = minutes * 60 * 1000;
        long now = (new Date()).getTime();
        double summ = 0;
        int iCount = 0;
        for (int i = vectorDouble.size() - 1; i >= 0; i--) {
            DoubleEntry someEntry = (DoubleEntry)vectorDouble.elementAt(i);
            long timeStamp = someEntry.timeStamp.getTime();
            if ((now - timeStamp) >= milliSeconds) {
                summ += someEntry.valueDouble;
                iCount++;
            }
        }
        if (iCount != 0) {
            customAverage = summ / iCount;
        }

        return customAverage;
    }

    @Override
    public String toString() {
        Enumeration en = vectorDouble.elements();
        String tmp = "";
        int iCount = 0;
        while (en.hasMoreElements()) {
            DoubleEntry entry = (DoubleEntry)en.nextElement();
            if (iCount == 0) {
                tmp = ""+ entry.valueDouble;
            } else if (iCount == vectorDouble.size() - 1) {
                tmp = tmp + Constants.DELIMITER + entry.valueDouble;
            } else {
                tmp = tmp + Constants.DELIMITER + entry.valueDouble;
            }
            iCount++;
        }
        return tmp;
    }
}
