package org.lucci.madhoc.node_memory;

import java.util.Collection;
import java.util.Iterator;

import org.lucci.madhoc.network.Station;
import org.lucci.madhoc.simulation.measure.NaturalNumberSensor;
import org.lucci.madhoc.simulation.measure.Unit;
import org.lucci.madhoc.simulation.projection.Projection;

/*
 * Created on May 16, 2005
 */

/**
 * @author luc.hogie
 */
public class MemorySizeSensor extends NaturalNumberSensor
{

    /* (non-Javadoc)
     * @see org.lucci.madhoc.simulation.measure.NumericalSensor#getUnit()
     */
    public Unit getUnit()
    {
        return Unit.NUMBER_OF_STATIONS;
    }

    /* (non-Javadoc)
     * @see org.lucci.madhoc.simulation.measure.Sensor#getName()
     */
    public String getName()
    {
        return "number of known computers";
    }

    /* (non-Javadoc)
     * @see org.lucci.madhoc.simulation.measure.Sensor#takeNewValue(org.lucci.madhoc.simulation.projection.Projection)
     */
    public Double takeNewDoubleValue(Projection projection)
    {
        double sum = 0;
        Collection computers = projection.getNetwork().getStations();
        Iterator i = computers.iterator();
        
        while (i.hasNext())
        {
            Station computer = (Station) i.next();
            NodeMemory app = (NodeMemory) computer.getApplicationMap().getValue(NodeMemory.class);
            sum += app.getKnownComputers().size();
        }
        
        return sum / computers.size();
    }
}