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 | 16 | import org.eclipse.jdt.annotation.NonNull; |
6cfc180e GB |
17 | import org.eclipse.jdt.annotation.NonNullByDefault; |
18 | import org.eclipse.jdt.annotation.Nullable; | |
5f490d2f | 19 | |
8c8bf09f | 20 | /** |
4c564a2d | 21 | * A utility class to define and manage time ranges. |
4593bd5b | 22 | * |
b9e37ffd | 23 | * @author Francois Chouinard |
4593bd5b | 24 | * |
b9e37ffd | 25 | * @see ITmfTimestamp |
8c8bf09f | 26 | */ |
6cfc180e | 27 | @NonNullByDefault |
56e3f319 | 28 | public class TmfTimeRange { |
8c8bf09f | 29 | |
bbc1c411 | 30 | // ------------------------------------------------------------------------ |
62d1696a | 31 | // Constants |
bbc1c411 | 32 | // ------------------------------------------------------------------------ |
62d1696a | 33 | |
d7dbf09a FC |
34 | /** |
35 | * The full possible time range | |
36 | */ | |
6cfc180e | 37 | public static final TmfTimeRange ETERNITY = new EternityTimeRange(); |
bbc1c411 | 38 | |
d7dbf09a FC |
39 | /** |
40 | * The null time range | |
41 | */ | |
b8e2854f | 42 | public static final TmfTimeRange NULL_RANGE = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_BANG); |
bbc1c411 | 43 | |
44 | // ------------------------------------------------------------------------ | |
8c8bf09f | 45 | // Attributes |
bbc1c411 | 46 | // ------------------------------------------------------------------------ |
8c8bf09f | 47 | |
4593bd5b AM |
48 | private final ITmfTimestamp fStartTime; |
49 | private final ITmfTimestamp fEndTime; | |
8c8bf09f | 50 | |
bbc1c411 | 51 | // ------------------------------------------------------------------------ |
8c8bf09f | 52 | // Constructors |
bbc1c411 | 53 | // ------------------------------------------------------------------------ |
54 | ||
bbc1c411 | 55 | /** |
56 | * Full constructor | |
4593bd5b | 57 | * |
bbc1c411 | 58 | * @param startTime start of the time range |
59 | * @param endTime end of the time range | |
60 | */ | |
085d898f | 61 | public TmfTimeRange(final ITmfTimestamp startTime, final ITmfTimestamp endTime) { |
d7dbf09a FC |
62 | fStartTime = startTime; |
63 | fEndTime = endTime; | |
bbc1c411 | 64 | } |
65 | ||
bbc1c411 | 66 | // ------------------------------------------------------------------------ |
4c564a2d | 67 | // Getters |
bbc1c411 | 68 | // ------------------------------------------------------------------------ |
69 | ||
70 | /** | |
71 | * @return the time range start time | |
72 | */ | |
73 | public ITmfTimestamp getStartTime() { | |
74 | return fStartTime; | |
75 | } | |
76 | ||
77 | /** | |
78 | * @return the time range end time | |
79 | */ | |
80 | public ITmfTimestamp getEndTime() { | |
81 | return fEndTime; | |
82 | } | |
83 | ||
84 | // ------------------------------------------------------------------------ | |
8c8bf09f | 85 | // Predicates |
bbc1c411 | 86 | // ------------------------------------------------------------------------ |
87 | ||
88 | /** | |
89 | * Check if the timestamp is within the time range | |
4593bd5b | 90 | * |
2d17cf16 AM |
91 | * @param ts |
92 | * The timestamp to check | |
93 | * @return True if [startTime] <= [ts] <= [endTime] | |
bbc1c411 | 94 | */ |
085d898f | 95 | public boolean contains(final ITmfTimestamp ts) { |
065cc19b | 96 | return (fStartTime.compareTo(ts) <= 0) && (fEndTime.compareTo(ts) >= 0); |
bbc1c411 | 97 | } |
98 | ||
99 | /** | |
100 | * Check if the time range is within the time range | |
4593bd5b | 101 | * |
2d17cf16 AM |
102 | * @param range |
103 | * The other time range | |
104 | * @return True if [range] is fully contained | |
bbc1c411 | 105 | */ |
085d898f FC |
106 | public boolean contains(final TmfTimeRange range) { |
107 | final ITmfTimestamp startTime = range.getStartTime(); | |
108 | final ITmfTimestamp endTime = range.getEndTime(); | |
065cc19b | 109 | return (fStartTime.compareTo(startTime) <= 0) && (fEndTime.compareTo(endTime) >= 0); |
bbc1c411 | 110 | } |
111 | ||
112 | // ------------------------------------------------------------------------ | |
113 | // Operations | |
114 | // ------------------------------------------------------------------------ | |
115 | ||
116 | /** | |
117 | * Get intersection of two time ranges | |
4593bd5b | 118 | * |
bbc1c411 | 119 | * @param range the other time range |
120 | * @return the intersection time range, or null if no intersection exists | |
121 | */ | |
6cfc180e | 122 | public @Nullable TmfTimeRange getIntersection(final TmfTimeRange range) { |
065cc19b | 123 | if (fStartTime.compareTo(range.fEndTime) > 0 || fEndTime.compareTo(range.fStartTime) < 0) { |
bbc1c411 | 124 | return null; // no intersection |
b9e37ffd | 125 | } |
bbc1c411 | 126 | |
065cc19b | 127 | return new TmfTimeRange(fStartTime.compareTo(range.fStartTime) < 0 |
4593bd5b | 128 | ? range.fStartTime |
065cc19b | 129 | : fStartTime, fEndTime.compareTo(range.fEndTime) > 0 |
4593bd5b | 130 | ? range.fEndTime |
bbc1c411 | 131 | : fEndTime); |
132 | } | |
133 | ||
bbc1c411 | 134 | // ------------------------------------------------------------------------ |
cbd4ad82 | 135 | // Object |
bbc1c411 | 136 | // ------------------------------------------------------------------------ |
cbd4ad82 | 137 | |
bbc1c411 | 138 | @Override |
cbd4ad82 | 139 | public int hashCode() { |
bbc1c411 | 140 | final int prime = 31; |
141 | int result = 1; | |
34480e89 FC |
142 | result = prime * result + fEndTime.hashCode(); |
143 | result = prime * result + fStartTime.hashCode(); | |
cbd4ad82 FC |
144 | return result; |
145 | } | |
146 | ||
bbc1c411 | 147 | @Override |
6cfc180e | 148 | public boolean equals(final @Nullable Object obj) { |
b9e37ffd | 149 | if (this == obj) { |
bbc1c411 | 150 | return true; |
b9e37ffd FC |
151 | } |
152 | if (obj == null) { | |
bbc1c411 | 153 | return false; |
b9e37ffd FC |
154 | } |
155 | if (!(obj instanceof TmfTimeRange)) { | |
bbc1c411 | 156 | return false; |
b9e37ffd | 157 | } |
085d898f | 158 | final TmfTimeRange other = (TmfTimeRange) obj; |
b9e37ffd | 159 | if (!fEndTime.equals(other.fEndTime)) { |
bbc1c411 | 160 | return false; |
b9e37ffd FC |
161 | } |
162 | if (!fStartTime.equals(other.fStartTime)) { | |
bbc1c411 | 163 | return false; |
b9e37ffd | 164 | } |
bbc1c411 | 165 | return true; |
cbd4ad82 FC |
166 | } |
167 | ||
bbc1c411 | 168 | @Override |
3b38ea61 | 169 | @SuppressWarnings("nls") |
bbc1c411 | 170 | public String toString() { |
619d7d7f | 171 | return "TmfTimeRange [fStartTime=" + fStartTime + ", fEndTime=" + fEndTime + "]"; |
bbc1c411 | 172 | } |
8d2e2848 | 173 | |
56e3f319 AM |
174 | // ------------------------------------------------------------------------ |
175 | // Inner classes | |
176 | // ------------------------------------------------------------------------ | |
177 | ||
178 | /** | |
179 | * "Eternity" time range, representing the largest time range possible, | |
180 | * which includes any other time range or timestamp. | |
181 | */ | |
182 | private static final class EternityTimeRange extends TmfTimeRange { | |
183 | ||
184 | public EternityTimeRange() { | |
185 | super(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH); | |
186 | } | |
187 | ||
188 | @Override | |
189 | public boolean contains(ITmfTimestamp ts) { | |
190 | return true; | |
191 | } | |
192 | ||
193 | @Override | |
194 | public boolean contains(TmfTimeRange range) { | |
195 | return true; | |
196 | } | |
197 | ||
198 | @Override | |
6cfc180e | 199 | public @NonNull TmfTimeRange getIntersection(TmfTimeRange range) { |
56e3f319 AM |
200 | return range; |
201 | } | |
202 | } | |
8c8bf09f | 203 | } |