package com.onaro.sanscreen.client.view.tabular.value; import java.io.Serializable; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateUtils; import com.onaro.client.DateFormats; /** * Table column value representing a Date value. The date value is rounded to minute * precision (to match the default format). This value responds to sorting and toString() * properly, using the format by {@link #formatDate(Date, DateFormat)} (overridable). * * @author jmyers * */ public class DateValue implements Comparable, Serializable { private static final long serialVersionUID = 1L; protected final Date date; protected final DateFormat format; protected final String display; /** * Construct a date value, using the formatter specified in {@link #getDefaultDateFormat()}. * @param date the value's date */ public DateValue(final Date date) { this(date, getDefaultDateFormat()); } /** * Construct a date value, using a custom format. * @param date * @param format */ public DateValue(final Date date, final DateFormat format) { // Round the date value to minute precision this.date = date == null ? null : DateUtils.round(date, getDefaultDatePrecision()); this.format = format == null ? getDefaultDateFormat() : format; this.display = formatDate(this.date, this.format); } /** * Get the default date format to use for DateValue instances. * @return the default format */ protected static DateFormat getDefaultDateFormat() { return DateFormats.getDayShortDateTimeFormat(); } /** * Get the default date rounding precision. All dates will be rounded * to this precision. Normally, this precision should match the most significant * portion of the date format used. The returned value should be constants from the * {@link java.util.Calendar} class. * * @return the precision to use for the date values */ protected static int getDefaultDatePrecision() { return Calendar.MINUTE; } /** * Format the date value. If the date is null, an empty string is returned. * Otherwise the passed in formatter is used to format the date. * @param date the date value * @param format the format used to format the date * @return the formatted date string */ protected String formatDate(final Date date, final DateFormat format) { if (date == null) { return StringUtils.EMPTY; } return format.format(date); } public Date getDate() { return new Date(this.date.getTime()); } public DateFormat getFormat() { return this.format; } @Override public String toString() { return display; } @Override public int compareTo(DateValue o) { if (date == null) { if (o.date == null) return 0; else return 1; } if (o.date == null) return -1; return date.compareTo(o.date); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((date == null) ? 0 : date.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; DateValue other = (DateValue) obj; if (date == null) { if (other.date != null) return false; } else if (!date.equals(other.date)) return false; return true; } }