Package play.libs
Class Scala
- Object
-
- play.libs.Scala
-
public class Scala extends Object
Class that contains useful java <-> scala conversion helpers.
-
-
Constructor Summary
Constructors Constructor Description Scala()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T[]
asArray(Class<T> clazz, scala.collection.Seq<T> scalaList)
Converts a Scala List to an Array.static <K,V>
Map<K,V>asJava(scala.collection.Map<K,V> scalaMap)
Converts a Scala Map to Java.static <T> List<T>
asJava(scala.collection.Seq<T> scalaList)
Converts a Scala List to Java.static <A,B>
F.Tuple<A,B>asJava(scala.Tuple2<A,B> tuple)
Converts a scalaTuple2
to a java F.Tuple.static <A extends B,B>
scala.collection.immutable.Seq<B>asScala(Collection<A> javaCollection)
Converts a Java Collection to a Scala Seq.static <A> scala.Function0<A>
asScala(Callable<A> callable)
Converts a Java Callable to a Scala Function0.static <K,V>
scala.collection.immutable.Map<K,V>asScala(Map<K,V> javaMap)
Converts a Java Map to Scala.static <A> scala.Function0<scala.concurrent.Future<A>>
asScalaWithFuture(Callable<CompletionStage<A>> callable)
Converts a Java Callable to a Scala Function0.static <C> scala.reflect.ClassTag<C>
classTag()
static <A,B>
scala.collection.immutable.Map<A,B>emptyMap()
static <T> scala.collection.Seq<T>
emptySeq()
static RuntimeException
noMatch()
Throw this exception to indicate that a partial function doesn't match.static <T> scala.Option<T>
None()
static <T> scala.Option<T>
Option(T t)
Wrap a value into a Scala Option.static <T> T
orElse(scala.Option<T> opt, T defaultValue)
Wraps a Scala Option, handling None by returning a defaultValuestatic <T> T
orNull(scala.Option<T> opt)
Wraps a Scala Option, handling None as null.static <A,B>
scala.PartialFunction<A,B>partialFunction(Function<A,B> f)
Create a Scala PartialFunction from a function.static <T> scala.collection.immutable.Seq<T>
toSeq(List<T> list)
Converts a Java List to Scala Seq.static <T> scala.collection.immutable.Seq<T>
toSeq(T[] array)
Converts a Java Array to Scala Seq.static <A,B>
scala.Tuple2<A,B>Tuple(A a, B b)
Creates a ScalaTuple2
.static <T> scala.collection.immutable.Seq<T>
varargs(T... array)
Converts a Java varargs to Scala varargs.
-
-
-
Method Detail
-
toSeq
public static <T> scala.collection.immutable.Seq<T> toSeq(List<T> list)
Converts a Java List to Scala Seq.- Type Parameters:
T
- the element type.- Parameters:
list
- the java list.- Returns:
- the converted Seq.
-
toSeq
public static <T> scala.collection.immutable.Seq<T> toSeq(T[] array)
Converts a Java Array to Scala Seq.- Type Parameters:
T
- the element type.- Parameters:
array
- the java array.- Returns:
- the converted Seq.
-
varargs
@SafeVarargs public static <T> scala.collection.immutable.Seq<T> varargs(T... array)
Converts a Java varargs to Scala varargs.- Type Parameters:
T
- the element type.- Parameters:
array
- the java array.- Returns:
- the Scala varargs
-
orNull
public static <T> T orNull(scala.Option<T> opt)
Wraps a Scala Option, handling None as null.- Type Parameters:
T
- the type in the Option.- Parameters:
opt
- the scala option.- Returns:
- the value of the option, or null if opt.isDefined is false.
-
orElse
public static <T> T orElse(scala.Option<T> opt, T defaultValue)
Wraps a Scala Option, handling None by returning a defaultValue- Type Parameters:
T
- the type in the Option.- Parameters:
opt
- the scala option.defaultValue
- the default value if None is found.- Returns:
- the return value.
-
asJava
public static <K,V> Map<K,V> asJava(scala.collection.Map<K,V> scalaMap)
Converts a Scala Map to Java.- Type Parameters:
K
- key typeV
- value type- Parameters:
scalaMap
- the scala map.- Returns:
- the java map.
-
asScala
public static <K,V> scala.collection.immutable.Map<K,V> asScala(Map<K,V> javaMap)
Converts a Java Map to Scala.- Type Parameters:
K
- key typeV
- value type- Parameters:
javaMap
- the java map- Returns:
- the scala map.
-
asScala
public static <A extends B,B> scala.collection.immutable.Seq<B> asScala(Collection<A> javaCollection)
Converts a Java Collection to a Scala Seq.- Type Parameters:
A
- the input type of Seq elementB
- the output type of Seq element- Parameters:
javaCollection
- the java collection- Returns:
- the scala Seq.
-
asScala
public static <A> scala.Function0<A> asScala(Callable<A> callable)
Converts a Java Callable to a Scala Function0.- Type Parameters:
A
- the return type.- Parameters:
callable
- the java callable.- Returns:
- the scala function.
-
asScalaWithFuture
public static <A> scala.Function0<scala.concurrent.Future<A>> asScalaWithFuture(Callable<CompletionStage<A>> callable)
Converts a Java Callable to a Scala Function0.- Type Parameters:
A
- the return type.- Parameters:
callable
- the java callable.- Returns:
- the scala function in a Scala Future.
-
asJava
public static <T> List<T> asJava(scala.collection.Seq<T> scalaList)
Converts a Scala List to Java.- Type Parameters:
T
- the return type.- Parameters:
scalaList
- the scala list.- Returns:
- the java list
-
asArray
public static <T> T[] asArray(Class<T> clazz, scala.collection.Seq<T> scalaList)
Converts a Scala List to an Array.- Type Parameters:
T
- the return type.- Parameters:
clazz
- the element class typescalaList
- the scala list.- Returns:
- the array
-
Option
public static <T> scala.Option<T> Option(T t)
Wrap a value into a Scala Option.- Type Parameters:
T
- the element type.- Parameters:
t
- the java value.- Returns:
- the converted Option.
-
None
public static <T> scala.Option<T> None()
- Type Parameters:
T
- the type parameter- Returns:
- a scala
None
.
-
Tuple
public static <A,B> scala.Tuple2<A,B> Tuple(A a, B b)
Creates a ScalaTuple2
.- Type Parameters:
A
- input parameter typeB
- return type.- Parameters:
a
- element one of the tuple.b
- element two of the tuple.- Returns:
- an instance of Tuple2 with the elements.
-
asJava
public static <A,B> F.Tuple<A,B> asJava(scala.Tuple2<A,B> tuple)
Converts a scalaTuple2
to a java F.Tuple.- Type Parameters:
A
- input parameter typeB
- return type.- Parameters:
tuple
- the Scala Tuple.- Returns:
- an instance of Tuple with the elements.
-
emptySeq
public static <T> scala.collection.Seq<T> emptySeq()
- Type Parameters:
T
- the type parameter- Returns:
- an empty Scala Seq.
-
emptyMap
public static <A,B> scala.collection.immutable.Map<A,B> emptyMap()
- Type Parameters:
A
- input parameter typeB
- return type.- Returns:
- an empty Scala Map.
-
classTag
public static <C> scala.reflect.ClassTag<C> classTag()
- Type Parameters:
C
- the classtag's type.- Returns:
- an any ClassTag typed according to the Java compiler as C.
-
partialFunction
public static <A,B> scala.PartialFunction<A,B> partialFunction(Function<A,B> f)
Create a Scala PartialFunction from a function.A PartialFunction is one that isn't defined for the whole of its domain. If the function isn't defined for a particular input parameter, it can throw
F.noMatch()
, and this will be translated into the semantics of a Scala PartialFunction.For example:
Flow<String, Integer, ?> collectInts = Flow.<String>collect(Scala.partialFunction( str -> { try { return Integer.parseInt(str); } catch (NumberFormatException e) { throw Scala.noMatch(); } }));
The above code will convert a flow of String into a flow of Integer, dropping any strings that can't be parsed as integers.- Type Parameters:
A
- input parameter typeB
- return type.- Parameters:
f
- The function to make a partial function from.- Returns:
- a Scala PartialFunction.
-
noMatch
public static RuntimeException noMatch()
Throw this exception to indicate that a partial function doesn't match.- Returns:
- An exception that indicates a partial function doesn't match.
-
-