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