Simon Hunt

GUI -- Added Locale and Time Zone decorators to TimeFormatter.

- Fixed unit test to be insensitive to the zone and locale in which it is run.

Change-Id: Ib79c1840c5543ec1a6016d4345b7d60f0e9f65a4
...@@ -18,27 +18,55 @@ ...@@ -18,27 +18,55 @@
18 package org.onosproject.ui.table.cell; 18 package org.onosproject.ui.table.cell;
19 19
20 import org.joda.time.DateTime; 20 import org.joda.time.DateTime;
21 +import org.joda.time.DateTimeZone;
21 import org.joda.time.format.DateTimeFormat; 22 import org.joda.time.format.DateTimeFormat;
22 import org.joda.time.format.DateTimeFormatter; 23 import org.joda.time.format.DateTimeFormatter;
23 -import org.onosproject.ui.table.CellFormatter; 24 +
25 +import java.util.Locale;
24 26
25 /** 27 /**
26 * Formats time values using {@link DateTimeFormatter}. 28 * Formats time values using {@link DateTimeFormatter}.
27 */ 29 */
28 public final class TimeFormatter extends AbstractCellFormatter { 30 public final class TimeFormatter extends AbstractCellFormatter {
29 31
30 - private static final DateTimeFormatter DTF = DateTimeFormat.longTime(); 32 + private DateTimeFormatter dtf;
31 33
32 - // non-instantiable 34 + // NOTE: Unlike other formatters in this package, this one is not
33 - private TimeFormatter() { } 35 + // implemented as a Singleton, because instances may be
36 + // decorated with alternate locale and/or timezone.
34 37
35 - @Override 38 + /**
36 - protected String nonNullFormat(Object value) { 39 + * Constructs a time formatter that uses the default locale and timezone.
37 - return DTF.print((DateTime) value); 40 + */
41 + public TimeFormatter() {
42 + dtf = DateTimeFormat.longTime();
38 } 43 }
39 44
40 /** 45 /**
41 - * An instance of this class. 46 + * Sets the locale to use for formatting the time.
47 + *
48 + * @param locale locale to use for formatting
49 + * @return self, for chaining
42 */ 50 */
43 - public static final CellFormatter INSTANCE = new TimeFormatter(); 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 +
44 } 72 }
......
...@@ -18,9 +18,12 @@ ...@@ -18,9 +18,12 @@
18 package org.onosproject.ui.table.cell; 18 package org.onosproject.ui.table.cell;
19 19
20 import org.joda.time.DateTime; 20 import org.joda.time.DateTime;
21 +import org.joda.time.DateTimeZone;
21 import org.junit.Test; 22 import org.junit.Test;
22 import org.onosproject.ui.table.CellFormatter; 23 import org.onosproject.ui.table.CellFormatter;
23 24
25 +import java.util.Locale;
26 +
24 import static org.junit.Assert.assertEquals; 27 import static org.junit.Assert.assertEquals;
25 28
26 /** 29 /**
...@@ -28,10 +31,17 @@ import static org.junit.Assert.assertEquals; ...@@ -28,10 +31,17 @@ import static org.junit.Assert.assertEquals;
28 */ 31 */
29 public class TimeFormatterTest { 32 public class TimeFormatterTest {
30 33
31 - private static final DateTime TIME = DateTime.parse("2010-06-30T01:20"); 34 + private static final Locale LOCALE = Locale.ENGLISH;
32 - private static final String EXP_OUTPUT = "1:20:00 AM PDT"; 35 + private static final DateTimeZone ZONE = DateTimeZone.UTC;
36 +
37 + private static final DateTime TIME = new DateTime(2015, 5, 4, 15, 30, ZONE);
38 + private static final String EXP_OUTPUT = "3:30:00 PM UTC";
33 39
34 - private CellFormatter fmt = TimeFormatter.INSTANCE; 40 + // Have to use explicit Locale and TimeZone for the unit test, so that
41 + // irrespective of which locale and time zone the test is run in, it
42 + // always produces the same result...
43 + private CellFormatter fmt =
44 + new TimeFormatter().withLocale(LOCALE).withZone(ZONE);
35 45
36 @Test 46 @Test
37 public void basic() { 47 public void basic() {
......
...@@ -74,7 +74,7 @@ public class ClusterViewMessageHandler extends UiMessageHandler { ...@@ -74,7 +74,7 @@ public class ClusterViewMessageHandler extends UiMessageHandler {
74 protected TableModel createTableModel() { 74 protected TableModel createTableModel() {
75 TableModel tm = super.createTableModel(); 75 TableModel tm = super.createTableModel();
76 tm.setComparator(TCP_PORT, IntComparator.INSTANCE); 76 tm.setComparator(TCP_PORT, IntComparator.INSTANCE);
77 - tm.setFormatter(UPDATED, TimeFormatter.INSTANCE); 77 + tm.setFormatter(UPDATED, new TimeFormatter());
78 return tm; 78 return tm;
79 } 79 }
80 80
......