Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -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. |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package org.onosproject.ui.table.cell; |
| 18 | |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 19 | |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 20 | import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_AMPM; |
| 21 | import static java.time.temporal.ChronoField.MINUTE_OF_HOUR; |
| 22 | import static java.time.temporal.ChronoField.SECOND_OF_MINUTE; |
| 23 | |
Yuta HIGUCHI | 905cc82 | 2017-10-20 16:55:21 -0700 | [diff] [blame] | 24 | import java.time.DateTimeException; |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 25 | import java.time.Instant; |
| 26 | import java.time.ZoneId; |
| 27 | import java.time.format.DateTimeFormatter; |
| 28 | import java.time.format.DateTimeFormatterBuilder; |
| 29 | import java.time.temporal.ChronoField; |
| 30 | import java.time.temporal.TemporalAccessor; |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 31 | import java.util.Locale; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 32 | |
Yuta HIGUCHI | 905cc82 | 2017-10-20 16:55:21 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 35 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Formats time values using {@link DateTimeFormatter}. |
| 38 | */ |
Simon Hunt | 66f7543 | 2015-05-06 14:07:03 -0700 | [diff] [blame] | 39 | public final class TimeFormatter extends AbstractCellFormatter { |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 40 | |
Yuta HIGUCHI | 905cc82 | 2017-10-20 16:55:21 -0700 | [diff] [blame] | 41 | private static final Logger log = LoggerFactory.getLogger(TimeFormatter.class); |
| 42 | |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 43 | private DateTimeFormatter dtf; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 44 | |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 45 | // NOTE: Unlike other formatters in this package, this one is not |
| 46 | // implemented as a Singleton, because instances may be |
| 47 | // decorated with alternate locale and/or timezone. |
Simon Hunt | 66f7543 | 2015-05-06 14:07:03 -0700 | [diff] [blame] | 48 | |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Constructs a time formatter that uses the default locale and timezone. |
| 51 | */ |
| 52 | public TimeFormatter() { |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 53 | dtf = new DateTimeFormatterBuilder() |
| 54 | .appendValue(CLOCK_HOUR_OF_AMPM) |
| 55 | .appendLiteral(':') |
| 56 | .appendValue(MINUTE_OF_HOUR, 2) |
| 57 | .optionalStart() |
| 58 | .appendLiteral(':') |
| 59 | .appendValue(SECOND_OF_MINUTE, 2) |
| 60 | .appendLiteral(' ') |
| 61 | .appendText(ChronoField.AMPM_OF_DAY) |
| 62 | .optionalStart() |
| 63 | .appendLiteral(' ') |
| 64 | .appendOffset("+HH:MM", "+00:00") |
Yuta HIGUCHI | 905cc82 | 2017-10-20 16:55:21 -0700 | [diff] [blame] | 65 | .toFormatter() |
| 66 | .withLocale(Locale.getDefault()) |
| 67 | .withZone(ZoneId.systemDefault()); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 71 | * Sets the locale to use for formatting the time. |
| 72 | * |
| 73 | * @param locale locale to use for formatting |
| 74 | * @return self, for chaining |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 75 | */ |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 76 | public TimeFormatter withLocale(Locale locale) { |
| 77 | dtf = dtf.withLocale(locale); |
| 78 | return this; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Sets the time zone to use for formatting the time. |
| 83 | * |
| 84 | * @param zone time zone to use |
| 85 | * @return self, for chaining |
| 86 | */ |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 87 | public TimeFormatter withZone(ZoneId zone) { |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 88 | dtf = dtf.withZone(zone); |
| 89 | return this; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | protected String nonNullFormat(Object value) { |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 94 | if (value instanceof TemporalAccessor) { |
Yuta HIGUCHI | 905cc82 | 2017-10-20 16:55:21 -0700 | [diff] [blame] | 95 | try { |
| 96 | return dtf.format((TemporalAccessor) value); |
| 97 | } catch (DateTimeException e) { |
| 98 | log.error("Failed formatting {} [{}]", value, value.getClass().getSimpleName(), e); |
| 99 | log.warn("dtf zone was {}", dtf.getZone()); |
| 100 | throw e; |
| 101 | } |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 102 | } else if (value instanceof org.joda.time.DateTime) { |
| 103 | return dtf.format(Instant.ofEpochMilli(((org.joda.time.DateTime) value).getMillis())); |
| 104 | } |
| 105 | // should never reach here |
| 106 | return String.valueOf(value); |
Simon Hunt | 5939e65 | 2015-05-06 16:20:23 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 109 | } |