summaryrefslogtreecommitdiff
path: root/java/src/TimeValue.java
blob: 018e64e17d3004061e76a6b90df178a013171823 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*************************************************
 *
 * = PACKAGE
 *    ACE.Reactor
 *
 * = FILENAME
 *    TimeValue.java
 *
 *@author Prashant Jain
 *
 *************************************************/
//package ACE.Reactor;
package ACE.ASX;

public class TimeValue
{
  public final static TimeValue zero = new TimeValue (0,0);

  /** 
   * Default constructor
   */
  public TimeValue ()
  {
    this (0, 0);
  }

  /** 
   * Constructor
   *@param sec seconds
   */
  public TimeValue (long sec)
  {
    this (sec, 0);
  }

  /** 
   * Constructor
   *@param sec seconds
   *@param nanos nanoseconds
   */
  public TimeValue (long sec, int nanos)
  {
    this.set (sec, nanos);
  }

  /** 
   * Sets the seconds and nanoseconds of Time Value
   *@param sec seconds
   *@param nanos nanoseconds
   */
  public void set (long sec, int nanos)
  {
    this.millisec_ = sec * 1000;
    this.nanos_ = nanos;
    this.normalize ();
  }

  /** 
   * Get seconds
   *@return Seconds
   */
  public long sec ()
  {
    return this.millisec_/1000;
  }

  /** 
   * Get nanoseconds
   *@return Nanoseconds
   */
  public int nanos ()
  {
    return this.nanos_;
  }

  /** 
   * Get time in milliseconds.
   *@return time in milliseconds
   */
  public long getMilliTime ()
  {
    return this.millisec_;
  }

  /**
   * Get a String representation of the Time Value.
   *@return String representation of the Time Value
   */
  public String toString ()
  {
    return (new Long (this.millisec_/1000)).toString () + ":" +
      (new Integer (this.nanos_)).toString ();
  }

  /** 
   * Get current time.
   *@return the current system time
   */
  public static TimeValue getTimeOfDay ()
  {
    return new TimeValue (System.currentTimeMillis ()/1000);
  }

  /**
   * Compare two Time Values for equality.
   *@param tv Time Value to compare with
   *@return true if the two Time Values are equal, false otherwise
   */
  public boolean equals (TimeValue tv)
  {
    return this.millisec_ == (tv.sec () * 1000) && this.nanos_ == tv.nanos ();
  }

  /**
   * Compare two Time Values for non-equality.
   *@param tv Time Value to compare with
   *@return true if the two Time Values are not equal, false otherwise
   */
  public boolean notEquals (TimeValue tv)
  {
    return !this.equals (tv);
  }

  /**
   * Add two Time Values.
   *@param tv1 The first Time Value
   *@param tv2 The second Time Value
   *@return sum of the two Time Values.
   */
  public static TimeValue plus (TimeValue tv1, TimeValue tv2)
  {
    TimeValue tv = new TimeValue (tv1.sec () + tv2.sec (), 
				  tv1.nanos () + tv2.nanos ());
    tv.normalize ();
    return tv;
  }

  /**
   * Subtract two Time Values.
   *@param tv1 The first Time Value
   *@param tv2 The second Time Value
   *@return difference of the two Time Values.
   */
  public static TimeValue minus (TimeValue tv1, TimeValue tv2)
  {
    TimeValue tv = new TimeValue (tv1.sec () - tv2.sec (), 
				  tv1.nanos () - tv2.nanos ());
    tv.normalize ();
    return tv;
  }

  /**
   * Add Time Value to "this".
   *@param tv The Time Value to add to this.
   */
  public void plusEquals (TimeValue tv)
  {
    this.set (this.sec () + tv.sec (),
	      this.nanos () + tv.nanos ());
    this.normalize ();
  }

  /**
   * Subtract Time Value from "this".
   *@param tv The Time Value to subtract from this.
   */
  public void minusEquals (TimeValue tv)
  {
    this.set (this.sec () - tv.sec (),
	      this.nanos () - tv.nanos ());
    this.normalize ();
  }

  /**
   * Compare two Time Values for less than.
   *@param tv Time Value to compare with
   *@return true if "this" is less than tv, false otherwise
   */
  public boolean lessThan (TimeValue tv)
  {
    return tv.greaterThan (this);
  }

  /**
   * Compare two Time Values for greater than.
   *@param tv Time Value to compare with
   *@return true if "this" is greater than tv, false otherwise
   */
  public boolean greaterThan (TimeValue tv)
  {
    if (this.sec () > tv.sec ())
      return true;
    else if (this.sec () == tv.sec ()
	     && this.nanos () > tv.nanos ()) 
      return true;
    else
      return false;
  }

  /**
   * Compare two Time Values for <=.
   *@param tv Time Value to compare with
   *@return true if "this" <= tv, false otherwise
   */
  public boolean lessThanEqual (TimeValue tv)
  {
    return tv.greaterThanEqual (this);
  }

  /**
   * Compare two Time Values for >=.
   *@param tv Time Value to compare with
   *@return true if "this" >= tv, false otherwise
   */
  public boolean greaterThanEqual (TimeValue tv)
  {
    return this.sec () >= tv.sec () && this.nanos () >= tv.nanos ();
  }

  private void normalize ()
  {
    if (this.nanos_ >= ONE_MILLISECOND)
      {
	do
	  { 
	    this.millisec_++;
	    this.nanos_ -= ONE_MILLISECOND;
	  }
	while (this.nanos_ >= ONE_MILLISECOND);
      }
    else if (this.nanos_ <= -ONE_MILLISECOND)
      {
	do
	  { 
	    this.millisec_--;
	    this.nanos_ += ONE_MILLISECOND;
	  }
	while (this.nanos_ <= -ONE_MILLISECOND);
      }
    
    if (this.millisec_ >= 1 && this.nanos_ < 0)
      {
	this.millisec_--;
	this.nanos_ += ONE_MILLISECOND;
      }
    else if (this.millisec_ < 0 && this.nanos_ > 0)
      {
	this.millisec_++;
	this.nanos_ -= ONE_MILLISECOND;
      }
  }

  private long millisec_;
  private int nanos_;
  private final static int ONE_MILLISECOND = 1000000;
}