2010-10-06 Francois Chouinard <fchouinard@gmail.com> Fix for Bug327125
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / event / TmfTimeRange.java
CommitLineData
8c8bf09f
ASL
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
13package org.eclipse.linuxtools.tmf.event;
14
15/**
951d134a 16 * <b><u>TmfTimeRange</u></b>
8c8bf09f
ASL
17 * <p>
18 * A utility class to define time ranges.
19 */
20public class TmfTimeRange {
21
cbd4ad82 22 // ------------------------------------------------------------------------
62d1696a 23 // Constants
cbd4ad82 24 // ------------------------------------------------------------------------
62d1696a 25
cbd4ad82 26 public static final TmfTimeRange Eternity = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch);
62d1696a 27
cbd4ad82 28 // ------------------------------------------------------------------------
8c8bf09f 29 // Attributes
cbd4ad82 30 // ------------------------------------------------------------------------
8c8bf09f
ASL
31
32 private final TmfTimestamp fStartTime;
33 private final TmfTimestamp fEndTime;
34
cbd4ad82 35 // ------------------------------------------------------------------------
8c8bf09f 36 // Constructors
cbd4ad82 37 // ------------------------------------------------------------------------
8c8bf09f 38
28b94d61
FC
39 @SuppressWarnings("unused")
40 private TmfTimeRange() {
cbd4ad82 41 throw new AssertionError();
28b94d61
FC
42 }
43
8c8bf09f
ASL
44 /**
45 * @param startTime
46 * @param endTime
47 */
48 public TmfTimeRange(TmfTimestamp startTime, TmfTimestamp endTime) {
cbd4ad82
FC
49 if (startTime == null || endTime == null) {
50 throw new IllegalArgumentException();
51 }
52 fStartTime = new TmfTimestamp(startTime);
53 fEndTime = new TmfTimestamp(endTime);
8c8bf09f 54 }
28b94d61
FC
55
56 /**
cbd4ad82 57 * Copy constructor
28b94d61
FC
58 * @param other
59 */
60 public TmfTimeRange(TmfTimeRange other) {
cbd4ad82
FC
61 if (other == null) {
62 throw new IllegalArgumentException();
951d134a 63 }
cbd4ad82
FC
64 fStartTime = new TmfTimestamp(other.fStartTime);
65 fEndTime = new TmfTimestamp(other.fEndTime);
66 }
951d134a 67
cbd4ad82 68 // ------------------------------------------------------------------------
8c8bf09f 69 // Accessors
cbd4ad82 70 // ------------------------------------------------------------------------
8c8bf09f
ASL
71
72 /**
73 * @return The time range start time
74 */
75 public TmfTimestamp getStartTime() {
cbd4ad82 76 return new TmfTimestamp(fStartTime);
8c8bf09f
ASL
77 }
78
79 /**
80 * @return The time range end time
81 */
82 public TmfTimestamp getEndTime() {
cbd4ad82 83 return new TmfTimestamp(fEndTime);
8c8bf09f
ASL
84 }
85
cbd4ad82 86 // ------------------------------------------------------------------------
8c8bf09f 87 // Predicates
cbd4ad82 88 // ------------------------------------------------------------------------
8c8bf09f
ASL
89
90 /**
91 * Check if the timestamp is within the time range
92 *
93 * @param ts
94 * @return
95 */
96 public boolean contains(TmfTimestamp ts) {
ae2c17d3
FC
97 // Zero acts as a "universal donor" timestamp
98 if (ts.equals(TmfTimestamp.Zero)) return true;
cbd4ad82 99 return (fStartTime.compareTo(ts, true) <= 0) && (fEndTime.compareTo(ts, true) >= 0);
8c8bf09f
ASL
100 }
101
550d787e
FC
102 /**
103 * Check if the time range is within the time range
104 *
105 * @param range
106 * @return
107 */
108 public boolean contains(TmfTimeRange range) {
109 TmfTimestamp startTime = range.getStartTime();
110 TmfTimestamp endTime = range.getEndTime();
111 return (fStartTime.compareTo(startTime, true) <= 0) && (fEndTime.compareTo(endTime, true) >= 0);
112 }
113
cbd4ad82
FC
114 // ------------------------------------------------------------------------
115 // Object
116 // ------------------------------------------------------------------------
117
118 @Override
119 public int hashCode() {
120 int result = 17;
121 result = 37 * result + fStartTime.hashCode();
122 result = 37 * result + fEndTime.hashCode();
123 return result;
124 }
125
126 @Override
127 public boolean equals(Object other) {
128 if (!(other instanceof TmfTimeRange))
129 return false;
130 TmfTimeRange range = (TmfTimeRange) other;
131 return range.fStartTime.equals(fStartTime) && range.fEndTime.equals(fEndTime);
132 }
133
8d2e2848
FC
134 @Override
135 public String toString() {
cbd4ad82 136 return "[TmfTimeRange(" + fStartTime + ":" + fEndTime + ")]";
8d2e2848
FC
137 }
138
8c8bf09f 139}
This page took 0.032582 seconds and 5 git commands to generate.