akka.japi.JavaAPI.scala Maven / Gradle / Ivy
/**
* Copyright (C) 2009-2012 Typesafe Inc.
*/
package akka.japi
import scala.Some
/**
* A Function interface. Used to create first-class-functions is Java.
*/
trait Function[T, R] {
@throws(classOf[Exception])
def apply(param: T): R
}
/**
* A Function interface. Used to create 2-arg first-class-functions is Java.
*/
trait Function2[T1, T2, R] {
@throws(classOf[Exception])
def apply(arg1: T1, arg2: T2): R
}
/**
* A Procedure is like a Function, but it doesn't produce a return value.
*/
trait Procedure[T] {
@throws(classOf[Exception])
def apply(param: T)
}
/**
* A Procedure is like a Function, but it doesn't produce a return value.
*/
trait Procedure2[T1, T2] {
@throws(classOf[Exception])
def apply(param: T1, param2: T2)
}
/**
* An executable piece of code that takes no parameters and doesn't return any value.
*/
trait SideEffect {
@throws(classOf[Exception])
def apply(): Unit
}
/**
* An executable piece of code that takes no parameters and doesn't return any value.
*/
trait Effect {
@throws(classOf[Exception])
def apply(): Unit
}
/**
* A constructor/factory, takes no parameters but creates a new value of type T every call.
*/
trait Creator[T] {
/**
* This method must return a different instance upon every call.
*/
@throws(classOf[Exception])
def create(): T
}
/**
* This class represents optional values. Instances of Option
* are either instances of case class Some
or it is case
* object None
.
*
* Java API
*/
sealed abstract class Option[A] extends java.lang.Iterable[A] {
import scala.collection.JavaConversions._
def get: A
def isEmpty: Boolean
def isDefined = !isEmpty
def asScala: scala.Option[A]
def iterator = if (isEmpty) Iterator.empty else Iterator.single(get)
}
object Option {
/**
* Option
factory that creates Some
*/
def some[A](v: A): Option[A] = Some(v)
/**
* Option
factory that creates None
*/
def none[A] = None.asInstanceOf[Option[A]]
/**
* Option
factory that creates None
if
* v
is null
, Some(v)
otherwise.
*/
def option[A](v: A): Option[A] = if (v == null) none else some(v)
/**
* 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
}
/**
* Class Some[A]
represents existing values of type
* A
.
*/
final case class Some[A](v: A) extends Option[A] {
def get = v
def isEmpty = false
def asScala = scala.Some(v)
}
/**
* This case object represents non-existent values.
*/
private case object None extends Option[Nothing] {
def get = throw new NoSuchElementException("None.get")
def isEmpty = true
def asScala = scala.None
}
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
}
/**
* This class hold common utilities for Java
*/
object Util {
/**
* Given a Class returns a Scala Manifest of that Class
*/
def manifest[T](clazz: Class[T]): Manifest[T] = Manifest.classType(clazz)
}