summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormichelou <michelou@epfl.ch>2008-08-26 13:48:19 +0000
committermichelou <michelou@epfl.ch>2008-08-26 13:48:19 +0000
commita3e8c0637f63aceb43506aefac8c14b8068d80b9 (patch)
treec60065a6b56521fd5f3e96cfd36e2d73c897fd0b
parent6a7a1eeff9a3975d1eb76f3ecd9000bfc97be824 (diff)
downloadscala-a3e8c0637f63aceb43506aefac8c14b8068d80b9.tar.gz
scala-a3e8c0637f63aceb43506aefac8c14b8068d80b9.tar.bz2
scala-a3e8c0637f63aceb43506aefac8c14b8068d80b9.zip
Fixed serialization of enums
-rw-r--r--src/library/scala/BigDecimal.scala1
-rw-r--r--src/library/scala/Enumeration.scala73
-rw-r--r--src/library/scala/runtime/BoxedArray.scala3
-rwxr-xr-xtest/files/jvm/serialization.check28
-rwxr-xr-xtest/files/jvm/serialization.scala55
5 files changed, 123 insertions, 37 deletions
diff --git a/src/library/scala/BigDecimal.scala b/src/library/scala/BigDecimal.scala
index 66162b2e3c..263a830481 100644
--- a/src/library/scala/BigDecimal.scala
+++ b/src/library/scala/BigDecimal.scala
@@ -18,6 +18,7 @@ import java.math.{BigDecimal => BigDec}
*/
object BigDecimal {
+ @serializable
object RoundingMode extends Enumeration {
type RoundingMode = Value
val ROUND_UP, ROUND_DOWN, ROUND_CEILING, ROUND_FLOOR, ROUND_HALF_UP,
diff --git a/src/library/scala/Enumeration.scala b/src/library/scala/Enumeration.scala
index 93ed320f64..6d6203e415 100644
--- a/src/library/scala/Enumeration.scala
+++ b/src/library/scala/Enumeration.scala
@@ -1,6 +1,6 @@
/* __ *\
** ________ ___ / / ___ Scala API **
-** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL **
+** / __/ __// _ | / / / _ | (c) 2002-2008, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
@@ -34,14 +34,15 @@ import scala.collection.mutable.{Map, HashMap}
* </p><pre>
* <b>object</b> Main <b>extends</b> Application {
*
- * <b>object</b> WeekDays <b>extends</b> Enumeration {
+ * <b>object</b> WeekDay <b>extends</b> Enumeration {
+ * <b>type</b> WeekDay</b> = Value
* <b>val</b> Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
* }
+ * <b>import</b> WeekDay._
*
- * <b>def</b> isWorkingDay(d: WeekDays.Value) =
- * ! (d == WeekDays.Sat || d == WeekDays.Sun)
+ * <b>def</b> isWorkingDay(d: WeekDay) = ! (d == Sat || d == Sun)
*
- * WeekDays filter (isWorkingDay) foreach { d =&gt; Console.println(d) }
+ * WeekDay filter isWorkingDay foreach println
* }</pre>
*
* @param initial The initial value from which to count the integers that
@@ -49,8 +50,10 @@ import scala.collection.mutable.{Map, HashMap}
* @param names The sequence of names to give to this enumeration's values.
*
* @author Matthias Zenger
- * @version 1.0, 10/02/04
+ * @version 1.0, 10/02/2004
*/
+@serializable
+@SerialVersionUID(8476000850333817230L)
abstract class Enumeration(initial: Int, names: String*) {
def this() = this(0, null)
@@ -60,9 +63,9 @@ abstract class Enumeration(initial: Int, names: String*) {
/** The name of this enumeration. */
def name = {
val cname = this.getClass().getName()
- if (cname.endsWith("$"))
+ if (cname endsWith "$")
cname.substring(0, cname.length() - 1)
- else if (cname.endsWith("$class"))
+ else if (cname endsWith "$class")
cname.substring(0, cname.length() - 6)
else
cname
@@ -73,11 +76,11 @@ abstract class Enumeration(initial: Int, names: String*) {
private val values: Map[Int, Value] = new HashMap
/** The cache listing all values of this enumeration. */
- private var vcache: List[Value] = null
+ @transient private var vcache: List[Value] = null
private def updateCache: List[Value] =
if (vcache eq null) {
- vcache = values.values.toList.sort((p1, p2) => p1.id < p2.id);
+ vcache = values.values.toList.sort(_.id < _.id);
vcache
} else
vcache;
@@ -115,11 +118,11 @@ abstract class Enumeration(initial: Int, names: String*) {
/** Returns an iterator resulting from applying the given function f to each
* value of this enumeration. */
- def map[b](f: Value => b): Iterator[b] = elements map f
+ def map[B](f: Value => B): Iterator[B] = elements map f
/** Applies the given function f to each value of this enumeration, then
* concatenates the results. */
- def flatMap[b](f: Value => Iterator[b]): Iterator[b] = elements flatMap f
+ def flatMap[B](f: Value => Iterator[B]): Iterator[B] = elements flatMap f
/** Returns all values of this enumeration that satisfy the predicate p.
* The order of values is preserved. */
@@ -158,14 +161,18 @@ abstract class Enumeration(initial: Int, names: String*) {
protected final def Value(i: Int, name: String): Value = new Val(i, name)
/** The type of the enumerated values. */
+ @serializable
+ @SerialVersionUID(7091335633555234129L)
abstract class Value extends Ordered[Value] {
/** the id and bit location of this enumeration value */
def id: Int
override def compare(that: Value): Int = this.id - that.id
- override def equals(other : Any) : Boolean =
- other match { case that : Value => compare(that) == 0
- case _ => false }
- override def hashCode : Int = id.hashCode
+ override def equals(other: Any): Boolean =
+ other match {
+ case that: Value => compare(that) == 0
+ case _ => false
+ }
+ override def hashCode: Int = id.hashCode
/** this enumeration value as an <code>Int</code> bit mask.
* @throws IllegalArgumentException if <code>id</code> is greater than 31
*/
@@ -187,6 +194,8 @@ abstract class Enumeration(initial: Int, names: String*) {
* overriden to change the enumeration's naming and integer identification
* behaviour.
*/
+ @serializable
+ @SerialVersionUID(0 - 3501153230598116017L)
protected class Val(i: Int, name: String) extends Value {
def this(i: Int) =
this(i, if (nextName.hasNext) nextName.next else i.toString())
@@ -196,12 +205,14 @@ abstract class Enumeration(initial: Int, names: String*) {
assert(!values.isDefinedAt(i))
values(i) = this
nextId = i + 1
- if (nextId > topId)
- topId = nextId
+ if (nextId > topId) topId = nextId
def id = i
override def toString() =
if (name eq null) Enumeration.this.name + "(" + i + ")"
else name
+ private def readResolve(): AnyRef =
+ if (values ne null) values(i)
+ else this
}
/** A set that efficiently stores enumeration values as bits.
@@ -297,7 +308,7 @@ abstract class Enumeration(initial: Int, names: String*) {
/** An enumeration bit set that can handle enumeration values with ids up
* to 31 in an <code>Int</code>.
*/
- class Set32(val underlying : Int) extends SetXX {
+ class Set32(val underlying: Int) extends SetXX {
def this() = this(0)
type Underlying = Int
type TSet = Set32
@@ -309,32 +320,32 @@ abstract class Enumeration(initial: Int, names: String*) {
underlying0.toLong | (1L << 31)
}
}
- def contains(value : Value) = (underlying & value.mask32) != 0
- def |(set : Set32) = new Set32(underlying | set.underlying)
- def |(value : Value) = new Set32(underlying | value.mask32)
- def &~(value : Value) = new Set32(underlying & (~value.mask32))
- def &(set : Set32) = new Set32(underlying & set.underlying)
+ def contains(value: Value) = (underlying & value.mask32) != 0
+ def |(set: Set32) = new Set32(underlying | set.underlying)
+ def |(value: Value) = new Set32(underlying | value.mask32)
+ def &~(value: Value) = new Set32(underlying & (~value.mask32))
+ def &(set: Set32) = new Set32(underlying & set.underlying)
}
/** create an empty 32 bit enumeration set */
def Set32 = new Set32
/** create a bit enumeration set according ot underlying */
- def Set32(underlying : Int) = new Set32(underlying)
+ def Set32(underlying: Int) = new Set32(underlying)
/** An enumeration bit set that can handle enumeration values with ids up
* to 63 in a <code>Long</code>.
*/
- class Set64(val underlying : Long) extends SetXX {
+ class Set64(val underlying: Long) extends SetXX {
def this() = this(0)
type Underlying = Long
type TSet = Set64
def underlyingAsLong = underlying
- def contains(value : Value) = (underlying & value.mask64) != 0
- def |( set : Set64) = new Set64(underlying | set.underlying)
- def |(value : Value) = new Set64(underlying | value.mask64)
- def &~(value : Value) = new Set64(underlying & (~value.mask64))
- def &(set : Set64) = new Set64(underlying & set.underlying)
+ def contains(value: Value) = (underlying & value.mask64) != 0
+ def |(set: Set64) = new Set64(underlying | set.underlying)
+ def |(value: Value) = new Set64(underlying | value.mask64)
+ def &~(value: Value) = new Set64(underlying & (~value.mask64))
+ def &(set: Set64) = new Set64(underlying & set.underlying)
}
/** create an empty 64 bit enumeration set */
diff --git a/src/library/scala/runtime/BoxedArray.scala b/src/library/scala/runtime/BoxedArray.scala
index ea0d7c654d..4d1ae68d65 100644
--- a/src/library/scala/runtime/BoxedArray.scala
+++ b/src/library/scala/runtime/BoxedArray.scala
@@ -42,11 +42,12 @@ abstract class BoxedArray extends Array.Array0[Any] {
override def isDefinedAt(x: Int): Boolean = 0 <= x && x < length
- override def elements = new Iterator[Any] {
+ @serializable protected class AnyIterator extends Iterator[Any] {
var index = 0
def hasNext: Boolean = index < length
def next(): Any = { val i = index; index = i + 1; apply(i) }
}
+ override def elements = new AnyIterator
/** The underlying array value
*/
diff --git a/test/files/jvm/serialization.check b/test/files/jvm/serialization.check
index d7ec08e016..863857d4cc 100755
--- a/test/files/jvm/serialization.check
+++ b/test/files/jvm/serialization.check
@@ -1,3 +1,8 @@
+x0 = List(1, 2, 3)
+y0 = List(1, 2, 3)
+x0 eq y0: false - y0 eq x0: false
+x0 equals y0: true - y0 equals x0: true
+
x1 = List()
y1 = List()
x1 eq y1: true - y1 eq x1: true
@@ -24,6 +29,29 @@ y6 = (BannerLimit,12345)
x6 eq y6: false - y6 eq x6: false
x6 equals y6: true - y6 equals x6: true
+x7 = {scala.BigDecimal$RoundingMode(0), scala.BigDecimal$RoundingMode(1), scala.BigDecimal$RoundingMode(2), scala.BigDecimal$RoundingMode(3), scala.BigDecimal$RoundingMode(4), scala.BigDecimal$RoundingMode(5), scala.BigDecimal$RoundingMode(6), scala.BigDecimal$RoundingMode(7)}
+y7 = {scala.BigDecimal$RoundingMode(0), scala.BigDecimal$RoundingMode(1), scala.BigDecimal$RoundingMode(2), scala.BigDecimal$RoundingMode(3), scala.BigDecimal$RoundingMode(4), scala.BigDecimal$RoundingMode(5), scala.BigDecimal$RoundingMode(6), scala.BigDecimal$RoundingMode(7)}
+x7 eq y7: true - y7 eq x7: true
+x7 equals y7: true - y7 equals x7: true
+
+x8 = {Test1_scala$WeekDay(0), Test1_scala$WeekDay(1), Test1_scala$WeekDay(2), Test1_scala$WeekDay(3), Test1_scala$WeekDay(4), Test1_scala$WeekDay(5), Test1_scala$WeekDay(6)}
+y8 = {Test1_scala$WeekDay(0), Test1_scala$WeekDay(1), Test1_scala$WeekDay(2), Test1_scala$WeekDay(3), Test1_scala$WeekDay(4), Test1_scala$WeekDay(5), Test1_scala$WeekDay(6)}
+x8 eq y8: true - y8 eq x8: true
+x8 equals y8: true - y8 equals x8: true
+
+x9 = scala.BigDecimal$RoundingMode(0)
+y9 = scala.BigDecimal$RoundingMode(0)
+x9 eq y9: true - y9 eq x9: true
+x9 equals y9: true - y9 equals x9: true
+
+x10 = Test1_scala$WeekDay(0)
+y10 = Test1_scala$WeekDay(0)
+x10 eq y10: true - y10 eq x10: true
+x10 equals y10: true - y10 equals x10: true
+
+x9 eq x10: false - x10 eq x9: false
+x9 equals x10: false - x10 equals x9: false
+
x = List((buffers,20), (layers,2), (title,3))
y = List((buffers,20), (layers,2), (title,3))
x equals y: true - y equals x: true
diff --git a/test/files/jvm/serialization.scala b/test/files/jvm/serialization.scala
index 93f3841b6e..d95d2e809f 100755
--- a/test/files/jvm/serialization.scala
+++ b/test/files/jvm/serialization.scala
@@ -1,7 +1,7 @@
//############################################################################
// Serialization
//############################################################################
-// $Id: serialization.scala 12990 2007-10-03 16:52:05Z michelou $
+// $Id: serialization.scala 15934 2008-08-26 12:10:05Z michelou $
import java.lang.System
@@ -41,25 +41,47 @@ object Test1_scala {
private def arrayToString[A](arr: Array[A]): String =
List.fromArray(arr).mkString("Array[",",","]")
- private def arrayEquals[A, B](a1: Array[A], a2: Array[B]) =
- (a1.length == a2.length) &&
- (Iterator.range(0, a1.length) forall { i => a1(i) == a2(i) })
+ private def arrayEquals[A, B](a1: Array[A], a2: Array[B]): Boolean =
+ (a1.length == a2.length) &&
+ (Iterator.range(0, a1.length) forall { i => a1(i) == a2(i) })
+ @serializable
+ object WeekDay extends Enumeration {
+ type WeekDay = Value
+ val Monday, Tuesday, Wednesday, Thusday, Friday, Saturday, Sunday = Value
+ }
+ import WeekDay._, BigDecimal._, RoundingMode._
+
+ val x0 = List(1, 2, 3)
val x1 = Nil
val x2 = None
val x3 = Array(1, 2, 3)
val x4 = { x: Int => 2 * x }
val x5 = 'hello
val x6 = ("BannerLimit", 12345)
+ val x7 = BigDecimal.RoundingMode
+ val x8 = WeekDay
+ val x9 = ROUND_UP
+ val x10 = Monday
try {
+ val y0: List[Int] = Serialize.read(Serialize.write(x0))
val y1: List[Nothing] = Serialize.read(Serialize.write(x1))
val y2: Option[Nothing] = Serialize.read(Serialize.write(x2))
val y3: Array[Int] = Serialize.read(Serialize.write(x3))
val y4: Function[Int, Int] = Serialize.read(Serialize.write(x4))
val y5: Symbol = Serialize.read(Serialize.write(x5))
val y6: (String, Int) = Serialize.read(Serialize.write(x6))
-
+ val y7: RoundingMode.type = Serialize.read(Serialize.write(x7))
+ val y8: WeekDay.type = Serialize.read(Serialize.write(x8))
+ val y9: RoundingMode = Serialize.read(Serialize.write(x9))
+ val y10: WeekDay = Serialize.read(Serialize.write(x10))
+
+ println("x0 = " + x0)
+ println("y0 = " + y0)
+ println("x0 eq y0: " + (x0 eq y0) + " - y0 eq x0: " + (y0 eq x0))
+ println("x0 equals y0: " + (x0 equals y0) + " - y0 equals x0: " + (y0 equals x0))
+ println()
println("x1 = " + x1)
println("y1 = " + y1)
println("x1 eq y1: " + (x1 eq y1) + " - y1 eq x1: " + (y1 eq x1))
@@ -86,6 +108,29 @@ object Test1_scala {
println("x6 eq y6: " + (x6 eq y6) + " - y6 eq x6: " + (y6 eq x6))
println("x6 equals y6: " + (x6 equals y6) + " - y6 equals x6: " + (y6 equals x6))
println()
+ println("x7 = " + x7)
+ println("y7 = " + y7)
+ println("x7 eq y7: " + (x7 eq y7) + " - y7 eq x7: " + (y7 eq x7))
+ println("x7 equals y7: " + (x7 equals y7) + " - y7 equals x7: " + (y7 equals x7))
+ println()
+ println("x8 = " + x8)
+ println("y8 = " + y8)
+ println("x8 eq y8: " + (x8 eq y8) + " - y8 eq x8: " + (y8 eq x8))
+ println("x8 equals y8: " + (x8 equals y8) + " - y8 equals x8: " + (y8 equals x8))
+ println()
+ println("x9 = " + x9)
+ println("y9 = " + y9)
+ println("x9 eq y9: " + (x9 eq y9) + " - y9 eq x9: " + (y9 eq x9))
+ println("x9 equals y9: " + (x9 equals y9) + " - y9 equals x9: " + (y9 equals x9))
+ println()
+ println("x10 = " + x10)
+ println("y10 = " + y10)
+ println("x10 eq y10: " + (x10 eq y10) + " - y10 eq x10: " + (y10 eq x10))
+ println("x10 equals y10: " + (x10 equals y10) + " - y10 equals x10: " + (y10 equals x10))
+ println()
+ println("x9 eq x10: " + (x9 eq x10) + " - x10 eq x9: " + (x10 eq x9))
+ println("x9 equals x10: " + (x9 equals x10) + " - x10 equals x9: " + (x10 equals x9))
+ println()
}
catch {
case e: Exception =>