ss: Add a custom state value type
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / statesystem / core / statevalue / DoubleStateValue.java
CommitLineData
a3c22e8e 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2013, 2014 Ericsson
a3c22e8e
AM
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
e894a508 13package org.eclipse.tracecompass.statesystem.core.statevalue;
a3c22e8e 14
75d2d348 15import org.eclipse.jdt.annotation.Nullable;
f0247a1a 16import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
75d2d348 17
a3c22e8e
AM
18/**
19 * A state value containing a double primitive.
20 *
21 * @author Alexandre Montplaisir
22 */
23final class DoubleStateValue extends TmfStateValue {
24
6f7e27a0 25 private final double value;
a3c22e8e
AM
26
27 public DoubleStateValue(double value) {
6f7e27a0 28 this.value = value;
a3c22e8e
AM
29 }
30
31 @Override
32 public Type getType() {
ec5fec10 33 return Type.DOUBLE;
a3c22e8e
AM
34 }
35
36 @Override
37 public boolean isNull() {
38 return false;
39 }
40
41 @Override
6f7e27a0
EB
42 public boolean equals(@Nullable Object object) {
43 if (!(object instanceof DoubleStateValue)) {
44 return false;
45 }
46 DoubleStateValue other = (DoubleStateValue) object;
47 return (Double.compare(this.value, other.value) == 0);
48 }
49
50 @Override
51 public int hashCode() {
52 long bits = Double.doubleToLongBits(value);
53 return ((int) bits) ^ ((int) (bits >>> 32));
a3c22e8e
AM
54 }
55
56 @Override
75d2d348 57 public @Nullable String toString() {
6f7e27a0 58 return String.format("%3f", value); //$NON-NLS-1$
a3c22e8e
AM
59 }
60
61 // ------------------------------------------------------------------------
62 // Unboxing methods
63 // ------------------------------------------------------------------------
64
65 @Override
66 public double unboxDouble() {
6f7e27a0 67 return value;
a3c22e8e 68 }
f0247a1a
NE
69
70 @Override
71 public int compareTo(@Nullable ITmfStateValue other) {
72 if (other == null) {
73 throw new IllegalArgumentException();
74 }
75
76 switch (other.getType()) {
77 case INTEGER:
78 double otherDoubleValue = ((IntegerStateValue) other).unboxInt();
79 return Double.compare(this.value, otherDoubleValue);
80 case DOUBLE:
81 otherDoubleValue = ((DoubleStateValue) other).unboxDouble();
82 return Double.compare(this.value, otherDoubleValue);
83 case LONG:
84 otherDoubleValue = ((LongStateValue) other).unboxLong();
85 return Double.compare(this.value, otherDoubleValue);
86 case NULL:
87 return Double.compare(this.value, other.unboxDouble());
88 case STRING:
89 throw new StateValueTypeException("A Double state value cannot be compared to a String state value."); //$NON-NLS-1$
7ef85bff 90 case CUSTOM:
f0247a1a
NE
91 default:
92 throw new StateValueTypeException("A Double state value cannot be compared to the type " + other.getType()); //$NON-NLS-1$
93 }
94
95 }
96
a3c22e8e 97}
This page took 0.066845 seconds and 5 git commands to generate.