Commit | Line | Data |
---|---|---|
1cbf1a19 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2013, 2014 École Polytechnique de Montréal |
1cbf1a19 FR |
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 | * François Rajotte - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
e894a508 | 13 | package org.eclipse.tracecompass.statesystem.core.statevalue; |
1cbf1a19 | 14 | |
ce148788 AM |
15 | import java.nio.ByteBuffer; |
16 | ||
77c2c2c9 | 17 | import org.eclipse.jdt.annotation.Nullable; |
f0247a1a | 18 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; |
77c2c2c9 | 19 | |
1cbf1a19 FR |
20 | /** |
21 | * A state value containing a long integer (8 bytes). | |
22 | * | |
23 | * @version 1.0 | |
24 | * @author François Rajotte | |
25 | */ | |
26 | final class LongStateValue extends TmfStateValue { | |
27 | ||
6f7e27a0 | 28 | private final long value; |
1cbf1a19 FR |
29 | |
30 | public LongStateValue(long valueAsLong) { | |
6f7e27a0 | 31 | this.value = valueAsLong; |
1cbf1a19 FR |
32 | } |
33 | ||
34 | @Override | |
35 | public Type getType() { | |
36 | return Type.LONG; | |
37 | } | |
38 | ||
39 | @Override | |
40 | public boolean isNull() { | |
41 | return false; | |
42 | } | |
f0247a1a | 43 | |
6f7e27a0 EB |
44 | @Override |
45 | public boolean equals(@Nullable Object object) { | |
46 | if (!(object instanceof LongStateValue)) { | |
47 | return false; | |
48 | } | |
49 | LongStateValue other = (LongStateValue) object; | |
50 | return (this.value == other.value); | |
51 | } | |
1cbf1a19 FR |
52 | |
53 | @Override | |
6f7e27a0 EB |
54 | public int hashCode() { |
55 | return ((int) value) ^ ((int) (value >>> 32)); | |
1cbf1a19 FR |
56 | } |
57 | ||
1cbf1a19 | 58 | @Override |
77c2c2c9 | 59 | public @Nullable String toString() { |
6f7e27a0 | 60 | return String.format("%3d", value); //$NON-NLS-1$ |
1cbf1a19 | 61 | } |
a9cdbafc | 62 | |
ce148788 AM |
63 | @Override |
64 | public byte[] serialize() { | |
65 | ByteBuffer buffer = ByteBuffer.allocate(Byte.BYTES + Long.BYTES); | |
66 | buffer.put(getType().getByte()); | |
67 | buffer.putLong(value); | |
68 | return buffer.array(); | |
69 | } | |
70 | ||
a9cdbafc AM |
71 | // ------------------------------------------------------------------------ |
72 | // Unboxing methods | |
73 | // ------------------------------------------------------------------------ | |
74 | ||
75 | @Override | |
76 | public long unboxLong() { | |
6f7e27a0 | 77 | return value; |
a9cdbafc | 78 | } |
f0247a1a NE |
79 | |
80 | @Override | |
81 | public int compareTo(@Nullable ITmfStateValue other) { | |
82 | if (other == null) { | |
83 | throw new IllegalArgumentException(); | |
84 | } | |
85 | ||
86 | switch (other.getType()) { | |
87 | case INTEGER: | |
88 | long otherLongValue = ((IntegerStateValue) other).unboxInt(); | |
89 | return Long.compare(this.value, otherLongValue); | |
90 | case DOUBLE: | |
91 | double otherDoubleValue = ((DoubleStateValue) other).unboxDouble(); | |
92 | return Double.compare(this.value, otherDoubleValue); | |
93 | case LONG: | |
94 | otherLongValue = ((LongStateValue) other).unboxLong(); | |
95 | return Long.compare(this.value, otherLongValue); | |
96 | case NULL: | |
97 | return Long.compare(this.value, other.unboxLong()); | |
98 | case STRING: | |
99 | throw new StateValueTypeException("A Long state value cannot be compared to a String state value."); //$NON-NLS-1$ | |
100 | default: | |
101 | throw new StateValueTypeException("A Long state value cannot be compared to the type " + other.getType()); //$NON-NLS-1$ | |
102 | } | |
103 | ||
104 | } | |
105 | ||
1cbf1a19 | 106 | } |