Merging in master, huge work trying to get things to compile, tests not green at this stage
This commit is contained in:
commit
ac5b5de90a
68 changed files with 3759 additions and 2144 deletions
|
|
@ -8,6 +8,7 @@ import language.implicitConversions
|
|||
|
||||
import scala.Some
|
||||
import scala.reflect.ClassTag
|
||||
import scala.util.control.NoStackTrace
|
||||
|
||||
/**
|
||||
* A Function interface. Used to create first-class-functions is Java.
|
||||
|
|
@ -47,6 +48,76 @@ trait Creator[T] {
|
|||
def create(): T
|
||||
}
|
||||
|
||||
object PurePartialFunction {
|
||||
sealed abstract class NoMatchException extends RuntimeException with NoStackTrace
|
||||
case object NoMatch extends NoMatchException
|
||||
final def noMatch(): RuntimeException = NoMatch
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* <i>that</i> expensive).
|
||||
*
|
||||
* {{{
|
||||
* new PurePartialFunction<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
|
||||
* return 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)`.
|
||||
*/
|
||||
abstract class PurePartialFunction[A, B] extends scala.runtime.AbstractFunction1[A, B] with PartialFunction[A, B] {
|
||||
import PurePartialFunction._
|
||||
|
||||
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 }
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
|
||||
/**
|
||||
* This class represents optional values. Instances of <code>Option</code>
|
||||
* are either instances of case class <code>Some</code> or it is case
|
||||
|
|
@ -116,8 +187,9 @@ object Option {
|
|||
* This class hold common utilities for Java
|
||||
*/
|
||||
object Util {
|
||||
/**
|
||||
* Given a Class returns a Scala Manifest of that Class
|
||||
*/
|
||||
def classTag[T](clazz: Class[T]): ClassTag[T] = ClassTag(clazz)
|
||||
|
||||
def arrayToSeq[T](arr: Array[T]): Seq[T] = arr.toSeq
|
||||
|
||||
def arrayToSeq(classes: Array[Class[_]]): Seq[Class[_]] = classes.toSeq
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue