1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.event
;
16 * <b><u>TmfTimeRange</u></b>
18 * A utility class to define time ranges.
20 public class TmfTimeRange
{
22 // ------------------------------------------------------------------------
24 // ------------------------------------------------------------------------
26 public static final TmfTimeRange Eternity
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
27 public static final TmfTimeRange Null
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigBang
);
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
33 private final TmfTimestamp fStartTime
;
34 private final TmfTimestamp fEndTime
;
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
40 @SuppressWarnings("unused")
41 private TmfTimeRange() {
42 throw new AssertionError();
49 public TmfTimeRange(TmfTimestamp startTime
, TmfTimestamp endTime
) {
50 if (startTime
== null || endTime
== null) {
51 throw new IllegalArgumentException();
53 fStartTime
= new TmfTimestamp(startTime
);
54 fEndTime
= new TmfTimestamp(endTime
);
61 public TmfTimeRange(TmfTimeRange other
) {
63 throw new IllegalArgumentException();
65 fStartTime
= new TmfTimestamp(other
.fStartTime
);
66 fEndTime
= new TmfTimestamp(other
.fEndTime
);
69 // ------------------------------------------------------------------------
71 // ------------------------------------------------------------------------
74 * @return The time range start time
76 public TmfTimestamp
getStartTime() {
77 return new TmfTimestamp(fStartTime
);
81 * @return The time range end time
83 public TmfTimestamp
getEndTime() {
84 return new TmfTimestamp(fEndTime
);
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
92 * Check if the timestamp is within the time range
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);
104 * Get intersection of two time ranges
107 * the other time range
108 * @return the intersection time range, or null if no intersection exists
110 public TmfTimeRange
getIntersection(TmfTimeRange other
)
112 if (fStartTime
.compareTo(other
.fEndTime
, true) > 0 || fEndTime
.compareTo(other
.fStartTime
, true) < 0)
113 return null; // no intersection
115 return new TmfTimeRange(
116 fStartTime
.compareTo(other
.fStartTime
, true) < 0 ? other
.fStartTime
: fStartTime
,
117 fEndTime
.compareTo(other
.fEndTime
, true) > 0 ? other
.fEndTime
: fEndTime
);
121 * Check if the time range is within the time range
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);
132 // ------------------------------------------------------------------------
134 // ------------------------------------------------------------------------
137 public int hashCode() {
139 result
= 37 * result
+ fStartTime
.hashCode();
140 result
= 37 * result
+ fEndTime
.hashCode();
145 public boolean equals(Object other
) {
146 if (!(other
instanceof TmfTimeRange
))
148 TmfTimeRange range
= (TmfTimeRange
) other
;
149 return range
.fStartTime
.equals(fStartTime
) && range
.fEndTime
.equals(fEndTime
);
153 @SuppressWarnings("nls")
154 public String
toString() {
155 return "[TmfTimeRange(" + fStartTime
+ ":" + fEndTime
+ ")]";
This page took 0.036012 seconds and 5 git commands to generate.