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 | Class and Description |
---|---|
static class |
OptionConverters.RichOptionalDouble
Provides conversions from `java.util.OptionalDouble` to the generic `Optional` and Scala `Option`
|
static class |
OptionConverters.RichOptionalDouble$ |
static class |
OptionConverters.RichOptionalGeneric<A>
Provides conversions from `java.util.Optional` to Scala `Option` or primitive `java.util.Optional` types
|
static class |
OptionConverters.RichOptionalGeneric$ |
static class |
OptionConverters.RichOptionalInt
Provides conversions from `java.util.OptionalInt` to the generic `Optional` and Scala `Option`
|
static class |
OptionConverters.RichOptionalInt$ |
static class |
OptionConverters.RichOptionalLong
Provides conversions from `java.util.OptionalLong` to the generic `Optional` and Scala `Option`
|
static class |
OptionConverters.RichOptionalLong$ |
static class |
OptionConverters.RichOptionForJava8<A>
Provides conversions from `scala.Option` to Java `Optional` types, either generic or primitive
|
static class |
OptionConverters.RichOptionForJava8$ |
static class |
OptionConverters.SpecializerOfOptions<A,That>
Type class implementing conversion from generic `Option` or `Optional` to manually specialized variants.
|
Constructor and Description |
---|
OptionConverters() |
Modifier and Type | Method and Description |
---|---|
static OptionConverters.SpecializerOfOptions<java.lang.Object,java.util.OptionalDouble> |
specializer_OptionalDouble()
Implementation of creation of `OptionalDouble` from `Option[Double]` or `Optional[Double]`
|
static OptionConverters.SpecializerOfOptions<java.lang.Object,java.util.OptionalInt> |
specializer_OptionalInt()
Implementation of creation of `OptionalInt` from `Option[Int]` or `Optional[Int]`
|
static OptionConverters.SpecializerOfOptions<java.lang.Object,java.util.OptionalLong> |
specializer_OptionalLong()
Implementation of creation of `OptionalLong` from `Option[Long]` or `Optional[Long]`
|
static <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.
|
static <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
|
static 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
|
static 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
|
static 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 OptionConverters.SpecializerOfOptions<java.lang.Object,java.util.OptionalDouble> specializer_OptionalDouble()
public static OptionConverters.SpecializerOfOptions<java.lang.Object,java.util.OptionalInt> specializer_OptionalInt()
public static OptionConverters.SpecializerOfOptions<java.lang.Object,java.util.OptionalLong> specializer_OptionalLong()
public static final <A> java.util.Optional<A> toJava(scala.Option<A> o)
public static final <A> scala.Option<A> toScala(java.util.Optional<A> o)
public static final scala.Option<java.lang.Object> toScala(java.util.OptionalDouble o)
public static final scala.Option<java.lang.Object> toScala(java.util.OptionalInt o)
public static final scala.Option<java.lang.Object> toScala(java.util.OptionalLong o)