package cnp.ew.util;

public class CpSteppedTimedIntervalPolicy implements CpTimedIntervalPolicy
{
    long startTime;
    long timeDelta;
    int step;
    int startValue;
    int endValue;

    public CpSteppedTimedIntervalPolicy(long newTimeDelta, int newStep, int newStartValue, int newEndValue)
    {
        timeDelta = newTimeDelta;
        step = newStep;
        startValue = newStartValue;
        endValue = newEndValue;
    }

    public void initialize()
    {
        startTime = -1;
    }

    public int getNextInterval()
    {
        if (startTime < 0) {
            startTime = System.currentTimeMillis();
            return startValue;
        }
        int value = startValue + (int)(step * (System.currentTimeMillis() - startTime) / timeDelta);
        if (step > 0) {
            return Math.min(value, endValue);
        } else {
            return Math.max(value, endValue);
        }
    }

}

