timing.ui: add Export to TSV to tables and statistics
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.ui / src / org / eclipse / tracecompass / analysis / timing / ui / views / segmentstore / SubSecondTimeWithUnitFormat.java
1 /**********************************************************************
2 * Copyright (c) 2015, 2016 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
10 package org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore;
11
12 import java.text.DecimalFormat;
13 import java.text.FieldPosition;
14 import java.text.Format;
15 import java.text.ParsePosition;
16
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.eclipse.tracecompass.common.core.NonNullUtils;
19
20 /**
21 * Time format, it will take a time in nano seconds and convert it to a string
22 * with 3 decimals max.
23 *
24 * examples:
25 * <ul>
26 * <li>100 -> "100 ns"</li>
27 * <li>1001 -> "1.001 us" (mu)</li>
28 * <li>314159264 -> "312.159 ms"</li>
29 * <li>10000002000000 -> "1000.002 s"</li>
30 * </ul>
31 */
32 public final class SubSecondTimeWithUnitFormat extends Format {
33
34 private static final long serialVersionUID = -5147827135781459548L;
35
36 private static final String SECONDS = "s"; //$NON-NLS-1$
37 private static final String NANOSECONDS = "ns"; //$NON-NLS-1$
38 private static final String MILLISECONDS = "ms"; //$NON-NLS-1$
39 private static final String MICROSECONDS = "\u00B5" + SECONDS; //$NON-NLS-1$
40
41 private static final int NANOS_PER_SEC = 1000000000;
42 private static final int NANOS_PER_MILLI = 1000000;
43 private static final int NANOS_PER_MICRO = 1000;
44
45 private final DecimalFormat fDecimalFormat = new DecimalFormat("#.000"); //$NON-NLS-1$
46
47 @Override
48 public Object parseObject(@Nullable String source, @Nullable ParsePosition pos) {
49 return source == null ? "" : source; //$NON-NLS-1$
50 }
51
52 @Override
53 public StringBuffer format(@Nullable Object obj, @Nullable StringBuffer toAppendTo, @Nullable FieldPosition pos) {
54 final @Nullable StringBuffer appender = toAppendTo;
55 if ((obj != null) && (obj instanceof Double || obj instanceof Long)) {
56 double formattedTime = obj instanceof Long ? ((Long) obj).doubleValue() : ((Double) obj).doubleValue();
57 if (Double.isNaN(formattedTime)) {
58 return appender == null ? new StringBuffer() : NonNullUtils.checkNotNull(appender.append("---")); //$NON-NLS-1$
59 }
60 String unit = NANOSECONDS;
61 if (formattedTime >= NANOS_PER_SEC) {
62 unit = SECONDS;
63 formattedTime /= NANOS_PER_SEC;
64 } else if (formattedTime >= NANOS_PER_MILLI) {
65 unit = MILLISECONDS;
66 formattedTime /= NANOS_PER_MILLI;
67 } else if (formattedTime >= NANOS_PER_MICRO) {
68 unit = MICROSECONDS;
69 formattedTime /= NANOS_PER_MICRO;
70 }
71 if (formattedTime == 0) {
72 return appender == null ? new StringBuffer() : NonNullUtils.checkNotNull(appender.append(0));
73 }
74 String timeString = unit.equals(NANOSECONDS) ? Long.toString((long) formattedTime) : fDecimalFormat.format(formattedTime);
75 return appender == null ? new StringBuffer() : NonNullUtils.checkNotNull(appender.append(timeString).append(' ').append(unit));
76 }
77 return new StringBuffer();
78 }
79 }
This page took 0.033447 seconds and 5 git commands to generate.