From 584f23a6868aae533a726052ec71b1377a973370 Mon Sep 17 00:00:00 2001 From: Etienne Bergeron Date: Wed, 27 Nov 2013 19:29:23 -0500 Subject: tmf: Cleanup TmfStateValue to remove 1-level of indirection. This is a refactoring to diminush the memory usage of the StateSystem. In the current implementation, each TmfStateValue has a "box" for an other object (i.e. Integer, Double, String, ...). We propose to remove this indirection and use the raw type inside the corresponding type. As an example, IntegerStateValue now contains an "int" instead of an "Integer". Change-Id: Iaa75172be42174ebd6dc7d9bdd434631077c0146 Signed-off-by: Etienne Bergeron Reviewed-on: https://git.eclipse.org/r/19027 Reviewed-by: Alexandre Montplaisir IP-Clean: Alexandre Montplaisir Tested-by: Hudson CI Reviewed-by: Matthew Khouzam IP-Clean: Matthew Khouzam Tested-by: Matthew Khouzam --- .../tmf/core/statevalue/DoubleStateValue.java | 22 +++++++--- .../tmf/core/statevalue/IntegerStateValue.java | 23 +++++++--- .../tmf/core/statevalue/LongStateValue.java | 20 ++++++--- .../tmf/core/statevalue/NullStateValue.java | 13 ++++-- .../tmf/core/statevalue/StringStateValue.java | 23 +++++++--- .../tmf/core/statevalue/TmfStateValue.java | 50 +--------------------- 6 files changed, 74 insertions(+), 77 deletions(-) diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/DoubleStateValue.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/DoubleStateValue.java index 13578b8245..dd3f98d821 100644 --- a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/DoubleStateValue.java +++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/DoubleStateValue.java @@ -21,10 +21,10 @@ import org.eclipse.jdt.annotation.Nullable; */ final class DoubleStateValue extends TmfStateValue { - private final Double valueDouble; + private final double value; public DoubleStateValue(double value) { - valueDouble = new Double(value); + this.value = value; } @Override @@ -38,13 +38,23 @@ final class DoubleStateValue extends TmfStateValue { } @Override - public Double getValue() { - return valueDouble; + public boolean equals(@Nullable Object object) { + if (!(object instanceof DoubleStateValue)) { + return false; + } + DoubleStateValue other = (DoubleStateValue) object; + return (Double.compare(this.value, other.value) == 0); + } + + @Override + public int hashCode() { + long bits = Double.doubleToLongBits(value); + return ((int) bits) ^ ((int) (bits >>> 32)); } @Override public @Nullable String toString() { - return String.format("%3f", valueDouble); //$NON-NLS-1$ + return String.format("%3f", value); //$NON-NLS-1$ } // ------------------------------------------------------------------------ @@ -53,6 +63,6 @@ final class DoubleStateValue extends TmfStateValue { @Override public double unboxDouble() { - return valueDouble; + return value; } } diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/IntegerStateValue.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/IntegerStateValue.java index 9a43a63b2c..9ac7d5ec8f 100644 --- a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/IntegerStateValue.java +++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/IntegerStateValue.java @@ -22,10 +22,10 @@ import org.eclipse.jdt.annotation.Nullable; */ final class IntegerStateValue extends TmfStateValue { - private final Integer valueInt; + private final int value; public IntegerStateValue(int valueAsInt) { - this.valueInt = new Integer(valueAsInt); + this.value = valueAsInt; } @Override @@ -39,13 +39,22 @@ final class IntegerStateValue extends TmfStateValue { } @Override - public Integer getValue() { - return valueInt; + public boolean equals(@Nullable Object object) { + if (!(object instanceof IntegerStateValue)) { + return false; + } + IntegerStateValue other = (IntegerStateValue) object; + return (this.value == other.value); + } + + @Override + public int hashCode() { + return value; } @Override public @Nullable String toString() { - return String.format("%3d", valueInt); //$NON-NLS-1$ + return String.format("%3d", value); //$NON-NLS-1$ } // ------------------------------------------------------------------------ @@ -54,12 +63,12 @@ final class IntegerStateValue extends TmfStateValue { @Override public int unboxInt() { - return valueInt; + return value; } @Override public long unboxLong() { /* It's always safe to up-cast a int into a long */ - return valueInt; + return value; } } diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/LongStateValue.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/LongStateValue.java index 1e314952d8..6afe4ae519 100644 --- a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/LongStateValue.java +++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/LongStateValue.java @@ -22,10 +22,10 @@ import org.eclipse.jdt.annotation.Nullable; */ final class LongStateValue extends TmfStateValue { - private final Long valueLong; + private final long value; public LongStateValue(long valueAsLong) { - this.valueLong = new Long(valueAsLong); + this.value = valueAsLong; } @Override @@ -37,15 +37,23 @@ final class LongStateValue extends TmfStateValue { public boolean isNull() { return false; } + @Override + public boolean equals(@Nullable Object object) { + if (!(object instanceof LongStateValue)) { + return false; + } + LongStateValue other = (LongStateValue) object; + return (this.value == other.value); + } @Override - public Long getValue() { - return valueLong; + public int hashCode() { + return ((int) value) ^ ((int) (value >>> 32)); } @Override public @Nullable String toString() { - return String.format("%3d", valueLong); //$NON-NLS-1$ + return String.format("%3d", value); //$NON-NLS-1$ } // ------------------------------------------------------------------------ @@ -54,6 +62,6 @@ final class LongStateValue extends TmfStateValue { @Override public long unboxLong() { - return valueLong; + return value; } } diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/NullStateValue.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/NullStateValue.java index 747eb55943..6460fb423c 100644 --- a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/NullStateValue.java +++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/NullStateValue.java @@ -12,6 +12,8 @@ package org.eclipse.linuxtools.tmf.core.statevalue; +import org.eclipse.jdt.annotation.Nullable; + /** * A state value that contains no particular value. It is sometimes needed over * a "null" reference, since we avoid NPE's this way. @@ -36,8 +38,13 @@ final class NullStateValue extends TmfStateValue { } @Override - public Object getValue() { - return value; + public boolean equals(@Nullable Object object) { + return (object instanceof NullStateValue); + } + + @Override + public int hashCode() { + return 0; } @Override @@ -66,6 +73,6 @@ final class NullStateValue extends TmfStateValue { @Override public String unboxStr() { - return "nullValue"; //$NON-NLS-1$ + return value; } } diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/StringStateValue.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/StringStateValue.java index 674abc176a..0543e9b5d5 100644 --- a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/StringStateValue.java +++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/StringStateValue.java @@ -12,6 +12,8 @@ package org.eclipse.linuxtools.tmf.core.statevalue; +import org.eclipse.jdt.annotation.Nullable; + /** * A state value containing a variable-sized string * @@ -20,10 +22,10 @@ package org.eclipse.linuxtools.tmf.core.statevalue; */ final class StringStateValue extends TmfStateValue { - private final String valueStr; + private final String value; public StringStateValue(String valueAsString) { - this.valueStr = valueAsString; + this.value = valueAsString; } @Override @@ -37,13 +39,22 @@ final class StringStateValue extends TmfStateValue { } @Override - public String getValue() { - return valueStr; + public boolean equals(@Nullable Object object) { + if (!(object instanceof StringStateValue)) { + return false; + } + StringStateValue other = (StringStateValue) object; + return value.equals(other.value); + } + + @Override + public int hashCode() { + return value.hashCode(); } @Override public String toString() { - return valueStr; + return value; } // ------------------------------------------------------------------------ @@ -52,6 +63,6 @@ final class StringStateValue extends TmfStateValue { @Override public String unboxStr() { - return valueStr; + return value; } } diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/TmfStateValue.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/TmfStateValue.java index 3ce0fd17c5..a2eaca6d62 100644 --- a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/TmfStateValue.java +++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/TmfStateValue.java @@ -29,54 +29,6 @@ import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException; * @author Alexandre Montplaisir */ public abstract class TmfStateValue implements ITmfStateValue { - - /** - * Retrieve directly the value object contained within. Implementing - * subclasses may limit the return type here. - * - * It's protected, since we do not want to expose this directly in the - * public API (and require all its users to manually cast to the right - * types). All accesses to the values should go through the "unbox-" - * methods. - * - * @return The underneath object assigned to this state value. - */ - protected abstract Object getValue(); - - @Override - public boolean equals(@Nullable Object other) { - if (this == other) { - return true; - } - if (!(other instanceof TmfStateValue)) { - return false; - } - - /* If both types are different they're necessarily not equal */ - if (this.getType() != ((TmfStateValue) other).getType()) { - return false; - } - - /* - * This checks for the case where we'd compare two null values (and so - * avoid a NPE below) - */ - if (this.isNull()) { - return true; - } - - /* The two are valid and comparable, let's compare them */ - return this.getValue().equals(((TmfStateValue) other).getValue()); - } - - @Override - public int hashCode() { - if (this.isNull()) { - return 0; - } - return this.getValue().hashCode(); - } - // ------------------------------------------------------------------------ // Factory methods to instantiate new state values // ------------------------------------------------------------------------ @@ -133,7 +85,7 @@ public abstract class TmfStateValue implements ITmfStateValue { * @return The newly-created TmfStateValue object */ public static TmfStateValue newValueDouble(double value) { - if (value == Double.NaN) { + if (Double.isNaN(value)) { return nullValue(); } return new DoubleStateValue(value); -- cgit v1.2.3