Minor updates
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / event / TmfTimeRange.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2012 Ericsson
3 *
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
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
11 * Francois Chouinard - Updated as per TMF Event Model 1.0
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.event;
15
16 /**
17 * <b><u>TmfTimeRange</u></b>
18 * <p>
19 * A utility class to define and manage time ranges.
20 */
21 public final class TmfTimeRange implements Cloneable {
22
23 // ------------------------------------------------------------------------
24 // Constants
25 // ------------------------------------------------------------------------
26
27 /**
28 * The full possible time range
29 */
30 public static final TmfTimeRange ETERNITY =
31 new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
32
33 /**
34 * The null time range
35 */
36 public static final TmfTimeRange NULL_RANGE =
37 new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_BANG);
38
39 // ------------------------------------------------------------------------
40 // Attributes
41 // ------------------------------------------------------------------------
42
43 private ITmfTimestamp fStartTime;
44 private ITmfTimestamp fEndTime;
45
46 // ------------------------------------------------------------------------
47 // Constructors
48 // ------------------------------------------------------------------------
49
50 /**
51 * Default constructor
52 */
53 @SuppressWarnings("unused")
54 private TmfTimeRange() {
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 }
67 fStartTime = startTime;
68 fEndTime = endTime;
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 }
80 fStartTime = range.getStartTime();
81 fEndTime = range.getEndTime();
82 }
83
84 // ------------------------------------------------------------------------
85 // Getters
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 // ------------------------------------------------------------------------
103 // Predicates
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
114 if (ts.equals(TmfTimestamp.ZERO))
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
154 /* (non-Javadoc)
155 * @see java.lang.Object#clone()
156 */
157 @Override
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 // ------------------------------------------------------------------------
171 // Object
172 // ------------------------------------------------------------------------
173
174 /* (non-Javadoc)
175 * @see java.lang.Object#hashCode()
176 */
177 @Override
178 public int hashCode() {
179 final int prime = 31;
180 int result = 1;
181 result = prime * result + fEndTime.hashCode();
182 result = prime * result + fStartTime.hashCode();
183 return result;
184 }
185
186 /* (non-Javadoc)
187 * @see java.lang.Object#equals(java.lang.Object)
188 */
189 @Override
190 public boolean equals(Object obj) {
191 if (this == obj)
192 return true;
193 if (obj == null)
194 return false;
195 if (!(obj instanceof TmfTimeRange))
196 return false;
197 TmfTimeRange other = (TmfTimeRange) obj;
198 if (!fEndTime.equals(other.fEndTime))
199 return false;
200 if (!fStartTime.equals(other.fStartTime))
201 return false;
202 return true;
203 }
204
205 /* (non-Javadoc)
206 * @see java.lang.Object#toString()
207 */
208 @Override
209 @SuppressWarnings("nls")
210 public String toString() {
211 return "TmfTimeRange [fStartTime=" + fStartTime + ", fEndTime=" + fEndTime + "]";
212 }
213
214 }
This page took 0.038771 seconds and 5 git commands to generate.