Commit | Line | Data |
---|---|---|
132a02b0 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2012, 2014 Ericsson |
132a02b0 MK |
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 | *******************************************************************************/ | |
9722e5d7 AM |
11 | |
12 | package org.eclipse.tracecompass.tmf.ctf.core.context; | |
132a02b0 | 13 | |
032ecd45 MAL |
14 | import java.nio.ByteBuffer; |
15 | ||
132a02b0 | 16 | /** |
f5df94f8 | 17 | * The data object to go in a {@link CtfLocation}. |
132a02b0 MK |
18 | * |
19 | * @author Matthew Khouzam | |
132a02b0 | 20 | */ |
f5df94f8 | 21 | public class CtfLocationInfo implements Comparable<CtfLocationInfo> { |
132a02b0 | 22 | |
b1443279 MK |
23 | private final long fTimestamp; |
24 | private final long fIndex; | |
132a02b0 MK |
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 | */ | |
f5df94f8 | 33 | public CtfLocationInfo(long ts, long index) { |
b1443279 MK |
34 | fTimestamp = ts; |
35 | fIndex = index; | |
132a02b0 MK |
36 | } |
37 | ||
032ecd45 MAL |
38 | /** |
39 | * Construct the location from the ByteBuffer. | |
40 | * | |
41 | * @param bufferIn | |
42 | * the buffer to read from | |
032ecd45 MAL |
43 | */ |
44 | public CtfLocationInfo(ByteBuffer bufferIn) { | |
b1443279 MK |
45 | fTimestamp = bufferIn.getLong(); |
46 | fIndex = bufferIn.getLong(); | |
032ecd45 MAL |
47 | } |
48 | ||
132a02b0 MK |
49 | /** |
50 | * @return The timestamp | |
51 | */ | |
52 | public long getTimestamp() { | |
b1443279 | 53 | return fTimestamp; |
132a02b0 MK |
54 | } |
55 | ||
56 | /** | |
57 | * @return The index of the element | |
58 | */ | |
59 | public long getIndex() { | |
b1443279 | 60 | return fIndex; |
132a02b0 MK |
61 | } |
62 | ||
f5df94f8 AM |
63 | // ------------------------------------------------------------------------ |
64 | // Object | |
65 | // ------------------------------------------------------------------------ | |
66 | ||
132a02b0 MK |
67 | @Override |
68 | public int hashCode() { | |
69 | final int prime = 31; | |
70 | int result = 1; | |
b1443279 MK |
71 | result = (prime * result) + (int) (fIndex ^ (fIndex >>> 32)); |
72 | result = (prime * result) + (int) (fTimestamp ^ (fTimestamp >>> 32)); | |
132a02b0 MK |
73 | return result; |
74 | } | |
75 | ||
132a02b0 MK |
76 | @Override |
77 | public boolean equals(Object obj) { | |
78 | if (this == obj) { | |
79 | return true; | |
80 | } | |
81 | if (obj == null) { | |
82 | return false; | |
83 | } | |
f5df94f8 | 84 | if (!(obj instanceof CtfLocationInfo)) { |
132a02b0 MK |
85 | return false; |
86 | } | |
f5df94f8 | 87 | CtfLocationInfo other = (CtfLocationInfo) obj; |
b1443279 | 88 | if (fIndex != other.fIndex) { |
132a02b0 MK |
89 | return false; |
90 | } | |
b1443279 | 91 | if (fTimestamp != other.fTimestamp) { |
132a02b0 MK |
92 | return false; |
93 | } | |
94 | return true; | |
95 | } | |
96 | ||
132a02b0 MK |
97 | @Override |
98 | public String toString() { | |
b1443279 | 99 | return "Element [" + fTimestamp + '/' + fIndex + ']'; //$NON-NLS-1$ |
132a02b0 MK |
100 | } |
101 | ||
f5df94f8 AM |
102 | // ------------------------------------------------------------------------ |
103 | // Comparable | |
104 | // ------------------------------------------------------------------------ | |
105 | ||
132a02b0 | 106 | @Override |
f5df94f8 | 107 | public int compareTo(CtfLocationInfo other) { |
b1443279 | 108 | if (fTimestamp > other.getTimestamp()) { |
132a02b0 MK |
109 | return 1; |
110 | } | |
b1443279 | 111 | if (fTimestamp < other.getTimestamp()) { |
132a02b0 MK |
112 | return -1; |
113 | } | |
b1443279 | 114 | if (fIndex > other.getIndex()) { |
132a02b0 MK |
115 | return 1; |
116 | } | |
b1443279 | 117 | if (fIndex < other.getIndex()) { |
132a02b0 MK |
118 | return -1; |
119 | } | |
120 | return 0; | |
121 | } | |
122 | ||
032ecd45 MAL |
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 | |
032ecd45 MAL |
128 | */ |
129 | public void serialize(ByteBuffer bufferOut) { | |
b1443279 MK |
130 | bufferOut.putLong(fTimestamp); |
131 | bufferOut.putLong(fIndex); | |
032ecd45 | 132 | } |
132a02b0 | 133 | } |