ss: Add serialization logic to state values
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / statesystem / core / statevalue / IntegerStateValue.java
CommitLineData
a52fde77 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2012, 2014 Ericsson
a52fde77
AM
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
2cb26548 5 *
a52fde77
AM
6 * All rights reserved. This program and the accompanying materials are
7 * made available under the terms of the Eclipse Public License v1.0 which
8 * accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
2cb26548 10 *
a52fde77
AM
11 *******************************************************************************/
12
e894a508 13package org.eclipse.tracecompass.statesystem.core.statevalue;
a52fde77 14
ce148788
AM
15import java.nio.ByteBuffer;
16
77c2c2c9 17import org.eclipse.jdt.annotation.Nullable;
f0247a1a 18import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
77c2c2c9 19
a52fde77
AM
20/**
21 * A state value containing a simple integer.
a52fde77 22 *
2cb26548
AM
23 * @version 1.0
24 * @author Alexandre Montplaisir
a52fde77
AM
25 */
26final class IntegerStateValue extends TmfStateValue {
27
6f7e27a0 28 private final int value;
a52fde77
AM
29
30 public IntegerStateValue(int valueAsInt) {
6f7e27a0 31 this.value = valueAsInt;
a52fde77
AM
32 }
33
34 @Override
b67a2540
AM
35 public Type getType() {
36 return Type.INTEGER;
a52fde77
AM
37 }
38
39 @Override
40 public boolean isNull() {
41 return false;
42 }
43
44 @Override
6f7e27a0
EB
45 public boolean equals(@Nullable Object object) {
46 if (!(object instanceof IntegerStateValue)) {
47 return false;
48 }
49 IntegerStateValue other = (IntegerStateValue) object;
50 return (this.value == other.value);
51 }
52
53 @Override
54 public int hashCode() {
55 return value;
a52fde77
AM
56 }
57
a52fde77 58 @Override
77c2c2c9 59 public @Nullable String toString() {
6f7e27a0 60 return String.format("%3d", value); //$NON-NLS-1$
a52fde77 61 }
a9cdbafc 62
ce148788
AM
63 @Override
64 public byte[] serialize() {
65 ByteBuffer buffer = ByteBuffer.allocate(Byte.BYTES + Integer.BYTES);
66 buffer.put(getType().getByte());
67 buffer.putInt(value);
68 return buffer.array();
69 }
70
a9cdbafc
AM
71 // ------------------------------------------------------------------------
72 // Unboxing methods
73 // ------------------------------------------------------------------------
74
75 @Override
76 public int unboxInt() {
6f7e27a0 77 return value;
a9cdbafc
AM
78 }
79
80 @Override
81 public long unboxLong() {
359eeba0 82 /* It's always safe to up-cast an int into a long */
6f7e27a0 83 return value;
a9cdbafc 84 }
f0247a1a
NE
85
86 @Override
87 public int compareTo(@Nullable ITmfStateValue other) {
88 if (other == null) {
89 throw new IllegalArgumentException();
90 }
91
92 switch (other.getType()) {
93 case INTEGER:
94 IntegerStateValue otherIntValue = (IntegerStateValue) other;
95 return Integer.compare(this.value, otherIntValue.value);
96 case DOUBLE:
97 double otherDoubleValue = ((DoubleStateValue) other).unboxDouble();
98 return Double.compare(this.value, otherDoubleValue);
99 case LONG:
100 long otherLongValue = ((LongStateValue) other).unboxLong();
101 return Long.compare(this.value, otherLongValue);
102 case NULL:
103 return Integer.compare(this.value, other.unboxInt());
104 case STRING:
105 throw new StateValueTypeException("An Integer state value cannot be compared to a String state value."); //$NON-NLS-1$
106 default:
107 throw new StateValueTypeException("An Integer state value cannot be compared to the type " + other.getType()); //$NON-NLS-1$
108 }
109
110 }
a52fde77 111}
This page took 0.125164 seconds and 5 git commands to generate.