2011-08-30 10:51:53 +02:00
|
|
|
/**
|
2012-01-19 18:21:06 +01:00
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
2011-08-30 10:51:53 +02:00
|
|
|
*/
|
|
|
|
|
|
2010-10-26 12:49:25 +02:00
|
|
|
package akka.japi
|
2010-10-07 14:45:40 +02:00
|
|
|
|
2012-06-15 13:04:10 +02:00
|
|
|
import language.implicitConversions
|
2012-10-30 15:08:41 +01:00
|
|
|
|
|
|
|
|
import scala.collection.immutable
|
2012-06-13 15:44:24 +02:00
|
|
|
import scala.reflect.ClassTag
|
2012-06-25 19:30:13 +02:00
|
|
|
import scala.util.control.NoStackTrace
|
2012-08-09 17:10:43 +02:00
|
|
|
import scala.runtime.AbstractPartialFunction
|
2012-10-23 12:07:38 +02:00
|
|
|
import java.util.Collections.{ emptyList, singletonList }
|
2011-08-26 17:25:18 +02:00
|
|
|
|
2010-10-07 14:45:40 +02:00
|
|
|
/**
|
2011-08-30 10:51:53 +02:00
|
|
|
* A Function interface. Used to create first-class-functions is Java.
|
2010-10-07 14:45:40 +02:00
|
|
|
*/
|
2011-05-18 17:25:30 +02:00
|
|
|
trait Function[T, R] {
|
2012-08-09 17:26:39 +02:00
|
|
|
@throws(classOf[Exception])
|
2010-10-07 14:45:40 +02:00
|
|
|
def apply(param: T): R
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-22 22:12:16 +01:00
|
|
|
/**
|
2011-08-30 10:51:53 +02:00
|
|
|
* A Function interface. Used to create 2-arg first-class-functions is Java.
|
2011-03-22 22:12:16 +01:00
|
|
|
*/
|
|
|
|
|
trait Function2[T1, T2, R] {
|
2012-08-09 17:26:39 +02:00
|
|
|
@throws(classOf[Exception])
|
2011-03-22 22:12:16 +01:00
|
|
|
def apply(arg1: T1, arg2: T2): R
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
/**
|
2011-08-30 10:51:53 +02:00
|
|
|
* A Procedure is like a Function, but it doesn't produce a return value.
|
2010-10-07 14:45:40 +02:00
|
|
|
*/
|
|
|
|
|
trait Procedure[T] {
|
2012-08-09 17:26:39 +02:00
|
|
|
@throws(classOf[Exception])
|
2012-05-18 13:37:26 +02:00
|
|
|
def apply(param: T): Unit
|
2010-10-07 14:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
2010-11-24 14:49:10 +01:00
|
|
|
/**
|
|
|
|
|
* An executable piece of code that takes no parameters and doesn't return any value.
|
|
|
|
|
*/
|
|
|
|
|
trait Effect {
|
2012-08-09 17:26:39 +02:00
|
|
|
@throws(classOf[Exception])
|
2012-05-18 13:37:26 +02:00
|
|
|
def apply(): Unit
|
2010-11-24 14:49:10 +01:00
|
|
|
}
|
|
|
|
|
|
2010-11-20 22:26:26 +01:00
|
|
|
/**
|
2011-08-30 10:51:53 +02:00
|
|
|
* A constructor/factory, takes no parameters but creates a new value of type T every call.
|
2011-05-18 17:25:30 +02:00
|
|
|
*/
|
2010-11-20 22:26:26 +01:00
|
|
|
trait Creator[T] {
|
2012-05-25 17:55:25 +02:00
|
|
|
/**
|
|
|
|
|
* This method must return a different instance upon every call.
|
|
|
|
|
*/
|
2012-08-09 17:26:39 +02:00
|
|
|
@throws(classOf[Exception])
|
2011-08-30 10:51:53 +02:00
|
|
|
def create(): T
|
2010-11-20 22:26:26 +01:00
|
|
|
}
|
|
|
|
|
|
2012-08-08 16:05:48 +02:00
|
|
|
object JavaPartialFunction {
|
2012-06-29 14:42:11 +02:00
|
|
|
sealed abstract class NoMatchException extends RuntimeException with NoStackTrace
|
|
|
|
|
case object NoMatch extends NoMatchException
|
|
|
|
|
final def noMatch(): RuntimeException = NoMatch
|
2012-06-25 19:30:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2012-07-04 10:29:44 +02:00
|
|
|
* 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).
|
|
|
|
|
*
|
2012-06-25 19:30:13 +02:00
|
|
|
* {{{
|
2012-08-08 16:05:48 +02:00
|
|
|
* new JavaPartialFunction<Object, String>() {
|
2012-06-25 19:30:13 +02:00
|
|
|
* 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:
|
|
|
|
|
*
|
|
|
|
|
* {{{
|
2012-07-04 10:29:44 +02:00
|
|
|
* if (pf.isDefinedAt(x)) {
|
|
|
|
|
* pf.apply(x);
|
|
|
|
|
* }
|
2012-06-25 19:30:13 +02:00
|
|
|
* }}}
|
|
|
|
|
*
|
|
|
|
|
* 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)`.
|
|
|
|
|
*/
|
2012-08-09 17:10:43 +02:00
|
|
|
abstract class JavaPartialFunction[A, B] extends AbstractPartialFunction[A, B] {
|
2012-08-08 16:05:48 +02:00
|
|
|
import JavaPartialFunction._
|
2012-06-25 19:30:13 +02:00
|
|
|
|
2012-08-09 17:26:39 +02:00
|
|
|
@throws(classOf[Exception])
|
2012-06-25 19:30:13 +02:00
|
|
|
def apply(x: A, isCheck: Boolean): B
|
|
|
|
|
|
|
|
|
|
final def isDefinedAt(x: A): Boolean = try { apply(x, true); true } catch { case NoMatch ⇒ false }
|
2012-08-09 17:10:43 +02:00
|
|
|
final override def apply(x: A): B = try apply(x, false) catch { case NoMatch ⇒ throw new MatchError(x) }
|
2012-08-08 16:05:48 +02:00
|
|
|
final override def applyOrElse[A1 <: A, B1 >: B](x: A1, default: A1 ⇒ B1): B1 = try apply(x, false) catch { case NoMatch ⇒ default(x) }
|
2012-06-25 19:30:13 +02:00
|
|
|
}
|
|
|
|
|
|
2010-10-07 14:45:40 +02:00
|
|
|
/**
|
|
|
|
|
* This class represents optional values. Instances of <code>Option</code>
|
|
|
|
|
* are either instances of case class <code>Some</code> or it is case
|
|
|
|
|
* object <code>None</code>.
|
|
|
|
|
* <p>
|
|
|
|
|
* Java API
|
|
|
|
|
*/
|
|
|
|
|
sealed abstract class Option[A] extends java.lang.Iterable[A] {
|
|
|
|
|
def get: A
|
|
|
|
|
def isEmpty: Boolean
|
2012-05-18 13:37:26 +02:00
|
|
|
def isDefined: Boolean = !isEmpty
|
2010-10-07 14:45:40 +02:00
|
|
|
def asScala: scala.Option[A]
|
2012-10-23 12:07:38 +02:00
|
|
|
def iterator: java.util.Iterator[A] = if (isEmpty) emptyList[A].iterator else singletonList(get).iterator
|
2010-10-07 14:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object Option {
|
|
|
|
|
/**
|
|
|
|
|
* <code>Option</code> factory that creates <code>Some</code>
|
|
|
|
|
*/
|
|
|
|
|
def some[A](v: A): Option[A] = Some(v)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <code>Option</code> factory that creates <code>None</code>
|
|
|
|
|
*/
|
|
|
|
|
def none[A] = None.asInstanceOf[Option[A]]
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <code>Option</code> factory that creates <code>None</code> if
|
|
|
|
|
* <code>v</code> is <code>null</code>, <code>Some(v)</code> otherwise.
|
|
|
|
|
*/
|
|
|
|
|
def option[A](v: A): Option[A] = if (v == null) none else some(v)
|
|
|
|
|
|
2011-08-26 17:25:18 +02:00
|
|
|
/**
|
|
|
|
|
* Converts a Scala Option to a Java Option
|
|
|
|
|
*/
|
|
|
|
|
def fromScalaOption[T](scalaOption: scala.Option[T]): Option[T] = scalaOption match {
|
|
|
|
|
case scala.Some(r) ⇒ some(r)
|
|
|
|
|
case scala.None ⇒ none
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-07 14:45:40 +02:00
|
|
|
/**
|
|
|
|
|
* Class <code>Some[A]</code> represents existing values of type
|
|
|
|
|
* <code>A</code>.
|
|
|
|
|
*/
|
|
|
|
|
final case class Some[A](v: A) extends Option[A] {
|
2012-05-18 13:37:26 +02:00
|
|
|
def get: A = v
|
|
|
|
|
def isEmpty: Boolean = false
|
|
|
|
|
def asScala: scala.Some[A] = scala.Some(v)
|
2010-10-07 14:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This case object represents non-existent values.
|
|
|
|
|
*/
|
|
|
|
|
private case object None extends Option[Nothing] {
|
2012-05-18 13:37:26 +02:00
|
|
|
def get: Nothing = throw new NoSuchElementException("None.get")
|
|
|
|
|
def isEmpty: Boolean = true
|
|
|
|
|
def asScala: scala.None.type = scala.None
|
2010-10-07 14:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
implicit def java2ScalaOption[A](o: Option[A]): scala.Option[A] = o.asScala
|
2011-08-26 17:25:18 +02:00
|
|
|
implicit def scala2JavaOption[A](o: scala.Option[A]): Option[A] = if (o.isDefined) some(o.get) else none
|
2010-10-29 16:33:31 +02:00
|
|
|
}
|
2012-01-26 15:11:49 +01:00
|
|
|
|
2012-01-26 17:47:31 +01:00
|
|
|
/**
|
|
|
|
|
* This class hold common utilities for Java
|
|
|
|
|
*/
|
|
|
|
|
object Util {
|
2012-06-13 15:44:24 +02:00
|
|
|
def classTag[T](clazz: Class[T]): ClassTag[T] = ClassTag(clazz)
|
2012-06-25 19:30:13 +02:00
|
|
|
|
2012-10-30 15:08:41 +01:00
|
|
|
def arrayToSeq[T](arr: Array[T]): immutable.Seq[T] = arr.to[immutable.Seq]
|
2012-06-29 14:42:11 +02:00
|
|
|
|
2012-10-30 15:08:41 +01:00
|
|
|
def arrayToSeq(classes: Array[Class[_]]): immutable.Seq[Class[_]] = classes.to[immutable.Seq]
|
2012-01-26 15:11:49 +01:00
|
|
|
}
|