1  import org.as2lib.env.event.EventSupport;
     2  import org.as2lib.data.type.Time;
     3  
     4  /**
     5   * {@code AbstractTimeConsumer} represents a time consuming class.
     6   * 
     7   * <p>It saves and provides informations about the time that the concrete 
     8   * class used.
     9   * 
    10   * <p>The concrete implementation needs to take care of {@code startTime},
    11   * {@code endTime},{@code getPercentage},{@code started} and {@code finished}.
    12   * 
    13   * @author Martin Heidegger
    14   * @version 1.0
    15   */
    16  class org.as2lib.app.exec.AbstractTimeConsumer extends EventSupport {
    17  	
    18  	/** Start time in ms of start point. */
    19  	private var startTime:Number;
    20  	
    21  	/** Finish time in ms of finishing point. */
    22  	private var endTime:Number;
    23  	
    24  	/** Duration time difference. */
    25  	private var duration:Time;
    26  	
    27  	/** Total time difference. */
    28  	private var totalTime:Time;
    29  	
    30  	/** Rest time difference. */
    31  	private var restTime:Time;
    32  	
    33  	/** Flag if execution was started. */
    34  	private var started:Boolean;
    35  	
    36  	/** Flag if execution was finished. */
    37  	private var finished:Boolean;
    38  	
    39  	/**
    40  	 * Constructs a new {@code AbstractTimeConsumer}.
    41  	 */
    42  	public function AbstractTimeConsumer(Void) {
    43  		duration = new Time(0);
    44  		totalTime = new Time(0);
    45  		restTime = new Time(0);
    46  		started = false;
    47  		finished = false;
    48  	}
    49  	
    50  	/**
    51  	 * Returns {@code true} if the process has finished.
    52  	 * 
    53  	 * <p>If the process has not been started it returns {@code false}.
    54  	 * 
    55  	 * @return {@code true} if the process has finished
    56  	 */
    57  	public function hasFinished(Void):Boolean {
    58  		return finished;
    59  	}
    60  	
    61  	/**
    62  	 * Returns {@code true} if the process has started.
    63  	 * 
    64  	 * <p>If the process has finished it returns {@code false}.
    65  	 * 
    66  	 * @return {@code true} if the process has started
    67  	 */
    68  	public function hasStarted(Void):Boolean {
    69  		return started;
    70  	}
    71  	
    72      /**
    73       * Returns the percentage of execution
    74       * 
    75       * <p>Override this implementation for a matching result.
    76       * 
    77       * @return {@code null}, override this implementation
    78       */
    79      public function getPercentage(Void):Number {
    80  		return null;
    81  	}
    82  	
    83  	/**
    84  	 * Returns the time of the execution of the process.
    85  	 * 
    86  	 * @return time difference between start time and end time/current time.
    87  	 */
    88  	public function getDuration(Void):Time {
    89  		if (endTime) {
    90  			return duration.setValue(endTime-startTime);
    91  		} else {
    92  			return duration.setValue(getTimer()-startTime);
    93  		}
    94  	}
    95  	
    96  	/**
    97  	 * Estimates the approximate time for the complete execution of the process.
    98  	 * 
    99  	 * @return estimated duration at the end of the process
   100  	 */
   101  	public function getEstimatedTotalTime(Void):Time {
   102  		if ((hasStarted() || hasFinished()) && getPercentage() != null) {
   103  			return totalTime.setValue(getDuration().inMilliSeconds()/getPercentage()*100);
   104  		} else {
   105  			return null;
   106  		}
   107  	}
   108  	
   109  	/**
   110  	 * Estimates the approximate time until the execution finishes.
   111  	 * 
   112  	 * @return estimated time until finish of the process
   113  	 */
   114  	public function getEstimatedRestTime(Void):Time {
   115  		var totalTime:Time = getEstimatedTotalTime();
   116  		if (totalTime == null) {
   117  			return null;
   118  		} else {
   119  			return restTime.setValue(getEstimatedTotalTime().inMilliSeconds()-getDuration().inMilliSeconds());
   120  		}
   121  	}
   122  }