2010-10-26 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug309042
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.jni / src / org / eclipse / linuxtools / lttng / jni / common / JniTime.java
1 package org.eclipse.linuxtools.lttng.jni.common;
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 *******************************************************************************/
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 */
145 @Override
146 public int compareTo(JniTime right) {
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
161 /**
162 * Overrided equals for JniTime type
163 *
164 * @param The object we want to compare too
165 *
166 * @return true if the time is the same, false otherwise.
167 */
168 @Override
169 public boolean equals(Object obj) {
170 boolean returnedValue = false;
171
172 if ( obj instanceof JniTime ) {
173 if ( ((JniTime)obj).time == this.time ) {
174 returnedValue = true;
175 }
176 }
177
178 return returnedValue;
179 }
180
181 /**
182 * Overridden hash code for JniTime type
183 *
184 */
185 @Override
186 public int hashCode() {
187 return this.toString().hashCode();
188 }
189
190
191 /**
192 * toString() method.
193 * <u>Intended to debug</u><p>
194 *
195 * NOTE : We output the time in the same format as LTT (seconds and nanosecond separatly)
196 *
197 * @return String Attributes of the object concatenated in String
198 */
199 @Override
200 public String toString() {
201 String returnData = "";
202
203 returnData += "seconds : " + this.getSeconds() + "\n";
204 returnData += "nanoSeconds : " + this.getNanoSeconds() + "\n";
205
206 return returnData;
207 }
208 }
This page took 0.035358 seconds and 5 git commands to generate.