package org.lucci.madhoc.simulation.measure;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.lucci.madhoc.simulation.projection.Projection;
import org.lucci.up.data.Figure;
import org.lucci.up.data.FigureFactory;

/*
 * Created on May 4, 2005
 */

/**
 * @author luc.hogie
 */
public class MeasureHistory
{
    private Sensor sensor;
    private Projection projection;
    
    // the class of the values are not know, the list hence cannot be typed 
    private List values = new Vector();
    private Collection nonNullValues = new Vector();
    
    
    public void addValue(Object value, int iteration)
    {
        if (sensor.isValueValid(value))
        {
            // if no new values have been taken during the previous iteration
            // the list has to be filled with the corresponding null values
            while (values.size() < iteration)
            {
                values.add(null);
            }
            
            values.add(value);

            if (value != null)
            {
                nonNullValues.add(value);
            }
        }
        else
        {
            throw new IllegalArgumentException("invalid value " + value + " for measure " + getSensor().getMonitor().getName() + "#" + getSensor().getName());
        }
    }

    /**
     * @return Returns the measures.
     */
    public List getValues()
    {
        return Collections.unmodifiableList(values);
    }

    public Object getLastValue()
    {
        if (values.isEmpty())
        {
            throw new IllegalStateException();
        }
        else
        {
            return values.get(values.size() - 1);
        }
    }

    public StringBuffer getAsText()
    {
        StringBuffer buf = new StringBuffer();
        
        for (int i = 0; i < values.size(); ++i)
        {
            buf.append(values.get(i).toString());
            buf.append('\n');
        }
        
        return buf;
    }
    
    public static Figure createFigure(MeasureHistory x, MeasureHistory y)
    {
        return FigureFactory.createFigure(x.getValues(), y.getValues());
    }
    
    
    public List createCumulativeMeasure()
    {
        List list = new Vector();
        double sum = 0;
        Iterator valueIterator = values.iterator();
        
        while (valueIterator.hasNext())
        {
            sum += ((Double) valueIterator.next()).doubleValue();
            list.add(new Double(sum));
        }
        
        return list;
    }

    /**
     * @return Returns the nonNullValues.
     */
    public Collection getNonNullValues()
    {
        return nonNullValues;
    }

    public Sensor getSensor()
    {
        return sensor;
    }

    public void setSensor(Sensor measure)
    {
        this.sensor = measure;
    }
    public Projection getProjection()
    {
        return projection;
    }
    public void setProjection(Projection projection)
    {
        this.projection = projection;
    }
}