diff options
author | Paul Phillips <paulp@improving.org> | 2009-11-12 22:13:04 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-11-12 22:13:04 +0000 |
commit | b88e47ced9595d4193723c41e491bac318a59b80 (patch) | |
tree | 3006e49f4949633f131f05dd415875301c621b77 /src/library/scala/runtime/BoxesRunTime.java | |
parent | 07c295560c191297b3e4dae591878913b6461f74 (diff) | |
download | scala-b88e47ced9595d4193723c41e491bac318a59b80.tar.gz scala-b88e47ced9595d4193723c41e491bac318a59b80.tar.bz2 scala-b88e47ced9595d4193723c41e491bac318a59b80.zip |
Bringing BigInt and BigDecimal into the club of...
Bringing BigInt and BigDecimal into the club of things which can be
equal to one another and which will have the same hashCode. Fixed
some old and some new bugs associated with equality. Note: not fully
optimized.
Diffstat (limited to 'src/library/scala/runtime/BoxesRunTime.java')
-rw-r--r-- | src/library/scala/runtime/BoxesRunTime.java | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/src/library/scala/runtime/BoxesRunTime.java b/src/library/scala/runtime/BoxesRunTime.java index 39ea9abcdd..869eb375ac 100644 --- a/src/library/scala/runtime/BoxesRunTime.java +++ b/src/library/scala/runtime/BoxesRunTime.java @@ -140,29 +140,37 @@ public class BoxesRunTime /* COMPARISON ... COMPARISON ... COMPARISON ... COMPARISON ... COMPARISON ... COMPARISON */ - // That's the method we should use from now on. + /** Since all applicable logic has to be present in the equals method of a ScalaNumber + * in any case, we dispatch to it as soon as we spot one on either side. + */ public static boolean equals(Object x, Object y) { if (x instanceof Number) { + if (x instanceof ScalaNumber) + return x.equals(y); + Number xn = (Number)x; if (y instanceof Number) { - Number yn = (Number)y; - if ((y instanceof ScalaNumber) && !(x instanceof ScalaNumber)) { + if (y instanceof ScalaNumber) return y.equals(x); - } + + Number yn = (Number)y; if ((xn instanceof Double) || (yn instanceof Double)) return xn.doubleValue() == yn.doubleValue(); if ((xn instanceof Float) || (yn instanceof Float)) return xn.floatValue() == yn.floatValue(); if ((xn instanceof Long) || (yn instanceof Long)) return xn.longValue() == yn.longValue(); - return xn.intValue() == yn.intValue(); + if (typeCode(x) <= INT && typeCode(y) <= INT) + return xn.intValue() == yn.intValue(); + + return x.equals(y); } if (y instanceof Character) return equalsNumChar(xn, (Character)y); } else if (x instanceof Character) { Character xc = (Character)x; if (y instanceof Character) - return (xc.charValue() == ((Character)y).charValue()); + return xc.equals(y); if (y instanceof Number) return equalsNumChar((Number)y, xc); } else if (x == null) { @@ -181,7 +189,10 @@ public class BoxesRunTime return x.longValue() == ch; if (x instanceof ScalaNumber) return x.equals(y); - return x.intValue() == ch; + if (typeCode(x) <= INT) + return x.intValue() == ch; + + return x.equals(y); } /** Hashcode algorithm is driven by the requirements imposed |