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
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
5 *
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
10 *
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 simple integer.
22 *
23 * @version 1.0
24 * @author Alexandre Montplaisir
25 */
26 final class IntegerStateValue extends TmfStateValue {
27
28 private final int value;
29
30 public IntegerStateValue(int valueAsInt) {
31 this.value = valueAsInt;
32 }
33
34 @Override
35 public Type getType() {
36 return Type.INTEGER;
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 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;
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 + Integer.BYTES);
66 buffer.put(getType().getByte());
67 buffer.putInt(value);
68 return buffer.array();
69 }
70
71 // ------------------------------------------------------------------------
72 // Unboxing methods
73 // ------------------------------------------------------------------------
74
75 @Override
76 public int unboxInt() {
77 return value;
78 }
79
80 @Override
81 public long unboxLong() {
82 /* It's always safe to up-cast an int into a long */
83 return value;
84 }
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 }
111 }
This page took 0.032838 seconds and 5 git commands to generate.