lttng: Bump Bundle-Version in lttng.ust.ui
[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
77c2c2c9 15import org.eclipse.jdt.annotation.Nullable;
f0247a1a 16import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
77c2c2c9 17
a52fde77
AM
18/**
19 * A state value containing a simple integer.
a52fde77 20 *
2cb26548
AM
21 * @version 1.0
22 * @author Alexandre Montplaisir
a52fde77
AM
23 */
24final class IntegerStateValue extends TmfStateValue {
25
6f7e27a0 26 private final int value;
a52fde77
AM
27
28 public IntegerStateValue(int valueAsInt) {
6f7e27a0 29 this.value = valueAsInt;
a52fde77
AM
30 }
31
32 @Override
b67a2540
AM
33 public Type getType() {
34 return Type.INTEGER;
a52fde77
AM
35 }
36
37 @Override
38 public boolean isNull() {
39 return false;
40 }
41
42 @Override
6f7e27a0
EB
43 public boolean equals(@Nullable Object object) {
44 if (!(object instanceof IntegerStateValue)) {
45 return false;
46 }
47 IntegerStateValue other = (IntegerStateValue) object;
48 return (this.value == other.value);
49 }
50
51 @Override
52 public int hashCode() {
53 return value;
a52fde77
AM
54 }
55
a52fde77 56 @Override
77c2c2c9 57 public @Nullable String toString() {
6f7e27a0 58 return String.format("%3d", value); //$NON-NLS-1$
a52fde77 59 }
a9cdbafc
AM
60
61 // ------------------------------------------------------------------------
62 // Unboxing methods
63 // ------------------------------------------------------------------------
64
65 @Override
66 public int unboxInt() {
6f7e27a0 67 return value;
a9cdbafc
AM
68 }
69
70 @Override
71 public long unboxLong() {
359eeba0 72 /* It's always safe to up-cast an int into a long */
6f7e27a0 73 return value;
a9cdbafc 74 }
f0247a1a
NE
75
76 @Override
77 public int compareTo(@Nullable ITmfStateValue other) {
78 if (other == null) {
79 throw new IllegalArgumentException();
80 }
81
82 switch (other.getType()) {
83 case INTEGER:
84 IntegerStateValue otherIntValue = (IntegerStateValue) other;
85 return Integer.compare(this.value, otherIntValue.value);
86 case DOUBLE:
87 double otherDoubleValue = ((DoubleStateValue) other).unboxDouble();
88 return Double.compare(this.value, otherDoubleValue);
89 case LONG:
90 long otherLongValue = ((LongStateValue) other).unboxLong();
91 return Long.compare(this.value, otherLongValue);
92 case NULL:
93 return Integer.compare(this.value, other.unboxInt());
94 case STRING:
95 throw new StateValueTypeException("An Integer state value cannot be compared to a String state value."); //$NON-NLS-1$
96 default:
97 throw new StateValueTypeException("An Integer state value cannot be compared to the type " + other.getType()); //$NON-NLS-1$
98 }
99
100 }
101
a52fde77 102}
This page took 0.081117 seconds and 5 git commands to generate.