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

package hw4_multithreadtrucks;
import java.util.*;
import pbxlogique.Pbx;
/**
 *
 * @author Peilos
 */
public class TimeCal {
    
    long startTime, currentTime;
    
    public void TakeStartTime()
    {
        GregorianCalendar gcl = new GregorianCalendar();
        gcl.setTime(new Date());
        startTime = gcl.getTimeInMillis();
    }
    
    long getNewTimeDifference()
    {
        GregorianCalendar gcl = new GregorianCalendar();
        gcl.setTime(new Date());
        currentTime = gcl.getTimeInMillis();
        return (currentTime - startTime);
    }
    
    @SuppressWarnings("empty-statement")
    public void RunForSomeTime(long timeInMillis)
    {
        this.TakeStartTime();
        while(this.getNewTimeDifference()<timeInMillis);
        //System.out.println("I've run for "+this.getNewTimeDifference()+" ms.");
    }
    
    @SuppressWarnings("empty-statement")
    public double RunAtPreDefinedRate(long minTimeInMilli, long maxTimeInMilli) //Returns ms / 1000
    {
        long timeInMillis = 0;
        long difference = maxTimeInMilli - minTimeInMilli;
        if (difference < 0)
        {
            difference= difference * (-1);
        }
        long boostFactor = Math.round(Math.random()*difference);
        timeInMillis = minTimeInMilli + boostFactor;
        this.TakeStartTime();
        while(this.getNewTimeDifference()<timeInMillis);
        //System.out.println("I've run for "+this.getNewTimeDifference()+" ms.");
        double temp = timeInMillis;
        temp = temp/1000;
        return  Pbx.antiDecimal(temp,2);
        
    }

}
