tmf: Fix regression in event requests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / event / TmfTimeRange.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2012 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 * Francois Chouinard - Updated as per TMF Event Model 1.0
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.event;
15
16 /**
17 * A utility class to define and manage time ranges.
18 *
19 * @version 1.0
20 * @author Francois Chouinard
21 *
22 * @see ITmfTimestamp
23 */
24 public final class TmfTimeRange {
25
26 // ------------------------------------------------------------------------
27 // Constants
28 // ------------------------------------------------------------------------
29
30 /**
31 * The full possible time range
32 */
33 public static final TmfTimeRange ETERNITY =
34 new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
35
36 /**
37 * The null time range
38 */
39 public static final TmfTimeRange NULL_RANGE = new TmfTimeRange();
40
41 // ------------------------------------------------------------------------
42 // Attributes
43 // ------------------------------------------------------------------------
44
45 private final ITmfTimestamp fStartTime;
46 private final ITmfTimestamp fEndTime;
47
48 // ------------------------------------------------------------------------
49 // Constructors
50 // ------------------------------------------------------------------------
51
52 /**
53 * Default constructor
54 */
55 private TmfTimeRange() {
56 fStartTime = TmfTimestamp.BIG_BANG;
57 fEndTime = TmfTimestamp.BIG_BANG;
58 }
59
60 /**
61 * Full constructor
62 *
63 * @param startTime start of the time range
64 * @param endTime end of the time range
65 */
66 public TmfTimeRange(final ITmfTimestamp startTime, final ITmfTimestamp endTime) {
67 if (startTime == null || endTime == null) {
68 throw new IllegalArgumentException();
69 }
70 fStartTime = startTime;
71 fEndTime = endTime;
72 }
73
74 /**
75 * Copy constructor
76 *
77 * @param range the other time range
78 */
79 public TmfTimeRange(final TmfTimeRange range) {
80 if (range == null) {
81 throw new IllegalArgumentException();
82 }
83 fStartTime = range.getStartTime();
84 fEndTime = range.getEndTime();
85 }
86
87 // ------------------------------------------------------------------------
88 // Getters
89 // ------------------------------------------------------------------------
90
91 /**
92 * @return the time range start time
93 */
94 public ITmfTimestamp getStartTime() {
95 return fStartTime;
96 }
97
98 /**
99 * @return the time range end time
100 */
101 public ITmfTimestamp getEndTime() {
102 return fEndTime;
103 }
104
105 // ------------------------------------------------------------------------
106 // Predicates
107 // ------------------------------------------------------------------------
108
109 /**
110 * Check if the timestamp is within the time range
111 *
112 * @param ts the timestamp to check
113 * @return true if [startTime] <= [ts] <= [endTime]
114 */
115 public boolean contains(final ITmfTimestamp ts) {
116 // Zero acts as a "universal donor" timestamp
117 if (ts.equals(TmfTimestamp.ZERO)) {
118 return true;
119 }
120 return (fStartTime.compareTo(ts, true) <= 0) && (fEndTime.compareTo(ts, true) >= 0);
121 }
122
123 /**
124 * Check if the time range is within the time range
125 *
126 * @param range the other time range
127 * @return true if [range] is fully contained
128 */
129 public boolean contains(final TmfTimeRange range) {
130 final ITmfTimestamp startTime = range.getStartTime();
131 final ITmfTimestamp endTime = range.getEndTime();
132 return (fStartTime.compareTo(startTime, true) <= 0) && (fEndTime.compareTo(endTime, true) >= 0);
133 }
134
135 // ------------------------------------------------------------------------
136 // Operations
137 // ------------------------------------------------------------------------
138
139 /**
140 * Get intersection of two time ranges
141 *
142 * @param range the other time range
143 * @return the intersection time range, or null if no intersection exists
144 */
145 public TmfTimeRange getIntersection(final TmfTimeRange range) {
146 if (fStartTime.compareTo(range.fEndTime, true) > 0 || fEndTime.compareTo(range.fStartTime, true) < 0) {
147 return null; // no intersection
148 }
149
150 return new TmfTimeRange(fStartTime.compareTo(range.fStartTime, true) < 0
151 ? range.fStartTime
152 : fStartTime, fEndTime.compareTo(range.fEndTime, true) > 0
153 ? range.fEndTime
154 : fEndTime);
155 }
156
157 // ------------------------------------------------------------------------
158 // Object
159 // ------------------------------------------------------------------------
160
161 /* (non-Javadoc)
162 * @see java.lang.Object#hashCode()
163 */
164 @Override
165 public int hashCode() {
166 final int prime = 31;
167 int result = 1;
168 result = prime * result + fEndTime.hashCode();
169 result = prime * result + fStartTime.hashCode();
170 return result;
171 }
172
173 /* (non-Javadoc)
174 * @see java.lang.Object#equals(java.lang.Object)
175 */
176 @Override
177 public boolean equals(final Object obj) {
178 if (this == obj) {
179 return true;
180 }
181 if (obj == null) {
182 return false;
183 }
184 if (!(obj instanceof TmfTimeRange)) {
185 return false;
186 }
187 final TmfTimeRange other = (TmfTimeRange) obj;
188 if (!fEndTime.equals(other.fEndTime)) {
189 return false;
190 }
191 if (!fStartTime.equals(other.fStartTime)) {
192 return false;
193 }
194 return true;
195 }
196
197 /* (non-Javadoc)
198 * @see java.lang.Object#toString()
199 */
200 @Override
201 @SuppressWarnings("nls")
202 public String toString() {
203 return "TmfTimeRange [fStartTime=" + fStartTime + ", fEndTime=" + fEndTime + "]";
204 }
205
206 }
This page took 0.034115 seconds and 5 git commands to generate.