Commit | Line | Data |
---|---|---|
0152140d | 1 | package org.eclipse.linuxtools.lttng.jni.common; |
c357e4b6 WB |
2 | /******************************************************************************* |
3 | * Copyright (c) 2009 Ericsson | |
4 | * | |
5 | * All rights reserved. This program and the accompanying materials are | |
6 | * made available under the terms of the Eclipse Public License v1.0 which | |
7 | * accompanies this distribution, and is available at | |
8 | * http://www.eclipse.org/legal/epl-v10.html | |
9 | * | |
10 | * Contributors: | |
11 | * William Bourque (wbourque@gmail.com) - Initial API and implementation | |
12 | *******************************************************************************/ | |
0152140d ASL |
13 | |
14 | /** | |
15 | * <b><u>JniTime</u></b> | |
16 | * <p> | |
17 | * Used to store (event, trace, tracefile, ...) timestamp. | |
18 | * | |
19 | * Mimic the behavior of the LttTime C structure. | |
20 | */ | |
21 | public class JniTime extends Jni_C_Constant implements Comparable<JniTime> | |
22 | { | |
23 | private long time = 0; | |
24 | ||
25 | /** | |
26 | * Default constructor.<p> | |
27 | * | |
28 | * Note : Time will be set to 0. | |
29 | * | |
30 | */ | |
31 | public JniTime() { | |
32 | time = 0; | |
33 | } | |
34 | ||
35 | /** | |
36 | * Copy constructor. | |
37 | * | |
38 | * @param oldTime Reference to the JniTime you want to copy. | |
39 | */ | |
40 | public JniTime(JniTime oldTime) { | |
41 | time = oldTime.getTime(); | |
42 | } | |
43 | ||
44 | /** | |
45 | * Constructor with parameters.<p> | |
46 | * | |
47 | * "LTT style" constructor with Seconds et Nanoseconds | |
48 | * | |
49 | * @param newSec Seconds of the JniTime | |
50 | * @param newNanoSec Nanoseconds of the JniTime | |
51 | */ | |
52 | public JniTime(long newSec, long newNanoSec) { | |
53 | time = (newSec * NANO) + newNanoSec; | |
54 | } | |
55 | ||
56 | /** | |
57 | * Constructor with parameters.<p> | |
58 | * | |
59 | * Usual "nanosecond only" constructor. | |
60 | * | |
61 | * @param newNanoSecTime Time in nanoseconds | |
62 | */ | |
63 | public JniTime(long newNanoSecTime) { | |
64 | time = newNanoSecTime; | |
65 | } | |
66 | ||
67 | /** | |
68 | * Second of the time.<p> | |
69 | * | |
70 | * Returns seconds, i.e. multiple of 1 000 000, of the stored nanoseconds time. | |
71 | * | |
72 | * @return Second of this time. | |
73 | */ | |
74 | public long getSeconds() { | |
75 | return (time / NANO); | |
76 | } | |
77 | ||
78 | /** | |
79 | * Getter for the nanosecond of the time.<p> | |
80 | * | |
81 | * Returns nanoseconds part, i.e. modulo of 1 000 000, of the stored nanoseconds time. | |
82 | * | |
83 | * @return Nanoseconds of this time | |
84 | */ | |
85 | public long getNanoSeconds() { | |
86 | return time % NANO; | |
87 | } | |
88 | ||
89 | /** | |
90 | * Full time, in nanoseconds.<p> | |
91 | * | |
92 | * @return Complete time in nanoseconds | |
93 | */ | |
94 | public long getTime() { | |
95 | return time; | |
96 | } | |
97 | ||
98 | /** | |
99 | * Changes the current time for this object<p> | |
100 | * | |
101 | * @param newTime New time to set, in nanoseconds. | |
102 | */ | |
103 | public void setTime(long newTime) { | |
104 | time = newTime; | |
105 | } | |
106 | ||
107 | /* | |
108 | * Populate this time object | |
109 | * | |
110 | * Note: This function is called from C side. | |
111 | * | |
112 | * @param newTime The time we want to populate | |
113 | */ | |
114 | @SuppressWarnings("unused") | |
115 | private void setTimeFromC(long newTime) { | |
116 | time = newTime; | |
117 | } | |
118 | ||
119 | /** | |
120 | * Comparaison operator smaller or equal than "<=" .<p> | |
121 | * | |
122 | * @param comparedTime The time we want to compare with this one | |
123 | * | |
124 | * @return true if compared time is smaller or equal, false otherwise | |
125 | */ | |
126 | public boolean isSmallerOrEqual(JniTime comparedTime) { | |
127 | ||
128 | // NOTE : We check <= instead of just < | |
129 | // This mean the LEFT OPERAND (comparedTime) always prevails | |
130 | if (this.getTime() <= comparedTime.getTime() ) { | |
131 | return true; | |
132 | } | |
133 | else { | |
134 | return false; | |
135 | } | |
136 | } | |
137 | ||
138 | /** | |
139 | * compareTo operator.<p> | |
140 | * | |
141 | * @param right The time we want to compare with this one | |
142 | * | |
143 | * @return int of value -1, 0 or 1, as the pased argument is bigger, equal or smaller than this time | |
144 | */ | |
d4011df2 FC |
145 | @Override |
146 | public int compareTo(JniTime right) { | |
0152140d ASL |
147 | long leftTime = this.getTime(); |
148 | long rightTime = right.getTime(); | |
149 | ||
150 | if ( leftTime < rightTime ) { | |
151 | return -1; | |
152 | } | |
153 | else if ( leftTime > rightTime ) { | |
154 | return 1; | |
155 | } | |
156 | else { | |
157 | return 0; | |
158 | } | |
159 | } | |
160 | ||
e0ea56dd | 161 | /** |
64fe8e8a FC |
162 | * faster equals since it is called very often |
163 | * @param other the object to the right of this | |
164 | * @return true if the times are the same, false otherwise. | |
165 | */ | |
166 | public boolean equals(JniTime other) { | |
167 | return ((other != null) && (this.time == other.time)); | |
168 | } | |
169 | ||
170 | /** | |
171 | * Overridden equals for JniTime type | |
e0ea56dd WB |
172 | * |
173 | * @param The object we want to compare too | |
174 | * | |
175 | * @return true if the time is the same, false otherwise. | |
176 | */ | |
177 | @Override | |
64fe8e8a FC |
178 | public boolean equals(Object obj) { |
179 | if (obj instanceof JniTime) { | |
180 | return (((JniTime) obj).time == this.time); | |
181 | } | |
182 | return false; | |
183 | } | |
e0ea56dd WB |
184 | |
185 | /** | |
186 | * Overridden hash code for JniTime type | |
187 | * | |
188 | */ | |
189 | @Override | |
190 | public int hashCode() { | |
191 | return this.toString().hashCode(); | |
192 | } | |
193 | ||
194 | ||
0152140d ASL |
195 | /** |
196 | * toString() method. | |
197 | * <u>Intended to debug</u><p> | |
198 | * | |
199 | * NOTE : We output the time in the same format as LTT (seconds and nanosecond separatly) | |
200 | * | |
201 | * @return String Attributes of the object concatenated in String | |
202 | */ | |
203 | @Override | |
3b38ea61 | 204 | @SuppressWarnings("nls") |
0152140d ASL |
205 | public String toString() { |
206 | String returnData = ""; | |
207 | ||
208 | returnData += "seconds : " + this.getSeconds() + "\n"; | |
209 | returnData += "nanoSeconds : " + this.getNanoSeconds() + "\n"; | |
210 | ||
211 | return returnData; | |
212 | } | |
213 | } |