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
diff --git a/core/api/src/main/java/org/onosproject/ui/table/cell/TimeFormatter.java b/core/api/src/main/java/org/onosproject/ui/table/cell/TimeFormatter.java
index cdfdf3d..44dc194 100644
--- a/core/api/src/main/java/org/onosproject/ui/table/cell/TimeFormatter.java
+++ b/core/api/src/main/java/org/onosproject/ui/table/cell/TimeFormatter.java
@@ -18,27 +18,55 @@
package org.onosproject.ui.table.cell;
import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
-import org.onosproject.ui.table.CellFormatter;
+
+import java.util.Locale;
/**
* Formats time values using {@link DateTimeFormatter}.
*/
public final class TimeFormatter extends AbstractCellFormatter {
- private static final DateTimeFormatter DTF = DateTimeFormat.longTime();
+ private DateTimeFormatter dtf;
- // non-instantiable
- private TimeFormatter() { }
+ // NOTE: Unlike other formatters in this package, this one is not
+ // implemented as a Singleton, because instances may be
+ // decorated with alternate locale and/or timezone.
- @Override
- protected String nonNullFormat(Object value) {
- return DTF.print((DateTime) value);
+ /**
+ * Constructs a time formatter that uses the default locale and timezone.
+ */
+ public TimeFormatter() {
+ dtf = DateTimeFormat.longTime();
}
/**
- * An instance of this class.
+ * Sets the locale to use for formatting the time.
+ *
+ * @param locale locale to use for formatting
+ * @return self, for chaining
*/
- public static final CellFormatter INSTANCE = new TimeFormatter();
+ public TimeFormatter withLocale(Locale locale) {
+ dtf = dtf.withLocale(locale);
+ return this;
+ }
+
+ /**
+ * Sets the time zone to use for formatting the time.
+ *
+ * @param zone time zone to use
+ * @return self, for chaining
+ */
+ public TimeFormatter withZone(DateTimeZone zone) {
+ dtf = dtf.withZone(zone);
+ return this;
+ }
+
+ @Override
+ protected String nonNullFormat(Object value) {
+ return dtf.print((DateTime) value);
+ }
+
}
diff --git a/core/api/src/test/java/org/onosproject/ui/table/cell/TimeFormatterTest.java b/core/api/src/test/java/org/onosproject/ui/table/cell/TimeFormatterTest.java
index 924902a..4d0ca51 100644
--- a/core/api/src/test/java/org/onosproject/ui/table/cell/TimeFormatterTest.java
+++ b/core/api/src/test/java/org/onosproject/ui/table/cell/TimeFormatterTest.java
@@ -18,9 +18,12 @@
package org.onosproject.ui.table.cell;
import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
import org.junit.Test;
import org.onosproject.ui.table.CellFormatter;
+import java.util.Locale;
+
import static org.junit.Assert.assertEquals;
/**
@@ -28,10 +31,17 @@
*/
public class TimeFormatterTest {
- private static final DateTime TIME = DateTime.parse("2010-06-30T01:20");
- private static final String EXP_OUTPUT = "1:20:00 AM PDT";
+ private static final Locale LOCALE = Locale.ENGLISH;
+ private static final DateTimeZone ZONE = DateTimeZone.UTC;
- private CellFormatter fmt = TimeFormatter.INSTANCE;
+ private static final DateTime TIME = new DateTime(2015, 5, 4, 15, 30, ZONE);
+ private static final String EXP_OUTPUT = "3:30:00 PM UTC";
+
+ // Have to use explicit Locale and TimeZone for the unit test, so that
+ // irrespective of which locale and time zone the test is run in, it
+ // always produces the same result...
+ private CellFormatter fmt =
+ new TimeFormatter().withLocale(LOCALE).withZone(ZONE);
@Test
public void basic() {
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/ClusterViewMessageHandler.java b/web/gui/src/main/java/org/onosproject/ui/impl/ClusterViewMessageHandler.java
index ab0da70..eeded83 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/ClusterViewMessageHandler.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/ClusterViewMessageHandler.java
@@ -74,7 +74,7 @@
protected TableModel createTableModel() {
TableModel tm = super.createTableModel();
tm.setComparator(TCP_PORT, IntComparator.INSTANCE);
- tm.setFormatter(UPDATED, TimeFormatter.INSTANCE);
+ tm.setFormatter(UPDATED, new TimeFormatter());
return tm;
}