blob: 597a8a4e8cd3b505c1589bed3aa7d37c0260dcc1 [file] [log] [blame]
Thomas Vachuska3ece3732015-09-22 23:58:50 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska3ece3732015-09-22 23:58:50 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.ui.table.cell;
18
Simon Huntbe60dde2016-01-13 12:26:56 -080019import org.onosproject.ui.table.CellFormatter;
20
Thomas Vachuska3ece3732015-09-22 23:58:50 -070021import java.text.DecimalFormat;
22import java.text.NumberFormat;
23
24/**
25 * Formats number using the specified format string".
26 */
27public final class NumberFormatter extends AbstractCellFormatter {
28
Simon Huntbe60dde2016-01-13 12:26:56 -080029 private static final String FMT_INTEGER = "#,##0";
30 private static final String FMT_5DP = "#,##0.00000";
31
32
Thomas Vachuska3ece3732015-09-22 23:58:50 -070033 private final NumberFormat format;
34
35 /**
Simon Huntbe60dde2016-01-13 12:26:56 -080036 * Creates a formatter using the default format (no decimal places).
37 * For example
38 * <pre>
39 * 12345 formatted as "12,345"
40 * </pre>
Thomas Vachuska3ece3732015-09-22 23:58:50 -070041 */
42 public NumberFormatter() {
Simon Huntbe60dde2016-01-13 12:26:56 -080043 this(FMT_INTEGER);
Thomas Vachuska3ece3732015-09-22 23:58:50 -070044 }
45
46 /**
Simon Huntbe60dde2016-01-13 12:26:56 -080047 * Creates a formatter using a {@link DecimalFormat} configured with the
48 * given format string.
49 *
50 * @param decimalFormat the format string
51 */
52 public NumberFormatter(String decimalFormat) {
53 this(new DecimalFormat(decimalFormat));
54 }
55
56 /**
57 * Creates a formatter using the specified {@link NumberFormat}.
Thomas Vachuska3ece3732015-09-22 23:58:50 -070058 *
59 * @param format number format
60 */
61 public NumberFormatter(NumberFormat format) {
62 this.format = format;
63 }
64
65 @Override
66 protected String nonNullFormat(Object value) {
67 return format.format(value);
68 }
69
Simon Huntbe60dde2016-01-13 12:26:56 -080070 /**
71 * An instance of this class that formats as integers (no decimal places).
72 * For example
73 * <pre>
74 * 12345 formatted as "12,345"
75 * </pre>
76 */
77 public static final CellFormatter INTEGER = new NumberFormatter();
78
79 /**
80 * An instance of this class that formats to 5 decimal places.
81 * For example
82 * <pre>
83 * 12.3 formatted as "12.30000"
84 * 1234 formatted as "1,234.00000"
85 * </pre>
86 */
87 public static final CellFormatter TO_5DP = new NumberFormatter(FMT_5DP);
88
Thomas Vachuska3ece3732015-09-22 23:58:50 -070089}