-
Notifications
You must be signed in to change notification settings - Fork 2
Overview
This library provides the mechanisms to convert between types provided by java-scala-utils to their Scala equivalence and vice-versa.
One can either perform explicit conversions by using a specific converter method or do implicit conversions using the decorator pattern provided by Scalas implicit method declaration.
This is the mechanism for performing the conversion by invoking a specific method taking the type you want to and converting it to its Scala or Java equivalence.
There's converters for Java -> Scala, javascalautils.converters.j2s.Converters
as well as from Scala -> Java, javascalautils.converters.s2j.Converters.
There's also an aggregate converter containing both of these javascalautils.converters.JavaScalaUtilConverters.
Either import the converters you need:
import javascalautils.converters.j2s.Converters._
import javascalautils.converters.s2j.Converters._
or use the aggregate object composed of both above converters
import javascalautils.converters.JavaScalaUtilConverters._
Option
import javascalautils.converters.JavaScalaUtilConverters._
import javascalautils.{Some => JSome, None => JNone}
//Java -> Scala
val optionSome = asScalaOption(new JSome("Some is never None"))
val optionNone = asScalaOption(new JNone())
//Scala -> Java
val joptionSome = asJavaOption(Some("Some is never None"))
val joptionNone = asJavaOption(None)
Try
import javascalautils.converters.JavaScalaUtilConverters._
import javascalautils.{Try => JTry, Success => JSuccess, Failure => JFailure}
//Java -> Scala
val trySuccess = asScalaTry(new JSuccess("Success is never a Failure"))
val tryFailure = asScalaTry(new JFailure(new Exception("Error, terror")))
//Scala -> Java
val jTryFailure = asJavaTry(Failure(new Exception("Error, terror!!!")))
val jTrySuccess = asJavaTry(Success("Success is not Failure"))
This utilizes the implicit mechanism in Scala to decorate any given class with new methods.
More precisely this library provides asScala methods on all supported Java types and asJava methods on all supported Scala types.
This magic is enabled by having the right imports in scope.
Just as with the explicit converters the implicit ones are divided into
Java -> Scala, javascalautils.converters.j2s.Implicits
as well as from Scala -> Java, javascalautils.converters.s2j.Implicits.
Or one can choose to use the aggregate implicit javascalautils.converters.JavaScalaUtilImplicits.
It's again a matter of having the right imports in scope.
import javascalautils.converters.j2s.Implicits._
import javascalautils.converters.s2j.Implicits._
or use the aggregate object composed of both above implicits.
import javascalautils.converters.JavaScalaUtilImplicits._
Option
import javascalautils.converters.JavaScalaUtilImplicits._
import javascalautils.{Some => JSome, None => JNone}
//Java -> Scala
val some = new JSome("Some is never None").asScala
val optionNone = new JNone().asScala
//Scala -> Java
val jsome = Some("Some is never None").asJava
val joptionNone = None.asJava
Try
import javascalautils.converters.JavaScalaUtilImplicits._
import javascalautils.{Try => JTry, Success => JSuccess, Failure => JFailure}
//Java -> Scala
val trySuccess = new JSuccess("Success is never a Failure").asScala
val tryFailure = new JFailure(new Exception("Error, terror")).asScala
//Scala -> Java
val jTryFailure = Failure(new Exception("Error, terror!!!")).asJava
val jTrySuccess = Success("Success is not Failure").asJava