fce69dbd2edd12d8eeb02fc06f797b1662ad1296
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / context / CtfLocationInfo.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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: Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
12 package org.eclipse.tracecompass.tmf.ctf.core.context;
13
14 import java.nio.ByteBuffer;
15
16 /**
17 * The data object to go in a {@link CtfLocation}.
18 *
19 * @author Matthew Khouzam
20 */
21 public class CtfLocationInfo implements Comparable<CtfLocationInfo> {
22
23 private final long fTimestamp;
24 private final long fIndex;
25
26 /**
27 * @param ts
28 * Timestamp
29 * @param index
30 * Index of this event (if there are N elements with the same
31 * timestamp, which one is it.)
32 */
33 public CtfLocationInfo(long ts, long index) {
34 fTimestamp = ts;
35 fIndex = index;
36 }
37
38 /**
39 * Construct the location from the ByteBuffer.
40 *
41 * @param bufferIn
42 * the buffer to read from
43 */
44 public CtfLocationInfo(ByteBuffer bufferIn) {
45 fTimestamp = bufferIn.getLong();
46 fIndex = bufferIn.getLong();
47 }
48
49 /**
50 * @return The timestamp
51 */
52 public long getTimestamp() {
53 return fTimestamp;
54 }
55
56 /**
57 * @return The index of the element
58 */
59 public long getIndex() {
60 return fIndex;
61 }
62
63 // ------------------------------------------------------------------------
64 // Object
65 // ------------------------------------------------------------------------
66
67 @Override
68 public int hashCode() {
69 final int prime = 31;
70 int result = 1;
71 result = (prime * result) + (int) (fIndex ^ (fIndex >>> 32));
72 result = (prime * result) + (int) (fTimestamp ^ (fTimestamp >>> 32));
73 return result;
74 }
75
76 @Override
77 public boolean equals(Object obj) {
78 if (this == obj) {
79 return true;
80 }
81 if (obj == null) {
82 return false;
83 }
84 if (!(obj instanceof CtfLocationInfo)) {
85 return false;
86 }
87 CtfLocationInfo other = (CtfLocationInfo) obj;
88 if (fIndex != other.fIndex) {
89 return false;
90 }
91 if (fTimestamp != other.fTimestamp) {
92 return false;
93 }
94 return true;
95 }
96
97 @Override
98 public String toString() {
99 return "Element [" + fTimestamp + '/' + fIndex + ']'; //$NON-NLS-1$
100 }
101
102 // ------------------------------------------------------------------------
103 // Comparable
104 // ------------------------------------------------------------------------
105
106 @Override
107 public int compareTo(CtfLocationInfo other) {
108 if (fTimestamp > other.getTimestamp()) {
109 return 1;
110 }
111 if (fTimestamp < other.getTimestamp()) {
112 return -1;
113 }
114 if (fIndex > other.getIndex()) {
115 return 1;
116 }
117 if (fIndex < other.getIndex()) {
118 return -1;
119 }
120 return 0;
121 }
122
123 /**
124 * Write the location to the ByteBuffer so that it can be saved to disk.
125 *
126 * @param bufferOut
127 * the buffer to write to
128 */
129 public void serialize(ByteBuffer bufferOut) {
130 bufferOut.putLong(fTimestamp);
131 bufferOut.putLong(fIndex);
132 }
133 }
This page took 0.033541 seconds and 4 git commands to generate.