From f0247a1a69df39e47308ffbe5a6b08edb46a462d Mon Sep 17 00:00:00 2001 From: Naser Ezzati Date: Mon, 22 Sep 2014 15:23:08 -0400 Subject: [PATCH] State system: Make ITmfStateValue Comparable It is required to compare two state values based on different comparison operators. It will be useful for the XML analysis. Change-Id: I1fcb9525a41b11a648922271b5c2e06d6bd309ac Signed-off-by: Naser Ezzati Signed-off-by: Alexandre Montplaisir Reviewed-on: https://git.eclipse.org/r/35414 Reviewed-by: Genevieve Bastien Tested-by: Hudson CI --- .../statesystem/core/tests/AllTests.java | 5 +- .../core/tests/statevalue/AllTests.java | 27 ++ .../statevalue/StateValueCompareToTest.java | 326 ++++++++++++++++++ .../core/statevalue/DoubleStateValue.java | 28 ++ .../core/statevalue/ITmfStateValue.java | 3 +- .../core/statevalue/IntegerStateValue.java | 28 ++ .../core/statevalue/LongStateValue.java | 29 ++ .../core/statevalue/NullStateValue.java | 18 +- .../core/statevalue/StringStateValue.java | 30 ++ 9 files changed, 490 insertions(+), 4 deletions(-) create mode 100644 org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/statevalue/AllTests.java create mode 100644 org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/statevalue/StateValueCompareToTest.java diff --git a/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/AllTests.java b/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/AllTests.java index 9cbc839473..5e6e2a4bbd 100644 --- a/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/AllTests.java +++ b/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/AllTests.java @@ -20,8 +20,9 @@ import org.junit.runners.Suite; */ @RunWith(Suite.class) @Suite.SuiteClasses({ - StateSystemPushPopTest.class, - org.eclipse.tracecompass.statesystem.core.tests.backend.AllTests.class + StateSystemPushPopTest.class, + org.eclipse.tracecompass.statesystem.core.tests.backend.AllTests.class, + org.eclipse.tracecompass.statesystem.core.tests.statevalue.AllTests.class }) public class AllTests { diff --git a/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/statevalue/AllTests.java b/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/statevalue/AllTests.java new file mode 100644 index 0000000000..c7c02edbda --- /dev/null +++ b/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/statevalue/AllTests.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2014 École Polytechnique de Montréal + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Naser Ezzati - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.tracecompass.statesystem.core.tests.statevalue; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * Test suite for the StateValue package + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + StateValueCompareToTest.class +}) +public class AllTests { + +} diff --git a/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/statevalue/StateValueCompareToTest.java b/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/statevalue/StateValueCompareToTest.java new file mode 100644 index 0000000000..f65b18df7a --- /dev/null +++ b/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/statevalue/StateValueCompareToTest.java @@ -0,0 +1,326 @@ +/******************************************************************************* + * Copyright (c) 2014 École Polytechnique de Montréal + * + * All rights reserved. This program and the accompanying materials are + * made availabComparisonOperator.LE under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is availabComparisonOperator.LE at + * http://www.eclipse.org/ComparisonOperator.LEgal/epl-v10.html + * + * Contributors: + * Naser Ezzati - Initial API and implementation + ******************************************************************************/ + +package org.eclipse.tracecompass.statesystem.core.tests.statevalue; + +import static org.junit.Assert.assertTrue; + +import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; +import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; +import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue; +import org.junit.Test; + +/** + * Unit test for the {@link ITmfStateValue#compareTo(ITmfStateValue)} method + * + * @author Naser Ezzati + */ +public class StateValueCompareToTest { + + // ------------------------------------------------------------------------ + // Static fields + // ------------------------------------------------------------------------ + + /* State values that will be used */ + private static final ITmfStateValue BASE_INT_VALUE = TmfStateValue.newValueInt(10); + private static final ITmfStateValue BIGGER_INT_VALUE = TmfStateValue.newValueInt(20); + private static final ITmfStateValue SMALLER_INT_VALUE = TmfStateValue.newValueInt(6); + + private static final ITmfStateValue BASE_LONG_VALUE = TmfStateValue.newValueLong(10); + private static final ITmfStateValue BIGGER_LONG_VALUE = TmfStateValue.newValueLong(20); + private static final ITmfStateValue SMALLER_LONG_VALUE = TmfStateValue.newValueLong(6); + private static final ITmfStateValue MIN_LONG_VALUE = TmfStateValue.newValueLong(Long.MIN_VALUE); + private static final ITmfStateValue MAX_LONG_VALUE = TmfStateValue.newValueLong(Long.MAX_VALUE); + + private static final ITmfStateValue BASE_DOUBLE_VALUE = TmfStateValue.newValueDouble(10.00); + private static final ITmfStateValue BIGGER_DOUBLE_VALUE1 = TmfStateValue.newValueDouble(20.00); + private static final ITmfStateValue BIGGER_DOUBLE_VALUE2 = TmfStateValue.newValueDouble(10.03); + private static final ITmfStateValue SMALLER_DOUBLE_VALUE1 = TmfStateValue.newValueDouble(6.00); + private static final ITmfStateValue SMALLER_DOUBLE_VALUE2 = TmfStateValue.newValueDouble(9.99); + private static final ITmfStateValue MIN_DOUBLE_VALUE = TmfStateValue.newValueDouble(Double.MIN_VALUE); + private static final ITmfStateValue MAX_DOUBLE_VALUE = TmfStateValue.newValueDouble(Double.MAX_VALUE); + private static final ITmfStateValue POSITIVE_INFINITY = TmfStateValue.newValueDouble(Double.POSITIVE_INFINITY); + private static final ITmfStateValue NEGATIVE_INFINITY = TmfStateValue.newValueDouble(Double.NEGATIVE_INFINITY); + + private static final ITmfStateValue BASE_STRING_VALUE = TmfStateValue.newValueString("D"); + private static final ITmfStateValue BIGGER_STRING_VALUE = TmfStateValue.newValueString("Z"); + private static final ITmfStateValue SMALLER_STRING_VALUE = TmfStateValue.newValueString("A"); + + private static final ITmfStateValue NULL_VALUE = TmfStateValue.nullValue(); + + // ------------------------------------------------------------------------ + // Comparisons of Integer state values + // ------------------------------------------------------------------------ + + /** + * Compare Integer state values together + */ + @Test + public void compareIntWithInt() { + assertTrue(BASE_INT_VALUE.compareTo(BASE_INT_VALUE) == 0); + assertTrue(BASE_INT_VALUE.compareTo(BIGGER_INT_VALUE) < 0); + assertTrue(BASE_INT_VALUE.compareTo(SMALLER_INT_VALUE) > 0); + } + + /** + * Compare Integer with Long state values + */ + @Test + public void compareIntWithLong() { + assertTrue(BASE_INT_VALUE.compareTo(BASE_LONG_VALUE) == 0); + assertTrue(BASE_INT_VALUE.compareTo(BIGGER_LONG_VALUE) < 0); + assertTrue(BASE_INT_VALUE.compareTo(MAX_LONG_VALUE) < 0); + + assertTrue(BASE_INT_VALUE.compareTo(SMALLER_LONG_VALUE) > 0); + assertTrue(BASE_INT_VALUE.compareTo(MIN_LONG_VALUE) > 0); + } + + /** + * Compare Integer with Double state values + */ + @Test + public void compareIntWithDouble() { + assertTrue(BASE_INT_VALUE.compareTo(BASE_DOUBLE_VALUE) == 0); + assertTrue(BASE_INT_VALUE.compareTo(BIGGER_DOUBLE_VALUE1) < 0); + assertTrue(BASE_INT_VALUE.compareTo(BIGGER_DOUBLE_VALUE2) < 0); + assertTrue(BASE_INT_VALUE.compareTo(MAX_DOUBLE_VALUE) < 0); + assertTrue(BASE_INT_VALUE.compareTo(POSITIVE_INFINITY) < 0); + assertTrue(BASE_INT_VALUE.compareTo(SMALLER_DOUBLE_VALUE1) > 0); + assertTrue(BASE_INT_VALUE.compareTo(SMALLER_DOUBLE_VALUE2) > 0); + assertTrue(BASE_INT_VALUE.compareTo(MIN_DOUBLE_VALUE) > 0); + assertTrue(BASE_INT_VALUE.compareTo(NEGATIVE_INFINITY) > 0); + } + + /** + * Compare Integer with Null state values + */ + @Test + public void compareIntWithNull() { + assertTrue(BASE_INT_VALUE.compareTo(NULL_VALUE) > 0); + } + + /** + * Compare Integer with String state values (should fail) + */ + @Test(expected = StateValueTypeException.class) + public void tcompareIntWithString() { + BASE_INT_VALUE.compareTo(BASE_STRING_VALUE); + } + + // ------------------------------------------------------------------------ + // Comparisons of Long state values + // ------------------------------------------------------------------------ + + /** + * Compare Long with Integer state values + */ + @Test + public void compareLongWithInt() { + // with Integer + assertTrue(BASE_LONG_VALUE.compareTo(BASE_INT_VALUE) == 0); + assertTrue(BASE_LONG_VALUE.compareTo(BIGGER_INT_VALUE) < 0); + assertTrue(BASE_LONG_VALUE.compareTo(SMALLER_INT_VALUE) > 0); + } + + /** + * Compare Long state values together + */ + @Test + public void compareLongWithLong() { + assertTrue(BASE_LONG_VALUE.compareTo(BASE_LONG_VALUE) == 0); + assertTrue(BASE_LONG_VALUE.compareTo(BIGGER_LONG_VALUE) < 0); + assertTrue(BASE_LONG_VALUE.compareTo(MAX_LONG_VALUE) < 0); + assertTrue(BASE_LONG_VALUE.compareTo(SMALLER_LONG_VALUE) > 0); + assertTrue(BASE_LONG_VALUE.compareTo(MIN_LONG_VALUE) > 0); + } + + /** + * Compare Long with Double state values + */ + @Test + public void compareLongWithDouble() { + assertTrue(BASE_LONG_VALUE.compareTo(BASE_DOUBLE_VALUE) == 0); + assertTrue(BASE_LONG_VALUE.compareTo(BIGGER_DOUBLE_VALUE1) < 0); + assertTrue(BASE_LONG_VALUE.compareTo(BIGGER_DOUBLE_VALUE2) < 0); + assertTrue(BASE_LONG_VALUE.compareTo(MAX_DOUBLE_VALUE) < 0); + assertTrue(BASE_LONG_VALUE.compareTo(POSITIVE_INFINITY) < 0); + assertTrue(BASE_LONG_VALUE.compareTo(SMALLER_DOUBLE_VALUE1) > 0); + assertTrue(BASE_LONG_VALUE.compareTo(SMALLER_DOUBLE_VALUE2) > 0); + assertTrue(BASE_LONG_VALUE.compareTo(MIN_DOUBLE_VALUE) > 0); + assertTrue(BASE_LONG_VALUE.compareTo(NEGATIVE_INFINITY) > 0); + } + + /** + * Compare Long with Null state values + */ + @Test + public void compareLongWithNull() { + assertTrue(BASE_LONG_VALUE.compareTo(NULL_VALUE) > 0); + } + + /** + * Compare Long with String state values (should fail) + */ + @Test(expected = StateValueTypeException.class) + public void compareLongWithString() { + BASE_LONG_VALUE.compareTo(BASE_STRING_VALUE); + } + + // ------------------------------------------------------------------------ + // Comparisons of Double state values + // ------------------------------------------------------------------------ + + /** + * Compare Double with Integer state values + */ + @Test + public void compareDoubleWithInt() { + assertTrue(BASE_DOUBLE_VALUE.compareTo(BASE_INT_VALUE) == 0); + assertTrue(BASE_DOUBLE_VALUE.compareTo(BIGGER_INT_VALUE) < 0); + assertTrue(BASE_DOUBLE_VALUE.compareTo(SMALLER_INT_VALUE) > 0); + } + + /** + * Compare Double with Long state values + */ + @Test + public void compareDoubleWithLong() { + assertTrue(BASE_DOUBLE_VALUE.compareTo(BASE_LONG_VALUE) == 0); + assertTrue(BASE_DOUBLE_VALUE.compareTo(BIGGER_LONG_VALUE) < 0); + assertTrue(SMALLER_DOUBLE_VALUE2.compareTo(BASE_LONG_VALUE) < 0); + assertTrue(BASE_DOUBLE_VALUE.compareTo(MAX_LONG_VALUE) < 0); + assertTrue(BIGGER_DOUBLE_VALUE1.compareTo(SMALLER_LONG_VALUE) > 0); + assertTrue(BIGGER_DOUBLE_VALUE2.compareTo(BASE_LONG_VALUE) > 0); + assertTrue(BASE_DOUBLE_VALUE.compareTo(MIN_LONG_VALUE) > 0); + } + + /** + * Compare Double state values together + */ + @Test + public void compareDoubleWithDouble() { + assertTrue(BASE_DOUBLE_VALUE.compareTo(BASE_DOUBLE_VALUE) == 0); + assertTrue(BASE_DOUBLE_VALUE.compareTo(BIGGER_DOUBLE_VALUE2) < 0); + assertTrue(BASE_DOUBLE_VALUE.compareTo(MAX_DOUBLE_VALUE) < 0); + assertTrue(BASE_DOUBLE_VALUE.compareTo(SMALLER_DOUBLE_VALUE2) > 0); + assertTrue(BASE_DOUBLE_VALUE.compareTo(MIN_DOUBLE_VALUE) > 0); + } + + /** + * Compare Double with Null state values + */ + @Test + public void compareDoubleWithNull() { + /* NullValue.unboxDouble returns NaN */ + assertTrue(BASE_DOUBLE_VALUE.compareTo(NULL_VALUE) < 0); + } + + /** + * Compare Double with String state values (should fail) + */ + @Test(expected = StateValueTypeException.class) + public void compareDoubleWithString() { + BASE_DOUBLE_VALUE.compareTo(BASE_STRING_VALUE); + } + + // ------------------------------------------------------------------------ + // Comparisons of String state values + // ------------------------------------------------------------------------ + + /** + * Compare String with Integer state values (should fail) + */ + @Test(expected = StateValueTypeException.class) + public void compareStringWithInt() { + BASE_STRING_VALUE.compareTo(BASE_INT_VALUE); + } + + /** + * Compare String with Long state values (should fail) + */ + @Test(expected = StateValueTypeException.class) + public void compareStringWithLong() { + BASE_STRING_VALUE.compareTo(BASE_LONG_VALUE); + } + + /** + * Compare String with Double state values (should fail) + */ + @Test(expected = StateValueTypeException.class) + public void compareStringWithDouble() { + BASE_STRING_VALUE.compareTo(BASE_DOUBLE_VALUE); + } + + /** + * Compare String state values together + */ + @Test + public void compareStringWithString() { + assertTrue(BASE_STRING_VALUE.compareTo(BASE_STRING_VALUE) == 0); + assertTrue(BASE_STRING_VALUE.compareTo(SMALLER_STRING_VALUE) > 0); + assertTrue(BASE_STRING_VALUE.compareTo(BIGGER_STRING_VALUE) < 0); + } + + /** + * Compare String with Null state values + */ + @Test + public void compareStringWithNull() { + assertTrue(BASE_STRING_VALUE.compareTo(NULL_VALUE) > 0); + } + + // ------------------------------------------------------------------------ + // Comparisons of Null state values + // ------------------------------------------------------------------------ + + /** + * Compare Null with Integer state values + */ + @Test + public void compareNullWithInt() { + assertTrue(NULL_VALUE.compareTo(BASE_INT_VALUE) < 0); + } + + /** + * Compare Null with Long state values + */ + @Test + public void compareNullWithLong() { + assertTrue(NULL_VALUE.compareTo(BASE_LONG_VALUE) < 0); + } + + /** + * Compare Null with Double state values + */ + @Test + public void compareNullWithDouble() { + /* NullValue.unboxDouble returns NaN */ + assertTrue(NULL_VALUE.compareTo(BASE_DOUBLE_VALUE) > 0); + } + + /** + * Compare Null with String state values + */ + @Test + public void compareNullWithString() { + assertTrue(NULL_VALUE.compareTo(BASE_STRING_VALUE) < 0); + } + + /** + * Compare Null state values together + */ + @Test + public void compareNullWithNull() { + assertTrue(NULL_VALUE.compareTo(NULL_VALUE) == 0); + } + +} diff --git a/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/DoubleStateValue.java b/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/DoubleStateValue.java index 10ecccddc3..36c2061d01 100644 --- a/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/DoubleStateValue.java +++ b/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/DoubleStateValue.java @@ -13,6 +13,7 @@ package org.eclipse.tracecompass.statesystem.core.statevalue; import org.eclipse.jdt.annotation.Nullable; +import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; /** * A state value containing a double primitive. @@ -65,4 +66,31 @@ final class DoubleStateValue extends TmfStateValue { public double unboxDouble() { return value; } + + @Override + public int compareTo(@Nullable ITmfStateValue other) { + if (other == null) { + throw new IllegalArgumentException(); + } + + switch (other.getType()) { + case INTEGER: + double otherDoubleValue = ((IntegerStateValue) other).unboxInt(); + return Double.compare(this.value, otherDoubleValue); + case DOUBLE: + otherDoubleValue = ((DoubleStateValue) other).unboxDouble(); + return Double.compare(this.value, otherDoubleValue); + case LONG: + otherDoubleValue = ((LongStateValue) other).unboxLong(); + return Double.compare(this.value, otherDoubleValue); + case NULL: + return Double.compare(this.value, other.unboxDouble()); + case STRING: + throw new StateValueTypeException("A Double state value cannot be compared to a String state value."); //$NON-NLS-1$ + default: + throw new StateValueTypeException("A Double state value cannot be compared to the type " + other.getType()); //$NON-NLS-1$ + } + + } + } diff --git a/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/ITmfStateValue.java b/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/ITmfStateValue.java index 1bad14084d..71b28ed46d 100644 --- a/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/ITmfStateValue.java +++ b/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/ITmfStateValue.java @@ -20,7 +20,7 @@ import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeExcept * @author Alexandre Montplaisir * @since 3.0 */ -public interface ITmfStateValue { +public interface ITmfStateValue extends Comparable { /** * The supported types of state values @@ -95,4 +95,5 @@ public interface ITmfStateValue { * If the contained value cannot be read as a String */ String unboxStr(); + } diff --git a/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/IntegerStateValue.java b/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/IntegerStateValue.java index 37dd2fd519..82015660fd 100644 --- a/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/IntegerStateValue.java +++ b/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/IntegerStateValue.java @@ -13,6 +13,7 @@ package org.eclipse.tracecompass.statesystem.core.statevalue; import org.eclipse.jdt.annotation.Nullable; +import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; /** * A state value containing a simple integer. @@ -71,4 +72,31 @@ final class IntegerStateValue extends TmfStateValue { /* It's always safe to up-cast an int into a long */ return value; } + + @Override + public int compareTo(@Nullable ITmfStateValue other) { + if (other == null) { + throw new IllegalArgumentException(); + } + + switch (other.getType()) { + case INTEGER: + IntegerStateValue otherIntValue = (IntegerStateValue) other; + return Integer.compare(this.value, otherIntValue.value); + case DOUBLE: + double otherDoubleValue = ((DoubleStateValue) other).unboxDouble(); + return Double.compare(this.value, otherDoubleValue); + case LONG: + long otherLongValue = ((LongStateValue) other).unboxLong(); + return Long.compare(this.value, otherLongValue); + case NULL: + return Integer.compare(this.value, other.unboxInt()); + case STRING: + throw new StateValueTypeException("An Integer state value cannot be compared to a String state value."); //$NON-NLS-1$ + default: + throw new StateValueTypeException("An Integer state value cannot be compared to the type " + other.getType()); //$NON-NLS-1$ + } + + } + } diff --git a/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/LongStateValue.java b/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/LongStateValue.java index cf510df1c1..75dd2d57e2 100644 --- a/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/LongStateValue.java +++ b/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/LongStateValue.java @@ -13,6 +13,7 @@ package org.eclipse.tracecompass.statesystem.core.statevalue; import org.eclipse.jdt.annotation.Nullable; +import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; /** * A state value containing a long integer (8 bytes). @@ -37,6 +38,7 @@ final class LongStateValue extends TmfStateValue { public boolean isNull() { return false; } + @Override public boolean equals(@Nullable Object object) { if (!(object instanceof LongStateValue)) { @@ -64,4 +66,31 @@ final class LongStateValue extends TmfStateValue { public long unboxLong() { return value; } + + @Override + public int compareTo(@Nullable ITmfStateValue other) { + if (other == null) { + throw new IllegalArgumentException(); + } + + switch (other.getType()) { + case INTEGER: + long otherLongValue = ((IntegerStateValue) other).unboxInt(); + return Long.compare(this.value, otherLongValue); + case DOUBLE: + double otherDoubleValue = ((DoubleStateValue) other).unboxDouble(); + return Double.compare(this.value, otherDoubleValue); + case LONG: + otherLongValue = ((LongStateValue) other).unboxLong(); + return Long.compare(this.value, otherLongValue); + case NULL: + return Long.compare(this.value, other.unboxLong()); + case STRING: + throw new StateValueTypeException("A Long state value cannot be compared to a String state value."); //$NON-NLS-1$ + default: + throw new StateValueTypeException("A Long state value cannot be compared to the type " + other.getType()); //$NON-NLS-1$ + } + + } + } diff --git a/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/NullStateValue.java b/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/NullStateValue.java index de852c7a99..dea061dc98 100644 --- a/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/NullStateValue.java +++ b/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/NullStateValue.java @@ -44,7 +44,7 @@ final class NullStateValue extends TmfStateValue { @Override public int hashCode() { - return 0; + return 0; } @Override @@ -75,4 +75,20 @@ final class NullStateValue extends TmfStateValue { public String unboxStr() { return value; } + + @Override + public int compareTo(@Nullable ITmfStateValue other) { + if (other == null) { + throw new IllegalArgumentException(); + } + if (other instanceof NullStateValue) { + return 0; + } + /* + * For every other state value type, we defer to how that type wants to + * be compared against null values. + */ + return -(other.compareTo(this)); + } + } diff --git a/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/StringStateValue.java b/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/StringStateValue.java index 6fae2ff6a2..06001e960b 100644 --- a/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/StringStateValue.java +++ b/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/StringStateValue.java @@ -13,6 +13,7 @@ package org.eclipse.tracecompass.statesystem.core.statevalue; import org.eclipse.jdt.annotation.Nullable; +import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; /** * A state value containing a variable-sized string @@ -65,4 +66,33 @@ final class StringStateValue extends TmfStateValue { public String unboxStr() { return value; } + + @Override + public int compareTo(@Nullable ITmfStateValue other) { + if (other == null) { + throw new IllegalArgumentException(); + } + + switch (other.getType()) { + case DOUBLE: + throw new StateValueTypeException("A String state value cannot be compared to a Double state value."); //$NON-NLS-1$ + case INTEGER: + throw new StateValueTypeException("A String state value cannot be compared to an Integer state value."); //$NON-NLS-1$ + case LONG: + throw new StateValueTypeException("A String state value cannot be compared to a Long state value."); //$NON-NLS-1$ + case NULL: + /* + * We assume that every string state value is greater than a null + * state value. + */ + return 1; + case STRING: + StringStateValue otherStringValue = (StringStateValue) other; + return value.compareTo(otherStringValue.value); + default: + throw new StateValueTypeException("A String state value cannot be compared to the type " + other.getType()); //$NON-NLS-1$ + } + + } + } -- 2.34.1