Commit | Line | Data |
---|---|---|
8c8bf09f | 1 | /******************************************************************************* |
bbc1c411 | 2 | * Copyright (c) 2009, 2012 Ericsson |
8c8bf09f | 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 | |
8 | * | |
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 | ||
6c13869b | 14 | package org.eclipse.linuxtools.tmf.core.event; |
8c8bf09f ASL |
15 | |
16 | /** | |
4c564a2d | 17 | * A utility class to define and manage time ranges. |
b9e37ffd | 18 | * |
b9e37ffd FC |
19 | * @version 1.0 |
20 | * @author Francois Chouinard | |
f7703ed6 | 21 | * |
b9e37ffd | 22 | * @see ITmfTimestamp |
8c8bf09f | 23 | */ |
34480e89 | 24 | public final class TmfTimeRange implements Cloneable { |
8c8bf09f | 25 | |
bbc1c411 | 26 | // ------------------------------------------------------------------------ |
62d1696a | 27 | // Constants |
bbc1c411 | 28 | // ------------------------------------------------------------------------ |
62d1696a | 29 | |
d7dbf09a FC |
30 | /** |
31 | * The full possible time range | |
32 | */ | |
a4115405 | 33 | public static final TmfTimeRange ETERNITY = |
085d898f | 34 | new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH); |
bbc1c411 | 35 | |
d7dbf09a FC |
36 | /** |
37 | * The null time range | |
38 | */ | |
a4115405 | 39 | public static final TmfTimeRange NULL_RANGE = |
4cf201de | 40 | new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_BANG); |
bbc1c411 | 41 | |
42 | // ------------------------------------------------------------------------ | |
8c8bf09f | 43 | // Attributes |
bbc1c411 | 44 | // ------------------------------------------------------------------------ |
8c8bf09f | 45 | |
bbc1c411 | 46 | private ITmfTimestamp fStartTime; |
47 | private ITmfTimestamp fEndTime; | |
8c8bf09f | 48 | |
bbc1c411 | 49 | // ------------------------------------------------------------------------ |
8c8bf09f | 50 | // Constructors |
bbc1c411 | 51 | // ------------------------------------------------------------------------ |
52 | ||
53 | /** | |
54 | * Default constructor | |
55 | */ | |
56 | @SuppressWarnings("unused") | |
57 | private TmfTimeRange() { | |
bbc1c411 | 58 | } |
59 | ||
60 | /** | |
61 | * Full constructor | |
62 | * | |
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 | |
76 | * | |
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 | |
111 | * | |
112 | * @param ts the timestamp to check | |
113 | * @return true if [startTime] <= [ts] <= [endTime] | |
114 | */ | |
085d898f | 115 | public boolean contains(final ITmfTimestamp ts) { |
bbc1c411 | 116 | // Zero acts as a "universal donor" timestamp |
b9e37ffd | 117 | if (ts.equals(TmfTimestamp.ZERO)) { |
bbc1c411 | 118 | return true; |
b9e37ffd | 119 | } |
bbc1c411 | 120 | return (fStartTime.compareTo(ts, true) <= 0) && (fEndTime.compareTo(ts, true) >= 0); |
121 | } | |
122 | ||
123 | /** | |
124 | * Check if the time range is within the time range | |
125 | * | |
126 | * @param range the other time range | |
127 | * @return true if [range] is fully contained | |
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 | |
141 | * | |
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 | |
b9e37ffd FC |
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 | ||
157 | // ------------------------------------------------------------------------ | |
158 | // Cloneable | |
159 | // ------------------------------------------------------------------------ | |
160 | ||
d7dbf09a FC |
161 | /* (non-Javadoc) |
162 | * @see java.lang.Object#clone() | |
163 | */ | |
164 | @Override | |
0316808c | 165 | public TmfTimeRange clone() throws CloneNotSupportedException { |
bbc1c411 | 166 | TmfTimeRange clone = null; |
167 | try { | |
168 | clone = (TmfTimeRange) super.clone(); | |
169 | clone.fStartTime = fStartTime.clone(); | |
170 | clone.fEndTime = fEndTime.clone(); | |
171 | } | |
085d898f | 172 | catch (final CloneNotSupportedException e) { |
bbc1c411 | 173 | } |
174 | return clone; | |
175 | } | |
176 | ||
177 | // ------------------------------------------------------------------------ | |
cbd4ad82 | 178 | // Object |
bbc1c411 | 179 | // ------------------------------------------------------------------------ |
cbd4ad82 | 180 | |
d7dbf09a FC |
181 | /* (non-Javadoc) |
182 | * @see java.lang.Object#hashCode() | |
183 | */ | |
bbc1c411 | 184 | @Override |
cbd4ad82 | 185 | public int hashCode() { |
bbc1c411 | 186 | final int prime = 31; |
187 | int result = 1; | |
34480e89 FC |
188 | result = prime * result + fEndTime.hashCode(); |
189 | result = prime * result + fStartTime.hashCode(); | |
cbd4ad82 FC |
190 | return result; |
191 | } | |
192 | ||
d7dbf09a FC |
193 | /* (non-Javadoc) |
194 | * @see java.lang.Object#equals(java.lang.Object) | |
195 | */ | |
bbc1c411 | 196 | @Override |
085d898f | 197 | public boolean equals(final Object obj) { |
b9e37ffd | 198 | if (this == obj) { |
bbc1c411 | 199 | return true; |
b9e37ffd FC |
200 | } |
201 | if (obj == null) { | |
bbc1c411 | 202 | return false; |
b9e37ffd FC |
203 | } |
204 | if (!(obj instanceof TmfTimeRange)) { | |
bbc1c411 | 205 | return false; |
b9e37ffd | 206 | } |
085d898f | 207 | final TmfTimeRange other = (TmfTimeRange) obj; |
b9e37ffd | 208 | if (!fEndTime.equals(other.fEndTime)) { |
bbc1c411 | 209 | return false; |
b9e37ffd FC |
210 | } |
211 | if (!fStartTime.equals(other.fStartTime)) { | |
bbc1c411 | 212 | return false; |
b9e37ffd | 213 | } |
bbc1c411 | 214 | return true; |
cbd4ad82 FC |
215 | } |
216 | ||
d7dbf09a FC |
217 | /* (non-Javadoc) |
218 | * @see java.lang.Object#toString() | |
219 | */ | |
bbc1c411 | 220 | @Override |
3b38ea61 | 221 | @SuppressWarnings("nls") |
bbc1c411 | 222 | public String toString() { |
619d7d7f | 223 | return "TmfTimeRange [fStartTime=" + fStartTime + ", fEndTime=" + fEndTime + "]"; |
bbc1c411 | 224 | } |
8d2e2848 | 225 | |
8c8bf09f | 226 | } |