1 /*******************************************************************************
2 * Copyright (c) 2009, 2012 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Updated as per TMF Event Model 1.0
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.event
;
17 * A utility class to define and manage time ranges.
20 * @author Francois Chouinard
24 public final class TmfTimeRange
{
26 // ------------------------------------------------------------------------
28 // ------------------------------------------------------------------------
31 * The full possible time range
33 public static final TmfTimeRange ETERNITY
=
34 new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
39 public static final TmfTimeRange NULL_RANGE
= new TmfTimeRange();
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
45 private final ITmfTimestamp fStartTime
;
46 private final ITmfTimestamp fEndTime
;
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
55 private TmfTimeRange() {
56 fStartTime
= TmfTimestamp
.BIG_BANG
;
57 fEndTime
= TmfTimestamp
.BIG_BANG
;
63 * @param startTime start of the time range
64 * @param endTime end of the time range
66 public TmfTimeRange(final ITmfTimestamp startTime
, final ITmfTimestamp endTime
) {
67 if (startTime
== null || endTime
== null) {
68 throw new IllegalArgumentException();
70 fStartTime
= startTime
;
77 * @param range the other time range
79 public TmfTimeRange(final TmfTimeRange range
) {
81 throw new IllegalArgumentException();
83 fStartTime
= range
.getStartTime();
84 fEndTime
= range
.getEndTime();
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
92 * @return the time range start time
94 public ITmfTimestamp
getStartTime() {
99 * @return the time range end time
101 public ITmfTimestamp
getEndTime() {
105 // ------------------------------------------------------------------------
107 // ------------------------------------------------------------------------
110 * Check if the timestamp is within the time range
112 * @param ts the timestamp to check
113 * @return true if [startTime] <= [ts] <= [endTime]
115 public boolean contains(final ITmfTimestamp ts
) {
116 // Zero acts as a "universal donor" timestamp
117 if (ts
.equals(TmfTimestamp
.ZERO
)) {
120 return (fStartTime
.compareTo(ts
, true) <= 0) && (fEndTime
.compareTo(ts
, true) >= 0);
124 * Check if the time range is within the time range
126 * @param range the other time range
127 * @return true if [range] is fully contained
129 public boolean contains(final TmfTimeRange range
) {
130 final ITmfTimestamp startTime
= range
.getStartTime();
131 final ITmfTimestamp endTime
= range
.getEndTime();
132 return (fStartTime
.compareTo(startTime
, true) <= 0) && (fEndTime
.compareTo(endTime
, true) >= 0);
135 // ------------------------------------------------------------------------
137 // ------------------------------------------------------------------------
140 * Get intersection of two time ranges
142 * @param range the other time range
143 * @return the intersection time range, or null if no intersection exists
145 public TmfTimeRange
getIntersection(final TmfTimeRange range
) {
146 if (fStartTime
.compareTo(range
.fEndTime
, true) > 0 || fEndTime
.compareTo(range
.fStartTime
, true) < 0) {
147 return null; // no intersection
150 return new TmfTimeRange(fStartTime
.compareTo(range
.fStartTime
, true) < 0
152 : fStartTime
, fEndTime
.compareTo(range
.fEndTime
, true) > 0
157 // ------------------------------------------------------------------------
159 // ------------------------------------------------------------------------
162 * @see java.lang.Object#hashCode()
165 public int hashCode() {
166 final int prime
= 31;
168 result
= prime
* result
+ fEndTime
.hashCode();
169 result
= prime
* result
+ fStartTime
.hashCode();
174 * @see java.lang.Object#equals(java.lang.Object)
177 public boolean equals(final Object obj
) {
184 if (!(obj
instanceof TmfTimeRange
)) {
187 final TmfTimeRange other
= (TmfTimeRange
) obj
;
188 if (!fEndTime
.equals(other
.fEndTime
)) {
191 if (!fStartTime
.equals(other
.fStartTime
)) {
198 * @see java.lang.Object#toString()
201 @SuppressWarnings("nls")
202 public String
toString() {
203 return "TmfTimeRange [fStartTime=" + fStartTime
+ ", fEndTime=" + fEndTime
+ "]";
This page took 0.036032 seconds and 5 git commands to generate.