Helper for implementing a *pure* partial function: it will possibly be
invoked multiple times for a single “application”, because its only abstract
method is used for both isDefinedAt() and apply(); the former is mapped to
isCheck == true and the latter to isCheck == false for those cases where
this is important to know.
Failure to match is signaled by throwing noMatch(), i.e. not returning
normally (the exception used in this case is pre-allocated, hence not
that expensive).
new JavaPartialFunction<Object, String>() {
public String apply(Object in, boolean isCheck) {
if (in instanceof TheThing) {
if (isCheck) returnnull; // to spare the expensive or side-effecting codereturn doSomethingWithTheThing((TheThing) in);
} else {
throw noMatch();
}
}
}
The typical use of partial functions from Akka looks like the following:
if (pf.isDefinedAt(x)) {
pf.apply(x);
}
i.e. it will first call PurePartialFunction.apply(x, true) and if that
does not throw noMatch() it will continue with calling
PurePartialFunction.apply(x, false).
Linear Supertypes
AbstractPartialFunction[A, B], PartialFunction[A, B], (A) ⇒ B, AnyRef, Any
Helper for implementing a *pure* partial function: it will possibly be invoked multiple times for a single “application”, because its only abstract method is used for both isDefinedAt() and apply(); the former is mapped to
isCheck == true
and the latter toisCheck == false
for those cases where this is important to know.Failure to match is signaled by throwing
noMatch()
, i.e. not returning normally (the exception used in this case is pre-allocated, hence not that expensive).The typical use of partial functions from Akka looks like the following:
if (pf.isDefinedAt(x)) { pf.apply(x); }
i.e. it will first call
PurePartialFunction.apply(x, true)
and if that does not thrownoMatch()
it will continue with callingPurePartialFunction.apply(x, false)
.