tmf.common: Add a data size and speed formatter
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Fri, 8 Apr 2016 17:55:56 +0000 (13:55 -0400)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Mon, 11 Apr 2016 14:25:59 +0000 (10:25 -0400)
This allows to format numbers representing sizes and speeds in bytes[/s] to
the closest thousand with 3 decimals. Format also includes the units.

This patch updates the kernel memory usage view to use this formatter.

Change-Id: I637f9e408d75f6a3948169d6fde5e8da596b17ce
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/70287
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
analysis/org.eclipse.tracecompass.analysis.os.linux.ui/src/org/eclipse/tracecompass/internal/analysis/os/linux/ui/views/kernelmemoryusage/KernelMemoryUsageViewer.java
common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/format/DataSizeFormatTest.java [new file with mode: 0644]
common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/format/DataSpeedFormatTest.java [new file with mode: 0644]
common/org.eclipse.tracecompass.common.core/META-INF/MANIFEST.MF
common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/format/DataSizeWithUnitFormat.java [new file with mode: 0644]
common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/format/DataSpeedWithUnitFormat.java [new file with mode: 0644]

index 11e3a97e8361a3370bc57171fffa6c05c00790a9..c54184aaa9b8b5b1024218bea516c8f111893173 100644 (file)
@@ -8,16 +8,13 @@
  **********************************************************************/
 package org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.kernelmemoryusage;
 
-import java.text.DecimalFormat;
-import java.text.FieldPosition;
-import java.text.Format;
-import java.text.ParsePosition;
 import java.util.List;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.tracecompass.analysis.os.linux.core.kernelmemoryusage.KernelMemoryAnalysisModule;
+import org.eclipse.tracecompass.common.core.format.DataSizeWithUnitFormat;
 import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Activator;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
@@ -38,47 +35,6 @@ import org.swtchart.Chart;
  */
 public class KernelMemoryUsageViewer extends TmfCommonXLineChartViewer {
 
-    /**
-     * MemoryFormat
-     *
-     * @author Matthew Khouzam
-     */
-    private static final class MemoryFormat extends Format {
-        private static final long serialVersionUID = 3934127385682676804L;
-        private static final String KB = "KB"; //$NON-NLS-1$
-        private static final String MB = "MB"; //$NON-NLS-1$
-        private static final String GB = "GB"; //$NON-NLS-1$
-        private static final String TB = "TB"; //$NON-NLS-1$
-        private static final long KILO = 1024;
-        private static final Format FORMAT = new DecimalFormat("#.###"); //$NON-NLS-1$
-
-        @Override
-        public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
-            if (obj instanceof Double) {
-                Double value = (Double) obj;
-                if (value == 0) {
-                    return toAppendTo.append("0"); //$NON-NLS-1$
-                }
-                if (value > KILO * KILO * KILO * KILO) {
-                    return toAppendTo.append(FORMAT.format(value / (KILO * KILO * KILO * KILO))).append(' ').append(TB);
-                }
-                if (value > KILO * KILO * KILO) {
-                    return toAppendTo.append(FORMAT.format(value / (KILO * KILO * KILO))).append(' ').append(GB);
-                }
-                if (value > KILO * KILO) {
-                    return toAppendTo.append(FORMAT.format(value / (KILO * KILO))).append(' ').append(MB);
-                }
-                return toAppendTo.append(FORMAT.format(value / (KILO))).append(' ').append(KB);
-            }
-            return toAppendTo;
-        }
-
-        @Override
-        public Object parseObject(String source, ParsePosition pos) {
-            return null;
-        }
-    }
-
     private static final String NOT_SELECTED = "-1"; //$NON-NLS-1$
 
     private TmfStateSystemAnalysisModule fModule = null;
@@ -93,7 +49,7 @@ public class KernelMemoryUsageViewer extends TmfCommonXLineChartViewer {
     public KernelMemoryUsageViewer(Composite parent) {
         super(parent, Messages.MemoryUsageViewer_title, Messages.MemoryUsageViewer_xAxis, Messages.MemoryUsageViewer_yAxis);
         Chart chart = getSwtChart();
-        chart.getAxisSet().getYAxis(0).getTick().setFormat(new MemoryFormat());
+        chart.getAxisSet().getYAxis(0).getTick().setFormat(new DataSizeWithUnitFormat());
         chart.getLegend().setPosition(SWT.BOTTOM);
     }
 
diff --git a/common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/format/DataSizeFormatTest.java b/common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/format/DataSizeFormatTest.java
new file mode 100644 (file)
index 0000000..f76eb7a
--- /dev/null
@@ -0,0 +1,90 @@
+/*******************************************************************************
+ * Copyright (c) 2016 École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.common.core.tests.format;
+
+import static org.junit.Assert.assertEquals;
+
+import java.text.Format;
+import java.util.Arrays;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.tracecompass.common.core.format.DataSizeWithUnitFormat;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+/**
+ * Test the {@link DataSizeWithUnitFormat} class
+ *
+ * @author Geneviève Bastien
+ */
+@RunWith(Parameterized.class)
+public class DataSizeFormatTest {
+
+    private static final @NonNull Format FORMAT = new DataSizeWithUnitFormat();
+
+    private final @NonNull Number fNumValue;
+    private final @NonNull String fExpected;
+
+    /**
+     * Constructor
+     *
+     * @param value
+     *            The numeric value to format
+     * @param expected
+     *            The expected formatted result
+     */
+    public DataSizeFormatTest(@NonNull Number value, @NonNull String expected) {
+        fNumValue = value;
+        fExpected = expected;
+    }
+
+    /**
+     * @return The arrays of parameters
+     */
+    @Parameters(name = "{index}: {0}")
+    public static Iterable<Object[]> getParameters() {
+        return Arrays.asList(new Object[][] {
+                { 0, "0" },
+                { 3, "3 B" },
+                { 975, "975 B" },
+                { 4096, "4 KB" },
+                { -4096, "-4 KB" },
+                { 4096L, "4 KB" },
+                { 4096.0, "4 KB" },
+                { 12345678, "11.774 MB" },
+                { Integer.MAX_VALUE, "2 GB" },
+                { Integer.MIN_VALUE, "-2 GB" },
+                { Long.MAX_VALUE, "8388608 TB" },
+                { 98765432.123456, "94.19 MB" },
+                { -98765432.123456, "-94.19 MB" },
+                { 555555555555L, "517.401 GB" },
+                { 555555555555555L, "505.275 TB" }
+        });
+    }
+
+    /**
+     * Get the formatted to use for the unit test
+     *
+     * @return The formatter to use for the unit test
+     */
+    protected Format getFormatter() {
+        return FORMAT;
+    }
+
+    /**
+     * Test the {@link Format#format(Object)} method
+     */
+    @Test
+    public void testFormat() {
+        assertEquals("format value", fExpected, getFormatter().format(fNumValue));
+    }
+}
diff --git a/common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/format/DataSpeedFormatTest.java b/common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/format/DataSpeedFormatTest.java
new file mode 100644 (file)
index 0000000..5db0da2
--- /dev/null
@@ -0,0 +1,43 @@
+/*******************************************************************************
+ * Copyright (c) 2016 École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.common.core.tests.format;
+
+import java.text.Format;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.tracecompass.common.core.format.DataSpeedWithUnitFormat;
+
+/**
+ * Test the {@link DataSpeedWithUnitFormat} class
+ *
+ * @author Geneviève Bastien
+ */
+public class DataSpeedFormatTest extends DataSizeFormatTest {
+
+    private static final @NonNull Format FORMAT = new DataSpeedWithUnitFormat();
+    private static final String PER_SECOND = "/s";
+
+    /**
+     * Constructor
+     *
+     * @param value
+     *            The numeric value to format
+     * @param expected
+     *            The expected formatted result
+     */
+    public DataSpeedFormatTest(@NonNull Number value, @NonNull String expected) {
+        super(value, expected + PER_SECOND);
+    }
+
+    @Override
+    protected Format getFormatter() {
+        return FORMAT;
+    }
+}
index 51ddb18ad4703560bdd30eeed98e765303a9164b..d006a9dd68a10e218e4e71a7a167e48ce0b17025 100644 (file)
@@ -12,5 +12,6 @@ Require-Bundle: org.eclipse.core.runtime,
  org.eclipse.core.resources
 Export-Package: org.eclipse.tracecompass.common.core,
  org.eclipse.tracecompass.common.core.collect,
+ org.eclipse.tracecompass.common.core.format,
  org.eclipse.tracecompass.internal.common.core;x-internal:=true
 Import-Package: com.google.common.collect
diff --git a/common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/format/DataSizeWithUnitFormat.java b/common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/format/DataSizeWithUnitFormat.java
new file mode 100644 (file)
index 0000000..ba915f4
--- /dev/null
@@ -0,0 +1,66 @@
+/*******************************************************************************
+ * Copyright (c) 2016 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.common.core.format;
+
+import java.text.DecimalFormat;
+import java.text.FieldPosition;
+import java.text.Format;
+import java.text.ParsePosition;
+
+/**
+ * Provides a formatter for data sizes along with the unit of size (KG, MB, GB
+ * ou TB). It receives a size in bytes and it formats a number in the closest
+ * thousand's unit, with at most 3 decimals.
+ *
+ * @author Matthew Khouzam
+ * @since 2.0
+ */
+public class DataSizeWithUnitFormat extends Format {
+
+    private static final long serialVersionUID = 3934127385682676804L;
+    private static final String B = "B"; //$NON-NLS-1$
+    private static final String KB = "KB"; //$NON-NLS-1$
+    private static final String MB = "MB"; //$NON-NLS-1$
+    private static final String GB = "GB"; //$NON-NLS-1$
+    private static final String TB = "TB"; //$NON-NLS-1$
+    private static final long KILO = 1024;
+    private static final Format FORMAT = new DecimalFormat("#.###"); //$NON-NLS-1$
+
+    @Override
+    public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
+        if (obj instanceof Number) {
+            Number num = (Number) obj;
+            double value = num.doubleValue();
+            double abs = Math.abs(value);
+            if (value == 0) {
+                return toAppendTo.append("0"); //$NON-NLS-1$
+            }
+            if (abs > KILO * KILO * KILO * KILO) {
+                return toAppendTo.append(FORMAT.format(value / (KILO * KILO * KILO * KILO))).append(' ').append(TB);
+            }
+            if (abs > KILO * KILO * KILO) {
+                return toAppendTo.append(FORMAT.format(value / (KILO * KILO * KILO))).append(' ').append(GB);
+            }
+            if (abs > KILO * KILO) {
+                return toAppendTo.append(FORMAT.format(value / (KILO * KILO))).append(' ').append(MB);
+            }
+            if (abs > KILO) {
+                return toAppendTo.append(FORMAT.format(value / (KILO))).append(' ').append(KB);
+            }
+            return toAppendTo.append(FORMAT.format(value)).append(' ').append(B);
+        }
+        return toAppendTo;
+    }
+
+    @Override
+    public Object parseObject(String source, ParsePosition pos) {
+        return source == null ? "" : source; //$NON-NLS-1$
+    }
+}
diff --git a/common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/format/DataSpeedWithUnitFormat.java b/common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/format/DataSpeedWithUnitFormat.java
new file mode 100644 (file)
index 0000000..73c16c4
--- /dev/null
@@ -0,0 +1,37 @@
+/*******************************************************************************
+ * Copyright (c) 2016 École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.common.core.format;
+
+import java.text.FieldPosition;
+
+/**
+ * Provides a formatter for data speeds in (XB/s). It receives a size in bytes
+ * and it will return a string precise at the closest thousand's with at most 3
+ * decimals.
+ *
+ * @author Geneviève Bastien
+ * @since 2.0
+ */
+public class DataSpeedWithUnitFormat extends DataSizeWithUnitFormat {
+
+    /**
+     *
+     */
+    private static final long serialVersionUID = -3603301320242441850L;
+
+    private static final String PER_SECOND = "/s"; //$NON-NLS-1$
+
+    @Override
+    public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
+        return super.format(obj, toAppendTo, pos).append(PER_SECOND);
+    }
+
+
+}
This page took 0.028992 seconds and 5 git commands to generate.