summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorGilles Dubochet <gilles.dubochet@epfl.ch>2007-11-06 17:08:42 +0000
committerGilles Dubochet <gilles.dubochet@epfl.ch>2007-11-06 17:08:42 +0000
commitdb0c13fdadf2d1b936f27822ef0bffb678bff345 (patch)
tree862fecb114e6dd46374cfcda63a6e8c2fa4c0eb6 /src/library
parentb2ed8091cfa6af5cd3e1e6fddb07c55aeadfc311 (diff)
downloadscala-db0c13fdadf2d1b936f27822ef0bffb678bff345.tar.gz
scala-db0c13fdadf2d1b936f27822ef0bffb678bff345.tar.bz2
scala-db0c13fdadf2d1b936f27822ef0bffb678bff345.zip
Faster rich comparator as sugested in ticket #38.
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/runtime/Comparator.java52
1 files changed, 27 insertions, 25 deletions
diff --git a/src/library/scala/runtime/Comparator.java b/src/library/scala/runtime/Comparator.java
index 705bf30d5c..9b25100e70 100644
--- a/src/library/scala/runtime/Comparator.java
+++ b/src/library/scala/runtime/Comparator.java
@@ -11,13 +11,15 @@
package scala.runtime;
-/**
- * @author Gilles Dubochet
- * @author Martin Odersky
- * @version 1.2 */
+/** An object (static class) providing a correct notion of equality in
+ * the general case, in particular with boxed values.
+ * @author Gilles Dubochet
+ * @author Martin Odersky
+ * @contributor Stepan Koltsov
+ * @version 1.2 */
public class Comparator {
- private static int CHAR = 0, BYTE = 1, SHORT = 2, INT = 3, LONG = 4, FLOAT = 5, DOUBLE = 6, OTHER = 7;
+ private static final int CHAR = 0, BYTE = 1, SHORT = 2, INT = 3, LONG = 4, FLOAT = 5, DOUBLE = 6, OTHER = 7;
private static int typeCode(Object a) {
if (a instanceof Integer) return INT;
@@ -31,21 +33,18 @@ public class Comparator {
}
/** A rich implementation of the equals method that overrides the default
- * equals because Java's boxed primitives are utterly broken. This equals
- * is inserted instead of a normal equals by the Scala compiler (in the
- * ICode phase, method <code>genEqEqPrimitive</code>) only when either
- * side of the comparison is a subclass of <code>AnyVal</code>, of
- * <code>java.lang.Number</code>, of <code>java.lang.Character</code> or
- * is exactly <code>Any</code> or <code>AnyRef</code>, but when both sides
- * have different types. */
+ * equals because Java's boxed primitives are utterly broken. This equals
+ * is inserted instead of a normal equals by the Scala compiler (in the
+ * ICode phase, method <code>genEqEqPrimitive</code>) only when either
+ * side of the comparison is a subclass of <code>AnyVal</code>, of
+ * <code>java.lang.Number</code>, of <code>java.lang.Character</code> or
+ * is exactly <code>Any</code> or <code>AnyRef</code>. */
public static boolean equals(Object a, Object b) {
- if (a == null)
- return b == null;
- else if (b == null)
- return false;
+ if (a == null || b == null)
+ return a == b;
else if (a.equals(b))
return true;
- else {
+ else if (a instanceof Number || a instanceof Character || b instanceof Number || b instanceof Character) {
int acode = typeCode(a);
int bcode = typeCode(b);
int maxcode = (acode < bcode) ? bcode : acode;
@@ -53,23 +52,26 @@ public class Comparator {
int aa = (acode == CHAR) ? ((Character) a).charValue() : ((Number) a).intValue();
int bb = (bcode == CHAR) ? ((Character) b).charValue() : ((Number) b).intValue();
return aa == bb;
- } else if (maxcode <= LONG) {
+ }
+ else if (maxcode <= LONG) {
long aa = (acode == CHAR) ? ((Character) a).charValue() : ((Number) a).longValue();
long bb = (bcode == CHAR) ? ((Character) b).charValue() : ((Number) b).longValue();
return aa == bb;
- } else if (maxcode <= FLOAT) {
+ }
+ else if (maxcode <= FLOAT) {
float aa = (acode == CHAR) ? ((Character) a).charValue() : ((Number) a).floatValue();
float bb = (bcode == CHAR) ? ((Character) b).charValue() : ((Number) b).floatValue();
return aa == bb;
- } else if (maxcode <= DOUBLE) {
+ }
+ else if (maxcode <= DOUBLE) {
double aa = (acode == CHAR) ? ((Character) a).charValue() : ((Number) a).doubleValue();
double bb = (bcode == CHAR) ? ((Character) b).charValue() : ((Number) b).doubleValue();
return aa == bb;
- } else if (acode != OTHER) {
- return b.equals(a);
- } else {
- return a == b;
}
- }
+ else
+ return b.equals(a);
+ }
+ else
+ return false;
}
}