308c98eabb2ba3016b7c730075920c45f2a0f101
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / timestamp / TmfTimeRange.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 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.tracecompass.tmf.core.timestamp;
15
16 import org.eclipse.jdt.annotation.NonNull;
17
18 /**
19 * A utility class to define and manage time ranges.
20 *
21 * @author Francois Chouinard
22 *
23 * @see ITmfTimestamp
24 */
25 public class TmfTimeRange {
26
27 // ------------------------------------------------------------------------
28 // Constants
29 // ------------------------------------------------------------------------
30
31 /**
32 * The full possible time range
33 */
34 public static final @NonNull TmfTimeRange ETERNITY = new EternityTimeRange();
35
36 /**
37 * The null time range
38 */
39 public static final @NonNull 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
113 * The timestamp to check
114 * @return True if [startTime] <= [ts] <= [endTime]
115 */
116 public boolean contains(final ITmfTimestamp ts) {
117 return (fStartTime.compareTo(ts) <= 0) && (fEndTime.compareTo(ts) >= 0);
118 }
119
120 /**
121 * Check if the time range is within the time range
122 *
123 * @param range
124 * The other time range
125 * @return True if [range] is fully contained
126 */
127 public boolean contains(final TmfTimeRange range) {
128 final ITmfTimestamp startTime = range.getStartTime();
129 final ITmfTimestamp endTime = range.getEndTime();
130 return (fStartTime.compareTo(startTime) <= 0) && (fEndTime.compareTo(endTime) >= 0);
131 }
132
133 // ------------------------------------------------------------------------
134 // Operations
135 // ------------------------------------------------------------------------
136
137 /**
138 * Get intersection of two time ranges
139 *
140 * @param range the other time range
141 * @return the intersection time range, or null if no intersection exists
142 */
143 public TmfTimeRange getIntersection(final TmfTimeRange range) {
144 if (fStartTime.compareTo(range.fEndTime) > 0 || fEndTime.compareTo(range.fStartTime) < 0) {
145 return null; // no intersection
146 }
147
148 return new TmfTimeRange(fStartTime.compareTo(range.fStartTime) < 0
149 ? range.fStartTime
150 : fStartTime, fEndTime.compareTo(range.fEndTime) > 0
151 ? range.fEndTime
152 : fEndTime);
153 }
154
155 // ------------------------------------------------------------------------
156 // Object
157 // ------------------------------------------------------------------------
158
159 @Override
160 public int hashCode() {
161 final int prime = 31;
162 int result = 1;
163 result = prime * result + fEndTime.hashCode();
164 result = prime * result + fStartTime.hashCode();
165 return result;
166 }
167
168 @Override
169 public boolean equals(final Object obj) {
170 if (this == obj) {
171 return true;
172 }
173 if (obj == null) {
174 return false;
175 }
176 if (!(obj instanceof TmfTimeRange)) {
177 return false;
178 }
179 final TmfTimeRange other = (TmfTimeRange) obj;
180 if (!fEndTime.equals(other.fEndTime)) {
181 return false;
182 }
183 if (!fStartTime.equals(other.fStartTime)) {
184 return false;
185 }
186 return true;
187 }
188
189 @Override
190 @SuppressWarnings("nls")
191 public String toString() {
192 return "TmfTimeRange [fStartTime=" + fStartTime + ", fEndTime=" + fEndTime + "]";
193 }
194
195 // ------------------------------------------------------------------------
196 // Inner classes
197 // ------------------------------------------------------------------------
198
199 /**
200 * "Eternity" time range, representing the largest time range possible,
201 * which includes any other time range or timestamp.
202 */
203 private static final class EternityTimeRange extends TmfTimeRange {
204
205 public EternityTimeRange() {
206 super(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
207 }
208
209 @Override
210 public boolean contains(ITmfTimestamp ts) {
211 return true;
212 }
213
214 @Override
215 public boolean contains(TmfTimeRange range) {
216 return true;
217 }
218
219 @Override
220 public TmfTimeRange getIntersection(TmfTimeRange range) {
221 return range;
222 }
223 }
224 }
This page took 0.043449 seconds and 4 git commands to generate.