Commit | Line | Data |
---|---|---|
8c8bf09f | 1 | /******************************************************************************* |
61759503 | 2 | * Copyright (c) 2009, 2013 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 | ||
3bd46eef | 14 | package org.eclipse.linuxtools.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 |
3bd46eef AM |
22 | * @version 1.0 |
23 | * @since 2.0 | |
4593bd5b | 24 | * |
b9e37ffd | 25 | * @see ITmfTimestamp |
8c8bf09f | 26 | */ |
56e3f319 | 27 | public class TmfTimeRange { |
8c8bf09f | 28 | |
bbc1c411 | 29 | // ------------------------------------------------------------------------ |
62d1696a | 30 | // Constants |
bbc1c411 | 31 | // ------------------------------------------------------------------------ |
62d1696a | 32 | |
d7dbf09a FC |
33 | /** |
34 | * The full possible time range | |
35 | */ | |
5f490d2f | 36 | public static final @NonNull TmfTimeRange ETERNITY = new EternityTimeRange(); |
bbc1c411 | 37 | |
d7dbf09a FC |
38 | /** |
39 | * The null time range | |
40 | */ | |
5f490d2f | 41 | public static final @NonNull TmfTimeRange NULL_RANGE = new TmfTimeRange(); |
bbc1c411 | 42 | |
43 | // ------------------------------------------------------------------------ | |
8c8bf09f | 44 | // Attributes |
bbc1c411 | 45 | // ------------------------------------------------------------------------ |
8c8bf09f | 46 | |
4593bd5b AM |
47 | private final ITmfTimestamp fStartTime; |
48 | private final ITmfTimestamp fEndTime; | |
8c8bf09f | 49 | |
bbc1c411 | 50 | // ------------------------------------------------------------------------ |
8c8bf09f | 51 | // Constructors |
bbc1c411 | 52 | // ------------------------------------------------------------------------ |
53 | ||
54 | /** | |
55 | * Default constructor | |
56 | */ | |
bbc1c411 | 57 | private TmfTimeRange() { |
4593bd5b AM |
58 | fStartTime = TmfTimestamp.BIG_BANG; |
59 | fEndTime = TmfTimestamp.BIG_BANG; | |
bbc1c411 | 60 | } |
61 | ||
62 | /** | |
63 | * Full constructor | |
4593bd5b | 64 | * |
bbc1c411 | 65 | * @param startTime start of the time range |
66 | * @param endTime end of the time range | |
67 | */ | |
085d898f | 68 | public TmfTimeRange(final ITmfTimestamp startTime, final ITmfTimestamp endTime) { |
b9e37ffd | 69 | if (startTime == null || endTime == null) { |
bbc1c411 | 70 | throw new IllegalArgumentException(); |
b9e37ffd | 71 | } |
d7dbf09a FC |
72 | fStartTime = startTime; |
73 | fEndTime = endTime; | |
bbc1c411 | 74 | } |
75 | ||
76 | /** | |
77 | * Copy constructor | |
4593bd5b | 78 | * |
bbc1c411 | 79 | * @param range the other time range |
80 | */ | |
085d898f | 81 | public TmfTimeRange(final TmfTimeRange range) { |
b9e37ffd | 82 | if (range == null) { |
bbc1c411 | 83 | throw new IllegalArgumentException(); |
b9e37ffd | 84 | } |
d7dbf09a FC |
85 | fStartTime = range.getStartTime(); |
86 | fEndTime = range.getEndTime(); | |
bbc1c411 | 87 | } |
88 | ||
89 | // ------------------------------------------------------------------------ | |
4c564a2d | 90 | // Getters |
bbc1c411 | 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 | // ------------------------------------------------------------------------ | |
8c8bf09f | 108 | // Predicates |
bbc1c411 | 109 | // ------------------------------------------------------------------------ |
110 | ||
111 | /** | |
112 | * Check if the timestamp is within the time range | |
4593bd5b | 113 | * |
2d17cf16 AM |
114 | * @param ts |
115 | * The timestamp to check | |
116 | * @return True if [startTime] <= [ts] <= [endTime] | |
bbc1c411 | 117 | */ |
085d898f | 118 | public boolean contains(final ITmfTimestamp ts) { |
bbc1c411 | 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 | |
4593bd5b | 124 | * |
2d17cf16 AM |
125 | * @param range |
126 | * The other time range | |
127 | * @return True if [range] is fully contained | |
bbc1c411 | 128 | */ |
085d898f FC |
129 | public boolean contains(final TmfTimeRange range) { |
130 | final ITmfTimestamp startTime = range.getStartTime(); | |
131 | final ITmfTimestamp endTime = range.getEndTime(); | |
bbc1c411 | 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 | |
4593bd5b | 141 | * |
bbc1c411 | 142 | * @param range the other time range |
143 | * @return the intersection time range, or null if no intersection exists | |
144 | */ | |
085d898f | 145 | public TmfTimeRange getIntersection(final TmfTimeRange range) { |
b9e37ffd | 146 | if (fStartTime.compareTo(range.fEndTime, true) > 0 || fEndTime.compareTo(range.fStartTime, true) < 0) { |
bbc1c411 | 147 | return null; // no intersection |
b9e37ffd | 148 | } |
bbc1c411 | 149 | |
4593bd5b AM |
150 | return new TmfTimeRange(fStartTime.compareTo(range.fStartTime, true) < 0 |
151 | ? range.fStartTime | |
152 | : fStartTime, fEndTime.compareTo(range.fEndTime, true) > 0 | |
153 | ? range.fEndTime | |
bbc1c411 | 154 | : fEndTime); |
155 | } | |
156 | ||
bbc1c411 | 157 | // ------------------------------------------------------------------------ |
cbd4ad82 | 158 | // Object |
bbc1c411 | 159 | // ------------------------------------------------------------------------ |
cbd4ad82 | 160 | |
bbc1c411 | 161 | @Override |
cbd4ad82 | 162 | public int hashCode() { |
bbc1c411 | 163 | final int prime = 31; |
164 | int result = 1; | |
34480e89 FC |
165 | result = prime * result + fEndTime.hashCode(); |
166 | result = prime * result + fStartTime.hashCode(); | |
cbd4ad82 FC |
167 | return result; |
168 | } | |
169 | ||
bbc1c411 | 170 | @Override |
085d898f | 171 | public boolean equals(final Object obj) { |
b9e37ffd | 172 | if (this == obj) { |
bbc1c411 | 173 | return true; |
b9e37ffd FC |
174 | } |
175 | if (obj == null) { | |
bbc1c411 | 176 | return false; |
b9e37ffd FC |
177 | } |
178 | if (!(obj instanceof TmfTimeRange)) { | |
bbc1c411 | 179 | return false; |
b9e37ffd | 180 | } |
085d898f | 181 | final TmfTimeRange other = (TmfTimeRange) obj; |
b9e37ffd | 182 | if (!fEndTime.equals(other.fEndTime)) { |
bbc1c411 | 183 | return false; |
b9e37ffd FC |
184 | } |
185 | if (!fStartTime.equals(other.fStartTime)) { | |
bbc1c411 | 186 | return false; |
b9e37ffd | 187 | } |
bbc1c411 | 188 | return true; |
cbd4ad82 FC |
189 | } |
190 | ||
bbc1c411 | 191 | @Override |
3b38ea61 | 192 | @SuppressWarnings("nls") |
bbc1c411 | 193 | public String toString() { |
619d7d7f | 194 | return "TmfTimeRange [fStartTime=" + fStartTime + ", fEndTime=" + fEndTime + "]"; |
bbc1c411 | 195 | } |
8d2e2848 | 196 | |
56e3f319 AM |
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 | } | |
8c8bf09f | 226 | } |