| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2012 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.linuxtools.tmf.core.event; |
| 15 | |
| 16 | /** |
| 17 | * <b><u>TmfTimeRange</u></b> |
| 18 | * <p> |
| 19 | * A utility class to define and manage time ranges. |
| 20 | */ |
| 21 | public class TmfTimeRange implements Cloneable { |
| 22 | |
| 23 | // ------------------------------------------------------------------------ |
| 24 | // Constants |
| 25 | // ------------------------------------------------------------------------ |
| 26 | |
| 27 | public static final TmfTimeRange Eternity = |
| 28 | new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
| 29 | |
| 30 | public static final TmfTimeRange Null = |
| 31 | new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigBang); |
| 32 | |
| 33 | // ------------------------------------------------------------------------ |
| 34 | // Attributes |
| 35 | // ------------------------------------------------------------------------ |
| 36 | |
| 37 | private ITmfTimestamp fStartTime; |
| 38 | private ITmfTimestamp fEndTime; |
| 39 | |
| 40 | // ------------------------------------------------------------------------ |
| 41 | // Constructors |
| 42 | // ------------------------------------------------------------------------ |
| 43 | |
| 44 | /** |
| 45 | * Default constructor |
| 46 | */ |
| 47 | @SuppressWarnings("unused") |
| 48 | private TmfTimeRange() { |
| 49 | throw new AssertionError(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Full constructor |
| 54 | * |
| 55 | * @param startTime start of the time range |
| 56 | * @param endTime end of the time range |
| 57 | */ |
| 58 | public TmfTimeRange(ITmfTimestamp startTime, ITmfTimestamp endTime) { |
| 59 | if (startTime == null || endTime == null) { |
| 60 | throw new IllegalArgumentException(); |
| 61 | } |
| 62 | fStartTime = startTime.clone(); |
| 63 | fEndTime = endTime.clone(); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Copy constructor |
| 68 | * |
| 69 | * @param range the other time range |
| 70 | */ |
| 71 | public TmfTimeRange(TmfTimeRange range) { |
| 72 | if (range == null) { |
| 73 | throw new IllegalArgumentException(); |
| 74 | } |
| 75 | fStartTime = range.getStartTime().clone(); |
| 76 | fEndTime = range.getEndTime().clone(); |
| 77 | } |
| 78 | |
| 79 | // ------------------------------------------------------------------------ |
| 80 | // Getters |
| 81 | // ------------------------------------------------------------------------ |
| 82 | |
| 83 | /** |
| 84 | * @return the time range start time |
| 85 | */ |
| 86 | public ITmfTimestamp getStartTime() { |
| 87 | return fStartTime; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @return the time range end time |
| 92 | */ |
| 93 | public ITmfTimestamp getEndTime() { |
| 94 | return fEndTime; |
| 95 | } |
| 96 | |
| 97 | // ------------------------------------------------------------------------ |
| 98 | // Predicates |
| 99 | // ------------------------------------------------------------------------ |
| 100 | |
| 101 | /** |
| 102 | * Check if the timestamp is within the time range |
| 103 | * |
| 104 | * @param ts the timestamp to check |
| 105 | * @return true if [startTime] <= [ts] <= [endTime] |
| 106 | */ |
| 107 | public boolean contains(ITmfTimestamp ts) { |
| 108 | // Zero acts as a "universal donor" timestamp |
| 109 | if (ts.equals(TmfTimestamp.Zero)) |
| 110 | return true; |
| 111 | return (fStartTime.compareTo(ts, true) <= 0) && (fEndTime.compareTo(ts, true) >= 0); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Check if the time range is within the time range |
| 116 | * |
| 117 | * @param range the other time range |
| 118 | * @return true if [range] is fully contained |
| 119 | */ |
| 120 | public boolean contains(TmfTimeRange range) { |
| 121 | ITmfTimestamp startTime = range.getStartTime(); |
| 122 | ITmfTimestamp endTime = range.getEndTime(); |
| 123 | return (fStartTime.compareTo(startTime, true) <= 0) && (fEndTime.compareTo(endTime, true) >= 0); |
| 124 | } |
| 125 | |
| 126 | // ------------------------------------------------------------------------ |
| 127 | // Operations |
| 128 | // ------------------------------------------------------------------------ |
| 129 | |
| 130 | /** |
| 131 | * Get intersection of two time ranges |
| 132 | * |
| 133 | * @param range the other time range |
| 134 | * @return the intersection time range, or null if no intersection exists |
| 135 | */ |
| 136 | public TmfTimeRange getIntersection(TmfTimeRange range) { |
| 137 | if (fStartTime.compareTo(range.fEndTime, true) > 0 || fEndTime.compareTo(range.fStartTime, true) < 0) |
| 138 | return null; // no intersection |
| 139 | |
| 140 | return new TmfTimeRange(fStartTime.compareTo(range.fStartTime, true) < 0 ? range.fStartTime |
| 141 | : fStartTime, fEndTime.compareTo(range.fEndTime, true) > 0 ? range.fEndTime |
| 142 | : fEndTime); |
| 143 | } |
| 144 | |
| 145 | // ------------------------------------------------------------------------ |
| 146 | // Cloneable |
| 147 | // ------------------------------------------------------------------------ |
| 148 | |
| 149 | public TmfTimeRange clone() { |
| 150 | TmfTimeRange clone = null; |
| 151 | try { |
| 152 | clone = (TmfTimeRange) super.clone(); |
| 153 | clone.fStartTime = fStartTime.clone(); |
| 154 | clone.fEndTime = fEndTime.clone(); |
| 155 | } |
| 156 | catch (CloneNotSupportedException e) { |
| 157 | } |
| 158 | return clone; |
| 159 | } |
| 160 | |
| 161 | // ------------------------------------------------------------------------ |
| 162 | // Object |
| 163 | // ------------------------------------------------------------------------ |
| 164 | |
| 165 | @Override |
| 166 | public int hashCode() { |
| 167 | final int prime = 31; |
| 168 | int result = 1; |
| 169 | result = prime * result + ((fEndTime == null) ? 0 : fEndTime.hashCode()); |
| 170 | result = prime * result + ((fStartTime == null) ? 0 : fStartTime.hashCode()); |
| 171 | return result; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public boolean equals(Object obj) { |
| 176 | if (this == obj) |
| 177 | return true; |
| 178 | if (obj == null) |
| 179 | return false; |
| 180 | if (getClass() != obj.getClass()) |
| 181 | return false; |
| 182 | TmfTimeRange other = (TmfTimeRange) obj; |
| 183 | if (fEndTime == null) { |
| 184 | if (other.fEndTime != null) |
| 185 | return false; |
| 186 | } else if (!fEndTime.equals(other.fEndTime)) |
| 187 | return false; |
| 188 | if (fStartTime == null) { |
| 189 | if (other.fStartTime != null) |
| 190 | return false; |
| 191 | } else if (!fStartTime.equals(other.fStartTime)) |
| 192 | return false; |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | @SuppressWarnings("nls") |
| 198 | public String toString() { |
| 199 | return "TmfTimeRange [fStartTime=" + fStartTime + ", fEndTime=" + fEndTime + "]"; |
| 200 | } |
| 201 | |
| 202 | } |