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