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