dae703845a8e4fb271b098cec5be9199e25ae2b5
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / statesystem / core / statevalue / LongStateValue.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
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
13 package org.eclipse.tracecompass.statesystem.core.statevalue;
14
15 import java.nio.ByteBuffer;
16
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
19
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
28 private final long value;
29
30 public LongStateValue(long valueAsLong) {
31 this.value = valueAsLong;
32 }
33
34 @Override
35 public Type getType() {
36 return Type.LONG;
37 }
38
39 @Override
40 public boolean isNull() {
41 return false;
42 }
43
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 }
52
53 @Override
54 public int hashCode() {
55 return ((int) value) ^ ((int) (value >>> 32));
56 }
57
58 @Override
59 public @Nullable String toString() {
60 return String.format("%3d", value); //$NON-NLS-1$
61 }
62
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
71 // ------------------------------------------------------------------------
72 // Unboxing methods
73 // ------------------------------------------------------------------------
74
75 @Override
76 public long unboxLong() {
77 return value;
78 }
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
106 }
This page took 0.048323 seconds and 4 git commands to generate.