From ed9669f57a77f787b2ed8d2cad2561e57f17dc5c Mon Sep 17 00:00:00 2001 From: Hubert Plociniczak Date: Tue, 17 Jul 2012 17:57:05 +0200 Subject: Fixes SI-5588. Correct compare for Enumeration. Slower than the original one but does comparison in the same spirit as IntOrdering. Review by @axel22. --- src/library/scala/Enumeration.scala | 7 +++++-- test/files/run/t5588.check | 2 ++ test/files/run/t5588.scala | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 test/files/run/t5588.check create mode 100644 test/files/run/t5588.scala diff --git a/src/library/scala/Enumeration.scala b/src/library/scala/Enumeration.scala index 2b658ee4f7..1151b04ca0 100644 --- a/src/library/scala/Enumeration.scala +++ b/src/library/scala/Enumeration.scala @@ -194,7 +194,10 @@ abstract class Enumeration (initial: Int) extends Serializable { /** a marker so we can tell whose values belong to whom come reflective-naming time */ private[Enumeration] val outerEnum = thisenum - override def compare(that: Value): Int = this.id - that.id + override def compare(that: Value): Int = + if (this.id < that.id) -1 + else if (this.id == that.id) 0 + else 1 override def equals(other: Any) = other match { case that: Enumeration#Value => (outerEnum eq that.outerEnum) && (id == that.id) case _ => false @@ -236,7 +239,7 @@ abstract class Enumeration (initial: Int) extends Serializable { /** An ordering by id for values of this set */ object ValueOrdering extends Ordering[Value] { - def compare(x: Value, y: Value): Int = x.id - y.id + def compare(x: Value, y: Value): Int = x compare y } /** A class for sets of values. diff --git a/test/files/run/t5588.check b/test/files/run/t5588.check new file mode 100644 index 0000000000..bb101b641b --- /dev/null +++ b/test/files/run/t5588.check @@ -0,0 +1,2 @@ +true +true diff --git a/test/files/run/t5588.scala b/test/files/run/t5588.scala new file mode 100644 index 0000000000..f214d16684 --- /dev/null +++ b/test/files/run/t5588.scala @@ -0,0 +1,14 @@ +object Test { + object MyEnum extends Enumeration { + val Foo = Value(2000000000) + val Bar = Value(-2000000000) + val X = Value(Integer.MAX_VALUE) + val Y = Value(Integer.MIN_VALUE) + } + + import MyEnum._ + def main(args: Array[String]) { + println(Foo > Bar) + println(X > Y) + } +} -- cgit v1.2.3