tmf: Cleanup TmfStateValue to remove 1-level of indirection.
authorEtienne Bergeron <etienne.bergeron@gmail.com>
Thu, 28 Nov 2013 00:29:23 +0000 (19:29 -0500)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Thu, 28 Nov 2013 19:41:54 +0000 (14:41 -0500)
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 <etienne.bergeron@gmail.com>
Reviewed-on: https://git.eclipse.org/r/19027
Reviewed-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
IP-Clean: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Tested-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
IP-Clean: Matthew Khouzam <matthew.khouzam@ericsson.com>
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/DoubleStateValue.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/IntegerStateValue.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/LongStateValue.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/NullStateValue.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/StringStateValue.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/TmfStateValue.java

index 13578b8245fcd87726f60ff1c54bce38c673d527..dd3f98d8212def3430fdcfd0f136a4c0369f098a 100644 (file)
@@ -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;
     }
 }
index 9a43a63b2cd41fc64871628e48ecab6f7f3ca309..9ac7d5ec8fc5a0b208510d1e6c5fd63055140ed7 100644 (file)
@@ -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;
     }
 }
index 1e314952d8bb8ba1ec339983341a013cab46b6e5..6afe4ae51909372b772626102cdaf96cf4e1f23c 100644 (file)
@@ -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;
     }
 }
index 747eb55943cb979d03aa7b13fe713da3785365a9..6460fb423c61076024bff3581e50ecd682660e8f 100644 (file)
@@ -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;
     }
 }
index 674abc176a19d592141dcfb24ceca96055225435..0543e9b5d5c99bb80ba118da5e0b308efd5c344a 100644 (file)
@@ -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;
     }
 }
index 3ce0fd17c5e19e721331334873004b01ff022249..a2eaca6d6248fb5015ca786df06b69b7be6250b2 100644 (file)
@@ -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);
This page took 0.032564 seconds and 5 git commands to generate.