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>
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
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
;
15 import java
.nio
.ByteBuffer
;
17 import org
.eclipse
.jdt
.annotation
.Nullable
;
18 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
21 * A state value containing a simple integer.
24 * @author Alexandre Montplaisir
26 final class IntegerStateValue
extends TmfStateValue
{
28 private final int value
;
30 public IntegerStateValue(int valueAsInt
) {
31 this.value
= valueAsInt
;
35 public Type
getType() {
40 public boolean isNull() {
45 public boolean equals(@Nullable Object object
) {
46 if (!(object
instanceof IntegerStateValue
)) {
49 IntegerStateValue other
= (IntegerStateValue
) object
;
50 return (this.value
== other
.value
);
54 public int hashCode() {
59 public @Nullable String
toString() {
60 return String
.format("%3d", value
); //$NON-NLS-1$
64 public byte[] serialize() {
65 ByteBuffer buffer
= ByteBuffer
.allocate(Byte
.BYTES
+ Integer
.BYTES
);
66 buffer
.put(getType().getByte());
68 return buffer
.array();
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
76 public int unboxInt() {
81 public long unboxLong() {
82 /* It's always safe to up-cast an int into a long */
87 public int compareTo(@Nullable ITmfStateValue other
) {
89 throw new IllegalArgumentException();
92 switch (other
.getType()) {
94 IntegerStateValue otherIntValue
= (IntegerStateValue
) other
;
95 return Integer
.compare(this.value
, otherIntValue
.value
);
97 double otherDoubleValue
= ((DoubleStateValue
) other
).unboxDouble();
98 return Double
.compare(this.value
, otherDoubleValue
);
100 long otherLongValue
= ((LongStateValue
) other
).unboxLong();
101 return Long
.compare(this.value
, otherLongValue
);
103 return Integer
.compare(this.value
, other
.unboxInt());
105 throw new StateValueTypeException("An Integer state value cannot be compared to a String state value."); //$NON-NLS-1$
107 throw new StateValueTypeException("An Integer state value cannot be compared to the type " + other
.getType()); //$NON-NLS-1$