home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 139 / dpcs0999.iso / Web / CFserver / data1.cab / Java / netscape / application / Timer.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-04-12  |  4.7 KB  |  183 lines

  1. package netscape.application;
  2.  
  3. import netscape.util.InconsistencyException;
  4. import netscape.util.Vector;
  5.  
  6. public class Timer implements EventProcessor, EventFilter {
  7.    EventLoop eventLoop;
  8.    Target target;
  9.    String command;
  10.    Object data;
  11.    long timeStamp;
  12.    int initialDelay;
  13.    int delay;
  14.    boolean repeats;
  15.    boolean coalesce;
  16.    boolean removeEvents;
  17.    long expirationTime;
  18.    Timer nextTimer;
  19.    boolean running;
  20.  
  21.    public Timer(EventLoop var1, Target var2, String var3, int var4) {
  22.       this.repeats = true;
  23.       this.coalesce = true;
  24.       if (var1 == null) {
  25.          throw new InconsistencyException("eventLoop parameter is null");
  26.       } else {
  27.          this.eventLoop = var1;
  28.          this.target = var2;
  29.          this.command = var3;
  30.          this.setDelay(var4);
  31.          this.setInitialDelay(var4);
  32.       }
  33.    }
  34.  
  35.    public Timer(Target var1, String var2, int var3) {
  36.       this(Application.application().eventLoop(), var1, var2, var3);
  37.    }
  38.  
  39.    TimerQueue timerQueue() {
  40.       return Application.application().timerQueue();
  41.    }
  42.  
  43.    public EventLoop eventLoop() {
  44.       return this.eventLoop;
  45.    }
  46.  
  47.    public void setTarget(Target var1) {
  48.       this.target = var1;
  49.    }
  50.  
  51.    public Target target() {
  52.       return this.target;
  53.    }
  54.  
  55.    public void setCommand(String var1) {
  56.       this.command = var1;
  57.    }
  58.  
  59.    public String command() {
  60.       return this.command;
  61.    }
  62.  
  63.    public void setData(Object var1) {
  64.       this.data = var1;
  65.    }
  66.  
  67.    public Object data() {
  68.       return this.data;
  69.    }
  70.  
  71.    public void setDelay(int var1) {
  72.       if (var1 < 0) {
  73.          throw new InconsistencyException("Invalid initial delay: " + var1);
  74.       } else {
  75.          this.delay = var1;
  76.          if (this.isRunning()) {
  77.             TimerQueue var2 = this.timerQueue();
  78.             var2.removeTimer(this);
  79.             this.removeEvents();
  80.             var2.addTimer(this, System.currentTimeMillis() + (long)var1);
  81.          }
  82.  
  83.       }
  84.    }
  85.  
  86.    public int delay() {
  87.       return this.delay;
  88.    }
  89.  
  90.    public void setInitialDelay(int var1) {
  91.       if (var1 < 0) {
  92.          throw new InconsistencyException("Invalid initial delay: " + var1);
  93.       } else {
  94.          this.initialDelay = var1;
  95.       }
  96.    }
  97.  
  98.    public int initialDelay() {
  99.       return this.initialDelay;
  100.    }
  101.  
  102.    public void setRepeats(boolean var1) {
  103.       this.repeats = var1;
  104.    }
  105.  
  106.    public boolean repeats() {
  107.       return this.repeats;
  108.    }
  109.  
  110.    public long timeStamp() {
  111.       return this.timeStamp;
  112.    }
  113.  
  114.    public void setCoalesce(boolean var1) {
  115.       this.coalesce = var1;
  116.    }
  117.  
  118.    public boolean doesCoalesce() {
  119.       return this.coalesce;
  120.    }
  121.  
  122.    public void start() {
  123.       this.timerQueue().addTimer(this, System.currentTimeMillis() + (long)this.initialDelay());
  124.    }
  125.  
  126.    public boolean isRunning() {
  127.       return this.timerQueue().containsTimer(this);
  128.    }
  129.  
  130.    public void stop() {
  131.       this.timerQueue().removeTimer(this);
  132.       this.removeEvents();
  133.    }
  134.  
  135.    synchronized void removeEvents() {
  136.       this.removeEvents = true;
  137.       this.eventLoop.filterEvents(this);
  138.    }
  139.  
  140.    synchronized boolean peekEvent() {
  141.       this.removeEvents = false;
  142.       return this.eventLoop.filterEvents(this) != null;
  143.    }
  144.  
  145.    public Object filterEvents(Vector var1) {
  146.       int var2 = var1.count();
  147.  
  148.       while(var2-- > 0) {
  149.          Event var3 = (Event)var1.elementAt(var2);
  150.          if (var3.processor() == this) {
  151.             if (!this.removeEvents) {
  152.                return var3;
  153.             }
  154.  
  155.             var1.removeElementAt(var2);
  156.          }
  157.       }
  158.  
  159.       return null;
  160.    }
  161.  
  162.    public void processEvent(Event var1) {
  163.       this.timeStamp = var1.timeStamp;
  164.       if (this.target != null) {
  165.          this.target.performCommand(this.command, this.data);
  166.       }
  167.  
  168.    }
  169.  
  170.    public String toString() {
  171.       return "Timer {target = " + this.target + "; command = " + this.command + "; delay = " + this.delay + "; initialDelay = " + this.initialDelay + "; repeats = " + this.repeats + "}";
  172.    }
  173.  
  174.    void post(long var1) {
  175.       if (!this.coalesce || !this.peekEvent()) {
  176.          Event var3 = new Event(var1);
  177.          var3.setProcessor(this);
  178.          this.eventLoop.addEvent(var3);
  179.       }
  180.  
  181.    }
  182. }
  183.