|
1 /* |
|
2 TUIO Java backend - part of the reacTIVision project |
|
3 http://reactivision.sourceforge.net/ |
|
4 |
|
5 Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten@iua.upf.edu> |
|
6 |
|
7 This program is free software; you can redistribute it and/or modify |
|
8 it under the terms of the GNU General Public License as published by |
|
9 the Free Software Foundation; either version 2 of the License, or |
|
10 (at your option) any later version. |
|
11 |
|
12 This program is distributed in the hope that it will be useful, |
|
13 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 GNU General Public License for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with this program; if not, write to the Free Software |
|
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
20 */ |
|
21 |
|
22 package TUIO; |
|
23 |
|
24 /** |
|
25 * The TuioTime class is a simple structure that is used to reprent the time that has elapsed since the session start. |
|
26 * The time is internally represented as seconds and fractions of microseconds which should be more than sufficient for gesture related timing requirements. |
|
27 * Therefore at the beginning of a typical TUIO session the static method initSession() will set the reference time for the session. |
|
28 * Another important static method getSessionTime will return a TuioTime object representing the time elapsed since the session start. |
|
29 * The class also provides various addtional convience method, which allow some simple time arithmetics. |
|
30 * |
|
31 * @author Martin Kaltenbrunner |
|
32 * @version 1.4 |
|
33 */ |
|
34 public class TuioTime { |
|
35 |
|
36 /** |
|
37 * the time since session start in seconds |
|
38 */ |
|
39 private long seconds = 0; |
|
40 /** |
|
41 * time fraction in microseconds |
|
42 */ |
|
43 private long micro_seconds = 0; |
|
44 /** |
|
45 * the session start time in seconds |
|
46 */ |
|
47 private static long start_seconds = 0; |
|
48 /** |
|
49 * start time fraction in microseconds |
|
50 */ |
|
51 private static long start_micro_seconds = 0; |
|
52 |
|
53 /** |
|
54 * The default constructor takes no arguments and sets |
|
55 * the Seconds and Microseconds attributes of the newly created TuioTime both to zero. |
|
56 */ |
|
57 public TuioTime () { |
|
58 this.seconds = 0; |
|
59 this.micro_seconds = 0; |
|
60 } |
|
61 |
|
62 /** |
|
63 * This constructor takes the provided time represented in total Milliseconds |
|
64 * and assigs this value to the newly created TuioTime. |
|
65 * |
|
66 * @param msec the total time in Millseconds |
|
67 */ |
|
68 public TuioTime (long msec) { |
|
69 this.seconds = msec/1000; |
|
70 this.micro_seconds = 1000*(msec%1000); |
|
71 } |
|
72 |
|
73 /** |
|
74 * This constructor takes the provided time represented in Seconds and Microseconds |
|
75 * and assigs these value to the newly created TuioTime. |
|
76 * |
|
77 * @param sec the total time in seconds |
|
78 * @param usec the microseconds time component |
|
79 */ |
|
80 public TuioTime (long sec, long usec) { |
|
81 this.seconds = sec; |
|
82 this.micro_seconds = usec; |
|
83 } |
|
84 |
|
85 /** |
|
86 * This constructor takes the provided TuioTime |
|
87 * and assigs its Seconds and Microseconds values to the newly created TuioTime. |
|
88 * |
|
89 * @param ttime the TuioTime used to copy |
|
90 */ |
|
91 public TuioTime (TuioTime ttime) { |
|
92 this.seconds = ttime.getSeconds(); |
|
93 this.micro_seconds = ttime.getMicroseconds(); |
|
94 } |
|
95 |
|
96 /** |
|
97 * Sums the provided time value represented in total Microseconds to this TuioTime. |
|
98 * |
|
99 * @param us the total time to add in Microseconds |
|
100 * @return the sum of this TuioTime with the provided argument in microseconds |
|
101 */ |
|
102 public TuioTime add(long us) { |
|
103 long sec = seconds + us/1000000; |
|
104 long usec = micro_seconds + us%1000000; |
|
105 return new TuioTime(sec,usec); |
|
106 } |
|
107 |
|
108 /** |
|
109 * Sums the provided TuioTime to the private Seconds and Microseconds attributes. |
|
110 * |
|
111 * @param ttime the TuioTime to add |
|
112 * @return the sum of this TuioTime with the provided TuioTime argument |
|
113 */ |
|
114 public TuioTime add(TuioTime ttime) { |
|
115 long sec = seconds + ttime.getSeconds(); |
|
116 long usec = micro_seconds + ttime.getMicroseconds(); |
|
117 sec += usec/1000000; |
|
118 usec = usec%1000000; |
|
119 return new TuioTime(sec,usec); |
|
120 } |
|
121 |
|
122 /** |
|
123 * Subtracts the provided time represented in Microseconds from the private Seconds and Microseconds attributes. |
|
124 * |
|
125 * @param us the total time to subtract in Microseconds |
|
126 * @return the subtraction result of this TuioTime minus the provided time in Microseconds |
|
127 */ |
|
128 public TuioTime subtract(long us) { |
|
129 long sec = seconds - us/1000000; |
|
130 long usec = micro_seconds - us%1000000; |
|
131 |
|
132 if (usec<0) { |
|
133 usec += 1000000; |
|
134 sec--; |
|
135 } |
|
136 |
|
137 return new TuioTime(sec,usec); |
|
138 } |
|
139 |
|
140 /** |
|
141 * Subtracts the provided TuioTime from the private Seconds and Microseconds attributes. |
|
142 * |
|
143 * @param ttime the TuioTime to subtract |
|
144 * @return the subtraction result of this TuioTime minus the provided TuioTime |
|
145 */ |
|
146 public TuioTime subtract(TuioTime ttime) { |
|
147 long sec = seconds - ttime.getSeconds(); |
|
148 long usec = micro_seconds - ttime.getMicroseconds(); |
|
149 |
|
150 if (usec<0) { |
|
151 usec += 1000000; |
|
152 sec--; |
|
153 } |
|
154 |
|
155 return new TuioTime(sec,usec); |
|
156 } |
|
157 |
|
158 /** |
|
159 * Takes a TuioTime argument and compares the provided TuioTime to the private Seconds and Microseconds attributes. |
|
160 * |
|
161 * @param ttime the TuioTime to compare |
|
162 * @return true if the two TuioTime have equal Seconds and Microseconds attributes |
|
163 */ |
|
164 public boolean equals(TuioTime ttime) { |
|
165 if ((seconds==ttime.getSeconds()) && (micro_seconds==ttime.getMicroseconds())) return true; |
|
166 else return false; |
|
167 } |
|
168 |
|
169 /** |
|
170 * Resets the seconds and micro_seconds attributes to zero. |
|
171 */ |
|
172 public void reset() { |
|
173 seconds = 0; |
|
174 micro_seconds = 0; |
|
175 } |
|
176 |
|
177 /** |
|
178 * Returns the TuioTime Seconds component. |
|
179 * @return the TuioTime Seconds component |
|
180 */ |
|
181 public long getSeconds() { |
|
182 return seconds; |
|
183 } |
|
184 |
|
185 /** |
|
186 * Returns the TuioTime Microseconds component. |
|
187 * @return the TuioTime Microseconds component |
|
188 */ |
|
189 public long getMicroseconds() { |
|
190 return micro_seconds; |
|
191 } |
|
192 |
|
193 /** |
|
194 * Returns the total TuioTime in Milliseconds. |
|
195 * @return the total TuioTime in Milliseconds |
|
196 */ |
|
197 public long getTotalMilliseconds() { |
|
198 return seconds*1000+micro_seconds/1000; |
|
199 } |
|
200 |
|
201 /** |
|
202 * This static method globally resets the TUIO session time. |
|
203 */ |
|
204 public static void initSession() { |
|
205 TuioTime startTime = getSystemTime(); |
|
206 start_seconds = startTime.getSeconds(); |
|
207 start_micro_seconds = startTime.getMicroseconds(); |
|
208 } |
|
209 |
|
210 /** |
|
211 * Returns the present TuioTime representing the time since session start. |
|
212 * @return the present TuioTime representing the time since session start |
|
213 */ |
|
214 public static TuioTime getSessionTime() { |
|
215 TuioTime sessionTime = getSystemTime().subtract(getStartTime()); |
|
216 return sessionTime; |
|
217 |
|
218 } |
|
219 |
|
220 /** |
|
221 * Returns the absolut TuioTime representing the session start. |
|
222 * @return the absolut TuioTime representing the session start |
|
223 */ |
|
224 public static TuioTime getStartTime() { |
|
225 return new TuioTime(start_seconds,start_micro_seconds); |
|
226 } |
|
227 |
|
228 /** |
|
229 * Returns the absolut TuioTime representing the current system time. |
|
230 * @return the absolut TuioTime representing the current system time |
|
231 */ |
|
232 public static TuioTime getSystemTime() { |
|
233 long usec = System.nanoTime()/1000; |
|
234 return new TuioTime(usec/1000000,usec%1000000); |
|
235 } |
|
236 } |