Packages

package java8

Ordering
  1. Alphabetic
Visibility
  1. Public
  2. Protected

Package Members

  1. package converterImpl
  2. package wrappers

Type Members

  1. class ScalaStreamSupport extends AnyRef

    This class contains static utility methods for creating Java Streams from Scala Collections, similar to the methods in java.util.stream.StreamSupport for other Java types.

    This class contains static utility methods for creating Java Streams from Scala Collections, similar to the methods in java.util.stream.StreamSupport for other Java types. It is intended for use from Java code. In Scala code, you can use the extension methods provided by scala.compat.java8.StreamConverters instead.

    Streams created from immutable Scala collections are also immutable. Mutable collections should not be modified concurrently. There are no guarantees for success or failure modes of existing streams in case of concurrent modifications.

  2. trait StreamExtensions extends AnyRef

    Defines extension methods to create Java Streams for Scala collections, available through scala.compat.java8.StreamConverters.

  3. trait WrappedAsJava[J] extends AnyRef

    A trait that indicates that the class is or can be converted to a Java version by wrapping a Scala class

  4. trait WrappedAsScala[S] extends AnyRef

    A trait that indicates that the class is or can be converted to a Scala version by wrapping a Java class

Value Members

  1. object DurationConverters

    This class contains static methods which convert between Java Durations and the durations from the Scala concurrency package.

    This class contains static methods which convert between Java Durations and the durations from the Scala concurrency package. This is useful when mediating between Scala and Java libraries with asynchronous APIs where timeouts for example are often expressed as durations.

  2. object FutureConverters

    This class contains static methods which convert between Java CompletionStage and Scala Future.

    This class contains static methods which convert between Java CompletionStage and Scala Future. This is useful when mediating between Scala and Java libraries with asynchronous APIs.

    Note that the bridge is implemented at the read-only side of asynchronous handles, namely scala.concurrent.Future instead of scala.concurrent.Promise and CompletionStage instead of CompletableFuture. This is intentional, as the semantics of bridging the write-handles would be prone to race conditions; if both ends (CompletableFuture and Promise) are completed independently at the same time, they may contain different values afterwards. For this reason, toCompletableFuture() is not supported on the created CompletionStages.

    Example usage:

    import java.util.concurrent.CompletionStage;
    import scala.concurrent.Future;
    import static scala.concurrent.java8.FutureConverters.*;
    
    final CompletionStage<String> cs = ... // from an async Java API
    final Future<String> f = toScala(cs);
    ...
    final Future<Integer> f2 = ... // from an async Scala API
    final CompletionStage<Integer> cs2 = toJava(f2);
  3. 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
  4. object PrimitiveIteratorConverters

    This class enables conversion from scala.Iterator to the set of java.util.PrimitiveIterator classes.

    This class enables conversion from scala.Iterator to the set of java.util.PrimitiveIterator classes.

    Scala's Iterator is generic, as is its java.util counterpart. However, java.util.PrimitiveIterator offers three manually-specialized variants of Iterator: OfDouble, OfInt, and OfLong. This class provides .asPrimitive extension methods for Scala and Java iterators to present the generic versions as the specialized version.

    Example usage:

    import scala.compat.java8.PrimitiveIteratorConverters._
    val it = Iterator(1.0, 2.0, math.Pi)
    val jpid = it.asPrimitive   // PrimitiveIterator.OfDouble
  5. object StreamConverters extends StreamExtensions with Priority1AccumulatorConverters

    StreamConverters provides extension methods and other functionality to ease interoperability of Scala collections with java.util.stream classes.

    StreamConverters provides extension methods and other functionality to ease interoperability of Scala collections with java.util.stream classes.

    Scala collections gain extension methods seqStream and parStream that allow them to be used as the source of a Stream. Some collections either intrinsically cannot be paralellized, or could be but an efficient implementation is missing. It this case, only seqStream is provided. If a collection cannot be stepped over at all (e.g. Traversable), then it gains neither method.

    Array also gains seqStream and parStream methods, and calling those on Array[Double], Array[Int], or Array[Long] will produce the corresponding primitive stream.

    Streams gain accumulate and toScala[_] methods, which collect the stream into a custom high-performance scala.collection.mutable.java8.Accumulator, which is not part of the standard collections hierarchy, or into a named Scala collection, respectively.

    Generic streams also gain an unboxed method that will convert to the corresponding unboxed primitive stream, if appropriate. Unboxed streams have custom accumulators with improved performance.

    Accumulators have toArray, toList, iterator, and to[_] methods to convert to standard Scala collections. Note that if you wish to create an array from a Stream, going through an Accumulator is not the most efficient option: just create the Array directly.

    Internally, Scala collections implement a hybrid of Iterator and java.util.Spliterator to implement Stream compatibility; these are called Steppers. In particular, they can test for the presence of a next element using hasStep, can retrieve the next value with nextStep, or can optionally retrieve and operate on a value if present with tryStep, which works like tryAdvance in java.util.Spliterator.

    Every Scala collection that can be stepped through has a stepper method implicitly provided. In addition, maps have keyStepper and valueStepper methods. A limited number of collections operations are defined on Steppers, including conversion to Scala collections with to or accumulation via accumulate. Steppers also implement seqStream and parStream to generate Streams. These are provided regardless of whether a Stepper can efficiently subdivide itself for parallel processing (though one can check for the presence of the EfficientSubstep trait to know that parallel execution will not be limited by long sequential searching steps, and one can call anticipateParallelism to warn a Stepper that it will be used in a parallel context and thus may wish to make different tradeoffs).

    Examples:

    import scala.compat.java8.StreamConverters._
    
    val s = Vector(1,2,3,4).parStream    // Stream[Int]
    val si = s.unboxed                   // Stream.OfInt
    val ai = si.accumulate               // IntAccumulator
    val v = ai.to[Vector]                // Vector[Int] again
    
    val t = Array(2.0, 3.0, 4.0).parStream               // DoubleStream
    val q = t.toScala[scala.collection.immutable.Queue]  // Queue[Double]
    
    val x = List(1L, 2L, 3L, 4L).stepper.parStream.sum   // 10, potentially computed in parallel

Ungrouped