Commit | Line | Data |
---|---|---|
8c8bf09f | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2009, 2014 Ericsson |
4593bd5b | 3 | * |
bbc1c411 | 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 | |
8c8bf09f ASL |
6 | * accompanies this distribution, and is available at |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
4593bd5b | 8 | * |
8c8bf09f ASL |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
bbc1c411 | 11 | * Francois Chouinard - Updated as per TMF Event Model 1.0 |
8c8bf09f ASL |
12 | *******************************************************************************/ |
13 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.tmf.core.timestamp; |
8c8bf09f | 15 | |
5f490d2f VP |
16 | import org.eclipse.jdt.annotation.NonNull; |
17 | ||
8c8bf09f | 18 | /** |
4c564a2d | 19 | * A utility class to define and manage time ranges. |
4593bd5b | 20 | * |
b9e37ffd | 21 | * @author Francois Chouinard |
4593bd5b | 22 | * |
b9e37ffd | 23 | * @see ITmfTimestamp |
8c8bf09f | 24 | */ |
56e3f319 | 25 | public class TmfTimeRange { |
8c8bf09f | 26 | |
bbc1c411 | 27 | // ------------------------------------------------------------------------ |
62d1696a | 28 | // Constants |
bbc1c411 | 29 | // ------------------------------------------------------------------------ |
62d1696a | 30 | |
d7dbf09a FC |
31 | /** |
32 | * The full possible time range | |
33 | */ | |
5f490d2f | 34 | public static final @NonNull TmfTimeRange ETERNITY = new EternityTimeRange(); |
bbc1c411 | 35 | |
d7dbf09a FC |
36 | /** |
37 | * The null time range | |
38 | */ | |
5f490d2f | 39 | public static final @NonNull TmfTimeRange NULL_RANGE = new TmfTimeRange(); |
bbc1c411 | 40 | |
41 | // ------------------------------------------------------------------------ | |
8c8bf09f | 42 | // Attributes |
bbc1c411 | 43 | // ------------------------------------------------------------------------ |
8c8bf09f | 44 | |
4593bd5b AM |
45 | private final ITmfTimestamp fStartTime; |
46 | private final ITmfTimestamp fEndTime; | |
8c8bf09f | 47 | |
bbc1c411 | 48 | // ------------------------------------------------------------------------ |
8c8bf09f | 49 | // Constructors |
bbc1c411 | 50 | // ------------------------------------------------------------------------ |
51 | ||
52 | /** | |
53 | * Default constructor | |
54 | */ | |
bbc1c411 | 55 | private TmfTimeRange() { |
4593bd5b AM |
56 | fStartTime = TmfTimestamp.BIG_BANG; |
57 | fEndTime = TmfTimestamp.BIG_BANG; | |
bbc1c411 | 58 | } |
59 | ||
60 | /** | |
61 | * Full constructor | |
4593bd5b | 62 | * |
bbc1c411 | 63 | * @param startTime start of the time range |
64 | * @param endTime end of the time range | |
65 | */ | |
085d898f | 66 | public TmfTimeRange(final ITmfTimestamp startTime, final ITmfTimestamp endTime) { |
b9e37ffd | 67 | if (startTime == null || endTime == null) { |
bbc1c411 | 68 | throw new IllegalArgumentException(); |
b9e37ffd | 69 | } |
d7dbf09a FC |
70 | fStartTime = startTime; |
71 | fEndTime = endTime; | |
bbc1c411 | 72 | } |
73 | ||
74 | /** | |
75 | * Copy constructor | |
4593bd5b | 76 | * |
bbc1c411 | 77 | * @param range the other time range |
78 | */ | |
085d898f | 79 | public TmfTimeRange(final TmfTimeRange range) { |
b9e37ffd | 80 | if (range == null) { |
bbc1c411 | 81 | throw new IllegalArgumentException(); |
b9e37ffd | 82 | } |
d7dbf09a FC |
83 | fStartTime = range.getStartTime(); |
84 | fEndTime = range.getEndTime(); | |
bbc1c411 | 85 | } |
86 | ||
87 | // ------------------------------------------------------------------------ | |
4c564a2d | 88 | // Getters |
bbc1c411 | 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 | // ------------------------------------------------------------------------ | |
8c8bf09f | 106 | // Predicates |
bbc1c411 | 107 | // ------------------------------------------------------------------------ |
108 | ||
109 | /** | |
110 | * Check if the timestamp is within the time range | |
4593bd5b | 111 | * |
2d17cf16 AM |
112 | * @param ts |
113 | * The timestamp to check | |
114 | * @return True if [startTime] <= [ts] <= [endTime] | |
bbc1c411 | 115 | */ |
085d898f | 116 | public boolean contains(final ITmfTimestamp ts) { |
065cc19b | 117 | return (fStartTime.compareTo(ts) <= 0) && (fEndTime.compareTo(ts) >= 0); |
bbc1c411 | 118 | } |
119 | ||
120 | /** | |
121 | * Check if the time range is within the time range | |
4593bd5b | 122 | * |
2d17cf16 AM |
123 | * @param range |
124 | * The other time range | |
125 | * @return True if [range] is fully contained | |
bbc1c411 | 126 | */ |
085d898f FC |
127 | public boolean contains(final TmfTimeRange range) { |
128 | final ITmfTimestamp startTime = range.getStartTime(); | |
129 | final ITmfTimestamp endTime = range.getEndTime(); | |
065cc19b | 130 | return (fStartTime.compareTo(startTime) <= 0) && (fEndTime.compareTo(endTime) >= 0); |
bbc1c411 | 131 | } |
132 | ||
133 | // ------------------------------------------------------------------------ | |
134 | // Operations | |
135 | // ------------------------------------------------------------------------ | |
136 | ||
137 | /** | |
138 | * Get intersection of two time ranges | |
4593bd5b | 139 | * |
bbc1c411 | 140 | * @param range the other time range |
141 | * @return the intersection time range, or null if no intersection exists | |
142 | */ | |
085d898f | 143 | public TmfTimeRange getIntersection(final TmfTimeRange range) { |
065cc19b | 144 | if (fStartTime.compareTo(range.fEndTime) > 0 || fEndTime.compareTo(range.fStartTime) < 0) { |
bbc1c411 | 145 | return null; // no intersection |
b9e37ffd | 146 | } |
bbc1c411 | 147 | |
065cc19b | 148 | return new TmfTimeRange(fStartTime.compareTo(range.fStartTime) < 0 |
4593bd5b | 149 | ? range.fStartTime |
065cc19b | 150 | : fStartTime, fEndTime.compareTo(range.fEndTime) > 0 |
4593bd5b | 151 | ? range.fEndTime |
bbc1c411 | 152 | : fEndTime); |
153 | } | |
154 | ||
bbc1c411 | 155 | // ------------------------------------------------------------------------ |
cbd4ad82 | 156 | // Object |
bbc1c411 | 157 | // ------------------------------------------------------------------------ |
cbd4ad82 | 158 | |
bbc1c411 | 159 | @Override |
cbd4ad82 | 160 | public int hashCode() { |
bbc1c411 | 161 | final int prime = 31; |
162 | int result = 1; | |
34480e89 FC |
163 | result = prime * result + fEndTime.hashCode(); |
164 | result = prime * result + fStartTime.hashCode(); | |
cbd4ad82 FC |
165 | return result; |
166 | } | |
167 | ||
bbc1c411 | 168 | @Override |
085d898f | 169 | public boolean equals(final Object obj) { |
b9e37ffd | 170 | if (this == obj) { |
bbc1c411 | 171 | return true; |
b9e37ffd FC |
172 | } |
173 | if (obj == null) { | |
bbc1c411 | 174 | return false; |
b9e37ffd FC |
175 | } |
176 | if (!(obj instanceof TmfTimeRange)) { | |
bbc1c411 | 177 | return false; |
b9e37ffd | 178 | } |
085d898f | 179 | final TmfTimeRange other = (TmfTimeRange) obj; |
b9e37ffd | 180 | if (!fEndTime.equals(other.fEndTime)) { |
bbc1c411 | 181 | return false; |
b9e37ffd FC |
182 | } |
183 | if (!fStartTime.equals(other.fStartTime)) { | |
bbc1c411 | 184 | return false; |
b9e37ffd | 185 | } |
bbc1c411 | 186 | return true; |
cbd4ad82 FC |
187 | } |
188 | ||
bbc1c411 | 189 | @Override |
3b38ea61 | 190 | @SuppressWarnings("nls") |
bbc1c411 | 191 | public String toString() { |
619d7d7f | 192 | return "TmfTimeRange [fStartTime=" + fStartTime + ", fEndTime=" + fEndTime + "]"; |
bbc1c411 | 193 | } |
8d2e2848 | 194 | |
56e3f319 AM |
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 | } | |
8c8bf09f | 224 | } |