KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DEOS > NewTimer


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package DEOS;
20
21 import gov.nasa.jpf.jvm.Verify;
22
23 import java.lang.*;
24
25
26 /**
27  * DOCUMENT ME!
28  */

29 public class NewTimer extends AbstractClockingDevice {
30   int startingTime = 0; // Time on the clock when timer was started
31
int setTime = 0; // Timer is set to this time
32
int stoppingTime = 0; // Time on clock when timer should stop
33
boolean isStarted = false;
34   boolean timeOut = false;
35   boolean isInterrupted = false;
36
37   /**
38    * Primary constructor
39    */

40   public NewTimer () {
41   }
42
43   /**
44    * To get the remaining time
45    * @param currentTime, the current time on the clock
46    * @return remainingTime, amount of time left
47    */

48   public int getRemainingTime (int currentTime) {
49     //Verify.beginAtomic();
50
int remainingTime = stoppingTime - currentTime;
51
52     //System.out.println("Thread used: " + (currentTime - startingTime));
53
// Assert: timeOut implies remainingTime = 0
54
//assert (!timeOut || remainingTime == 0);
55
//Verify.endAtomic();
56
return remainingTime;
57   }
58
59   /**
60    * To get the set-time
61    */

62   public int getSetTime () {
63     return setTime;
64   }
65
66   /**
67    * To get the stoppingTime
68    * @return stoppingTime, the stopping time (on the clock) for the timer
69    */

70   public int getStoppingTime () {
71     return stoppingTime;
72   }
73
74   /**
75    * Time out
76    * @param currentTime the current time on the clock
77    * @return true if time out occurs
78    */

79   public boolean isTimeOut () {
80     //assert (isStarted);
81
if (timeOut) {
82       isStarted = false;
83     }
84
85     return timeOut;
86   }
87
88   /**
89    * To reset the timer
90    * @param startingTimeIn value for setting timer
91    */

92   public void setTimer (int setTimeIn, int startingTimeIn) {
93     //Verify.beginAtomic();
94
//assert (setTimeIn > 0);
95
//System.out.println("--- Resetting timer ---");
96
//System.out.println("Starting time: " + startingTimeIn);
97
startingTime = startingTimeIn;
98     setTime = setTimeIn;
99     stoppingTime = startingTime + setTime;
100
101
102     //System.out.println("stopping time: " + stoppingTime);
103
isStarted = true;
104     isInterrupted = false;
105     timeOut = false;
106
107     // Verify.endAtomic();
108
}
109
110   /**
111    * Clear time out
112    */

113   public void clearTimeOut () {
114     timeOut = false;
115   }
116
117   /**
118    * When clock ticks, clock calls this method (inherited from
119    * super class)
120    */

121   public void clockTicks (int currentTime) {
122     if (stoppingTime == currentTime) {
123       timeOut = true;
124     }
125   }
126
127   /**
128    * Interrupt timer
129    */

130   public void interruptTimer () {
131     isStarted = false;
132     isInterrupted = true;
133   }
134 }
Popular Tags