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