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
CommitLineData
8c8bf09f 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2009, 2014 Ericsson
4593bd5b 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
4593bd5b 8 *
8c8bf09f
ASL
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
2bdf0193 14package org.eclipse.tracecompass.tmf.core.timestamp;
8c8bf09f 15
5f490d2f 16import org.eclipse.jdt.annotation.NonNull;
6cfc180e
GB
17import org.eclipse.jdt.annotation.NonNullByDefault;
18import org.eclipse.jdt.annotation.Nullable;
5f490d2f 19
8c8bf09f 20/**
4c564a2d 21 * A utility class to define and manage time ranges.
4593bd5b 22 *
b9e37ffd 23 * @author Francois Chouinard
4593bd5b 24 *
b9e37ffd 25 * @see ITmfTimestamp
8c8bf09f 26 */
6cfc180e 27@NonNullByDefault
56e3f319 28public class TmfTimeRange {
8c8bf09f 29
bbc1c411 30 // ------------------------------------------------------------------------
62d1696a 31 // Constants
bbc1c411 32 // ------------------------------------------------------------------------
62d1696a 33
d7dbf09a
FC
34 /**
35 * The full possible time range
36 */
6cfc180e 37 public static final TmfTimeRange ETERNITY = new EternityTimeRange();
bbc1c411 38
d7dbf09a
FC
39 /**
40 * The null time range
41 */
6cfc180e 42 public static final TmfTimeRange NULL_RANGE = new TmfTimeRange();
bbc1c411 43
44 // ------------------------------------------------------------------------
8c8bf09f 45 // Attributes
bbc1c411 46 // ------------------------------------------------------------------------
8c8bf09f 47
4593bd5b
AM
48 private final ITmfTimestamp fStartTime;
49 private final ITmfTimestamp fEndTime;
8c8bf09f 50
bbc1c411 51 // ------------------------------------------------------------------------
8c8bf09f 52 // Constructors
bbc1c411 53 // ------------------------------------------------------------------------
54
55 /**
56 * Default constructor
57 */
bbc1c411 58 private TmfTimeRange() {
4593bd5b
AM
59 fStartTime = TmfTimestamp.BIG_BANG;
60 fEndTime = TmfTimestamp.BIG_BANG;
bbc1c411 61 }
62
63 /**
64 * Full constructor
4593bd5b 65 *
bbc1c411 66 * @param startTime start of the time range
67 * @param endTime end of the time range
68 */
085d898f 69 public TmfTimeRange(final ITmfTimestamp startTime, final ITmfTimestamp endTime) {
d7dbf09a
FC
70 fStartTime = startTime;
71 fEndTime = endTime;
bbc1c411 72 }
73
74 /**
75 * Copy constructor
4593bd5b 76 *
bbc1c411 77 * @param range the other time range
78 */
085d898f 79 public TmfTimeRange(final TmfTimeRange range) {
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
4593bd5b 108 *
2d17cf16
AM
109 * @param ts
110 * The timestamp to check
111 * @return True if [startTime] <= [ts] <= [endTime]
bbc1c411 112 */
085d898f 113 public boolean contains(final ITmfTimestamp ts) {
065cc19b 114 return (fStartTime.compareTo(ts) <= 0) && (fEndTime.compareTo(ts) >= 0);
bbc1c411 115 }
116
117 /**
118 * Check if the time range is within the time range
4593bd5b 119 *
2d17cf16
AM
120 * @param range
121 * The other time range
122 * @return True if [range] is fully contained
bbc1c411 123 */
085d898f
FC
124 public boolean contains(final TmfTimeRange range) {
125 final ITmfTimestamp startTime = range.getStartTime();
126 final ITmfTimestamp endTime = range.getEndTime();
065cc19b 127 return (fStartTime.compareTo(startTime) <= 0) && (fEndTime.compareTo(endTime) >= 0);
bbc1c411 128 }
129
130 // ------------------------------------------------------------------------
131 // Operations
132 // ------------------------------------------------------------------------
133
134 /**
135 * Get intersection of two time ranges
4593bd5b 136 *
bbc1c411 137 * @param range the other time range
138 * @return the intersection time range, or null if no intersection exists
139 */
6cfc180e 140 public @Nullable TmfTimeRange getIntersection(final TmfTimeRange range) {
065cc19b 141 if (fStartTime.compareTo(range.fEndTime) > 0 || fEndTime.compareTo(range.fStartTime) < 0) {
bbc1c411 142 return null; // no intersection
b9e37ffd 143 }
bbc1c411 144
065cc19b 145 return new TmfTimeRange(fStartTime.compareTo(range.fStartTime) < 0
4593bd5b 146 ? range.fStartTime
065cc19b 147 : fStartTime, fEndTime.compareTo(range.fEndTime) > 0
4593bd5b 148 ? range.fEndTime
bbc1c411 149 : fEndTime);
150 }
151
bbc1c411 152 // ------------------------------------------------------------------------
cbd4ad82 153 // Object
bbc1c411 154 // ------------------------------------------------------------------------
cbd4ad82 155
bbc1c411 156 @Override
cbd4ad82 157 public int hashCode() {
bbc1c411 158 final int prime = 31;
159 int result = 1;
34480e89
FC
160 result = prime * result + fEndTime.hashCode();
161 result = prime * result + fStartTime.hashCode();
cbd4ad82
FC
162 return result;
163 }
164
bbc1c411 165 @Override
6cfc180e 166 public boolean equals(final @Nullable Object obj) {
b9e37ffd 167 if (this == obj) {
bbc1c411 168 return true;
b9e37ffd
FC
169 }
170 if (obj == null) {
bbc1c411 171 return false;
b9e37ffd
FC
172 }
173 if (!(obj instanceof TmfTimeRange)) {
bbc1c411 174 return false;
b9e37ffd 175 }
085d898f 176 final TmfTimeRange other = (TmfTimeRange) obj;
b9e37ffd 177 if (!fEndTime.equals(other.fEndTime)) {
bbc1c411 178 return false;
b9e37ffd
FC
179 }
180 if (!fStartTime.equals(other.fStartTime)) {
bbc1c411 181 return false;
b9e37ffd 182 }
bbc1c411 183 return true;
cbd4ad82
FC
184 }
185
bbc1c411 186 @Override
3b38ea61 187 @SuppressWarnings("nls")
bbc1c411 188 public String toString() {
619d7d7f 189 return "TmfTimeRange [fStartTime=" + fStartTime + ", fEndTime=" + fEndTime + "]";
bbc1c411 190 }
8d2e2848 191
56e3f319
AM
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
6cfc180e 217 public @NonNull TmfTimeRange getIntersection(TmfTimeRange range) {
56e3f319
AM
218 return range;
219 }
220 }
8c8bf09f 221}
This page took 0.083804 seconds and 5 git commands to generate.