b3c74722f3ab7158211c992c16a70a1d71171341
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / indexer / checkpoint / TmfCheckpoint.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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.linuxtools.tmf.core.trace.indexer.checkpoint;
16
17 import java.nio.ByteBuffer;
18
19 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
20 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
21 import org.eclipse.linuxtools.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 * @since 3.0
32 */
33 public class TmfCheckpoint implements ITmfCheckpoint {
34
35 // ------------------------------------------------------------------------
36 // Attributes
37 // ------------------------------------------------------------------------
38
39 // The checkpoint location
40 private final ITmfLocation fLocation;
41
42 // The checkpoint timestamp
43 private final ITmfTimestamp fTimestamp;
44
45 private final long fCheckpointRank;
46
47 // ------------------------------------------------------------------------
48 // Constructors
49 // ------------------------------------------------------------------------
50
51 /**
52 * Full constructor
53 *
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 *
79 * @since 3.0
80 */
81 public TmfCheckpoint(final ITmfTimestamp timestamp, final ITmfLocation location, ByteBuffer bufferIn) {
82 fTimestamp = timestamp;
83 fLocation = location;
84 fCheckpointRank = bufferIn.getLong();
85 }
86
87 /**
88 * Copy constructor
89 *
90 * @param other the other checkpoint
91 */
92 public TmfCheckpoint(final TmfCheckpoint other) {
93 if (other == null) {
94 throw new IllegalArgumentException();
95 }
96 fTimestamp = other.fTimestamp;
97 fLocation = other.fLocation;
98 fCheckpointRank = other.fCheckpointRank;
99 }
100
101 // ------------------------------------------------------------------------
102 // ITmfCheckpoint
103 // ------------------------------------------------------------------------
104
105 /**
106 * @since 2.0
107 */
108 @Override
109 public ITmfTimestamp getTimestamp() {
110 return fTimestamp;
111 }
112
113 @Override
114 public ITmfLocation getLocation() {
115 return fLocation;
116 }
117
118 // ------------------------------------------------------------------------
119 // Comparable
120 // ------------------------------------------------------------------------
121
122 @Override
123 @SuppressWarnings({ "unchecked", "rawtypes" })
124 public int compareTo(final ITmfCheckpoint other) {
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
132 }
133
134 if ((fLocation == null) && (other.getLocation() == null)) {
135 return 0;
136 }
137
138 // treat location of other as null location which is before any location
139 if ((fLocation != null) && (other.getLocation() == null)) {
140 return 1;
141 }
142
143 // treat this as null location which is before any other locations
144 if ((fLocation == null) && (other.getLocation() != null)) {
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);
152 }
153
154 // ------------------------------------------------------------------------
155 // Object
156 // ------------------------------------------------------------------------
157
158 @Override
159 public int hashCode() {
160 final int prime = 31;
161 int result = 1;
162 result = prime * result + ((fLocation == null) ? 0 : fLocation.hashCode());
163 result = prime * result + ((fTimestamp == null) ? 0 : fTimestamp.hashCode());
164 return result;
165 }
166
167 @Override
168 public boolean equals(final Object obj) {
169 if (this == obj) {
170 return true;
171 }
172 if (obj == null) {
173 return false;
174 }
175 if (!(obj instanceof TmfCheckpoint)) {
176 return false;
177 }
178 final TmfCheckpoint other = (TmfCheckpoint) obj;
179 if (fLocation == null) {
180 if (other.fLocation != null) {
181 return false;
182 }
183 } else if (!fLocation.equals(other.fLocation)) {
184 return false;
185 }
186 if (fTimestamp == null) {
187 if (other.fTimestamp != null) {
188 return false;
189 }
190 } else if (!fTimestamp.equals(other.fTimestamp)) {
191 return false;
192 }
193 return true;
194 }
195
196 @Override
197 @SuppressWarnings("nls")
198 public String toString() {
199 return getClass().getSimpleName() + " [fLocation=" + fLocation + ", fTimestamp=" + fTimestamp + ", fCheckpointRank=" + fCheckpointRank + "]";
200 }
201
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 }
222 }
This page took 0.050869 seconds and 4 git commands to generate.