object OptionConverters
This class enables bidirectional conversion between scala.Option
and the
set of java.util.Optional
classes.
The Scala Option
is generic; its generic counterpart in Java is
java.util.Optional
. Option
is enriched with an asJava
method, while
Optional
is enriched with asScala
to perform conversions.
In addition, both Option
and Optional
are enriched with asPrimitive
methods that will convert generically contained primitives to the manually
specialized Java versions for primitives, OptionalDouble
, OptionalInt
,
and OptionalLong
. The primitive versions can be converted to the Scala
generic Option
with asScala
and to the Java generic Optional
with
asGeneric
.
When calling from Java, methods are more convenient than extension methods,
so toJava
and toScala
methods are provided that convert to and from
Scala's Option
. Note that toJava(toScala(x))
will result in a generic
Optional
even if x
was one of the primitive versons.
Example usage:
import scala.compat.java8.OptionConverters._ val a = Option("example").asJava // Creates java.util.Optional[String] containing "example" val b = (None: Option[String]).asJava // Creates an empty java.util.Optional[String] val c = a.asScala // Back to Option("example") val d = b.asScala // Back to None typed as Option[String] val e = Option(2.7).asJava // java.util.Optional[Double] containing boxed 2.7 val f = Option(2.7).asPrimitive // java.util.OptionalDouble containing 2.7 (not boxed) val g = f.asScala // Back to Option(2.7) val h = f.asGeneric // Same as e val i = e.asPrimitive // Same as f val j = toJava(Option("example")) // Same as a val k = toScala(a) // Same as c
- Source
- OptionConverters.scala
- Alphabetic
- By Inheritance
- OptionConverters
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- implicit final class RichOptionForJava8[A] extends AnyVal
Provides conversions from
scala.Option
to JavaOptional
types, either generic or primitive - implicit final class RichOptionalDouble extends AnyVal
Provides conversions from
java.util.OptionalDouble
to the genericOptional
and ScalaOption
- implicit final class RichOptionalGeneric[A] extends AnyVal
Provides conversions from
java.util.Optional
to ScalaOption
or primitivejava.util.Optional
types - implicit final class RichOptionalInt extends AnyVal
Provides conversions from
java.util.OptionalInt
to the genericOptional
and ScalaOption
- implicit final class RichOptionalLong extends AnyVal
Provides conversions from
java.util.OptionalLong
to the genericOptional
and ScalaOption
- sealed abstract class SpecializerOfOptions[A, That] extends AnyRef
Type class implementing conversion from generic
Option
orOptional
to manually specialized variants.
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native() @IntrinsicCandidate()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @IntrinsicCandidate()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @IntrinsicCandidate()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @IntrinsicCandidate()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @IntrinsicCandidate()
- implicit val specializer_OptionalDouble: SpecializerOfOptions[Double, OptionalDouble]
Implementation of creation of
OptionalDouble
fromOption[Double]
orOptional[Double]
- implicit val specializer_OptionalInt: SpecializerOfOptions[Int, OptionalInt]
Implementation of creation of
OptionalInt
fromOption[Int]
orOptional[Int]
- implicit val specializer_OptionalLong: SpecializerOfOptions[Long, OptionalLong]
Implementation of creation of
OptionalLong
fromOption[Long]
orOptional[Long]
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- final def toJava[A](o: Option[A]): Optional[A]
Conversion from Scala
Option
to JavaOptional
without using implicits, for convenient use from Java. - final def toScala(o: OptionalLong): Option[Long]
Conversion from Java
OptionalLong
to ScalaOption
without using implicits, for convenient use from Java - final def toScala(o: OptionalInt): Option[Int]
Conversion from Java
OptionalInt
to ScalaOption
without using implicits, for convenient use from Java - final def toScala(o: OptionalDouble): Option[Double]
Conversion from Java
OptionalDouble
to ScalaOption
without using implicits, for convenient use from Java - final def toScala[A](o: Optional[A]): Option[A]
Conversion from Java
Optional
to ScalaOption
without using implicits, for convenient use from Java - def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])