ONOS-3755: use thousand separator for packet and byte counts, etc. Fix alignment (numbers right justified).
Change-Id: Idb407fb16a82d5e3fb6fd10a6599b263a777deb2
diff --git a/core/api/src/main/java/org/onosproject/ui/table/cell/NumberFormatter.java b/core/api/src/main/java/org/onosproject/ui/table/cell/NumberFormatter.java
index 76f4246..d1f9fab 100644
--- a/core/api/src/main/java/org/onosproject/ui/table/cell/NumberFormatter.java
+++ b/core/api/src/main/java/org/onosproject/ui/table/cell/NumberFormatter.java
@@ -16,6 +16,8 @@
package org.onosproject.ui.table.cell;
+import org.onosproject.ui.table.CellFormatter;
+
import java.text.DecimalFormat;
import java.text.NumberFormat;
@@ -24,17 +26,35 @@
*/
public final class NumberFormatter extends AbstractCellFormatter {
+ private static final String FMT_INTEGER = "#,##0";
+ private static final String FMT_5DP = "#,##0.00000";
+
+
private final NumberFormat format;
/**
- * Creates a formatter using a default decimal format.
+ * Creates a formatter using the default format (no decimal places).
+ * For example
+ * <pre>
+ * 12345 formatted as "12,345"
+ * </pre>
*/
public NumberFormatter() {
- this(new DecimalFormat("#,##0.00000"));
+ this(FMT_INTEGER);
}
/**
- * Creates a formatter using the specified format.
+ * Creates a formatter using a {@link DecimalFormat} configured with the
+ * given format string.
+ *
+ * @param decimalFormat the format string
+ */
+ public NumberFormatter(String decimalFormat) {
+ this(new DecimalFormat(decimalFormat));
+ }
+
+ /**
+ * Creates a formatter using the specified {@link NumberFormat}.
*
* @param format number format
*/
@@ -47,4 +67,23 @@
return format.format(value);
}
+ /**
+ * An instance of this class that formats as integers (no decimal places).
+ * For example
+ * <pre>
+ * 12345 formatted as "12,345"
+ * </pre>
+ */
+ public static final CellFormatter INTEGER = new NumberFormatter();
+
+ /**
+ * An instance of this class that formats to 5 decimal places.
+ * For example
+ * <pre>
+ * 12.3 formatted as "12.30000"
+ * 1234 formatted as "1,234.00000"
+ * </pre>
+ */
+ public static final CellFormatter TO_5DP = new NumberFormatter(FMT_5DP);
+
}
diff --git a/core/api/src/test/java/org/onosproject/ui/table/cell/NumberFormatterTest.java b/core/api/src/test/java/org/onosproject/ui/table/cell/NumberFormatterTest.java
new file mode 100644
index 0000000..2432977
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/ui/table/cell/NumberFormatterTest.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.ui.table.cell;
+
+import org.junit.Test;
+import org.onosproject.ui.table.CellFormatter;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for {@link NumberFormatter}.
+ */
+public class NumberFormatterTest {
+
+
+ private CellFormatter f5dp = NumberFormatter.TO_5DP;
+ private CellFormatter fInt = NumberFormatter.INTEGER;
+
+ @Test
+ public void defaultNullValue() {
+ assertEquals("default null value", "", f5dp.format(null));
+ }
+
+ @Test
+ public void defaultZero() {
+ assertEquals("default zero", "0.00000", f5dp.format(0));
+ }
+
+ @Test
+ public void defaultFifty() {
+ assertEquals("default fifty", "50.00000", f5dp.format(50));
+ }
+
+ @Test
+ public void default2G() {
+ assertEquals("default 2G", "2,000.00000", f5dp.format(2000));
+ }
+
+ @Test
+ public void integerNullValue() {
+ assertEquals("integer null value", "", fInt.format(null));
+ }
+
+ @Test
+ public void integerZero() {
+ assertEquals("integer zero", "0", fInt.format(0));
+ }
+
+ @Test
+ public void integerFifty() {
+ assertEquals("integer fifty", "50", fInt.format(50));
+ }
+
+ @Test
+ public void integer2G() {
+ assertEquals("integer 2G", "2,000", fInt.format(2000));
+ }
+
+ @Test
+ public void integer5M() {
+ assertEquals("integer 5M", "5,000,042", fInt.format(5000042));
+ }
+
+}