2010-11-09 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug315307
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / event / TmfTimeRange.java
1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.event;
14
15 /**
16 * <b><u>TmfTimeRange</u></b>
17 * <p>
18 * A utility class to define time ranges.
19 */
20 public class TmfTimeRange {
21
22 // ------------------------------------------------------------------------
23 // Constants
24 // ------------------------------------------------------------------------
25
26 public static final TmfTimeRange Eternity = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
27
28 // ------------------------------------------------------------------------
29 // Attributes
30 // ------------------------------------------------------------------------
31
32 private final TmfTimestamp fStartTime;
33 private final TmfTimestamp fEndTime;
34
35 // ------------------------------------------------------------------------
36 // Constructors
37 // ------------------------------------------------------------------------
38
39 @SuppressWarnings("unused")
40 private TmfTimeRange() {
41 throw new AssertionError();
42 }
43
44 /**
45 * @param startTime
46 * @param endTime
47 */
48 public TmfTimeRange(TmfTimestamp startTime, TmfTimestamp endTime) {
49 if (startTime == null || endTime == null) {
50 throw new IllegalArgumentException();
51 }
52 fStartTime = new TmfTimestamp(startTime);
53 fEndTime = new TmfTimestamp(endTime);
54 }
55
56 /**
57 * Copy constructor
58 * @param other
59 */
60 public TmfTimeRange(TmfTimeRange other) {
61 if (other == null) {
62 throw new IllegalArgumentException();
63 }
64 fStartTime = new TmfTimestamp(other.fStartTime);
65 fEndTime = new TmfTimestamp(other.fEndTime);
66 }
67
68 // ------------------------------------------------------------------------
69 // Accessors
70 // ------------------------------------------------------------------------
71
72 /**
73 * @return The time range start time
74 */
75 public TmfTimestamp getStartTime() {
76 return new TmfTimestamp(fStartTime);
77 }
78
79 /**
80 * @return The time range end time
81 */
82 public TmfTimestamp getEndTime() {
83 return new TmfTimestamp(fEndTime);
84 }
85
86 // ------------------------------------------------------------------------
87 // Predicates
88 // ------------------------------------------------------------------------
89
90 /**
91 * Check if the timestamp is within the time range
92 *
93 * @param ts
94 * @return
95 */
96 public boolean contains(TmfTimestamp ts) {
97 // Zero acts as a "universal donor" timestamp
98 if (ts.equals(TmfTimestamp.Zero)) return true;
99 return (fStartTime.compareTo(ts, true) <= 0) && (fEndTime.compareTo(ts, true) >= 0);
100 }
101
102 /**
103 * Get intersection of two time ranges
104 *
105 * @param other
106 * the other time range
107 * @return the intersection time range, or null if no intersection exists
108 */
109 public TmfTimeRange getIntersection(TmfTimeRange other)
110 {
111 if (fStartTime.compareTo(other.fEndTime, true) > 0 || fEndTime.compareTo(other.fStartTime, true) < 0)
112 return null; // no intersection
113
114 return new TmfTimeRange(
115 fStartTime.compareTo(other.fStartTime, true) < 0 ? other.fStartTime : fStartTime,
116 fEndTime.compareTo(other.fEndTime, true) > 0 ? other.fEndTime : fEndTime);
117 }
118
119 /**
120 * Check if the time range is within the time range
121 *
122 * @param range
123 * @return
124 */
125 public boolean contains(TmfTimeRange range) {
126 TmfTimestamp startTime = range.getStartTime();
127 TmfTimestamp endTime = range.getEndTime();
128 return (fStartTime.compareTo(startTime, true) <= 0) && (fEndTime.compareTo(endTime, true) >= 0);
129 }
130
131 // ------------------------------------------------------------------------
132 // Object
133 // ------------------------------------------------------------------------
134
135 @Override
136 public int hashCode() {
137 int result = 17;
138 result = 37 * result + fStartTime.hashCode();
139 result = 37 * result + fEndTime.hashCode();
140 return result;
141 }
142
143 @Override
144 public boolean equals(Object other) {
145 if (!(other instanceof TmfTimeRange))
146 return false;
147 TmfTimeRange range = (TmfTimeRange) other;
148 return range.fStartTime.equals(fStartTime) && range.fEndTime.equals(fEndTime);
149 }
150
151 @Override
152 @SuppressWarnings("nls")
153 public String toString() {
154 return "[TmfTimeRange(" + fStartTime + ":" + fEndTime + ")]";
155 }
156
157 }
This page took 0.032894 seconds and 5 git commands to generate.