31 * @author Martin Kaltenbrunner |
31 * @author Martin Kaltenbrunner |
32 * @version 1.4 |
32 * @version 1.4 |
33 */ |
33 */ |
34 public class TuioTime { |
34 public class TuioTime { |
35 |
35 |
36 /** |
36 /** |
37 * the time since session start in seconds |
37 * the time since session start in seconds |
38 */ |
38 */ |
39 private long seconds = 0; |
39 private long seconds = 0; |
40 /** |
40 /** |
41 * time fraction in microseconds |
41 * time fraction in microseconds |
42 */ |
42 */ |
43 private long micro_seconds = 0; |
43 private long micro_seconds = 0; |
44 /** |
44 /** |
45 * the session start time in seconds |
45 * the session start time in seconds |
46 */ |
46 */ |
47 private static long start_seconds = 0; |
47 private static long start_seconds = 0; |
48 /** |
48 /** |
49 * start time fraction in microseconds |
49 * start time fraction in microseconds |
50 */ |
50 */ |
51 private static long start_micro_seconds = 0; |
51 private static long start_micro_seconds = 0; |
52 |
52 |
53 /** |
53 /** |
54 * The default constructor takes no arguments and sets |
54 * The default constructor takes no arguments and sets |
55 * the Seconds and Microseconds attributes of the newly created TuioTime both to zero. |
55 * the Seconds and Microseconds attributes of the newly created TuioTime both to zero. |
56 */ |
56 */ |
57 public TuioTime () { |
57 public TuioTime () { |
58 this.seconds = 0; |
58 this.seconds = 0; |
59 this.micro_seconds = 0; |
59 this.micro_seconds = 0; |
60 } |
60 } |
61 |
61 |
62 /** |
62 /** |
63 * This constructor takes the provided time represented in total Milliseconds |
63 * This constructor takes the provided time represented in total Milliseconds |
64 * and assigs this value to the newly created TuioTime. |
64 * and assigs this value to the newly created TuioTime. |
65 * |
65 * |
66 * @param msec the total time in Millseconds |
66 * @param msec the total time in Millseconds |
67 */ |
67 */ |
68 public TuioTime (long msec) { |
68 public TuioTime (long msec) { |
69 this.seconds = msec/1000; |
69 this.seconds = msec/1000; |
70 this.micro_seconds = 1000*(msec%1000); |
70 this.micro_seconds = 1000*(msec%1000); |
71 } |
71 } |
72 |
72 |
73 /** |
73 /** |
74 * This constructor takes the provided time represented in Seconds and Microseconds |
74 * This constructor takes the provided time represented in Seconds and Microseconds |
75 * and assigs these value to the newly created TuioTime. |
75 * and assigs these value to the newly created TuioTime. |
76 * |
76 * |
77 * @param sec the total time in seconds |
77 * @param sec the total time in seconds |
78 * @param usec the microseconds time component |
78 * @param usec the microseconds time component |
79 */ |
79 */ |
80 public TuioTime (long sec, long usec) { |
80 public TuioTime (long sec, long usec) { |
81 this.seconds = sec; |
81 this.seconds = sec; |
82 this.micro_seconds = usec; |
82 this.micro_seconds = usec; |
83 } |
83 } |
84 |
84 |
85 /** |
85 /** |
86 * This constructor takes the provided TuioTime |
86 * This constructor takes the provided TuioTime |
87 * and assigs its Seconds and Microseconds values to the newly created TuioTime. |
87 * and assigs its Seconds and Microseconds values to the newly created TuioTime. |
88 * |
88 * |
89 * @param ttime the TuioTime used to copy |
89 * @param ttime the TuioTime used to copy |
90 */ |
90 */ |
91 public TuioTime (TuioTime ttime) { |
91 public TuioTime (TuioTime ttime) { |
92 this.seconds = ttime.getSeconds(); |
92 this.seconds = ttime.getSeconds(); |
93 this.micro_seconds = ttime.getMicroseconds(); |
93 this.micro_seconds = ttime.getMicroseconds(); |
94 } |
94 } |
95 |
95 |
96 /** |
96 /** |
97 * Sums the provided time value represented in total Microseconds to this TuioTime. |
97 * Sums the provided time value represented in total Microseconds to this TuioTime. |
98 * |
98 * |
99 * @param us the total time to add in Microseconds |
99 * @param us the total time to add in Microseconds |
100 * @return the sum of this TuioTime with the provided argument in microseconds |
100 * @return the sum of this TuioTime with the provided argument in microseconds |
101 */ |
101 */ |
102 public TuioTime add(long us) { |
102 public TuioTime add(long us) { |
103 long sec = seconds + us/1000000; |
103 long sec = seconds + us/1000000; |
104 long usec = micro_seconds + us%1000000; |
104 long usec = micro_seconds + us%1000000; |
105 return new TuioTime(sec,usec); |
105 return new TuioTime(sec,usec); |
106 } |
106 } |
107 |
107 |
108 /** |
108 /** |
109 * Sums the provided TuioTime to the private Seconds and Microseconds attributes. |
109 * Sums the provided TuioTime to the private Seconds and Microseconds attributes. |
110 * |
110 * |
111 * @param ttime the TuioTime to add |
111 * @param ttime the TuioTime to add |
112 * @return the sum of this TuioTime with the provided TuioTime argument |
112 * @return the sum of this TuioTime with the provided TuioTime argument |
113 */ |
113 */ |
114 public TuioTime add(TuioTime ttime) { |
114 public TuioTime add(TuioTime ttime) { |
115 long sec = seconds + ttime.getSeconds(); |
115 long sec = seconds + ttime.getSeconds(); |
116 long usec = micro_seconds + ttime.getMicroseconds(); |
116 long usec = micro_seconds + ttime.getMicroseconds(); |
117 sec += usec/1000000; |
117 sec += usec/1000000; |
118 usec = usec%1000000; |
118 usec = usec%1000000; |
119 return new TuioTime(sec,usec); |
119 return new TuioTime(sec,usec); |
120 } |
120 } |
121 |
121 |
122 /** |
122 /** |
123 * Subtracts the provided time represented in Microseconds from the private Seconds and Microseconds attributes. |
123 * Subtracts the provided time represented in Microseconds from the private Seconds and Microseconds attributes. |
124 * |
124 * |
125 * @param us the total time to subtract in Microseconds |
125 * @param us the total time to subtract in Microseconds |
126 * @return the subtraction result of this TuioTime minus the provided time in Microseconds |
126 * @return the subtraction result of this TuioTime minus the provided time in Microseconds |
127 */ |
127 */ |
128 public TuioTime subtract(long us) { |
128 public TuioTime subtract(long us) { |
129 long sec = seconds - us/1000000; |
129 long sec = seconds - us/1000000; |
130 long usec = micro_seconds - us%1000000; |
130 long usec = micro_seconds - us%1000000; |
131 |
131 |
132 if (usec<0) { |
132 if (usec<0) { |
133 usec += 1000000; |
133 usec += 1000000; |
134 sec--; |
134 sec--; |
135 } |
135 } |
136 |
136 |
137 return new TuioTime(sec,usec); |
137 return new TuioTime(sec,usec); |
138 } |
138 } |
139 |
139 |
140 /** |
140 /** |
141 * Subtracts the provided TuioTime from the private Seconds and Microseconds attributes. |
141 * Subtracts the provided TuioTime from the private Seconds and Microseconds attributes. |
142 * |
142 * |
143 * @param ttime the TuioTime to subtract |
143 * @param ttime the TuioTime to subtract |
144 * @return the subtraction result of this TuioTime minus the provided TuioTime |
144 * @return the subtraction result of this TuioTime minus the provided TuioTime |
145 */ |
145 */ |
146 public TuioTime subtract(TuioTime ttime) { |
146 public TuioTime subtract(TuioTime ttime) { |
147 long sec = seconds - ttime.getSeconds(); |
147 long sec = seconds - ttime.getSeconds(); |
148 long usec = micro_seconds - ttime.getMicroseconds(); |
148 long usec = micro_seconds - ttime.getMicroseconds(); |
149 |
149 |
150 if (usec<0) { |
150 if (usec<0) { |
151 usec += 1000000; |
151 usec += 1000000; |
152 sec--; |
152 sec--; |
153 } |
153 } |
154 |
154 |
155 return new TuioTime(sec,usec); |
155 return new TuioTime(sec,usec); |
156 } |
156 } |
157 |
157 |
158 /** |
158 /** |
159 * Takes a TuioTime argument and compares the provided TuioTime to the private Seconds and Microseconds attributes. |
159 * Takes a TuioTime argument and compares the provided TuioTime to the private Seconds and Microseconds attributes. |
160 * |
160 * |
161 * @param ttime the TuioTime to compare |
161 * @param ttime the TuioTime to compare |
162 * @return true if the two TuioTime have equal Seconds and Microseconds attributes |
162 * @return true if the two TuioTime have equal Seconds and Microseconds attributes |
163 */ |
163 */ |
164 public boolean equals(TuioTime ttime) { |
164 public boolean equals(TuioTime ttime) { |
165 if ((seconds==ttime.getSeconds()) && (micro_seconds==ttime.getMicroseconds())) return true; |
165 if ((seconds==ttime.getSeconds()) && (micro_seconds==ttime.getMicroseconds())) return true; |
166 else return false; |
166 else return false; |
167 } |
167 } |
168 |
168 |
169 /** |
169 /** |
170 * Resets the seconds and micro_seconds attributes to zero. |
170 * Resets the seconds and micro_seconds attributes to zero. |
171 */ |
171 */ |
172 public void reset() { |
172 public void reset() { |
173 seconds = 0; |
173 seconds = 0; |
174 micro_seconds = 0; |
174 micro_seconds = 0; |
175 } |
175 } |
176 |
176 |
177 /** |
177 /** |
178 * Returns the TuioTime Seconds component. |
178 * Returns the TuioTime Seconds component. |
179 * @return the TuioTime Seconds component |
179 * @return the TuioTime Seconds component |
180 */ |
180 */ |
181 public long getSeconds() { |
181 public long getSeconds() { |
182 return seconds; |
182 return seconds; |
183 } |
183 } |
184 |
184 |
185 /** |
185 /** |
186 * Returns the TuioTime Microseconds component. |
186 * Returns the TuioTime Microseconds component. |
187 * @return the TuioTime Microseconds component |
187 * @return the TuioTime Microseconds component |
188 */ |
188 */ |
189 public long getMicroseconds() { |
189 public long getMicroseconds() { |
190 return micro_seconds; |
190 return micro_seconds; |
191 } |
191 } |
192 |
192 |
193 /** |
193 /** |
194 * Returns the total TuioTime in Milliseconds. |
194 * Returns the total TuioTime in Milliseconds. |
195 * @return the total TuioTime in Milliseconds |
195 * @return the total TuioTime in Milliseconds |
196 */ |
196 */ |
197 public long getTotalMilliseconds() { |
197 public long getTotalMilliseconds() { |
198 return seconds*1000+micro_seconds/1000; |
198 return seconds*1000+micro_seconds/1000; |
199 } |
199 } |
200 |
200 |
201 /** |
201 /** |
202 * This static method globally resets the TUIO session time. |
202 * This static method globally resets the TUIO session time. |
203 */ |
203 */ |
204 public static void initSession() { |
204 public static void initSession() { |
205 TuioTime startTime = getSystemTime(); |
205 TuioTime startTime = getSystemTime(); |
206 start_seconds = startTime.getSeconds(); |
206 start_seconds = startTime.getSeconds(); |
207 start_micro_seconds = startTime.getMicroseconds(); |
207 start_micro_seconds = startTime.getMicroseconds(); |
208 } |
208 } |
209 |
209 |
210 /** |
210 /** |
211 * Returns the present TuioTime representing the time since session start. |
211 * Returns the present TuioTime representing the time since session start. |
212 * @return the present TuioTime representing the time since session start |
212 * @return the present TuioTime representing the time since session start |
213 */ |
213 */ |
214 public static TuioTime getSessionTime() { |
214 public static TuioTime getSessionTime() { |
215 TuioTime sessionTime = getSystemTime().subtract(getStartTime()); |
215 TuioTime sessionTime = getSystemTime().subtract(getStartTime()); |
216 return sessionTime; |
216 return sessionTime; |
217 |
217 |
218 } |
218 } |
219 |
219 |
220 /** |
220 /** |
221 * Returns the absolut TuioTime representing the session start. |
221 * Returns the absolut TuioTime representing the session start. |
222 * @return the absolut TuioTime representing the session start |
222 * @return the absolut TuioTime representing the session start |
223 */ |
223 */ |
224 public static TuioTime getStartTime() { |
224 public static TuioTime getStartTime() { |
225 return new TuioTime(start_seconds,start_micro_seconds); |
225 return new TuioTime(start_seconds,start_micro_seconds); |
226 } |
226 } |
227 |
227 |
228 /** |
228 /** |
229 * Returns the absolut TuioTime representing the current system time. |
229 * Returns the absolut TuioTime representing the current system time. |
230 * @return the absolut TuioTime representing the current system time |
230 * @return the absolut TuioTime representing the current system time |
231 */ |
231 */ |
232 public static TuioTime getSystemTime() { |
232 public static TuioTime getSystemTime() { |
233 long usec = System.nanoTime()/1000; |
233 long usec = System.nanoTime()/1000; |
234 return new TuioTime(usec/1000000,usec%1000000); |
234 return new TuioTime(usec/1000000,usec%1000000); |
235 } |
235 } |
236 } |
236 } |