tmf: Use tabs in statistics view for each traces
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.core / src / org / eclipse / linuxtools / internal / lttng / core / event / LttngTimestamp.java
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.internal.lttng.core.event;
14
15 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
16 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
17
18 /**
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.
24 */
25 public class LttngTimestamp extends TmfTimestamp {
26
27 /**
28 * Default Constructor.<p>
29 *
30 */
31 public LttngTimestamp() {
32 this(Long.MIN_VALUE);
33 }
34
35 /**
36 * Constructor with parameters.<p>
37 *
38 * @param newEventTime Time as long, unit expected to be nanoseconds
39 */
40 public LttngTimestamp(final long newEventTime) {
41 super(newEventTime, -9, 0);
42 }
43
44 /**
45 * Copy Constructor.<p>
46 *
47 * @param oldEventTime The timestamp object we want to copy from
48 */
49 public LttngTimestamp(final ITmfTimestamp oldEventTime) {
50 this(oldEventTime.getValue());
51 }
52
53 public void setValue(final long newValue) {
54 setValue(newValue, -9, 0);
55 }
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() {
65 return formatSecs(getValue());
66 }
67
68 /**
69 * Get the nanosecond part in timestamp.<p>
70 *
71 * Note : We do not use scale and assumes contents to be in nanoseconds.
72 *
73 * @return Seconds in the object, in string.
74 */
75 public String getNanoSeconds() {
76 return formatNs(getValue());
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
126 /**
127 * toString() method.
128 *
129 * @return timestamp, as string
130 */
131 @Override
132 @SuppressWarnings("nls")
133 public String toString() {
134
135 int scale = getScale();
136 long value = getValue();
137 if (value < 0)
138 value = -value;
139
140 final StringBuilder sb = new StringBuilder(String.valueOf(value));
141
142 // Prepend the correct number of "0" so we can insert a "." at the right location
143 final int nbZeroes = (-scale) - sb.length() + 1;
144 for (int i = 0; i < nbZeroes; i++)
145 sb.insert(i, "0");
146 sb.insert(sb.length() + scale, ".");
147
148 // Prepend "-" if negative
149 if (getValue() < 0)
150 sb.insert(0, "-");
151
152 return sb.toString();
153 }
154
155 @Override
156 public LttngTimestamp clone() {
157 return (LttngTimestamp) super.clone();
158 }
159
160 /**
161 * Compute the delta between two timestamps (adjusted to scale of current timestamp).
162 *
163 * @param reference the reference timestamp to synchronize with
164 * @return the delta timestamp
165 * @throws ArithmeticException
166 */
167 @Override
168 public LttngTimestamp getDelta(final ITmfTimestamp other) {
169 final TmfTimestamp delta = (TmfTimestamp) super.getDelta(other);
170 return new LttngTimestamp(delta);
171 }
172 }
This page took 0.035148 seconds and 5 git commands to generate.