Thomas Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.ui.table.cell; |
| 18 | |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 19 | import org.onosproject.ui.table.CellFormatter; |
| 20 | |
Thomas Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 21 | import java.text.DecimalFormat; |
| 22 | import java.text.NumberFormat; |
| 23 | |
| 24 | /** |
Simon Hunt | b058249 | 2016-09-20 18:26:38 -0700 | [diff] [blame] | 25 | * Formats number using the specified format string. |
Thomas Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 26 | */ |
| 27 | public final class NumberFormatter extends AbstractCellFormatter { |
| 28 | |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 29 | private static final String FMT_INTEGER = "#,##0"; |
| 30 | private static final String FMT_5DP = "#,##0.00000"; |
| 31 | |
| 32 | |
Thomas Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 33 | private final NumberFormat format; |
| 34 | |
| 35 | /** |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 36 | * Creates a formatter using the default format (no decimal places). |
| 37 | * For example |
| 38 | * <pre> |
| 39 | * 12345 formatted as "12,345" |
| 40 | * </pre> |
Thomas Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 41 | */ |
| 42 | public NumberFormatter() { |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 43 | this(FMT_INTEGER); |
Thomas Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /** |
Simon Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 47 | * 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 Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 58 | * |
| 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 Hunt | be60dde | 2016-01-13 12:26:56 -0800 | [diff] [blame] | 70 | /** |
| 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 Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 89 | } |