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 | /** | |
951d134a | 17 | * <b><u>TmfTimeRange</u></b> |
8c8bf09f | 18 | * <p> |
4c564a2d | 19 | * A utility class to define and manage time ranges. |
8c8bf09f | 20 | */ |
34480e89 | 21 | public final class TmfTimeRange implements Cloneable { |
8c8bf09f | 22 | |
bbc1c411 | 23 | // ------------------------------------------------------------------------ |
62d1696a | 24 | // Constants |
bbc1c411 | 25 | // ------------------------------------------------------------------------ |
62d1696a | 26 | |
d7dbf09a FC |
27 | /** |
28 | * The full possible time range | |
29 | */ | |
a4115405 FC |
30 | public static final TmfTimeRange ETERNITY = |
31 | new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH); | |
bbc1c411 | 32 | |
d7dbf09a FC |
33 | /** |
34 | * The null time range | |
35 | */ | |
a4115405 FC |
36 | public static final TmfTimeRange NULL_RANGE = |
37 | new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_BANG); | |
bbc1c411 | 38 | |
39 | // ------------------------------------------------------------------------ | |
8c8bf09f | 40 | // Attributes |
bbc1c411 | 41 | // ------------------------------------------------------------------------ |
8c8bf09f | 42 | |
bbc1c411 | 43 | private ITmfTimestamp fStartTime; |
44 | private ITmfTimestamp fEndTime; | |
8c8bf09f | 45 | |
bbc1c411 | 46 | // ------------------------------------------------------------------------ |
8c8bf09f | 47 | // Constructors |
bbc1c411 | 48 | // ------------------------------------------------------------------------ |
49 | ||
50 | /** | |
51 | * Default constructor | |
52 | */ | |
53 | @SuppressWarnings("unused") | |
54 | private TmfTimeRange() { | |
bbc1c411 | 55 | } |
56 | ||
57 | /** | |
58 | * Full constructor | |
59 | * | |
60 | * @param startTime start of the time range | |
61 | * @param endTime end of the time range | |
62 | */ | |
63 | public TmfTimeRange(ITmfTimestamp startTime, ITmfTimestamp endTime) { | |
64 | if (startTime == null || endTime == null) { | |
65 | throw new IllegalArgumentException(); | |
66 | } | |
d7dbf09a FC |
67 | fStartTime = startTime; |
68 | fEndTime = endTime; | |
bbc1c411 | 69 | } |
70 | ||
71 | /** | |
72 | * Copy constructor | |
73 | * | |
74 | * @param range the other time range | |
75 | */ | |
76 | public TmfTimeRange(TmfTimeRange range) { | |
77 | if (range == null) { | |
78 | throw new IllegalArgumentException(); | |
79 | } | |
d7dbf09a FC |
80 | fStartTime = range.getStartTime(); |
81 | fEndTime = range.getEndTime(); | |
bbc1c411 | 82 | } |
83 | ||
84 | // ------------------------------------------------------------------------ | |
4c564a2d | 85 | // Getters |
bbc1c411 | 86 | // ------------------------------------------------------------------------ |
87 | ||
88 | /** | |
89 | * @return the time range start time | |
90 | */ | |
91 | public ITmfTimestamp getStartTime() { | |
92 | return fStartTime; | |
93 | } | |
94 | ||
95 | /** | |
96 | * @return the time range end time | |
97 | */ | |
98 | public ITmfTimestamp getEndTime() { | |
99 | return fEndTime; | |
100 | } | |
101 | ||
102 | // ------------------------------------------------------------------------ | |
8c8bf09f | 103 | // Predicates |
bbc1c411 | 104 | // ------------------------------------------------------------------------ |
105 | ||
106 | /** | |
107 | * Check if the timestamp is within the time range | |
108 | * | |
109 | * @param ts the timestamp to check | |
110 | * @return true if [startTime] <= [ts] <= [endTime] | |
111 | */ | |
112 | public boolean contains(ITmfTimestamp ts) { | |
113 | // Zero acts as a "universal donor" timestamp | |
a4115405 | 114 | if (ts.equals(TmfTimestamp.ZERO)) |
bbc1c411 | 115 | return true; |
116 | return (fStartTime.compareTo(ts, true) <= 0) && (fEndTime.compareTo(ts, true) >= 0); | |
117 | } | |
118 | ||
119 | /** | |
120 | * Check if the time range is within the time range | |
121 | * | |
122 | * @param range the other time range | |
123 | * @return true if [range] is fully contained | |
124 | */ | |
125 | public boolean contains(TmfTimeRange range) { | |
126 | ITmfTimestamp startTime = range.getStartTime(); | |
127 | ITmfTimestamp endTime = range.getEndTime(); | |
128 | return (fStartTime.compareTo(startTime, true) <= 0) && (fEndTime.compareTo(endTime, true) >= 0); | |
129 | } | |
130 | ||
131 | // ------------------------------------------------------------------------ | |
132 | // Operations | |
133 | // ------------------------------------------------------------------------ | |
134 | ||
135 | /** | |
136 | * Get intersection of two time ranges | |
137 | * | |
138 | * @param range the other time range | |
139 | * @return the intersection time range, or null if no intersection exists | |
140 | */ | |
141 | public TmfTimeRange getIntersection(TmfTimeRange range) { | |
142 | if (fStartTime.compareTo(range.fEndTime, true) > 0 || fEndTime.compareTo(range.fStartTime, true) < 0) | |
143 | return null; // no intersection | |
144 | ||
145 | return new TmfTimeRange(fStartTime.compareTo(range.fStartTime, true) < 0 ? range.fStartTime | |
146 | : fStartTime, fEndTime.compareTo(range.fEndTime, true) > 0 ? range.fEndTime | |
147 | : fEndTime); | |
148 | } | |
149 | ||
150 | // ------------------------------------------------------------------------ | |
151 | // Cloneable | |
152 | // ------------------------------------------------------------------------ | |
153 | ||
d7dbf09a FC |
154 | /* (non-Javadoc) |
155 | * @see java.lang.Object#clone() | |
156 | */ | |
157 | @Override | |
bbc1c411 | 158 | public TmfTimeRange clone() { |
159 | TmfTimeRange clone = null; | |
160 | try { | |
161 | clone = (TmfTimeRange) super.clone(); | |
162 | clone.fStartTime = fStartTime.clone(); | |
163 | clone.fEndTime = fEndTime.clone(); | |
164 | } | |
165 | catch (CloneNotSupportedException e) { | |
166 | } | |
167 | return clone; | |
168 | } | |
169 | ||
170 | // ------------------------------------------------------------------------ | |
cbd4ad82 | 171 | // Object |
bbc1c411 | 172 | // ------------------------------------------------------------------------ |
cbd4ad82 | 173 | |
d7dbf09a FC |
174 | /* (non-Javadoc) |
175 | * @see java.lang.Object#hashCode() | |
176 | */ | |
bbc1c411 | 177 | @Override |
cbd4ad82 | 178 | public int hashCode() { |
bbc1c411 | 179 | final int prime = 31; |
180 | int result = 1; | |
34480e89 FC |
181 | result = prime * result + fEndTime.hashCode(); |
182 | result = prime * result + fStartTime.hashCode(); | |
cbd4ad82 FC |
183 | return result; |
184 | } | |
185 | ||
d7dbf09a FC |
186 | /* (non-Javadoc) |
187 | * @see java.lang.Object#equals(java.lang.Object) | |
188 | */ | |
bbc1c411 | 189 | @Override |
190 | public boolean equals(Object obj) { | |
191 | if (this == obj) | |
192 | return true; | |
193 | if (obj == null) | |
194 | return false; | |
bd78efc6 | 195 | if (!(obj instanceof TmfTimeRange)) |
bbc1c411 | 196 | return false; |
197 | TmfTimeRange other = (TmfTimeRange) obj; | |
34480e89 | 198 | if (!fEndTime.equals(other.fEndTime)) |
bbc1c411 | 199 | return false; |
34480e89 | 200 | if (!fStartTime.equals(other.fStartTime)) |
bbc1c411 | 201 | return false; |
202 | return true; | |
cbd4ad82 FC |
203 | } |
204 | ||
d7dbf09a FC |
205 | /* (non-Javadoc) |
206 | * @see java.lang.Object#toString() | |
207 | */ | |
bbc1c411 | 208 | @Override |
3b38ea61 | 209 | @SuppressWarnings("nls") |
bbc1c411 | 210 | public String toString() { |
211 | return "TmfTimeRange [fStartTime=" + fStartTime + ", fEndTime=" + fEndTime + "]"; | |
212 | } | |
8d2e2848 | 213 | |
8c8bf09f | 214 | } |