| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2014 Ericsson |
| 3 | * |
| 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 |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Francois Chouinard - Initial API and implementation |
| 11 | * Francois Chouinard - Updated as per TMF Event Model 1.0 |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.tracecompass.tmf.core.timestamp; |
| 15 | |
| 16 | import org.eclipse.jdt.annotation.NonNull; |
| 17 | import org.eclipse.jdt.annotation.NonNullByDefault; |
| 18 | import org.eclipse.jdt.annotation.Nullable; |
| 19 | |
| 20 | /** |
| 21 | * A utility class to define and manage time ranges. |
| 22 | * |
| 23 | * @author Francois Chouinard |
| 24 | * |
| 25 | * @see ITmfTimestamp |
| 26 | */ |
| 27 | @NonNullByDefault |
| 28 | public class TmfTimeRange { |
| 29 | |
| 30 | // ------------------------------------------------------------------------ |
| 31 | // Constants |
| 32 | // ------------------------------------------------------------------------ |
| 33 | |
| 34 | /** |
| 35 | * The full possible time range |
| 36 | */ |
| 37 | public static final TmfTimeRange ETERNITY = new EternityTimeRange(); |
| 38 | |
| 39 | /** |
| 40 | * The null time range |
| 41 | */ |
| 42 | public static final TmfTimeRange NULL_RANGE = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_BANG); |
| 43 | |
| 44 | // ------------------------------------------------------------------------ |
| 45 | // Attributes |
| 46 | // ------------------------------------------------------------------------ |
| 47 | |
| 48 | private final ITmfTimestamp fStartTime; |
| 49 | private final ITmfTimestamp fEndTime; |
| 50 | |
| 51 | // ------------------------------------------------------------------------ |
| 52 | // Constructors |
| 53 | // ------------------------------------------------------------------------ |
| 54 | |
| 55 | /** |
| 56 | * Full constructor |
| 57 | * |
| 58 | * @param startTime start of the time range |
| 59 | * @param endTime end of the time range |
| 60 | */ |
| 61 | public TmfTimeRange(final ITmfTimestamp startTime, final ITmfTimestamp endTime) { |
| 62 | fStartTime = startTime; |
| 63 | fEndTime = endTime; |
| 64 | } |
| 65 | |
| 66 | // ------------------------------------------------------------------------ |
| 67 | // Getters |
| 68 | // ------------------------------------------------------------------------ |
| 69 | |
| 70 | /** |
| 71 | * @return the time range start time |
| 72 | */ |
| 73 | public ITmfTimestamp getStartTime() { |
| 74 | return fStartTime; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @return the time range end time |
| 79 | */ |
| 80 | public ITmfTimestamp getEndTime() { |
| 81 | return fEndTime; |
| 82 | } |
| 83 | |
| 84 | // ------------------------------------------------------------------------ |
| 85 | // Predicates |
| 86 | // ------------------------------------------------------------------------ |
| 87 | |
| 88 | /** |
| 89 | * Check if the timestamp is within the time range |
| 90 | * |
| 91 | * @param ts |
| 92 | * The timestamp to check |
| 93 | * @return True if [startTime] <= [ts] <= [endTime] |
| 94 | */ |
| 95 | public boolean contains(final ITmfTimestamp ts) { |
| 96 | return (fStartTime.compareTo(ts) <= 0) && (fEndTime.compareTo(ts) >= 0); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Check if the time range is within the time range |
| 101 | * |
| 102 | * @param range |
| 103 | * The other time range |
| 104 | * @return True if [range] is fully contained |
| 105 | */ |
| 106 | public boolean contains(final TmfTimeRange range) { |
| 107 | final ITmfTimestamp startTime = range.getStartTime(); |
| 108 | final ITmfTimestamp endTime = range.getEndTime(); |
| 109 | return (fStartTime.compareTo(startTime) <= 0) && (fEndTime.compareTo(endTime) >= 0); |
| 110 | } |
| 111 | |
| 112 | // ------------------------------------------------------------------------ |
| 113 | // Operations |
| 114 | // ------------------------------------------------------------------------ |
| 115 | |
| 116 | /** |
| 117 | * Get intersection of two time ranges |
| 118 | * |
| 119 | * @param range the other time range |
| 120 | * @return the intersection time range, or null if no intersection exists |
| 121 | */ |
| 122 | public @Nullable TmfTimeRange getIntersection(final TmfTimeRange range) { |
| 123 | if (fStartTime.compareTo(range.fEndTime) > 0 || fEndTime.compareTo(range.fStartTime) < 0) { |
| 124 | return null; // no intersection |
| 125 | } |
| 126 | |
| 127 | return new TmfTimeRange(fStartTime.compareTo(range.fStartTime) < 0 |
| 128 | ? range.fStartTime |
| 129 | : fStartTime, fEndTime.compareTo(range.fEndTime) > 0 |
| 130 | ? range.fEndTime |
| 131 | : fEndTime); |
| 132 | } |
| 133 | |
| 134 | // ------------------------------------------------------------------------ |
| 135 | // Object |
| 136 | // ------------------------------------------------------------------------ |
| 137 | |
| 138 | @Override |
| 139 | public int hashCode() { |
| 140 | final int prime = 31; |
| 141 | int result = 1; |
| 142 | result = prime * result + fEndTime.hashCode(); |
| 143 | result = prime * result + fStartTime.hashCode(); |
| 144 | return result; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public boolean equals(final @Nullable Object obj) { |
| 149 | if (this == obj) { |
| 150 | return true; |
| 151 | } |
| 152 | if (obj == null) { |
| 153 | return false; |
| 154 | } |
| 155 | if (!(obj instanceof TmfTimeRange)) { |
| 156 | return false; |
| 157 | } |
| 158 | final TmfTimeRange other = (TmfTimeRange) obj; |
| 159 | if (!fEndTime.equals(other.fEndTime)) { |
| 160 | return false; |
| 161 | } |
| 162 | if (!fStartTime.equals(other.fStartTime)) { |
| 163 | return false; |
| 164 | } |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | @SuppressWarnings("nls") |
| 170 | public String toString() { |
| 171 | return "TmfTimeRange [fStartTime=" + fStartTime + ", fEndTime=" + fEndTime + "]"; |
| 172 | } |
| 173 | |
| 174 | // ------------------------------------------------------------------------ |
| 175 | // Inner classes |
| 176 | // ------------------------------------------------------------------------ |
| 177 | |
| 178 | /** |
| 179 | * "Eternity" time range, representing the largest time range possible, |
| 180 | * which includes any other time range or timestamp. |
| 181 | */ |
| 182 | private static final class EternityTimeRange extends TmfTimeRange { |
| 183 | |
| 184 | public EternityTimeRange() { |
| 185 | super(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public boolean contains(ITmfTimestamp ts) { |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public boolean contains(TmfTimeRange range) { |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public @NonNull TmfTimeRange getIntersection(TmfTimeRange range) { |
| 200 | return range; |
| 201 | } |
| 202 | } |
| 203 | } |