public class OptionConverters$
extends java.lang.Object
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
Modifier and Type | Field and Description |
---|---|
static OptionConverters$ |
MODULE$
Static reference to the singleton instance of this Scala object.
|
Constructor and Description |
---|
OptionConverters$() |
Modifier and Type | Method and Description |
---|---|
OptionConverters.SpecializerOfOptions<java.lang.Object,java.util.OptionalDouble> |
specializer_OptionalDouble()
Implementation of creation of `OptionalDouble` from `Option[Double]` or `Optional[Double]`
|
OptionConverters.SpecializerOfOptions<java.lang.Object,java.util.OptionalInt> |
specializer_OptionalInt()
Implementation of creation of `OptionalInt` from `Option[Int]` or `Optional[Int]`
|
OptionConverters.SpecializerOfOptions<java.lang.Object,java.util.OptionalLong> |
specializer_OptionalLong()
Implementation of creation of `OptionalLong` from `Option[Long]` or `Optional[Long]`
|
<A> java.util.Optional<A> |
toJava(scala.Option<A> o)
Conversion from Scala `Option` to Java `Optional` without using implicits, for convenient use from Java.
|
<A> scala.Option<A> |
toScala(java.util.Optional<A> o)
Conversion from Java `Optional` to Scala `Option` without using implicits, for convenient use from Java
|
scala.Option<java.lang.Object> |
toScala(java.util.OptionalDouble o)
Conversion from Java `OptionalDouble` to Scala `Option` without using implicits, for convenient use from Java
|
scala.Option<java.lang.Object> |
toScala(java.util.OptionalInt o)
Conversion from Java `OptionalInt` to Scala `Option` without using implicits, for convenient use from Java
|
scala.Option<java.lang.Object> |
toScala(java.util.OptionalLong o)
Conversion from Java `OptionalLong` to Scala `Option` without using implicits, for convenient use from Java
|
public static final OptionConverters$ MODULE$
public OptionConverters.SpecializerOfOptions<java.lang.Object,java.util.OptionalDouble> specializer_OptionalDouble()
public OptionConverters.SpecializerOfOptions<java.lang.Object,java.util.OptionalInt> specializer_OptionalInt()
public OptionConverters.SpecializerOfOptions<java.lang.Object,java.util.OptionalLong> specializer_OptionalLong()
public final <A> java.util.Optional<A> toJava(scala.Option<A> o)
public final <A> scala.Option<A> toScala(java.util.Optional<A> o)
public final scala.Option<java.lang.Object> toScala(java.util.OptionalDouble o)
public final scala.Option<java.lang.Object> toScala(java.util.OptionalInt o)
public final scala.Option<java.lang.Object> toScala(java.util.OptionalLong o)