tmf: Rename packages to org.eclipse.tracecompass.*
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / timestamp / TmfTimeRange.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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 * @version 1.0
23 * @since 2.0
24 *
25 * @see ITmfTimestamp
26 */
27 public class TmfTimeRange {
28
29 // ------------------------------------------------------------------------
30 // Constants
31 // ------------------------------------------------------------------------
32
33 /**
34 * The full possible time range
35 */
36 public static final @NonNull TmfTimeRange ETERNITY = new EternityTimeRange();
37
38 /**
39 * The null time range
40 */
41 public static final @NonNull TmfTimeRange NULL_RANGE = new TmfTimeRange();
42
43 // ------------------------------------------------------------------------
44 // Attributes
45 // ------------------------------------------------------------------------
46
47 private final ITmfTimestamp fStartTime;
48 private final ITmfTimestamp fEndTime;
49
50 // ------------------------------------------------------------------------
51 // Constructors
52 // ------------------------------------------------------------------------
53
54 /**
55 * Default constructor
56 */
57 private TmfTimeRange() {
58 fStartTime = TmfTimestamp.BIG_BANG;
59 fEndTime = TmfTimestamp.BIG_BANG;
60 }
61
62 /**
63 * Full constructor
64 *
65 * @param startTime start of the time range
66 * @param endTime end of the time range
67 */
68 public TmfTimeRange(final ITmfTimestamp startTime, final ITmfTimestamp endTime) {
69 if (startTime == null || endTime == null) {
70 throw new IllegalArgumentException();
71 }
72 fStartTime = startTime;
73 fEndTime = endTime;
74 }
75
76 /**
77 * Copy constructor
78 *
79 * @param range the other time range
80 */
81 public TmfTimeRange(final TmfTimeRange range) {
82 if (range == null) {
83 throw new IllegalArgumentException();
84 }
85 fStartTime = range.getStartTime();
86 fEndTime = range.getEndTime();
87 }
88
89 // ------------------------------------------------------------------------
90 // Getters
91 // ------------------------------------------------------------------------
92
93 /**
94 * @return the time range start time
95 */
96 public ITmfTimestamp getStartTime() {
97 return fStartTime;
98 }
99
100 /**
101 * @return the time range end time
102 */
103 public ITmfTimestamp getEndTime() {
104 return fEndTime;
105 }
106
107 // ------------------------------------------------------------------------
108 // Predicates
109 // ------------------------------------------------------------------------
110
111 /**
112 * Check if the timestamp is within the time range
113 *
114 * @param ts
115 * The timestamp to check
116 * @return True if [startTime] <= [ts] <= [endTime]
117 */
118 public boolean contains(final ITmfTimestamp ts) {
119 return (fStartTime.compareTo(ts, true) <= 0) && (fEndTime.compareTo(ts, true) >= 0);
120 }
121
122 /**
123 * Check if the time range is within the time range
124 *
125 * @param range
126 * 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 @Override
162 public int hashCode() {
163 final int prime = 31;
164 int result = 1;
165 result = prime * result + fEndTime.hashCode();
166 result = prime * result + fStartTime.hashCode();
167 return result;
168 }
169
170 @Override
171 public boolean equals(final Object obj) {
172 if (this == obj) {
173 return true;
174 }
175 if (obj == null) {
176 return false;
177 }
178 if (!(obj instanceof TmfTimeRange)) {
179 return false;
180 }
181 final TmfTimeRange other = (TmfTimeRange) obj;
182 if (!fEndTime.equals(other.fEndTime)) {
183 return false;
184 }
185 if (!fStartTime.equals(other.fStartTime)) {
186 return false;
187 }
188 return true;
189 }
190
191 @Override
192 @SuppressWarnings("nls")
193 public String toString() {
194 return "TmfTimeRange [fStartTime=" + fStartTime + ", fEndTime=" + fEndTime + "]";
195 }
196
197 // ------------------------------------------------------------------------
198 // Inner classes
199 // ------------------------------------------------------------------------
200
201 /**
202 * "Eternity" time range, representing the largest time range possible,
203 * which includes any other time range or timestamp.
204 */
205 private static final class EternityTimeRange extends TmfTimeRange {
206
207 public EternityTimeRange() {
208 super(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
209 }
210
211 @Override
212 public boolean contains(ITmfTimestamp ts) {
213 return true;
214 }
215
216 @Override
217 public boolean contains(TmfTimeRange range) {
218 return true;
219 }
220
221 @Override
222 public TmfTimeRange getIntersection(TmfTimeRange range) {
223 return range;
224 }
225 }
226 }
This page took 0.036165 seconds and 5 git commands to generate.