**********************************************************************/
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;
*/
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;
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);
}
--- /dev/null
+/*******************************************************************************
+ * 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));
+ }
+}
--- /dev/null
+/*******************************************************************************
+ * 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;
+ }
+}
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
--- /dev/null
+/*******************************************************************************
+ * 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$
+ }
+}
--- /dev/null
+/*******************************************************************************
+ * 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);
+ }
+
+
+}