Re-structure LTTng sub-project as per the Linux Tools guidelines
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / event / TmfTimeRange.java
1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made 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 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.event;
14
15 /**
16 * <b><u>TmfTimeRange</u></b>
17 * <p>
18 * A utility class to define time ranges.
19 */
20 public class TmfTimeRange {
21
22 // ------------------------------------------------------------------------
23 // Constants
24 // ------------------------------------------------------------------------
25
26 public static final TmfTimeRange Eternity = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
27 public static final TmfTimeRange Null = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigBang);
28
29 // ------------------------------------------------------------------------
30 // Attributes
31 // ------------------------------------------------------------------------
32
33 private final TmfTimestamp fStartTime;
34 private final TmfTimestamp fEndTime;
35
36 // ------------------------------------------------------------------------
37 // Constructors
38 // ------------------------------------------------------------------------
39
40 @SuppressWarnings("unused")
41 private TmfTimeRange() {
42 throw new AssertionError();
43 }
44
45 /**
46 * @param startTime
47 * @param endTime
48 */
49 public TmfTimeRange(TmfTimestamp startTime, TmfTimestamp endTime) {
50 if (startTime == null || endTime == null) {
51 throw new IllegalArgumentException();
52 }
53 fStartTime = new TmfTimestamp(startTime);
54 fEndTime = new TmfTimestamp(endTime);
55 }
56
57 /**
58 * Copy constructor
59 * @param other
60 */
61 public TmfTimeRange(TmfTimeRange other) {
62 if (other == null) {
63 throw new IllegalArgumentException();
64 }
65 fStartTime = new TmfTimestamp(other.fStartTime);
66 fEndTime = new TmfTimestamp(other.fEndTime);
67 }
68
69 // ------------------------------------------------------------------------
70 // Accessors
71 // ------------------------------------------------------------------------
72
73 /**
74 * @return The time range start time
75 */
76 public TmfTimestamp getStartTime() {
77 return new TmfTimestamp(fStartTime);
78 }
79
80 /**
81 * @return The time range end time
82 */
83 public TmfTimestamp getEndTime() {
84 return new TmfTimestamp(fEndTime);
85 }
86
87 // ------------------------------------------------------------------------
88 // Predicates
89 // ------------------------------------------------------------------------
90
91 /**
92 * Check if the timestamp is within the time range
93 *
94 * @param ts
95 * @return
96 */
97 public boolean contains(TmfTimestamp ts) {
98 // Zero acts as a "universal donor" timestamp
99 if (ts.equals(TmfTimestamp.Zero)) return true;
100 return (fStartTime.compareTo(ts, true) <= 0) && (fEndTime.compareTo(ts, true) >= 0);
101 }
102
103 /**
104 * Get intersection of two time ranges
105 *
106 * @param other
107 * the other time range
108 * @return the intersection time range, or null if no intersection exists
109 */
110 public TmfTimeRange getIntersection(TmfTimeRange other)
111 {
112 if (fStartTime.compareTo(other.fEndTime, true) > 0 || fEndTime.compareTo(other.fStartTime, true) < 0)
113 return null; // no intersection
114
115 return new TmfTimeRange(
116 fStartTime.compareTo(other.fStartTime, true) < 0 ? other.fStartTime : fStartTime,
117 fEndTime.compareTo(other.fEndTime, true) > 0 ? other.fEndTime : fEndTime);
118 }
119
120 /**
121 * Check if the time range is within the time range
122 *
123 * @param range
124 * @return
125 */
126 public boolean contains(TmfTimeRange range) {
127 TmfTimestamp startTime = range.getStartTime();
128 TmfTimestamp endTime = range.getEndTime();
129 return (fStartTime.compareTo(startTime, true) <= 0) && (fEndTime.compareTo(endTime, true) >= 0);
130 }
131
132 // ------------------------------------------------------------------------
133 // Object
134 // ------------------------------------------------------------------------
135
136 @Override
137 public int hashCode() {
138 int result = 17;
139 result = 37 * result + fStartTime.hashCode();
140 result = 37 * result + fEndTime.hashCode();
141 return result;
142 }
143
144 @Override
145 public boolean equals(Object other) {
146 if (!(other instanceof TmfTimeRange))
147 return false;
148 TmfTimeRange range = (TmfTimeRange) other;
149 return range.fStartTime.equals(fStartTime) && range.fEndTime.equals(fEndTime);
150 }
151
152 @Override
153 @SuppressWarnings("nls")
154 public String toString() {
155 return "[TmfTimeRange(" + fStartTime + ":" + fEndTime + ")]";
156 }
157
158 }
This page took 0.051492 seconds and 5 git commands to generate.