analysis: make statistics show standard deviation
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.ui / src / org / eclipse / tracecompass / analysis / timing / ui / views / segmentstore / SubSecondTimeWithUnitFormat.java
CommitLineData
b23cbbfc 1/**********************************************************************
105c43bd 2 * Copyright (c) 2015, 2016 Ericsson
b23cbbfc
MAL
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
105c43bd 10package org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore;
b23cbbfc
MAL
11
12import java.text.DecimalFormat;
13import java.text.FieldPosition;
14import java.text.Format;
15import java.text.ParsePosition;
16
17import org.eclipse.jdt.annotation.Nullable;
18import org.eclipse.tracecompass.common.core.NonNullUtils;
19
20/**
105c43bd
MK
21 * Time format, it will take a time in nano seconds and convert it to a string
22 * with 3 decimals max.
b23cbbfc 23 *
105c43bd
MK
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>
b23cbbfc 31 */
105c43bd 32public final class SubSecondTimeWithUnitFormat extends Format {
b23cbbfc 33
b23cbbfc
MAL
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
a0acb044 45 private final DecimalFormat fDecimalFormat = new DecimalFormat("#.000"); //$NON-NLS-1$
b23cbbfc
MAL
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();
a0acb044
MK
57 if (Double.isNaN(formattedTime)) {
58 return appender == null ? new StringBuffer() : NonNullUtils.checkNotNull(appender.append("---")); //$NON-NLS-1$
59 }
b23cbbfc
MAL
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 }
a0acb044 71 String timeString = unit.equals(NANOSECONDS) ? Long.toString((long) formattedTime) : fDecimalFormat.format(formattedTime);
b23cbbfc
MAL
72 return appender == null ? new StringBuffer() : NonNullUtils.checkNotNull(appender.append(timeString).append(' ').append(unit));
73 }
74 return new StringBuffer();
75 }
76}
This page took 0.029733 seconds and 5 git commands to generate.