*/
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
}
@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$
}
// ------------------------------------------------------------------------
@Override
public double unboxDouble() {
- return valueDouble;
+ return value;
}
}
*/
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
}
@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$
}
// ------------------------------------------------------------------------
@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;
}
}
*/
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
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$
}
// ------------------------------------------------------------------------
@Override
public long unboxLong() {
- return valueLong;
+ return value;
}
}
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.
}
@Override
- public Object getValue() {
- return value;
+ public boolean equals(@Nullable Object object) {
+ return (object instanceof NullStateValue);
+ }
+
+ @Override
+ public int hashCode() {
+ return 0;
}
@Override
@Override
public String unboxStr() {
- return "nullValue"; //$NON-NLS-1$
+ return value;
}
}
package org.eclipse.linuxtools.tmf.core.statevalue;
+import org.eclipse.jdt.annotation.Nullable;
+
/**
* A state value containing a variable-sized string
*
*/
final class StringStateValue extends TmfStateValue {
- private final String valueStr;
+ private final String value;
public StringStateValue(String valueAsString) {
- this.valueStr = valueAsString;
+ this.value = valueAsString;
}
@Override
}
@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;
}
// ------------------------------------------------------------------------
@Override
public String unboxStr() {
- return valueStr;
+ return value;
}
}
* @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
// ------------------------------------------------------------------------
* @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);