Commit | Line | Data |
---|---|---|
5d10d135 ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * William Bourque (wbourque@gmail.com) - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.lttng.event; | |
14 | ||
15 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
16 | ||
17 | /** | |
07d9e2ee FC |
18 | * <b><u>LttngTimestamp</u></b><p> |
19 | * | |
20 | * Lttng specific implementation of the TmfTimestamp.<p> | |
21 | * | |
22 | * The Lttng implementation is the same as the basic Tmf Implementation but allow construction with a TmfTimestamp or a long. | |
5d10d135 ASL |
23 | */ |
24 | public class LttngTimestamp extends TmfTimestamp { | |
25 | ||
07d9e2ee | 26 | // Required by Serializable |
98029bc9 | 27 | private static final long serialVersionUID = -7016853105162491273L; |
28b94d61 FC |
28 | |
29 | /** | |
30 | * Default Constructor.<p> | |
31 | * | |
32 | */ | |
33 | public LttngTimestamp() { | |
34 | super(Long.MIN_VALUE, (byte) -9); | |
35 | } | |
36 | ||
98029bc9 | 37 | /** |
07d9e2ee | 38 | * Constructor with parameters.<p> |
5d10d135 | 39 | * |
28b94d61 | 40 | * @param newEventTime Time as long, unit expected to be nanoseconds |
5d10d135 ASL |
41 | */ |
42 | public LttngTimestamp(long newEventTime) { | |
43 | super(newEventTime, (byte) -9); | |
44 | } | |
45 | ||
46 | /** | |
07d9e2ee | 47 | * Copy Constructor.<p> |
5d10d135 | 48 | * |
07d9e2ee | 49 | * @param oldEventTime The timestamp object we want to copy from |
5d10d135 | 50 | */ |
3fbd810a FC |
51 | public LttngTimestamp(TmfTimestamp oldEventTime) { |
52 | this(oldEventTime.getValue()); | |
5d10d135 | 53 | } |
28b94d61 FC |
54 | |
55 | @Override | |
56 | public long getValue() { | |
57 | return fValue; | |
58 | } | |
59 | ||
60 | public void setValue(long newValue) { | |
61 | fValue = newValue; | |
62 | } | |
63 | ||
5d10d135 | 64 | /** |
07d9e2ee FC |
65 | * Get the second part in timestamp.<p> |
66 | * | |
67 | * Note : We do not use scale and assumes contents to be in nano seconds. | |
5d10d135 | 68 | * |
07d9e2ee | 69 | * @return Seconds in the object, in string. |
5d10d135 ASL |
70 | */ |
71 | public String getSeconds() { | |
72 | return formatSecs(fValue); | |
73 | } | |
74 | ||
75 | /** | |
07d9e2ee FC |
76 | * Get the nanosecond part in timestamp.<p> |
77 | * | |
78 | * Note : We do not use scale and assumes contents to be in nanoseconds. | |
5d10d135 | 79 | * |
07d9e2ee | 80 | * @return Seconds in the object, in string. |
5d10d135 ASL |
81 | */ |
82 | public String getNanoSeconds() { | |
83 | return formatNs(fValue); | |
84 | } | |
28b94d61 | 85 | |
07d9e2ee FC |
86 | /* |
87 | * Use the exponent to format the second in the correct format. | |
5d10d135 ASL |
88 | */ |
89 | private String formatSecs(long time) { | |
90 | long sec = (long) (time * 1E-9); | |
91 | return String.valueOf(sec); | |
92 | } | |
93 | ||
07d9e2ee | 94 | /* |
5d10d135 | 95 | * Obtains the remainder fraction on unit Seconds of the entered value in |
07d9e2ee FC |
96 | * nanoseconds. e.g. input: 1241207054171080214 ns. |
97 | * The number of fraction seconds can be obtained by removing the last 9 digits: | |
98 | * In 1241207054, the fractional portion of seconds, expressed in ns is: 171080214 | |
5d10d135 ASL |
99 | */ |
100 | private String formatNs(long time) { | |
101 | boolean neg = time < 0; | |
102 | if (neg) { | |
103 | time = -time; | |
104 | } | |
105 | // The following approach could be used although performance | |
106 | // decreases in half. | |
107 | // String strVal = String.format("%09d", time); | |
07d9e2ee | 108 | // String tmp = strVal.substring(strVal.length() - 9) |
5d10d135 ASL |
109 | StringBuffer temp = new StringBuffer(); |
110 | long ns = time; | |
111 | ns %= 1000000000; | |
112 | if (ns < 10) { | |
9c4eb5f7 | 113 | temp.append("00000000"); //$NON-NLS-1$ |
5d10d135 | 114 | } else if (ns < 100) { |
9c4eb5f7 | 115 | temp.append("0000000"); //$NON-NLS-1$ |
5d10d135 | 116 | } else if (ns < 1000) { |
9c4eb5f7 | 117 | temp.append("000000"); //$NON-NLS-1$ |
5d10d135 | 118 | } else if (ns < 10000) { |
9c4eb5f7 | 119 | temp.append("00000"); //$NON-NLS-1$ |
5d10d135 | 120 | } else if (ns < 100000) { |
9c4eb5f7 | 121 | temp.append("0000"); //$NON-NLS-1$ |
5d10d135 | 122 | } else if (ns < 1000000) { |
9c4eb5f7 | 123 | temp.append("000"); //$NON-NLS-1$ |
5d10d135 | 124 | } else if (ns < 10000000) { |
9c4eb5f7 | 125 | temp.append("00"); //$NON-NLS-1$ |
5d10d135 | 126 | } else if (ns < 100000000) { |
9c4eb5f7 | 127 | temp.append("0"); //$NON-NLS-1$ |
5d10d135 ASL |
128 | } |
129 | ||
130 | temp.append(ns); | |
131 | return temp.toString(); | |
132 | } | |
133 | ||
3fbd810a FC |
134 | |
135 | /** | |
136 | * toString() method. | |
137 | * | |
28b94d61 | 138 | * @return timestamp, as string |
3fbd810a FC |
139 | */ |
140 | @Override | |
3b38ea61 | 141 | @SuppressWarnings("nls") |
3fbd810a FC |
142 | public String toString() { |
143 | ||
73005152 BH |
144 | long value = fValue; |
145 | if (fValue < 0) { | |
146 | value = -fValue; | |
147 | } | |
148 | ||
149 | StringBuilder sb = new StringBuilder(String.valueOf(value)); | |
3fbd810a | 150 | |
a610acec FC |
151 | // Prepend the correct number of "0" so we can insert a "." at the right location |
152 | int nbZeroes = (-fScale) - sb.length() + 1; | |
153 | for (int i = 0; i < nbZeroes; i++) { | |
154 | sb.insert(i, "0"); | |
155 | } | |
156 | sb.insert(sb.length() + fScale, "."); | |
73005152 BH |
157 | |
158 | // Prepend "-" if negative | |
159 | if (fValue < 0) { | |
160 | sb.insert(0, "-"); | |
161 | } | |
162 | ||
a610acec | 163 | return sb.toString(); |
3fbd810a | 164 | } |
1a971e96 FC |
165 | |
166 | @Override | |
167 | public LttngTimestamp clone() { | |
168 | return (LttngTimestamp) super.clone(); | |
169 | } | |
170 | ||
73005152 BH |
171 | /** |
172 | * Compute the delta between two timestamps (adjusted to scale of current timestamp). | |
173 | * | |
174 | * @param reference the reference timestamp to synchronize with | |
175 | * @return the delta timestamp | |
176 | * @throws ArithmeticException | |
177 | */ | |
178 | @Override | |
179 | public LttngTimestamp getDelta(TmfTimestamp other) throws ArithmeticException { | |
180 | TmfTimestamp delta = super.getDelta(other); | |
181 | return new LttngTimestamp(delta); | |
182 | } | |
5d10d135 | 183 | } |