Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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. |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package org.onosproject.ui.table.cell; |
| 18 | |
| 19 | import org.joda.time.DateTime; |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 20 | import org.joda.time.DateTimeZone; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 21 | import org.joda.time.format.DateTimeFormat; |
| 22 | import org.joda.time.format.DateTimeFormatter; |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 23 | |
| 24 | import java.util.Locale; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Formats time values using {@link DateTimeFormatter}. |
| 28 | */ |
Simon Hunt | 66f7543 | 2015-05-06 14:07:03 -0700 | [diff] [blame] | 29 | public final class TimeFormatter extends AbstractCellFormatter { |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 30 | |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 31 | private DateTimeFormatter dtf; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 32 | |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 33 | // NOTE: Unlike other formatters in this package, this one is not |
| 34 | // implemented as a Singleton, because instances may be |
| 35 | // decorated with alternate locale and/or timezone. |
Simon Hunt | 66f7543 | 2015-05-06 14:07:03 -0700 | [diff] [blame] | 36 | |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Constructs a time formatter that uses the default locale and timezone. |
| 39 | */ |
| 40 | public TimeFormatter() { |
| 41 | dtf = DateTimeFormat.longTime(); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /** |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 45 | * Sets the locale to use for formatting the time. |
| 46 | * |
| 47 | * @param locale locale to use for formatting |
| 48 | * @return self, for chaining |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 49 | */ |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 50 | public TimeFormatter withLocale(Locale locale) { |
| 51 | dtf = dtf.withLocale(locale); |
| 52 | return this; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Sets the time zone to use for formatting the time. |
| 57 | * |
| 58 | * @param zone time zone to use |
| 59 | * @return self, for chaining |
| 60 | */ |
| 61 | public TimeFormatter withZone(DateTimeZone zone) { |
| 62 | dtf = dtf.withZone(zone); |
| 63 | return this; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | protected String nonNullFormat(Object value) { |
| 68 | return dtf.print((DateTime) value); |
| 69 | } |
| 70 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 71 | } |