Merge branch 'master' into lttng-luna
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / indexer / checkpoint / TmfCheckpoint.java
CommitLineData
8c8bf09f 1/*******************************************************************************
61759503 2 * Copyright (c) 2009, 2013 Ericsson
6256d8ad 3 *
8c8bf09f
ASL
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
6256d8ad 8 *
8c8bf09f
ASL
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
5d837f9b 11 * Francois Chouinard - Updated as per TMF Trace Model 1.0
ea271da6 12 * Patrick Tasse - Updated for location in checkpoint
5d837f9b 13 ******************************************************************************/
8c8bf09f 14
a3db8436 15package org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint;
8c8bf09f 16
032ecd45
MAL
17import java.nio.ByteBuffer;
18
3bd46eef 19import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
032ecd45 20import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
a3db8436 21import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
8c8bf09f
ASL
22
23/**
2848c377
FC
24 * A basic implementation of ITmfCheckpoint. It simply maps an event timestamp
25 * to a generic location.
6256d8ad 26 *
f7703ed6
FC
27 * @see ITmfLocation
28 * @see ITmfTimestamp
a3db8436
AM
29 *
30 * @author Francois Chouinard
31 * @since 3.0
8c8bf09f 32 */
a12b0e0f 33public class TmfCheckpoint implements ITmfCheckpoint {
8c8bf09f
ASL
34
35 // ------------------------------------------------------------------------
36 // Attributes
37 // ------------------------------------------------------------------------
cbdacf03 38
ea271da6
PT
39 // The checkpoint location
40 private final ITmfLocation fLocation;
5d837f9b
FC
41
42 // The checkpoint timestamp
a12b0e0f 43 private final ITmfTimestamp fTimestamp;
8c8bf09f 44
032ecd45
MAL
45 private final long fCheckpointRank;
46
8c8bf09f
ASL
47 // ------------------------------------------------------------------------
48 // Constructors
49 // ------------------------------------------------------------------------
50
8c8bf09f 51 /**
5d837f9b 52 * Full constructor
6256d8ad 53 *
032ecd45
MAL
54 * @param timestamp
55 * the checkpoint timestamp
56 * @param location
57 * the corresponding trace location
58 * @param checkpointRank
59 * the rank of the checkpoint
60 * @since 3.0
61 */
62 public TmfCheckpoint(final ITmfTimestamp timestamp, final ITmfLocation location, long checkpointRank) {
63 fTimestamp = timestamp;
64 fLocation = location;
65 fCheckpointRank = checkpointRank;
66 }
67
68 /**
69 * Constructs a checkpoint using also a byte buffer to read the rank from
70 * disk.
71 *
72 * @param timestamp
73 * the checkpoint timestamp
74 * @param location
75 * the corresponding trace location
76 * @param bufferIn
77 * the byte buffer to read from
78 *
a3db8436 79 * @since 3.0
8c8bf09f 80 */
032ecd45 81 public TmfCheckpoint(final ITmfTimestamp timestamp, final ITmfLocation location, ByteBuffer bufferIn) {
5d837f9b 82 fTimestamp = timestamp;
ea271da6 83 fLocation = location;
032ecd45 84 fCheckpointRank = bufferIn.getLong();
8c8bf09f
ASL
85 }
86
ff4ed569 87 /**
5d837f9b 88 * Copy constructor
6256d8ad 89 *
ff4ed569
FC
90 * @param other the other checkpoint
91 */
cbdacf03 92 public TmfCheckpoint(final TmfCheckpoint other) {
0316808c 93 if (other == null) {
cbdacf03 94 throw new IllegalArgumentException();
0316808c 95 }
5d837f9b 96 fTimestamp = other.fTimestamp;
ea271da6 97 fLocation = other.fLocation;
032ecd45 98 fCheckpointRank = other.fCheckpointRank;
5d837f9b
FC
99 }
100
8c8bf09f 101 // ------------------------------------------------------------------------
8ca8c4f1 102 // ITmfCheckpoint
8c8bf09f
ASL
103 // ------------------------------------------------------------------------
104
3bd46eef
AM
105 /**
106 * @since 2.0
8c8bf09f 107 */
5d837f9b 108 @Override
4df4581d 109 public ITmfTimestamp getTimestamp() {
8c8bf09f
ASL
110 return fTimestamp;
111 }
112
37419bf3 113 @Override
1e1bef82 114 public ITmfLocation getLocation() {
ea271da6 115 return fLocation;
8c8bf09f
ASL
116 }
117
cbd4ad82 118 // ------------------------------------------------------------------------
5d837f9b 119 // Comparable
cbd4ad82
FC
120 // ------------------------------------------------------------------------
121
550d787e 122 @Override
ff1ccee6 123 @SuppressWarnings({ "unchecked", "rawtypes" })
5d837f9b 124 public int compareTo(final ITmfCheckpoint other) {
3eade83c
BH
125 int comp = 0;
126 if ((fTimestamp != null) && (other.getTimestamp() != null)) {
127 comp = fTimestamp.compareTo(other.getTimestamp(), false);
128 if (comp != 0) {
129 return comp;
130 }
131 // compare locations if timestamps are the same
cbdacf03 132 }
3eade83c 133
ea271da6 134 if ((fLocation == null) && (other.getLocation() == null)) {
3eade83c
BH
135 return 0;
136 }
137
138 // treat location of other as null location which is before any location
ea271da6 139 if ((fLocation != null) && (other.getLocation() == null)) {
3eade83c
BH
140 return 1;
141 }
142
143 // treat this as null location which is before any other locations
ea271da6 144 if ((fLocation == null) && (other.getLocation() != null)) {
3eade83c
BH
145 return -1;
146 }
147
148 // compare location
149 final Comparable location1 = getLocation().getLocationInfo();
150 final Comparable location2 = other.getLocation().getLocationInfo();
151 return location1.compareTo(location2);
550d787e 152 }
cbdacf03 153
5d837f9b
FC
154 // ------------------------------------------------------------------------
155 // Object
156 // ------------------------------------------------------------------------
157
cbd4ad82
FC
158 @Override
159 public int hashCode() {
5d837f9b
FC
160 final int prime = 31;
161 int result = 1;
ea271da6 162 result = prime * result + ((fLocation == null) ? 0 : fLocation.hashCode());
5d837f9b
FC
163 result = prime * result + ((fTimestamp == null) ? 0 : fTimestamp.hashCode());
164 return result;
cbd4ad82 165 }
cbdacf03 166
cbd4ad82 167 @Override
5d837f9b 168 public boolean equals(final Object obj) {
0316808c 169 if (this == obj) {
5d837f9b 170 return true;
0316808c
FC
171 }
172 if (obj == null) {
5d837f9b 173 return false;
0316808c
FC
174 }
175 if (!(obj instanceof TmfCheckpoint)) {
cbdacf03 176 return false;
0316808c 177 }
5d837f9b 178 final TmfCheckpoint other = (TmfCheckpoint) obj;
ea271da6
PT
179 if (fLocation == null) {
180 if (other.fLocation != null) {
5d837f9b 181 return false;
0316808c 182 }
ea271da6 183 } else if (!fLocation.equals(other.fLocation)) {
5d837f9b 184 return false;
0316808c 185 }
5d837f9b 186 if (fTimestamp == null) {
0316808c 187 if (other.fTimestamp != null) {
5d837f9b 188 return false;
0316808c
FC
189 }
190 } else if (!fTimestamp.equals(other.fTimestamp)) {
5d837f9b 191 return false;
0316808c 192 }
5d837f9b 193 return true;
cbd4ad82 194 }
cbdacf03 195
ff4ed569 196 @Override
3b38ea61 197 @SuppressWarnings("nls")
ff4ed569 198 public String toString() {
032ecd45 199 return getClass().getSimpleName() + " [fLocation=" + fLocation + ", fTimestamp=" + fTimestamp + ", fCheckpointRank=" + fCheckpointRank + "]";
8c8bf09f
ASL
200 }
201
032ecd45
MAL
202 /**
203 * @since 3.0
204 */
205 @Override
206 public void serialize(ByteBuffer bufferOut) {
207 fLocation.serialize(bufferOut);
208 // Always serialize as base TmfTimestamp, this should be sufficient for indexing.
209 // If not, we can add API for the test to restore the time stamp, similarly to the location.
210 TmfTimestamp t = new TmfTimestamp(fTimestamp);
211 t.serialize(bufferOut);
212 bufferOut.putLong(fCheckpointRank);
213 }
214
215 /**
216 * @since 3.0
217 */
218 @Override
219 public long getCheckpointRank() {
220 return fCheckpointRank;
221 }
8c8bf09f 222}
This page took 0.058666 seconds and 5 git commands to generate.