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