package org.eclipse.linuxtools.tmf.event;
/**
- * <b><u>TmfTimeWindow</u></b>
+ * <b><u>TmfTimeRange</u></b>
* <p>
* A utility class to define time ranges.
*/
public class TmfTimeRange {
- // ========================================================================
+ // ------------------------------------------------------------------------
// Constants
- // ========================================================================
+ // ------------------------------------------------------------------------
- public static TmfTimeRange Eternity = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
+ public static final TmfTimeRange Eternity = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
+ public static final TmfTimeRange Null = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigBang);
- // ========================================================================
+ // ------------------------------------------------------------------------
// Attributes
- // ========================================================================
+ // ------------------------------------------------------------------------
private final TmfTimestamp fStartTime;
private final TmfTimestamp fEndTime;
- // ========================================================================
+ // ------------------------------------------------------------------------
// Constructors
- // ========================================================================
+ // ------------------------------------------------------------------------
- /**
- *
- */
@SuppressWarnings("unused")
private TmfTimeRange() {
- fStartTime = null;
- fEndTime = null;
+ throw new AssertionError();
}
/**
* @param endTime
*/
public TmfTimeRange(TmfTimestamp startTime, TmfTimestamp endTime) {
- fStartTime = startTime;
- fEndTime = endTime;
+ if (startTime == null || endTime == null) {
+ throw new IllegalArgumentException();
+ }
+ fStartTime = new TmfTimestamp(startTime);
+ fEndTime = new TmfTimestamp(endTime);
}
/**
+ * Copy constructor
* @param other
*/
public TmfTimeRange(TmfTimeRange other) {
- assert(other != null);
- fStartTime = other.fStartTime;
- fEndTime = other.fEndTime;
+ if (other == null) {
+ throw new IllegalArgumentException();
+ }
+ fStartTime = new TmfTimestamp(other.fStartTime);
+ fEndTime = new TmfTimestamp(other.fEndTime);
}
- // ========================================================================
+ // ------------------------------------------------------------------------
// Accessors
- // ========================================================================
+ // ------------------------------------------------------------------------
/**
* @return The time range start time
*/
public TmfTimestamp getStartTime() {
- return fStartTime;
+ return new TmfTimestamp(fStartTime);
}
/**
* @return The time range end time
*/
public TmfTimestamp getEndTime() {
- return fEndTime;
+ return new TmfTimestamp(fEndTime);
}
- // ========================================================================
+ // ------------------------------------------------------------------------
// Predicates
- // ========================================================================
+ // ------------------------------------------------------------------------
/**
* Check if the timestamp is within the time range
* @return
*/
public boolean contains(TmfTimestamp ts) {
- boolean result = (fStartTime.compareTo(ts, true) <= 0) && (fEndTime.compareTo(ts, true) >= 0);
- return result;
+ // Zero acts as a "universal donor" timestamp
+ if (ts.equals(TmfTimestamp.Zero)) return true;
+ return (fStartTime.compareTo(ts, true) <= 0) && (fEndTime.compareTo(ts, true) >= 0);
}
- /* (non-Javadoc)
- * @see java.lang.Object#toString()
+ /**
+ * Get intersection of two time ranges
+ *
+ * @param other
+ * the other time range
+ * @return the intersection time range, or null if no intersection exists
+ */
+ public TmfTimeRange getIntersection(TmfTimeRange other)
+ {
+ if (fStartTime.compareTo(other.fEndTime, true) > 0 || fEndTime.compareTo(other.fStartTime, true) < 0)
+ return null; // no intersection
+
+ return new TmfTimeRange(
+ fStartTime.compareTo(other.fStartTime, true) < 0 ? other.fStartTime : fStartTime,
+ fEndTime.compareTo(other.fEndTime, true) > 0 ? other.fEndTime : fEndTime);
+ }
+
+ /**
+ * Check if the time range is within the time range
+ *
+ * @param range
+ * @return
*/
+ public boolean contains(TmfTimeRange range) {
+ TmfTimestamp startTime = range.getStartTime();
+ TmfTimestamp endTime = range.getEndTime();
+ return (fStartTime.compareTo(startTime, true) <= 0) && (fEndTime.compareTo(endTime, true) >= 0);
+ }
+
+ // ------------------------------------------------------------------------
+ // Object
+ // ------------------------------------------------------------------------
+
+ @Override
+ public int hashCode() {
+ int result = 17;
+ result = 37 * result + fStartTime.hashCode();
+ result = 37 * result + fEndTime.hashCode();
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (!(other instanceof TmfTimeRange))
+ return false;
+ TmfTimeRange range = (TmfTimeRange) other;
+ return range.fStartTime.equals(fStartTime) && range.fEndTime.equals(fEndTime);
+ }
+
@Override
+ @SuppressWarnings("nls")
public String toString() {
- return "[TmfTimeRange(" + fStartTime.toString() + ":" + fEndTime.toString() + ")]";
+ return "[TmfTimeRange(" + fStartTime + ":" + fEndTime + ")]";
}
}