TMF: Add some non null annotations to TmfTimeRange
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / timestamp / TmfTimeRange.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 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.tracecompass.tmf.core.timestamp;
15
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19
20 /**
21 * A utility class to define and manage time ranges.
22 *
23 * @author Francois Chouinard
24 *
25 * @see ITmfTimestamp
26 */
27 @NonNullByDefault
28 public class TmfTimeRange {
29
30 // ------------------------------------------------------------------------
31 // Constants
32 // ------------------------------------------------------------------------
33
34 /**
35 * The full possible time range
36 */
37 public static final TmfTimeRange ETERNITY = new EternityTimeRange();
38
39 /**
40 * The null time range
41 */
42 public static final TmfTimeRange NULL_RANGE = new TmfTimeRange();
43
44 // ------------------------------------------------------------------------
45 // Attributes
46 // ------------------------------------------------------------------------
47
48 private final ITmfTimestamp fStartTime;
49 private final ITmfTimestamp fEndTime;
50
51 // ------------------------------------------------------------------------
52 // Constructors
53 // ------------------------------------------------------------------------
54
55 /**
56 * Default constructor
57 */
58 private TmfTimeRange() {
59 fStartTime = TmfTimestamp.BIG_BANG;
60 fEndTime = TmfTimestamp.BIG_BANG;
61 }
62
63 /**
64 * Full constructor
65 *
66 * @param startTime start of the time range
67 * @param endTime end of the time range
68 */
69 public TmfTimeRange(final ITmfTimestamp startTime, final ITmfTimestamp endTime) {
70 fStartTime = startTime;
71 fEndTime = endTime;
72 }
73
74 /**
75 * Copy constructor
76 *
77 * @param range the other time range
78 */
79 public TmfTimeRange(final TmfTimeRange range) {
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
110 * The timestamp to check
111 * @return True if [startTime] <= [ts] <= [endTime]
112 */
113 public boolean contains(final ITmfTimestamp ts) {
114 return (fStartTime.compareTo(ts) <= 0) && (fEndTime.compareTo(ts) >= 0);
115 }
116
117 /**
118 * Check if the time range is within the time range
119 *
120 * @param range
121 * The other time range
122 * @return True if [range] is fully contained
123 */
124 public boolean contains(final TmfTimeRange range) {
125 final ITmfTimestamp startTime = range.getStartTime();
126 final ITmfTimestamp endTime = range.getEndTime();
127 return (fStartTime.compareTo(startTime) <= 0) && (fEndTime.compareTo(endTime) >= 0);
128 }
129
130 // ------------------------------------------------------------------------
131 // Operations
132 // ------------------------------------------------------------------------
133
134 /**
135 * Get intersection of two time ranges
136 *
137 * @param range the other time range
138 * @return the intersection time range, or null if no intersection exists
139 */
140 public @Nullable TmfTimeRange getIntersection(final TmfTimeRange range) {
141 if (fStartTime.compareTo(range.fEndTime) > 0 || fEndTime.compareTo(range.fStartTime) < 0) {
142 return null; // no intersection
143 }
144
145 return new TmfTimeRange(fStartTime.compareTo(range.fStartTime) < 0
146 ? range.fStartTime
147 : fStartTime, fEndTime.compareTo(range.fEndTime) > 0
148 ? range.fEndTime
149 : fEndTime);
150 }
151
152 // ------------------------------------------------------------------------
153 // Object
154 // ------------------------------------------------------------------------
155
156 @Override
157 public int hashCode() {
158 final int prime = 31;
159 int result = 1;
160 result = prime * result + fEndTime.hashCode();
161 result = prime * result + fStartTime.hashCode();
162 return result;
163 }
164
165 @Override
166 public boolean equals(final @Nullable Object obj) {
167 if (this == obj) {
168 return true;
169 }
170 if (obj == null) {
171 return false;
172 }
173 if (!(obj instanceof TmfTimeRange)) {
174 return false;
175 }
176 final TmfTimeRange other = (TmfTimeRange) obj;
177 if (!fEndTime.equals(other.fEndTime)) {
178 return false;
179 }
180 if (!fStartTime.equals(other.fStartTime)) {
181 return false;
182 }
183 return true;
184 }
185
186 @Override
187 @SuppressWarnings("nls")
188 public String toString() {
189 return "TmfTimeRange [fStartTime=" + fStartTime + ", fEndTime=" + fEndTime + "]";
190 }
191
192 // ------------------------------------------------------------------------
193 // Inner classes
194 // ------------------------------------------------------------------------
195
196 /**
197 * "Eternity" time range, representing the largest time range possible,
198 * which includes any other time range or timestamp.
199 */
200 private static final class EternityTimeRange extends TmfTimeRange {
201
202 public EternityTimeRange() {
203 super(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
204 }
205
206 @Override
207 public boolean contains(ITmfTimestamp ts) {
208 return true;
209 }
210
211 @Override
212 public boolean contains(TmfTimeRange range) {
213 return true;
214 }
215
216 @Override
217 public @NonNull TmfTimeRange getIntersection(TmfTimeRange range) {
218 return range;
219 }
220 }
221 }
This page took 0.035911 seconds and 5 git commands to generate.