package com.onaro.util.jfc.date; import java.io.Serializable; import java.util.Date; import com.onaro.sanscreen.client.view.performance.DataSamples; /** * Immutable class that specifies a time range and possibly the * {@link DataSamples} value associated with the range. */ public class TimeSpan implements Comparable, Serializable { private static final long serialVersionUID = 1L; private final Date fromTime; private final Date toTime; private final DataSamples dataSamples; /** * Creates a {@link TimeSpan}. * @param fromTime * @param toTime * @param dataSamples */ public TimeSpan(Date fromTime, Date toTime, DataSamples dataSamples) { super(); this.fromTime = fromTime; this.toTime = toTime; this.dataSamples = dataSamples; } /** * Creates a {@link TimeSpan} with no associated {@link DataSamples}. * @param fromTime * @param toTime */ public TimeSpan(Date fromTime, Date toTime) { this(fromTime, toTime, null); } /** * Gets the start of the time span * @return start time */ public Date getFromTime() { return fromTime; } /** * Gets the end of the time span * @return end time */ public Date getToTime() { return toTime; } /** * Gets the {@link DataSamples}, if any, associated with the time span. * @return the DataSamples or {@code null} if none */ public DataSamples getDataSamples() { return dataSamples; } @Override public int hashCode() { final int PRIME = 31; int result = 1; result = PRIME * result + ((dataSamples == null) ? 0 : dataSamples.hashCode()); result = PRIME * result + ((fromTime == null) ? 0 : fromTime.hashCode()); result = PRIME * result + ((toTime == null) ? 0 : toTime.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; final TimeSpan other = (TimeSpan) obj; if (dataSamples == null) { if (other.dataSamples != null) return false; } else if (!dataSamples.equals(other.dataSamples)) return false; if (fromTime == null) { if (other.fromTime != null) return false; } else if (!fromTime.equals(other.fromTime)) return false; if (toTime == null) { if (other.toTime != null) return false; } else if (!toTime.equals(other.toTime)) return false; return true; } @Override public int compareTo(TimeSpan o) { if (fromTime == null) { if (o.fromTime == null) return 0; else return 1; } if (o.fromTime == null) return -1; int compareTo = fromTime.compareTo(o.fromTime); if (compareTo != 0) { return compareTo; } if (toTime == null) { if (o.toTime == null) return 0; else return 1; } if (o.toTime == null) return -1; return toTime.compareTo(o.toTime); } @Override public String toString() { return Messages.INSTANCE.getFromToTimeMessage(String.valueOf(fromTime), String.valueOf(toTime)); } /** * Is the provided date within this time span (inclusive)?

* * @param date date/time to test * @return true if the date occurs during time span, inclusive of from and to dates */ public boolean includes(Date date) { if (date == null) { return false; } return date.equals(getFromTime()) || date.equals(getToTime()) || (date.after(getFromTime()) && date.before(getToTime())); } /** * Is the provided date within this time span (inclusive)?

* * @param date date/time to test * @return true if the date occurs during time span, inclusive of from and to dates */ public boolean includes(long date) { return includes(new Date(date)); } }