Skip to content

Commit 87f95ec

Browse files
committed
Unfinalize the class DefaultPromise
Doing so seems like the cleanest way to enabled conversions like `javaFuture.toScala.toJava` to return the original `javaFuture` in scala-java8-compat. I have made the methods defined in this class final as an alternative lockdown. Discussion, Motivation: scala/scala-java8-compat#46 scala/scala-java8-compat#50
1 parent 2f7a622 commit 87f95ec

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/library/scala/concurrent/impl/Promise.scala

+8-6
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ private[concurrent] object Promise {
178178
* DefaultPromises, and `linkedRootOf` is currently only designed to be called
179179
* by Future.flatMap.
180180
*/
181-
final class DefaultPromise[T] extends AtomicReference[AnyRef](Nil) with Promise[T] {
181+
// Left non-final to enable addition of extra fields by Java/Scala converters
182+
// in scala-java8-compat.
183+
class DefaultPromise[T] extends AtomicReference[AnyRef](Nil) with Promise[T] {
182184

183185
/** Get the root promise for this promise, compressing the link chain to that
184186
* promise if necessary.
@@ -248,12 +250,12 @@ private[concurrent] object Promise {
248250

249251
@throws(classOf[TimeoutException])
250252
@throws(classOf[InterruptedException])
251-
def ready(atMost: Duration)(implicit permit: CanAwait): this.type =
253+
final def ready(atMost: Duration)(implicit permit: CanAwait): this.type =
252254
if (tryAwait(atMost)) this
253255
else throw new TimeoutException("Futures timed out after [" + atMost + "]")
254256

255257
@throws(classOf[Exception])
256-
def result(atMost: Duration)(implicit permit: CanAwait): T =
258+
final def result(atMost: Duration)(implicit permit: CanAwait): T =
257259
ready(atMost).value.get.get // ready throws TimeoutException if timeout so value.get is safe here
258260

259261
def value: Option[Try[T]] = value0
@@ -265,7 +267,7 @@ private[concurrent] object Promise {
265267
case _ => None
266268
}
267269

268-
override def isCompleted: Boolean = isCompleted0
270+
override final def isCompleted: Boolean = isCompleted0
269271

270272
@tailrec
271273
private def isCompleted0: Boolean = get() match {
@@ -274,7 +276,7 @@ private[concurrent] object Promise {
274276
case _ => false
275277
}
276278

277-
def tryComplete(value: Try[T]): Boolean = {
279+
final def tryComplete(value: Try[T]): Boolean = {
278280
val resolved = resolveTry(value)
279281
tryCompleteAndGetListeners(resolved) match {
280282
case null => false
@@ -297,7 +299,7 @@ private[concurrent] object Promise {
297299
}
298300
}
299301

300-
def onComplete[U](func: Try[T] => U)(implicit executor: ExecutionContext): Unit =
302+
final def onComplete[U](func: Try[T] => U)(implicit executor: ExecutionContext): Unit =
301303
dispatchOrAddCallback(new CallbackRunnable[T](executor.prepare(), func))
302304

303305
/** Tries to add the callback, if already completed, it dispatches the callback to be executed.

0 commit comments

Comments
 (0)