package compat

Ordering
  1. Alphabetic
Visibility
  1. Public
  2. All

Value Members

  1. object OptionConverters

    This class enables bidirectional conversion between scala.Option and the set of java.util.Optional classes.

    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

Ungrouped