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