(re)move akka.japi.*PartialFunction and optimize TestKit, see #2357
This commit is contained in:
parent
d4ce9e6750
commit
0f923c0978
3 changed files with 37 additions and 31 deletions
|
|
@ -48,7 +48,7 @@ trait Creator[T] {
|
|||
def create(): T
|
||||
}
|
||||
|
||||
object PurePartialFunction {
|
||||
object JavaPartialFunction {
|
||||
sealed abstract class NoMatchException extends RuntimeException with NoStackTrace
|
||||
case object NoMatch extends NoMatchException
|
||||
final def noMatch(): RuntimeException = NoMatch
|
||||
|
|
@ -66,7 +66,7 @@ object PurePartialFunction {
|
|||
* <i>that</i> expensive).
|
||||
*
|
||||
* {{{
|
||||
* new PurePartialFunction<Object, String>() {
|
||||
* new JavaPartialFunction<Object, String>() {
|
||||
* public String apply(Object in, boolean isCheck) {
|
||||
* if (in instanceof TheThing) {
|
||||
* if (isCheck) return null; // to spare the expensive or side-effecting code
|
||||
|
|
@ -90,32 +90,14 @@ object PurePartialFunction {
|
|||
* does not throw `noMatch()` it will continue with calling
|
||||
* `PurePartialFunction.apply(x, false)`.
|
||||
*/
|
||||
abstract class PurePartialFunction[A, B] extends scala.runtime.AbstractFunction1[A, B] with PartialFunction[A, B] {
|
||||
import PurePartialFunction._
|
||||
abstract class JavaPartialFunction[A, B] extends scala.runtime.AbstractFunction1[A, B] with PartialFunction[A, B] {
|
||||
import JavaPartialFunction._
|
||||
|
||||
def apply(x: A, isCheck: Boolean): B
|
||||
|
||||
final def isDefinedAt(x: A): Boolean = try { apply(x, true); true } catch { case NoMatch ⇒ false }
|
||||
final def apply(x: A): B = try apply(x, false) catch { case NoMatch ⇒ throw new MatchError(x) }
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a specialized variant of PartialFunction which is <b><i>only
|
||||
* applicable if you know that `isDefinedAt(x)` is always called before
|
||||
* `apply(x)`—with the same `x` of course.</i></b>
|
||||
*
|
||||
* `match(x)` will be called for `isDefinedAt(x)` only, and its semantics
|
||||
* are the same as for [[akka.japi.PurePartialFunction]] (apart from the
|
||||
* missing because unneeded boolean argument).
|
||||
*/
|
||||
abstract class CachingPartialFunction[A, B <: AnyRef] extends scala.runtime.AbstractFunction1[A, B] with PartialFunction[A, B] {
|
||||
import PurePartialFunction._
|
||||
|
||||
def `match`(x: A): B
|
||||
|
||||
var cache: B = _
|
||||
final def isDefinedAt(x: A): Boolean = try { cache = `match`(x); true } catch { case NoMatch ⇒ cache = null.asInstanceOf[B]; false }
|
||||
final def apply(x: A): B = cache
|
||||
final override def applyOrElse[A1 <: A, B1 >: B](x: A1, default: A1 ⇒ B1): B1 = try apply(x, false) catch { case NoMatch ⇒ default(x) }
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -8,8 +8,7 @@ import akka.actor.ActorRef;
|
|||
import akka.actor.ActorSystem;
|
||||
import akka.event.Logging;
|
||||
import akka.event.Logging.LogEvent;
|
||||
import akka.japi.PurePartialFunction;
|
||||
import akka.japi.CachingPartialFunction;
|
||||
import akka.japi.JavaPartialFunction;
|
||||
import akka.japi.Util;
|
||||
import scala.concurrent.util.Duration;
|
||||
|
||||
|
|
@ -79,7 +78,7 @@ public class JavaTestKit {
|
|||
abstract protected boolean ignore(Object msg);
|
||||
|
||||
public IgnoreMsg() {
|
||||
p.ignoreMsg(new PurePartialFunction<Object, Object>() {
|
||||
p.ignoreMsg(new JavaPartialFunction<Object, Object>() {
|
||||
public Boolean apply(Object in, boolean isCheck) {
|
||||
return ignore(in);
|
||||
}
|
||||
|
|
@ -148,7 +147,7 @@ public class JavaTestKit {
|
|||
final Object received = p.receiveOne(max);
|
||||
try {
|
||||
result = match(received);
|
||||
} catch (PurePartialFunction.NoMatchException ex) {
|
||||
} catch (JavaPartialFunction.NoMatchException ex) {
|
||||
throw new AssertionError("while expecting '" + hint
|
||||
+ "' received unexpected: " + received);
|
||||
}
|
||||
|
|
@ -157,7 +156,7 @@ public class JavaTestKit {
|
|||
abstract protected T match(Object msg);
|
||||
|
||||
protected RuntimeException noMatch() {
|
||||
throw PurePartialFunction.noMatch();
|
||||
throw JavaPartialFunction.noMatch();
|
||||
}
|
||||
|
||||
public T get() {
|
||||
|
|
@ -245,7 +244,7 @@ public class JavaTestKit {
|
|||
}
|
||||
|
||||
protected RuntimeException noMatch() {
|
||||
throw PurePartialFunction.noMatch();
|
||||
throw JavaPartialFunction.noMatch();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@ import scala.annotation.tailrec
|
|||
import akka.util.{ Timeout, BoxedType }
|
||||
import scala.annotation.varargs
|
||||
import scala.reflect.ClassTag
|
||||
import akka.japi.PurePartialFunction
|
||||
|
||||
object TestActor {
|
||||
type Ignore = Option[PartialFunction[AnyRef, Boolean]]
|
||||
|
|
@ -48,6 +47,8 @@ object TestActor {
|
|||
override def msg: AnyRef = throw new IllegalActorStateException("last receive did not dequeue a message")
|
||||
override def sender: ActorRef = throw new IllegalActorStateException("last receive did not dequeue a message")
|
||||
}
|
||||
|
||||
val FALSE = (x: Any) ⇒ false
|
||||
}
|
||||
|
||||
class TestActor(queue: BlockingDeque[TestActor.Message]) extends Actor {
|
||||
|
|
@ -67,7 +68,7 @@ class TestActor(queue: BlockingDeque[TestActor.Message]) extends Actor {
|
|||
case KeepRunning ⇒ autopilot
|
||||
case other ⇒ other
|
||||
}
|
||||
val observe = ignore map (ignoreFunc ⇒ if (ignoreFunc isDefinedAt x) !ignoreFunc(x) else true) getOrElse true
|
||||
val observe = ignore map (ignoreFunc ⇒ !ignoreFunc.applyOrElse(x, FALSE)) getOrElse true
|
||||
if (observe) queue.offerLast(RealMessage(x, sender))
|
||||
}
|
||||
|
||||
|
|
@ -713,3 +714,27 @@ trait ImplicitSender { this: TestKit ⇒
|
|||
trait DefaultTimeout { this: TestKit ⇒
|
||||
implicit val timeout: Timeout = testKitSettings.DefaultTimeout
|
||||
}
|
||||
|
||||
/**
|
||||
* INTERNAL API
|
||||
*
|
||||
* This is a specialized variant of PartialFunction which is <b><i>only
|
||||
* applicable if you know that `isDefinedAt(x)` is always called before
|
||||
* `apply(x)`—with the same `x` of course.</i></b>
|
||||
*
|
||||
* `match(x)` will be called for `isDefinedAt(x)` only, and its semantics
|
||||
* are the same as for [[akka.japi.PurePartialFunction]] (apart from the
|
||||
* missing because unneeded boolean argument).
|
||||
*
|
||||
* This class is used internal to JavaTestKit and should not be extended
|
||||
* by client code directly.
|
||||
*/
|
||||
private[testkit] abstract class CachingPartialFunction[A, B <: AnyRef] extends scala.runtime.AbstractFunction1[A, B] with PartialFunction[A, B] {
|
||||
import akka.japi.JavaPartialFunction._
|
||||
|
||||
def `match`(x: A): B
|
||||
|
||||
var cache: B = _
|
||||
final def isDefinedAt(x: A): Boolean = try { cache = `match`(x); true } catch { case NoMatch ⇒ cache = null.asInstanceOf[B]; false }
|
||||
final def apply(x: A): B = cache
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue