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 @@
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);
}
}
......
......@@ -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 @@ import static org.junit.Assert.assertEquals;
*/
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 static final DateTime TIME = new DateTime(2015, 5, 4, 15, 30, ZONE);
private static final String EXP_OUTPUT = "3:30:00 PM UTC";
private CellFormatter fmt = TimeFormatter.INSTANCE;
// 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() {
......
......@@ -74,7 +74,7 @@ public class ClusterViewMessageHandler extends UiMessageHandler {
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;
}
......