| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 2013 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 | * Matthew Khouzam - Initial API and implementation |
| 11 | * Alexandre Montplaisir - Extends TmfLocation |
| 12 | *******************************************************************************/ |
| 13 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; |
| 14 | |
| 15 | import java.nio.ByteBuffer; |
| 16 | |
| 17 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; |
| 18 | import org.eclipse.linuxtools.tmf.core.trace.location.TmfLocation; |
| 19 | |
| 20 | /** |
| 21 | * The nugget of information that is unique to a location in a CTF trace. |
| 22 | * |
| 23 | * It can be copied and used to restore a position in a given trace. |
| 24 | * |
| 25 | * @version 1.0 |
| 26 | * @author Matthew Khouzam |
| 27 | */ |
| 28 | public final class CtfLocation extends TmfLocation { |
| 29 | |
| 30 | // ------------------------------------------------------------------------ |
| 31 | // Attributes |
| 32 | // ------------------------------------------------------------------------ |
| 33 | |
| 34 | /** |
| 35 | * An invalid location |
| 36 | */ |
| 37 | public static final CtfLocationInfo INVALID_LOCATION = new CtfLocationInfo(-1, -1); |
| 38 | |
| 39 | // ------------------------------------------------------------------------ |
| 40 | // Constructors |
| 41 | // ------------------------------------------------------------------------ |
| 42 | |
| 43 | /** |
| 44 | * Basic constructor for CtfLocation. Uses a default index of 0. |
| 45 | * |
| 46 | * @param timestamp |
| 47 | * The timestamp of this location |
| 48 | * @since 2.0 |
| 49 | */ |
| 50 | public CtfLocation(final ITmfTimestamp timestamp) { |
| 51 | this(timestamp.getValue(), 0); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Constructor using timestamp object and index |
| 56 | * |
| 57 | * @param timestamp |
| 58 | * The timestamp of this location |
| 59 | * @param index |
| 60 | * The index of this location for this timestamp |
| 61 | * @since 2.0 |
| 62 | */ |
| 63 | public CtfLocation(final ITmfTimestamp timestamp, long index) { |
| 64 | this(timestamp.getValue(), index); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Constructor using a long value for the timestamp, and an index |
| 69 | * |
| 70 | * @param timestampValue |
| 71 | * The new timestamp |
| 72 | * @param index |
| 73 | * The new index |
| 74 | * @since 2.0 |
| 75 | */ |
| 76 | public CtfLocation(final long timestampValue, final long index) { |
| 77 | super(new CtfLocationInfo(timestampValue, index)); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Constructor using a pre-made locationInfo object |
| 82 | * |
| 83 | * @param locationInfo |
| 84 | * The locationInfo object to use |
| 85 | * @since 2.0 |
| 86 | */ |
| 87 | public CtfLocation(CtfLocationInfo locationInfo) { |
| 88 | super(locationInfo); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Copy constructor |
| 93 | * |
| 94 | * @param location |
| 95 | * Other location to copy |
| 96 | * @since 2.0 |
| 97 | */ |
| 98 | public CtfLocation(final CtfLocation location) { |
| 99 | super(location); |
| 100 | } |
| 101 | |
| 102 | // ------------------------------------------------------------------------ |
| 103 | // TmfLocation |
| 104 | // ------------------------------------------------------------------------ |
| 105 | |
| 106 | /** |
| 107 | * Construct the location from the ByteBuffer. |
| 108 | * |
| 109 | * @param bufferIn |
| 110 | * the buffer to read from |
| 111 | * |
| 112 | * @since 3.0 |
| 113 | */ |
| 114 | public CtfLocation(ByteBuffer bufferIn) { |
| 115 | super(new CtfLocationInfo(bufferIn)); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @since 2.0 |
| 120 | */ |
| 121 | @Override |
| 122 | public CtfLocationInfo getLocationInfo() { |
| 123 | return (CtfLocationInfo) super.getLocationInfo(); |
| 124 | } |
| 125 | |
| 126 | // ------------------------------------------------------------------------ |
| 127 | // Object |
| 128 | // ------------------------------------------------------------------------ |
| 129 | |
| 130 | @Override |
| 131 | public String toString() { |
| 132 | if( this.getLocationInfo().equals(CtfLocation.INVALID_LOCATION )) { |
| 133 | return getClass().getSimpleName() + " [INVALID]"; //$NON-NLS-1$ |
| 134 | } |
| 135 | return super.toString(); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Constructs the location from the ByteBuffer. This typically happens when reading from disk. |
| 140 | * |
| 141 | * @since 3.0 |
| 142 | */ |
| 143 | @Override |
| 144 | public void serialize(ByteBuffer bufferOut) { |
| 145 | getLocationInfo().serialize(bufferOut); |
| 146 | |
| 147 | } |
| 148 | } |