Skip to content

Commit d7aacbb

Browse files
committed
Merge pull request scala#4416 from Ichoran/issue/9197
SI-9197 Duration.Inf not a singleton when deserialized
2 parents 8f99b58 + 71777cc commit d7aacbb

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/library/scala/concurrent/duration/Duration.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ object Duration {
182182
def compare(other: Duration) = if (other eq this) 0 else 1
183183
def unary_- : Duration = this
184184
def toUnit(unit: TimeUnit): Double = Double.NaN
185+
private def readResolve(): AnyRef = Undefined // Instructs deserialization to use this same instance
185186
}
186187

187188
sealed abstract class Infinite extends Duration {
@@ -230,7 +231,7 @@ object Duration {
230231
* but itself. This value closely corresponds to Double.PositiveInfinity,
231232
* matching its semantics in arithmetic operations.
232233
*/
233-
val Inf: Infinite = new Infinite {
234+
val Inf: Infinite = new Infinite {
234235
override def toString = "Duration.Inf"
235236
def compare(other: Duration) = other match {
236237
case x if x eq Undefined => -1 // Undefined != Undefined
@@ -239,6 +240,7 @@ object Duration {
239240
}
240241
def unary_- : Duration = MinusInf
241242
def toUnit(unit: TimeUnit): Double = Double.PositiveInfinity
243+
private def readResolve(): AnyRef = Inf // Instructs deserialization to use this same instance
242244
}
243245

244246
/**
@@ -251,6 +253,7 @@ object Duration {
251253
def compare(other: Duration) = if (other eq this) 0 else -1
252254
def unary_- : Duration = Inf
253255
def toUnit(unit: TimeUnit): Double = Double.NegativeInfinity
256+
private def readResolve(): AnyRef = MinusInf // Instructs deserialization to use this same instance
254257
}
255258

256259
// Java Factories
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package scala.concurrent.duration
2+
3+
import org.junit.runner.RunWith
4+
import org.junit.runners.JUnit4
5+
import org.junit.Test
6+
7+
8+
@RunWith(classOf[JUnit4])
9+
class SerializationTest {
10+
@Test
11+
def test_SI9197 {
12+
def ser(a: AnyRef): Array[Byte] = {
13+
val bais = new java.io.ByteArrayOutputStream
14+
(new java.io.ObjectOutputStream(bais)).writeObject(a)
15+
bais.toByteArray
16+
}
17+
def des(ab: Array[Byte]): AnyRef =
18+
(new java.io.ObjectInputStream(new java.io.ByteArrayInputStream(ab))).readObject
19+
20+
assert(Duration.Undefined eq des(ser(Duration.Undefined)))
21+
assert(Duration.Inf eq des(ser(Duration.Inf)))
22+
assert(Duration.MinusInf eq des(ser(Duration.MinusInf)))
23+
}
24+
}

0 commit comments

Comments
 (0)