All times in this Timer object are in milliseconds. The timer starts from the moment of it's creation. : Timer « Development Class « Java






All times in this Timer object are in milliseconds. The timer starts from the moment of it's creation.

      
//package com.studiofortress.sf.util;

/**
 * All times in this Timer object are in milliseconds. The timer starts from the
 * moment of it's creation.
 * 
 * @author Joseph Lenton
 */
public class Timer {
  private final long startTime;
  private final long endTime;
  private final long duration;

  /**
   * @param duration
   *            The amount of time, in milliseconds, that this timer will last
   *            for.
   */
  public Timer(long duration, double percentDone) {
    if (duration <= 0) {
      throw new IllegalArgumentException(
          "The duration cannot be less or equal to 0, given: "
              + duration);
    } else if (percentDone < 0) {
      throw new IllegalArgumentException(
          "The percentDone cannot be less or equal to 0, given: "
              + percentDone);
    }

    this.duration = duration;
    this.startTime = System.currentTimeMillis()
        - Math.round(duration * percentDone);
    this.endTime = startTime + duration;
  }

  /**
   * @param duration
   *            The amount of time, in milliseconds, that this timer will last
   *            for.
   */
  public Timer(long duration) {
    if (duration <= 0) {
      throw new IllegalArgumentException(
          "The duration cannot be less or equal to 0, given: "
              + duration);
    }

    this.duration = duration;
    this.startTime = System.currentTimeMillis();
    this.endTime = startTime + duration;
  }

  /**
   * An inverse timer is a timer with the same duration as this timer, but
   * where the percentage that is complete is 1.0 - the current amount of
   * duration done. So if this timer has completed 0.3 of it's duration so
   * far, then the inverse timer will have completed 0.7 so far.
   * 
   * This method is to help with timer based algorithms (like a fade) where
   * you want to reverse the algorithm half way through.
   * 
   * @return A new Timer which has it's percentage complete as the inverse of
   *         this one.
   */
  public Timer getInverse() {
    return new Timer(duration, 1.0 - getPercentDuration());
  }

  /**
   * @return The time that this Timer was created.
   */
  public long getStartTime() {
    return startTime;
  }

  /**
   * @return The time that this Timer was created, plus it's duration.
   */
  public long getEndTime() {
    return endTime;
  }

  /**
   * @return True if this Timer is now past it's end time.
   */
  public boolean isPastDuration() {
    return System.currentTimeMillis() > getEndTime();
  }

  /**
   * @return The amount of time between the start and end time of this Timer.
   */
  public long getDuration() {
    return duration;
  }

  /**
   * The percent duration is based on this moment, now, when it is called.
   * 
   * @return A value from 0 to 1 describing how much of the duration this
   *         timer has gone through.
   */
  public float getPercentDuration() {
    final float percent = (float) (System.currentTimeMillis() - startTime)
        / (float) duration;
    return Math.min(Math.max(0.0f, percent), 1.0f);
  }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Timer Skipping BeepTimer Skipping Beep
2.Timer Schedule a task that executes once every secondTimer Schedule a task that executes once every second
3.Use java.util.Timer to schedule a task to execute once 5 seconds have passedUse java.util.Timer to schedule a task to execute once 5 seconds have passed
4.Timer utilities
5.Timer and TimerTask Classes
6.Pause and start a timer task
7.Create a Timer object
8.Swing also provide a Timer class. A Timer object will send an ActionEvent to the registered ActionListener.
9.Schedule a task by using Timer and TimerTask.
10.Scheduling a Timer Task to Run Repeatedly
11.Create a scheduled task using timer
12.extends TimerTask to create your own task
13.A simple implementation of the Java 1.3 java.util.Timer API
14.Scheduling a Timer Task to Run at a Certain Time
15.Class encapsulating timer functionality
16.Timeout Observer
17.A pool of objects that should only be used by one thread at a time
18.A class that allows a programmer to determine the amount of time spend doing certain routines
19.Thread Timer
20.Cancel Aware Timer
21.HRTimer is a simple system-wide timer facility using a singleton Timer, with additional instrumentation.
22.Used for timing events with millisecond precision.
23.A class to wait until a condition becomes true.