Commit | Line | Data |
---|---|---|
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 | ||
6c13869b | 13 | package org.eclipse.linuxtools.tmf.core.event; |
8c8bf09f ASL |
14 | |
15 | /** | |
951d134a | 16 | * <b><u>TmfTimeRange</u></b> |
8c8bf09f ASL |
17 | * <p> |
18 | * A utility class to define time ranges. | |
19 | */ | |
20 | public 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); |
a79913eb | 27 | public static final TmfTimeRange Null = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigBang); |
62d1696a | 28 | |
cbd4ad82 | 29 | // ------------------------------------------------------------------------ |
8c8bf09f | 30 | // Attributes |
cbd4ad82 | 31 | // ------------------------------------------------------------------------ |
8c8bf09f ASL |
32 | |
33 | private final TmfTimestamp fStartTime; | |
34 | private final TmfTimestamp fEndTime; | |
35 | ||
cbd4ad82 | 36 | // ------------------------------------------------------------------------ |
8c8bf09f | 37 | // Constructors |
cbd4ad82 | 38 | // ------------------------------------------------------------------------ |
8c8bf09f | 39 | |
28b94d61 FC |
40 | @SuppressWarnings("unused") |
41 | private TmfTimeRange() { | |
cbd4ad82 | 42 | throw new AssertionError(); |
28b94d61 FC |
43 | } |
44 | ||
8c8bf09f ASL |
45 | /** |
46 | * @param startTime | |
47 | * @param endTime | |
48 | */ | |
49 | public TmfTimeRange(TmfTimestamp startTime, TmfTimestamp endTime) { | |
cbd4ad82 FC |
50 | if (startTime == null || endTime == null) { |
51 | throw new IllegalArgumentException(); | |
52 | } | |
53 | fStartTime = new TmfTimestamp(startTime); | |
54 | fEndTime = new TmfTimestamp(endTime); | |
8c8bf09f | 55 | } |
28b94d61 FC |
56 | |
57 | /** | |
cbd4ad82 | 58 | * Copy constructor |
28b94d61 FC |
59 | * @param other |
60 | */ | |
61 | public TmfTimeRange(TmfTimeRange other) { | |
cbd4ad82 FC |
62 | if (other == null) { |
63 | throw new IllegalArgumentException(); | |
951d134a | 64 | } |
cbd4ad82 FC |
65 | fStartTime = new TmfTimestamp(other.fStartTime); |
66 | fEndTime = new TmfTimestamp(other.fEndTime); | |
67 | } | |
951d134a | 68 | |
cbd4ad82 | 69 | // ------------------------------------------------------------------------ |
8c8bf09f | 70 | // Accessors |
cbd4ad82 | 71 | // ------------------------------------------------------------------------ |
8c8bf09f ASL |
72 | |
73 | /** | |
74 | * @return The time range start time | |
75 | */ | |
76 | public TmfTimestamp getStartTime() { | |
cbd4ad82 | 77 | return new TmfTimestamp(fStartTime); |
8c8bf09f ASL |
78 | } |
79 | ||
80 | /** | |
81 | * @return The time range end time | |
82 | */ | |
83 | public TmfTimestamp getEndTime() { | |
cbd4ad82 | 84 | return new TmfTimestamp(fEndTime); |
8c8bf09f ASL |
85 | } |
86 | ||
cbd4ad82 | 87 | // ------------------------------------------------------------------------ |
8c8bf09f | 88 | // Predicates |
cbd4ad82 | 89 | // ------------------------------------------------------------------------ |
8c8bf09f ASL |
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) { | |
ae2c17d3 FC |
98 | // Zero acts as a "universal donor" timestamp |
99 | if (ts.equals(TmfTimestamp.Zero)) return true; | |
cbd4ad82 | 100 | return (fStartTime.compareTo(ts, true) <= 0) && (fEndTime.compareTo(ts, true) >= 0); |
8c8bf09f ASL |
101 | } |
102 | ||
023761c4 FC |
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 | ||
550d787e FC |
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 | ||
cbd4ad82 FC |
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 | ||
8d2e2848 | 152 | @Override |
3b38ea61 | 153 | @SuppressWarnings("nls") |
8d2e2848 | 154 | public String toString() { |
cbd4ad82 | 155 | return "[TmfTimeRange(" + fStartTime + ":" + fEndTime + ")]"; |
8d2e2848 FC |
156 | } |
157 | ||
8c8bf09f | 158 | } |