2010-09-06 15:41:48 +02:00
|
|
|
package se.scalablesolutions.akka.util
|
|
|
|
|
|
2010-10-05 11:13:27 +02:00
|
|
|
object JavaAPI {
|
|
|
|
|
/** A Function interface
|
|
|
|
|
* Used to create first-class-functions is Java (sort of)
|
|
|
|
|
* Java API
|
|
|
|
|
*/
|
|
|
|
|
trait Function[T,R] {
|
|
|
|
|
def apply(param: T): R
|
|
|
|
|
}
|
2010-09-06 15:41:48 +02:00
|
|
|
|
2010-10-05 11:13:27 +02:00
|
|
|
/** A Procedure is like a Function, but it doesn't produce a return value
|
|
|
|
|
* Java API
|
|
|
|
|
*/
|
|
|
|
|
trait Procedure[T] {
|
|
|
|
|
def apply(param: T): Unit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An executable piece of code that takes no parameters and doesn't return any value
|
|
|
|
|
*/
|
|
|
|
|
trait SideEffect {
|
|
|
|
|
def apply: Unit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 isDefined: Boolean
|
2010-10-05 15:49:11 +02:00
|
|
|
def asScala: scala.Option[A]
|
2010-10-05 11:13:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class <code>Some[A]</code> represents existing values of type
|
|
|
|
|
* <code>A</code>.
|
|
|
|
|
* <p>
|
|
|
|
|
* Java API
|
|
|
|
|
*/
|
|
|
|
|
final case class Some[A](v: A) extends Option[A] {
|
|
|
|
|
import scala.collection.JavaConversions._
|
2010-09-15 16:49:05 +02:00
|
|
|
|
2010-10-05 15:49:11 +02:00
|
|
|
def get = v
|
|
|
|
|
def iterator = Iterator.single(v)
|
2010-10-05 11:13:27 +02:00
|
|
|
def isDefined = true
|
2010-10-05 15:49:11 +02:00
|
|
|
def asScala = scala.Some(v)
|
2010-10-05 11:13:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This case object represents non-existent values.
|
|
|
|
|
* <p>
|
|
|
|
|
* Java API
|
|
|
|
|
*/
|
|
|
|
|
case class None[A]() extends Option[A] {
|
|
|
|
|
import scala.collection.JavaConversions._
|
|
|
|
|
|
|
|
|
|
def get = throw new NoSuchElementException("None.get")
|
2010-10-05 15:49:11 +02:00
|
|
|
def iterator = Iterator.empty
|
2010-10-05 11:13:27 +02:00
|
|
|
def isDefined = false
|
2010-10-05 15:49:11 +02:00
|
|
|
def asScala = scala.None
|
2010-10-05 11:13:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def some[A](v: A) = Some(v)
|
|
|
|
|
def none[A] = None[A]
|
2010-10-05 15:49:11 +02:00
|
|
|
|
|
|
|
|
implicit def java2ScalaOption[A](o: Option[A]): scala.Option[A] = o.asScala
|
|
|
|
|
implicit def scala2JavaOption[A](o: scala.Option[A]): Option[A] =
|
|
|
|
|
if (o.isDefined) Some(o.get) else None[A]
|
2010-09-15 16:49:05 +02:00
|
|
|
}
|
2010-10-05 11:13:27 +02:00
|
|
|
|