41216f2153ca8395bf771dffd6664ccefe8ed575
[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 org.eclipse.jdt.annotation.Nullable;
16 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
17
18 /**
19 * A state value containing a simple integer.
20 *
21 * @version 1.0
22 * @author Alexandre Montplaisir
23 */
24 final class IntegerStateValue extends TmfStateValue {
25
26 private final int value;
27
28 public IntegerStateValue(int valueAsInt) {
29 this.value = valueAsInt;
30 }
31
32 @Override
33 public Type getType() {
34 return Type.INTEGER;
35 }
36
37 @Override
38 public boolean isNull() {
39 return false;
40 }
41
42 @Override
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;
54 }
55
56 @Override
57 public @Nullable String toString() {
58 return String.format("%3d", value); //$NON-NLS-1$
59 }
60
61 // ------------------------------------------------------------------------
62 // Unboxing methods
63 // ------------------------------------------------------------------------
64
65 @Override
66 public int unboxInt() {
67 return value;
68 }
69
70 @Override
71 public long unboxLong() {
72 /* It's always safe to up-cast an int into a long */
73 return value;
74 }
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
102 }
This page took 0.033684 seconds and 4 git commands to generate.