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