diff options
Diffstat (limited to 'src/de/lmu/ifi/dbs/elki/distance/distancevalue')
10 files changed, 241 insertions, 204 deletions
diff --git a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/BitDistance.java b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/BitDistance.java index 24e33d35..9c81188f 100644 --- a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/BitDistance.java +++ b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/BitDistance.java @@ -40,7 +40,7 @@ public class BitDistance extends NumberDistance<BitDistance, Bit> { /** * The static factory instance */ - public final static BitDistance FACTORY = new BitDistance(); + public static final BitDistance FACTORY = new BitDistance(); /** * The distance value @@ -108,7 +108,7 @@ public class BitDistance extends NumberDistance<BitDistance, Bit> { */ @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeBoolean(this.bitValue()); + out.writeBoolean(value); } /** @@ -116,7 +116,7 @@ public class BitDistance extends NumberDistance<BitDistance, Bit> { */ @Override public void readExternal(ObjectInput in) throws IOException { - setValue(new Bit(in.readBoolean())); + value = in.readBoolean(); } /** @@ -131,16 +131,6 @@ public class BitDistance extends NumberDistance<BitDistance, Bit> { } @Override - public Bit getValue() { - return new Bit(this.value); - } - - @Override - void setValue(Bit value) { - this.value = value.bitValue(); - } - - @Override public double doubleValue() { return value ? 1.0 : 0.0; } @@ -162,10 +152,9 @@ public class BitDistance extends NumberDistance<BitDistance, Bit> { @Override public BitDistance parseString(String val) throws IllegalArgumentException { - if(testInputPattern(val)) { + if (testInputPattern(val)) { return new BitDistance(Bit.valueOf(val).bitValue()); - } - else { + } else { throw new IllegalArgumentException("Given pattern \"" + val + "\" does not match required pattern \"" + requiredInputPattern() + "\""); } } @@ -197,11 +186,36 @@ public class BitDistance extends NumberDistance<BitDistance, Bit> { @Override public boolean isNullDistance() { - return (value == false); + return !value; } @Override public boolean isUndefinedDistance() { return false; } -}
\ No newline at end of file + + @Override + public String toString() { + return Boolean.toString(value); + } + + @Override + public int hashCode() { + return (value ? 1231 : 1237); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + BitDistance other = (BitDistance) obj; + return (value == other.value); + } +} diff --git a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/CorrelationDistance.java b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/CorrelationDistance.java index f1203887..080f13c9 100644 --- a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/CorrelationDistance.java +++ b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/CorrelationDistance.java @@ -43,13 +43,13 @@ public abstract class CorrelationDistance<D extends CorrelationDistance<D>> exte * * Note: Do NOT use regular expression syntax characters! */ - public final static String SEPARATOR = "x"; + public static final String SEPARATOR = "x"; /** * The pattern used for correlation distances */ - public final static Pattern CORRELATION_DISTANCE_PATTERN = Pattern.compile("\\d+" + Pattern.quote(SEPARATOR) + "\\d+(\\.\\d+)?([eE][-]?\\d+)?"); - + public static final Pattern CORRELATION_DISTANCE_PATTERN = Pattern.compile("\\d+" + Pattern.quote(SEPARATOR) + "\\d+(\\.\\d+)?([eE][-]?\\d+)?"); + /** * Generated SerialVersionUID. */ @@ -110,11 +110,10 @@ public abstract class CorrelationDistance<D extends CorrelationDistance<D>> exte */ @Override public int compareTo(D other) { - int compare = new Integer(this.correlationValue).compareTo(other.getCorrelationValue()); - if(compare != 0) { + int compare = (this.correlationValue < other.getCorrelationValue()) ? -1 : (this.correlationValue > other.getCorrelationValue()) ? +1 : 0; + if (compare != 0) { return compare; - } - else { + } else { return Double.compare(this.euclideanValue, other.getEuclideanValue()); } } @@ -124,7 +123,7 @@ public abstract class CorrelationDistance<D extends CorrelationDistance<D>> exte int result; long temp; result = correlationValue; - temp = euclideanValue != +0.0d ? Double.doubleToLongBits(euclideanValue) : 0l; + temp = euclideanValue >= Double.MIN_NORMAL ? Double.doubleToLongBits(euclideanValue) : 0L; result = 29 * result + (int) (temp ^ (temp >>> 32)); return result; } @@ -132,17 +131,17 @@ public abstract class CorrelationDistance<D extends CorrelationDistance<D>> exte @SuppressWarnings("unchecked") @Override public boolean equals(Object obj) { - if(obj == null) { + if (obj == null) { return false; } - if(getClass() != obj.getClass()) { + if (getClass() != obj.getClass()) { return false; } final CorrelationDistance<D> other = (CorrelationDistance<D>) obj; - if(this.correlationValue != other.correlationValue) { + if (this.correlationValue != other.correlationValue) { return false; } - if(this.euclideanValue != other.euclideanValue) { + if (this.euclideanValue != other.euclideanValue) { return false; } return true; @@ -196,4 +195,4 @@ public abstract class CorrelationDistance<D extends CorrelationDistance<D>> exte public int externalizableSize() { return 12; } -}
\ No newline at end of file +} diff --git a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/DoubleDistance.java b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/DoubleDistance.java index ca84fbfd..acf72c8d 100644 --- a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/DoubleDistance.java +++ b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/DoubleDistance.java @@ -28,6 +28,8 @@ import java.io.ObjectInput; import java.io.ObjectOutput; import java.util.regex.Pattern; +import de.lmu.ifi.dbs.elki.utilities.FormatUtil; + /** * Provides a Distance for a double-valued distance. * @@ -35,9 +37,24 @@ import java.util.regex.Pattern; */ public class DoubleDistance extends NumberDistance<DoubleDistance, Double> { /** + * Zero distance constant + */ + public static final DoubleDistance ZERO_DISTANCE = new DoubleDistance(0.0); + + /** + * Infinite distance constant + */ + public static final DoubleDistance INFINITE_DISTANCE = new DoubleDistance(Double.POSITIVE_INFINITY); + + /** + * Undefined distance constant + */ + public static final DoubleDistance UNDEFINED_DISTANCE = new DoubleDistance(Double.NaN); + + /** * The static factory instance */ - public final static DoubleDistance FACTORY = new DoubleDistance(); + public static final DoubleDistance FACTORY = UNDEFINED_DISTANCE; /** * The actual value. @@ -118,7 +135,7 @@ public class DoubleDistance extends NumberDistance<DoubleDistance, Double> { */ @Override public void readExternal(ObjectInput in) throws IOException { - setValue(in.readDouble()); + this.value = in.readDouble(); } /** @@ -133,16 +150,6 @@ public class DoubleDistance extends NumberDistance<DoubleDistance, Double> { } @Override - public Double getValue() { - return this.value; - } - - @Override - void setValue(Double value) { - this.value = value; - } - - @Override public double doubleValue() { return value; } @@ -157,25 +164,13 @@ public class DoubleDistance extends NumberDistance<DoubleDistance, Double> { return Double.compare(this.value, other.value); } - @Override - public boolean equals(Object o) { - if(this == o) { - return true; - } - if(o == null || getClass() != o.getClass()) { - return false; - } - double delta = Math.abs(value - ((DoubleDistance) o).value); - return delta < Double.MIN_NORMAL; - } - /** * An infinite DoubleDistance is based on {@link Double#POSITIVE_INFINITY * Double.POSITIVE_INFINITY}. */ @Override public DoubleDistance infiniteDistance() { - return new DoubleDistance(Double.POSITIVE_INFINITY); + return INFINITE_DISTANCE; } /** @@ -183,7 +178,7 @@ public class DoubleDistance extends NumberDistance<DoubleDistance, Double> { */ @Override public DoubleDistance nullDistance() { - return new DoubleDistance(0.0); + return ZERO_DISTANCE; } /** @@ -191,7 +186,7 @@ public class DoubleDistance extends NumberDistance<DoubleDistance, Double> { */ @Override public DoubleDistance undefinedDistance() { - return new DoubleDistance(Double.NaN); + return UNDEFINED_DISTANCE; } /** @@ -199,13 +194,12 @@ public class DoubleDistance extends NumberDistance<DoubleDistance, Double> { */ @Override public DoubleDistance parseString(String val) throws IllegalArgumentException { - if(val.equals(INFINITY_PATTERN)) { + if (val.equals(INFINITY_PATTERN)) { return infiniteDistance(); } - if(testInputPattern(val)) { + if (testInputPattern(val)) { return new DoubleDistance(Double.parseDouble(val)); - } - else { + } else { throw new IllegalArgumentException("Given pattern \"" + val + "\" does not match required pattern \"" + requiredInputPattern() + "\""); } } @@ -217,7 +211,7 @@ public class DoubleDistance extends NumberDistance<DoubleDistance, Double> { @Override public boolean isNullDistance() { - return (value == 0.0); + return (value <= 0.0); } @Override @@ -231,8 +225,31 @@ public class DoubleDistance extends NumberDistance<DoubleDistance, Double> { } @Override + public String toString() { + return FormatUtil.NF8.format(value); + } + + @Override public int hashCode() { - long bits = Double.doubleToLongBits(value); + final long bits = Double.doubleToLongBits(value); return (int) (bits ^ (bits >>> 32)); } -}
\ No newline at end of file + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + DoubleDistance other = (DoubleDistance) obj; + if (Double.doubleToLongBits(value) != Double.doubleToLongBits(other.value)) { + return false; + } + return true; + } +} diff --git a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/FloatDistance.java b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/FloatDistance.java index adb951ae..2e88b147 100644 --- a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/FloatDistance.java +++ b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/FloatDistance.java @@ -28,6 +28,8 @@ import java.io.ObjectInput; import java.io.ObjectOutput; import java.util.regex.Pattern; +import de.lmu.ifi.dbs.elki.utilities.FormatUtil; + /** * Provides a Distance for a float-valued distance. * @@ -37,7 +39,7 @@ public class FloatDistance extends NumberDistance<FloatDistance, Float> { /** * The static factory instance */ - public final static FloatDistance FACTORY = new FloatDistance(); + public static final FloatDistance FACTORY = new FloatDistance(); /** * The distance value. @@ -50,6 +52,21 @@ public class FloatDistance extends NumberDistance<FloatDistance, Float> { private static final long serialVersionUID = -5702250266358369075L; /** + * Infinite distance. + */ + public static final FloatDistance INFINITE_DISTANCE = new FloatDistance(Float.POSITIVE_INFINITY); + + /** + * Zero distance. + */ + public static final FloatDistance ZERO_DISTANCE = new FloatDistance(0.0F); + + /** + * Undefined distance. + */ + public static final FloatDistance UNDEFINED_DISTANCE = new FloatDistance(Float.NaN); + + /** * Empty constructor for serialization purposes. */ public FloatDistance() { @@ -73,12 +90,12 @@ public class FloatDistance extends NumberDistance<FloatDistance, Float> { @Override public FloatDistance plus(FloatDistance distance) { - return new FloatDistance(this.getValue() + distance.getValue()); + return new FloatDistance(value + distance.value); } @Override public FloatDistance minus(FloatDistance distance) { - return new FloatDistance(this.getValue() - distance.getValue()); + return new FloatDistance(value - distance.value); } /** @@ -90,7 +107,7 @@ public class FloatDistance extends NumberDistance<FloatDistance, Float> { * distance */ public FloatDistance times(FloatDistance distance) { - return new FloatDistance(this.getValue() * distance.getValue()); + return new FloatDistance(value * distance.value); } /** @@ -102,7 +119,7 @@ public class FloatDistance extends NumberDistance<FloatDistance, Float> { * value */ public FloatDistance times(float lambda) { - return new FloatDistance(this.getValue() * lambda); + return new FloatDistance(value * lambda); } /** @@ -110,7 +127,7 @@ public class FloatDistance extends NumberDistance<FloatDistance, Float> { */ @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeFloat(getValue()); + out.writeFloat(value); } /** @@ -118,7 +135,7 @@ public class FloatDistance extends NumberDistance<FloatDistance, Float> { */ @Override public void readExternal(ObjectInput in) throws IOException { - setValue(in.readFloat()); + value = in.readFloat(); } /** @@ -133,16 +150,6 @@ public class FloatDistance extends NumberDistance<FloatDistance, Float> { } @Override - public Float getValue() { - return this.value; - } - - @Override - void setValue(Float value) { - this.value = value; - } - - @Override public double doubleValue() { return value; } @@ -162,25 +169,13 @@ public class FloatDistance extends NumberDistance<FloatDistance, Float> { return Float.compare(this.value, other.value); } - @Override - public boolean equals(Object o) { - if(this == o) { - return true; - } - if(o == null || getClass() != o.getClass()) { - return false; - } - float delta = Math.abs(value - ((FloatDistance) o).value); - return delta < Float.MIN_NORMAL; - } - /** * An infinite FloatDistance is based on {@link Float#POSITIVE_INFINITY * Float.POSITIVE_INFINITY}. */ @Override public FloatDistance infiniteDistance() { - return new FloatDistance(Float.POSITIVE_INFINITY); + return INFINITE_DISTANCE; } /** @@ -188,7 +183,7 @@ public class FloatDistance extends NumberDistance<FloatDistance, Float> { */ @Override public FloatDistance nullDistance() { - return new FloatDistance(0.0F); + return ZERO_DISTANCE; } /** @@ -196,7 +191,7 @@ public class FloatDistance extends NumberDistance<FloatDistance, Float> { */ @Override public FloatDistance undefinedDistance() { - return new FloatDistance(Float.NaN); + return UNDEFINED_DISTANCE; } /** @@ -204,35 +199,62 @@ public class FloatDistance extends NumberDistance<FloatDistance, Float> { */ @Override public FloatDistance parseString(String val) throws IllegalArgumentException { - if(val.equals(INFINITY_PATTERN)) { + if (val.equals(INFINITY_PATTERN)) { return infiniteDistance(); } - if(DoubleDistance.DOUBLE_PATTERN.matcher(val).matches()) { + if (DoubleDistance.DOUBLE_PATTERN.matcher(val).matches()) { return new FloatDistance(Float.parseFloat(val)); - } - else { + } else { throw new IllegalArgumentException("Given pattern \"" + val + "\" does not match required pattern \"" + requiredInputPattern() + "\""); } } @Override public boolean isInfiniteDistance() { - return Double.isInfinite(value); + return Float.isInfinite(value); } @Override public boolean isNullDistance() { - return (value == 0.0); + return (value <= 0.0); } @Override public boolean isUndefinedDistance() { - return Double.isNaN(value); + return Float.isNaN(value); } @Override public Pattern getPattern() { return DOUBLE_PATTERN; } -}
\ No newline at end of file + + @Override + public String toString() { + return FormatUtil.NF8.format(value); + } + + @Override + public int hashCode() { + return Float.floatToIntBits(value); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + FloatDistance other = (FloatDistance) obj; + if (Float.floatToIntBits(value) != Float.floatToIntBits(other.value)) { + return false; + } + return true; + } +} diff --git a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/IntegerDistance.java b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/IntegerDistance.java index b99b88c3..d776b3ae 100644 --- a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/IntegerDistance.java +++ b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/IntegerDistance.java @@ -39,7 +39,7 @@ public class IntegerDistance extends NumberDistance<IntegerDistance, Integer> { /** * The static factory instance */ - public final static IntegerDistance FACTORY = new IntegerDistance(); + public static final IntegerDistance FACTORY = new IntegerDistance(); /** * The distance value @@ -75,12 +75,12 @@ public class IntegerDistance extends NumberDistance<IntegerDistance, Integer> { @Override public IntegerDistance minus(IntegerDistance distance) { - return new IntegerDistance(this.getValue() - distance.getValue()); + return new IntegerDistance(this.value - distance.value); } @Override public IntegerDistance plus(IntegerDistance distance) { - return new IntegerDistance(this.getValue() + distance.getValue()); + return new IntegerDistance(this.value + distance.value); } /** @@ -88,7 +88,7 @@ public class IntegerDistance extends NumberDistance<IntegerDistance, Integer> { */ @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeInt(getValue()); + out.writeInt(value); } /** @@ -96,7 +96,7 @@ public class IntegerDistance extends NumberDistance<IntegerDistance, Integer> { */ @Override public void readExternal(ObjectInput in) throws IOException { - setValue(in.readInt()); + value = in.readInt(); } /** @@ -111,16 +111,6 @@ public class IntegerDistance extends NumberDistance<IntegerDistance, Integer> { } @Override - public Integer getValue() { - return this.value; - } - - @Override - void setValue(Integer value) { - this.value = value; - } - - @Override public double doubleValue() { return value; } @@ -172,16 +162,43 @@ public class IntegerDistance extends NumberDistance<IntegerDistance, Integer> { @Override public IntegerDistance parseString(String val) throws IllegalArgumentException { - if(testInputPattern(val)) { + if (testInputPattern(val)) { return new IntegerDistance(Integer.parseInt(val)); - } - else { + } else { throw new IllegalArgumentException("Given pattern \"" + val + "\" does not match required pattern \"" + requiredInputPattern() + "\""); } } @Override + public String toString() { + return Integer.toString(value); + } + + @Override + public int hashCode() { + return value; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + IntegerDistance other = (IntegerDistance) obj; + if (value != other.value) { + return false; + } + return true; + } + + @Override public Pattern getPattern() { return INTEGER_PATTERN; } -}
\ No newline at end of file +} diff --git a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/NumberDistance.java b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/NumberDistance.java index a3aff609..9a915c2b 100644 --- a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/NumberDistance.java +++ b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/NumberDistance.java @@ -23,7 +23,6 @@ package de.lmu.ifi.dbs.elki.distance.distancevalue; along with this program. If not, see <http://www.gnu.org/licenses/>. */ - /** * Provides a Distance for a number-valued distance. * @@ -42,7 +41,7 @@ public abstract class NumberDistance<D extends NumberDistance<D, N>, N extends N public NumberDistance() { super(); } - + /** * Build a new instance from a double value. * @@ -52,66 +51,12 @@ public abstract class NumberDistance<D extends NumberDistance<D, N>, N extends N public abstract D fromDouble(double val); /** - * Returns the hash code for this NumberDistance, which is the hash code of - * its value. - * - * @return the hash code of the value - */ - @Override - public int hashCode() { - return getValue().hashCode(); - } - - /** - * Compares this NumberDistance with the given NumberDistance wrt the - * represented value. - * <p/> - * <code>d1.compareTo(d2)</code> is the same as - * {@link Double#compare(double,double) Double.compare(d1.value.doubleValue(), - * d2.value.doubleValue())}. Subclasses may need to overwrite this method if - * necessary. - * - * @param other Other object - * @return a negative integer, zero, or a positive integer as the value of - * this NumberDistance is less than, equal to, or greater than the - * value of the specified NumberDistance. - */ - @Override - public int compareTo(D other) { - return Double.compare(this.doubleValue(), other.doubleValue()); - } - - /** - * Returns a string representation of this NumberDistance. - * - * @return the value of this NumberDistance. - */ - @Override - public final String toString() { - return getValue().toString(); - } - - /** - * Returns the value of this NumberDistance. - * - * @return the value of this NumberDistance - */ - public abstract N getValue(); - - /** - * Sets the value of this NumberDistance. - * - * @param value the value to be set - */ - abstract void setValue(N value); - - /** * Get the value as double. * * @return same result as getValue().doubleValue() but may be more efficient. */ public abstract double doubleValue(); - + /** * Get the value as float. * @@ -120,7 +65,7 @@ public abstract class NumberDistance<D extends NumberDistance<D, N>, N extends N public float floatValue() { return (float) doubleValue(); } - + /** * Get the value as int. * @@ -129,14 +74,14 @@ public abstract class NumberDistance<D extends NumberDistance<D, N>, N extends N public int intValue() { return (int) longValue(); } - + /** * Get the value as long. * * @return same result as getValue().longValue() but may be more efficient. */ public abstract long longValue(); - + /** * Get the value as short. * @@ -145,7 +90,7 @@ public abstract class NumberDistance<D extends NumberDistance<D, N>, N extends N public short shortValue() { return (short) longValue(); } - + /** * Get the value as byte. * diff --git a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/PCACorrelationDistance.java b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/PCACorrelationDistance.java index c2e2da6d..79f27055 100644 --- a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/PCACorrelationDistance.java +++ b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/PCACorrelationDistance.java @@ -38,7 +38,7 @@ public class PCACorrelationDistance extends CorrelationDistance<PCACorrelationDi /** * The static factory instance */ - public final static PCACorrelationDistance FACTORY = new PCACorrelationDistance(); + public static final PCACorrelationDistance FACTORY = new PCACorrelationDistance(); /** * Serial diff --git a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/PreferenceVectorBasedCorrelationDistance.java b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/PreferenceVectorBasedCorrelationDistance.java index c62cacc4..a5617372 100644 --- a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/PreferenceVectorBasedCorrelationDistance.java +++ b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/PreferenceVectorBasedCorrelationDistance.java @@ -40,7 +40,7 @@ public class PreferenceVectorBasedCorrelationDistance extends CorrelationDistanc /** * The static factory instance */ - public final static PreferenceVectorBasedCorrelationDistance FACTORY = new PreferenceVectorBasedCorrelationDistance(); + public static final PreferenceVectorBasedCorrelationDistance FACTORY = new PreferenceVectorBasedCorrelationDistance(); /** * Serial version @@ -202,7 +202,7 @@ public class PreferenceVectorBasedCorrelationDistance extends CorrelationDistanc */ @Override public int externalizableSize() { - return super.externalizableSize() + 4 + dimensionality * 4; + return super.externalizableSize() + 4 + dimensionality << 2; } @Override diff --git a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/SubspaceDistance.java b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/SubspaceDistance.java index 3d4b7f4c..094961b4 100644 --- a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/SubspaceDistance.java +++ b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/SubspaceDistance.java @@ -41,8 +41,8 @@ public class SubspaceDistance extends AbstractDistance<SubspaceDistance> { /** * The static factory instance */ - public final static SubspaceDistance FACTORY = new SubspaceDistance(); - + public static final SubspaceDistance FACTORY = new SubspaceDistance(); + /** * Serial version number. */ @@ -124,25 +124,46 @@ public class SubspaceDistance extends AbstractDistance<SubspaceDistance> { @Override public int compareTo(SubspaceDistance other) { int compare = Double.compare(this.subspaceDistance, other.subspaceDistance); - if(compare != 0) { + if (compare != 0) { return compare; - } - else { + } else { return Double.compare(this.affineDistance, other.affineDistance); } } @Override public int hashCode() { - int result; + final int prime = 31; + int result = 1; long temp; - temp = subspaceDistance != 0.0d ? Double.doubleToLongBits(subspaceDistance) : 0L; - result = (int) (temp ^ (temp >>> 32)); - temp = affineDistance != 0.0d ? Double.doubleToLongBits(affineDistance) : 0L; - result = 29 * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(subspaceDistance); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(affineDistance); + result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + SubspaceDistance other = (SubspaceDistance) obj; + if (Double.doubleToLongBits(affineDistance) != Double.doubleToLongBits(other.affineDistance)) { + return false; + } + if (Double.doubleToLongBits(subspaceDistance) != Double.doubleToLongBits(other.subspaceDistance)) { + return false; + } + return true; + } + /** * Returns the value of the subspace distance. * @@ -199,14 +220,13 @@ public class SubspaceDistance extends AbstractDistance<SubspaceDistance> { @Override public SubspaceDistance parseString(String val) throws IllegalArgumentException { - if(val.equals(INFINITY_PATTERN)) { + if (val.equals(INFINITY_PATTERN)) { return infiniteDistance(); } - if(testInputPattern(val)) { + if (testInputPattern(val)) { String[] values = SEPARATOR.split(val); return new SubspaceDistance(Double.parseDouble(values[0]), Double.parseDouble(values[1])); - } - else { + } else { throw new IllegalArgumentException("Given pattern \"" + val + "\" does not match required pattern \"" + requiredInputPattern() + "\""); } } @@ -225,4 +245,4 @@ public class SubspaceDistance extends AbstractDistance<SubspaceDistance> { public SubspaceDistance undefinedDistance() { return new SubspaceDistance(Double.NaN, Double.NaN); } -}
\ No newline at end of file +} diff --git a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/package-info.java b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/package-info.java index 30fcdf35..f302b854 100644 --- a/src/de/lmu/ifi/dbs/elki/distance/distancevalue/package-info.java +++ b/src/de/lmu/ifi/dbs/elki/distance/distancevalue/package-info.java @@ -5,6 +5,9 @@ * <p>Distances follow a factory pattern. Usually, a class will have a static instance * called <code>FACTORY</code> that can be used to obtain e.g. infinity or zero distances * as well as parse a string value into a new distance value.</p> + * + * @apiviz.exclude java.io.* + * @apiviz.exclude java.lang.* */ /* This file is part of ELKI: @@ -28,4 +31,4 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package de.lmu.ifi.dbs.elki.distance.distancevalue;
\ No newline at end of file +package de.lmu.ifi.dbs.elki.distance.distancevalue; |