| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2010, 2012 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 | * Francois Chouinard - Updated as per TMF Trace Model 1.0 |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.linuxtools.tmf.core.trace; |
| 15 | |
| 16 | /** |
| 17 | * A basic implementation of ITmfContext. |
| 18 | * <p> |
| 19 | * It ties a trace location to an event rank. The context should be enough to |
| 20 | * restore the trace state so the corresponding event can be read. |
| 21 | * |
| 22 | * @version 1.0 |
| 23 | * @author Francois Chouinard |
| 24 | * |
| 25 | * @see ITmfLocation |
| 26 | */ |
| 27 | public class TmfContext implements ITmfContext, Cloneable { |
| 28 | |
| 29 | // ------------------------------------------------------------------------ |
| 30 | // Attributes |
| 31 | // ------------------------------------------------------------------------ |
| 32 | |
| 33 | // The trace location |
| 34 | private ITmfLocation fLocation; |
| 35 | |
| 36 | // The event rank |
| 37 | private long fRank; |
| 38 | |
| 39 | // ------------------------------------------------------------------------ |
| 40 | // Constructors |
| 41 | // ------------------------------------------------------------------------ |
| 42 | |
| 43 | /** |
| 44 | * Default constructor |
| 45 | */ |
| 46 | public TmfContext() { |
| 47 | this(null, UNKNOWN_RANK); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Simple constructor (unknown rank) |
| 52 | * |
| 53 | * @param location the event location |
| 54 | */ |
| 55 | public TmfContext(final ITmfLocation location) { |
| 56 | this(location, UNKNOWN_RANK); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Full constructor |
| 61 | * |
| 62 | * @param location the event location |
| 63 | * @param rank the event rank |
| 64 | */ |
| 65 | public TmfContext(final ITmfLocation location, final long rank) { |
| 66 | fLocation = location; |
| 67 | fRank = rank; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Copy constructor |
| 72 | * |
| 73 | * @param context the other context |
| 74 | */ |
| 75 | public TmfContext(final TmfContext context) { |
| 76 | if (context == null) { |
| 77 | throw new IllegalArgumentException(); |
| 78 | } |
| 79 | fLocation = context.fLocation; |
| 80 | fRank = context.fRank; |
| 81 | } |
| 82 | |
| 83 | // ------------------------------------------------------------------------ |
| 84 | // Cloneable |
| 85 | // ------------------------------------------------------------------------ |
| 86 | |
| 87 | /* (non-Javadoc) |
| 88 | * @see java.lang.Object#clone() |
| 89 | */ |
| 90 | @Override |
| 91 | public TmfContext clone() { |
| 92 | TmfContext clone = null; |
| 93 | try { |
| 94 | clone = (TmfContext) super.clone(); |
| 95 | clone.fLocation = (fLocation != null) ? fLocation : null; |
| 96 | clone.fRank = fRank; |
| 97 | } catch (final CloneNotSupportedException e) { |
| 98 | } |
| 99 | return clone; |
| 100 | } |
| 101 | |
| 102 | // ------------------------------------------------------------------------ |
| 103 | // ITmfContext |
| 104 | // ------------------------------------------------------------------------ |
| 105 | |
| 106 | /* (non-Javadoc) |
| 107 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getLocation() |
| 108 | */ |
| 109 | @Override |
| 110 | public ITmfLocation getLocation() { |
| 111 | return fLocation; |
| 112 | } |
| 113 | |
| 114 | /* (non-Javadoc) |
| 115 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setLocation(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation) |
| 116 | */ |
| 117 | @Override |
| 118 | public void setLocation(final ITmfLocation location) { |
| 119 | fLocation = location; |
| 120 | } |
| 121 | |
| 122 | /* (non-Javadoc) |
| 123 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getRank() |
| 124 | */ |
| 125 | @Override |
| 126 | public long getRank() { |
| 127 | return fRank; |
| 128 | } |
| 129 | |
| 130 | /* (non-Javadoc) |
| 131 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setRank(long) |
| 132 | */ |
| 133 | @Override |
| 134 | public void setRank(final long rank) { |
| 135 | fRank = rank; |
| 136 | } |
| 137 | |
| 138 | /* (non-Javadoc) |
| 139 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#increaseRank() |
| 140 | */ |
| 141 | @Override |
| 142 | public void increaseRank() { |
| 143 | if (hasValidRank()) { |
| 144 | fRank++; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /* (non-Javadoc) |
| 149 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#hasValidRank() |
| 150 | */ |
| 151 | @Override |
| 152 | public boolean hasValidRank() { |
| 153 | return fRank != UNKNOWN_RANK; |
| 154 | } |
| 155 | |
| 156 | /* (non-Javadoc) |
| 157 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#dispose() |
| 158 | */ |
| 159 | @Override |
| 160 | public void dispose() { |
| 161 | } |
| 162 | |
| 163 | // ------------------------------------------------------------------------ |
| 164 | // Object |
| 165 | // ------------------------------------------------------------------------ |
| 166 | |
| 167 | /* (non-Javadoc) |
| 168 | * @see java.lang.Object#hashCode() |
| 169 | */ |
| 170 | @Override |
| 171 | public int hashCode() { |
| 172 | final int prime = 31; |
| 173 | int result = 1; |
| 174 | result = prime * result + ((fLocation == null) ? 0 : fLocation.hashCode()); |
| 175 | result = prime * result + (int) (fRank ^ (fRank >>> 32)); |
| 176 | return result; |
| 177 | } |
| 178 | |
| 179 | /* (non-Javadoc) |
| 180 | * @see java.lang.Object#equals(java.lang.Object) |
| 181 | */ |
| 182 | @Override |
| 183 | public boolean equals(final Object obj) { |
| 184 | if (this == obj) { |
| 185 | return true; |
| 186 | } |
| 187 | if (obj == null) { |
| 188 | return false; |
| 189 | } |
| 190 | if (getClass() != obj.getClass()) { |
| 191 | return false; |
| 192 | } |
| 193 | final TmfContext other = (TmfContext) obj; |
| 194 | if (fLocation == null) { |
| 195 | if (other.fLocation != null) { |
| 196 | return false; |
| 197 | } |
| 198 | } else if (!fLocation.equals(other.fLocation)) { |
| 199 | return false; |
| 200 | } |
| 201 | if (fRank != other.fRank) { |
| 202 | return false; |
| 203 | } |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | /* (non-Javadoc) |
| 208 | * @see java.lang.Object#toString() |
| 209 | */ |
| 210 | @Override |
| 211 | @SuppressWarnings("nls") |
| 212 | public String toString() { |
| 213 | return "TmfContext [fLocation=" + fLocation + ", fRank=" + fRank + "]"; |
| 214 | } |
| 215 | |
| 216 | } |