diff options
Diffstat (limited to 'src/library/scala')
61 files changed, 471 insertions, 225 deletions
diff --git a/src/library/scala/Array.scala b/src/library/scala/Array.scala index d89e9d291d..7f3200b90a 100644 --- a/src/library/scala/Array.scala +++ b/src/library/scala/Array.scala @@ -486,6 +486,37 @@ object Array extends FallbackArrayBuilding { * @see [[http://www.scala-lang.org/files/archive/spec/2.11/ Scala Language Specification]], for in-depth information on the transformations the Scala compiler makes on Arrays (Sections 6.6 and 6.15 respectively.) * @see [[http://docs.scala-lang.org/sips/completed/scala-2-8-arrays.html "Scala 2.8 Arrays"]] the Scala Improvement Document detailing arrays since Scala 2.8. * @see [[http://docs.scala-lang.org/overviews/collections/arrays.html "The Scala 2.8 Collections' API"]] section on `Array` by Martin Odersky for more information. + * @hideImplicitConversion scala.Predef.booleanArrayOps + * @hideImplicitConversion scala.Predef.byteArrayOps + * @hideImplicitConversion scala.Predef.charArrayOps + * @hideImplicitConversion scala.Predef.doubleArrayOps + * @hideImplicitConversion scala.Predef.floatArrayOps + * @hideImplicitConversion scala.Predef.intArrayOps + * @hideImplicitConversion scala.Predef.longArrayOps + * @hideImplicitConversion scala.Predef.refArrayOps + * @hideImplicitConversion scala.Predef.shortArrayOps + * @hideImplicitConversion scala.Predef.unitArrayOps + * @hideImplicitConversion scala.Predef._booleanArrayOps + * @hideImplicitConversion scala.Predef._byteArrayOps + * @hideImplicitConversion scala.Predef._charArrayOps + * @hideImplicitConversion scala.Predef._doubleArrayOps + * @hideImplicitConversion scala.Predef._floatArrayOps + * @hideImplicitConversion scala.Predef._intArrayOps + * @hideImplicitConversion scala.Predef._longArrayOps + * @hideImplicitConversion scala.Predef._refArrayOps + * @hideImplicitConversion scala.Predef._shortArrayOps + * @hideImplicitConversion scala.Predef._unitArrayOps + * @hideImplicitConversion scala.LowPriorityImplicits.wrapRefArray + * @hideImplicitConversion scala.LowPriorityImplicits.wrapIntArray + * @hideImplicitConversion scala.LowPriorityImplicits.wrapDoubleArray + * @hideImplicitConversion scala.LowPriorityImplicits.wrapLongArray + * @hideImplicitConversion scala.LowPriorityImplicits.wrapFloatArray + * @hideImplicitConversion scala.LowPriorityImplicits.wrapCharArray + * @hideImplicitConversion scala.LowPriorityImplicits.wrapByteArray + * @hideImplicitConversion scala.LowPriorityImplicits.wrapShortArray + * @hideImplicitConversion scala.LowPriorityImplicits.wrapBooleanArray + * @hideImplicitConversion scala.LowPriorityImplicits.wrapUnitArray + * @hideImplicitConversion scala.LowPriorityImplicits.genericWrapArray * @define coll array * @define Coll `Array` * @define orderDependent diff --git a/src/library/scala/Byte.scala b/src/library/scala/Byte.scala index 413231c0d1..fb662911b3 100644 --- a/src/library/scala/Byte.scala +++ b/src/library/scala/Byte.scala @@ -79,8 +79,8 @@ final abstract class Byte private extends AnyVal { */ def >>>(x: Long): Int /** - * Returns this value bit-shifted left by the specified number of bits, - * filling in the right bits with the same value as the left-most bit of this. + * Returns this value bit-shifted right by the specified number of bits, + * filling in the left bits with the same value as the left-most bit of this. * The effect of this is to retain the sign of the value. * @example {{{ * -21 >> 3 == -3 @@ -90,8 +90,8 @@ final abstract class Byte private extends AnyVal { */ def >>(x: Int): Int /** - * Returns this value bit-shifted left by the specified number of bits, - * filling in the right bits with the same value as the left-most bit of this. + * Returns this value bit-shifted right by the specified number of bits, + * filling in the left bits with the same value as the left-most bit of this. * The effect of this is to retain the sign of the value. * @example {{{ * -21 >> 3 == -3 diff --git a/src/library/scala/Char.scala b/src/library/scala/Char.scala index ec2d48c181..9f06503569 100644 --- a/src/library/scala/Char.scala +++ b/src/library/scala/Char.scala @@ -79,8 +79,8 @@ final abstract class Char private extends AnyVal { */ def >>>(x: Long): Int /** - * Returns this value bit-shifted left by the specified number of bits, - * filling in the right bits with the same value as the left-most bit of this. + * Returns this value bit-shifted right by the specified number of bits, + * filling in the left bits with the same value as the left-most bit of this. * The effect of this is to retain the sign of the value. * @example {{{ * -21 >> 3 == -3 @@ -90,8 +90,8 @@ final abstract class Char private extends AnyVal { */ def >>(x: Int): Int /** - * Returns this value bit-shifted left by the specified number of bits, - * filling in the right bits with the same value as the left-most bit of this. + * Returns this value bit-shifted right by the specified number of bits, + * filling in the left bits with the same value as the left-most bit of this. * The effect of this is to retain the sign of the value. * @example {{{ * -21 >> 3 == -3 diff --git a/src/library/scala/Int.scala b/src/library/scala/Int.scala index 72e5ebf81b..3bd3775eba 100644 --- a/src/library/scala/Int.scala +++ b/src/library/scala/Int.scala @@ -79,8 +79,8 @@ final abstract class Int private extends AnyVal { */ def >>>(x: Long): Int /** - * Returns this value bit-shifted left by the specified number of bits, - * filling in the right bits with the same value as the left-most bit of this. + * Returns this value bit-shifted right by the specified number of bits, + * filling in the left bits with the same value as the left-most bit of this. * The effect of this is to retain the sign of the value. * @example {{{ * -21 >> 3 == -3 @@ -90,8 +90,8 @@ final abstract class Int private extends AnyVal { */ def >>(x: Int): Int /** - * Returns this value bit-shifted left by the specified number of bits, - * filling in the right bits with the same value as the left-most bit of this. + * Returns this value bit-shifted right by the specified number of bits, + * filling in the left bits with the same value as the left-most bit of this. * The effect of this is to retain the sign of the value. * @example {{{ * -21 >> 3 == -3 diff --git a/src/library/scala/Long.scala b/src/library/scala/Long.scala index 1bd0fe88b1..b27a66647f 100644 --- a/src/library/scala/Long.scala +++ b/src/library/scala/Long.scala @@ -79,8 +79,8 @@ final abstract class Long private extends AnyVal { */ def >>>(x: Long): Long /** - * Returns this value bit-shifted left by the specified number of bits, - * filling in the right bits with the same value as the left-most bit of this. + * Returns this value bit-shifted right by the specified number of bits, + * filling in the left bits with the same value as the left-most bit of this. * The effect of this is to retain the sign of the value. * @example {{{ * -21 >> 3 == -3 @@ -90,8 +90,8 @@ final abstract class Long private extends AnyVal { */ def >>(x: Int): Long /** - * Returns this value bit-shifted left by the specified number of bits, - * filling in the right bits with the same value as the left-most bit of this. + * Returns this value bit-shifted right by the specified number of bits, + * filling in the left bits with the same value as the left-most bit of this. * The effect of this is to retain the sign of the value. * @example {{{ * -21 >> 3 == -3 diff --git a/src/library/scala/Product1.scala b/src/library/scala/Product1.scala index dbc34ba66a..e82300adf6 100644 --- a/src/library/scala/Product1.scala +++ b/src/library/scala/Product1.scala @@ -24,7 +24,7 @@ trait Product1[@specialized(Int, Long, Double) +T1] extends Any with Product { override def productArity = 1 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product10.scala b/src/library/scala/Product10.scala index 70de79d49a..5fc4874048 100644 --- a/src/library/scala/Product10.scala +++ b/src/library/scala/Product10.scala @@ -24,7 +24,7 @@ trait Product10[+T1, +T2, +T3, +T4, +T5, +T6, +T7, +T8, +T9, +T10] extends Any w override def productArity = 10 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product11.scala b/src/library/scala/Product11.scala index 1bb79ac017..dcebc90e3e 100644 --- a/src/library/scala/Product11.scala +++ b/src/library/scala/Product11.scala @@ -24,7 +24,7 @@ trait Product11[+T1, +T2, +T3, +T4, +T5, +T6, +T7, +T8, +T9, +T10, +T11] extends override def productArity = 11 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product12.scala b/src/library/scala/Product12.scala index d7e1e1b05c..2221170452 100644 --- a/src/library/scala/Product12.scala +++ b/src/library/scala/Product12.scala @@ -24,7 +24,7 @@ trait Product12[+T1, +T2, +T3, +T4, +T5, +T6, +T7, +T8, +T9, +T10, +T11, +T12] e override def productArity = 12 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product13.scala b/src/library/scala/Product13.scala index 8571b45a40..e76f326766 100644 --- a/src/library/scala/Product13.scala +++ b/src/library/scala/Product13.scala @@ -24,7 +24,7 @@ trait Product13[+T1, +T2, +T3, +T4, +T5, +T6, +T7, +T8, +T9, +T10, +T11, +T12, + override def productArity = 13 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product14.scala b/src/library/scala/Product14.scala index a2f5140370..a076e2cc7a 100644 --- a/src/library/scala/Product14.scala +++ b/src/library/scala/Product14.scala @@ -24,7 +24,7 @@ trait Product14[+T1, +T2, +T3, +T4, +T5, +T6, +T7, +T8, +T9, +T10, +T11, +T12, + override def productArity = 14 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product15.scala b/src/library/scala/Product15.scala index 1c6ad0011c..4568aff1fe 100644 --- a/src/library/scala/Product15.scala +++ b/src/library/scala/Product15.scala @@ -24,7 +24,7 @@ trait Product15[+T1, +T2, +T3, +T4, +T5, +T6, +T7, +T8, +T9, +T10, +T11, +T12, + override def productArity = 15 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product16.scala b/src/library/scala/Product16.scala index f03b0b34a2..84dccb0ac8 100644 --- a/src/library/scala/Product16.scala +++ b/src/library/scala/Product16.scala @@ -24,7 +24,7 @@ trait Product16[+T1, +T2, +T3, +T4, +T5, +T6, +T7, +T8, +T9, +T10, +T11, +T12, + override def productArity = 16 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product17.scala b/src/library/scala/Product17.scala index 72df1b496a..0d50898bf4 100644 --- a/src/library/scala/Product17.scala +++ b/src/library/scala/Product17.scala @@ -24,7 +24,7 @@ trait Product17[+T1, +T2, +T3, +T4, +T5, +T6, +T7, +T8, +T9, +T10, +T11, +T12, + override def productArity = 17 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product18.scala b/src/library/scala/Product18.scala index 0402f90a01..9b32265d71 100644 --- a/src/library/scala/Product18.scala +++ b/src/library/scala/Product18.scala @@ -24,7 +24,7 @@ trait Product18[+T1, +T2, +T3, +T4, +T5, +T6, +T7, +T8, +T9, +T10, +T11, +T12, + override def productArity = 18 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product19.scala b/src/library/scala/Product19.scala index b9770db47b..fe6b95669b 100644 --- a/src/library/scala/Product19.scala +++ b/src/library/scala/Product19.scala @@ -24,7 +24,7 @@ trait Product19[+T1, +T2, +T3, +T4, +T5, +T6, +T7, +T8, +T9, +T10, +T11, +T12, + override def productArity = 19 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product2.scala b/src/library/scala/Product2.scala index a43a4a285c..091bcc89de 100644 --- a/src/library/scala/Product2.scala +++ b/src/library/scala/Product2.scala @@ -24,7 +24,7 @@ trait Product2[@specialized(Int, Long, Double) +T1, @specialized(Int, Long, Doub override def productArity = 2 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product20.scala b/src/library/scala/Product20.scala index 7b0df201ec..81315e3558 100644 --- a/src/library/scala/Product20.scala +++ b/src/library/scala/Product20.scala @@ -24,7 +24,7 @@ trait Product20[+T1, +T2, +T3, +T4, +T5, +T6, +T7, +T8, +T9, +T10, +T11, +T12, + override def productArity = 20 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product21.scala b/src/library/scala/Product21.scala index f81347aac0..b5967c06e1 100644 --- a/src/library/scala/Product21.scala +++ b/src/library/scala/Product21.scala @@ -24,7 +24,7 @@ trait Product21[+T1, +T2, +T3, +T4, +T5, +T6, +T7, +T8, +T9, +T10, +T11, +T12, + override def productArity = 21 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product22.scala b/src/library/scala/Product22.scala index 7a25891c6e..c7b9da5ce8 100644 --- a/src/library/scala/Product22.scala +++ b/src/library/scala/Product22.scala @@ -24,7 +24,7 @@ trait Product22[+T1, +T2, +T3, +T4, +T5, +T6, +T7, +T8, +T9, +T10, +T11, +T12, + override def productArity = 22 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product3.scala b/src/library/scala/Product3.scala index 9976240935..7154bf5bdf 100644 --- a/src/library/scala/Product3.scala +++ b/src/library/scala/Product3.scala @@ -24,7 +24,7 @@ trait Product3[+T1, +T2, +T3] extends Any with Product { override def productArity = 3 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product4.scala b/src/library/scala/Product4.scala index d6c1543390..046f8c7a7c 100644 --- a/src/library/scala/Product4.scala +++ b/src/library/scala/Product4.scala @@ -24,7 +24,7 @@ trait Product4[+T1, +T2, +T3, +T4] extends Any with Product { override def productArity = 4 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product5.scala b/src/library/scala/Product5.scala index 5f1b11a30d..3e952c8c55 100644 --- a/src/library/scala/Product5.scala +++ b/src/library/scala/Product5.scala @@ -24,7 +24,7 @@ trait Product5[+T1, +T2, +T3, +T4, +T5] extends Any with Product { override def productArity = 5 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product6.scala b/src/library/scala/Product6.scala index efd9408d73..010c68711a 100644 --- a/src/library/scala/Product6.scala +++ b/src/library/scala/Product6.scala @@ -24,7 +24,7 @@ trait Product6[+T1, +T2, +T3, +T4, +T5, +T6] extends Any with Product { override def productArity = 6 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product7.scala b/src/library/scala/Product7.scala index fab0a997a1..24e5a5c05a 100644 --- a/src/library/scala/Product7.scala +++ b/src/library/scala/Product7.scala @@ -24,7 +24,7 @@ trait Product7[+T1, +T2, +T3, +T4, +T5, +T6, +T7] extends Any with Product { override def productArity = 7 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product8.scala b/src/library/scala/Product8.scala index 41391f7050..4a9f65b00e 100644 --- a/src/library/scala/Product8.scala +++ b/src/library/scala/Product8.scala @@ -24,7 +24,7 @@ trait Product8[+T1, +T2, +T3, +T4, +T5, +T6, +T7, +T8] extends Any with Product override def productArity = 8 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Product9.scala b/src/library/scala/Product9.scala index e22538e1ee..9af11f709a 100644 --- a/src/library/scala/Product9.scala +++ b/src/library/scala/Product9.scala @@ -24,7 +24,7 @@ trait Product9[+T1, +T2, +T3, +T4, +T5, +T6, +T7, +T8, +T9] extends Any with Pro override def productArity = 9 - /** Returns the n-th projection of this product if 0 < n <= productArity, + /** Returns the n-th projection of this product if 0 <= n < productArity, * otherwise throws an `IndexOutOfBoundsException`. * * @param n number of the projection to be returned diff --git a/src/library/scala/Short.scala b/src/library/scala/Short.scala index 36b9ec4df9..2cbbf3cc59 100644 --- a/src/library/scala/Short.scala +++ b/src/library/scala/Short.scala @@ -79,8 +79,8 @@ final abstract class Short private extends AnyVal { */ def >>>(x: Long): Int /** - * Returns this value bit-shifted left by the specified number of bits, - * filling in the right bits with the same value as the left-most bit of this. + * Returns this value bit-shifted right by the specified number of bits, + * filling in the left bits with the same value as the left-most bit of this. * The effect of this is to retain the sign of the value. * @example {{{ * -21 >> 3 == -3 @@ -90,8 +90,8 @@ final abstract class Short private extends AnyVal { */ def >>(x: Int): Int /** - * Returns this value bit-shifted left by the specified number of bits, - * filling in the right bits with the same value as the left-most bit of this. + * Returns this value bit-shifted right by the specified number of bits, + * filling in the left bits with the same value as the left-most bit of this. * The effect of this is to retain the sign of the value. * @example {{{ * -21 >> 3 == -3 diff --git a/src/library/scala/collection/GenTraversableOnce.scala b/src/library/scala/collection/GenTraversableOnce.scala index 244ff26397..4af2ca23be 100644 --- a/src/library/scala/collection/GenTraversableOnce.scala +++ b/src/library/scala/collection/GenTraversableOnce.scala @@ -67,6 +67,23 @@ trait GenTraversableOnce[+A] extends Any { */ def foreach[U](f: A => U): Unit + /** Tests whether this $coll is known to have a finite size. + * All strict collections are known to have finite size. For a non-strict + * collection such as `Stream`, the predicate returns `'''true'''` if all + * elements have been computed. It returns `'''false'''` if the stream is + * not yet evaluated to the end. Non-empty Iterators usually return + * `'''false'''` even if they were created from a collection with a known + * finite size. + * + * Note: many collection methods will not work on collections of infinite sizes. + * The typical failure mode is an infinite loop. These methods always attempt a + * traversal without checking first that `hasDefiniteSize` returns `'''true'''`. + * However, checking `hasDefiniteSize` can provide an assurance that size is + * well-defined and non-termination is not a concern. + * + * @return `'''true'''` if this collection is known to have finite size, + * `'''false'''` otherwise. + */ def hasDefiniteSize: Boolean def seq: TraversableOnce[A] @@ -81,6 +98,9 @@ trait GenTraversableOnce[+A] extends Any { /** Tests whether the $coll is empty. * + * Note: Implementations in subclasses that are not repeatedly traversable must take + * care not to consume any elements when `isEmpty` is called. + * * @return `true` if the $coll contains no elements, `false` otherwise. */ def isEmpty: Boolean diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala index 17bb83e52e..518bba6b6d 100644 --- a/src/library/scala/collection/Iterator.scala +++ b/src/library/scala/collection/Iterator.scala @@ -10,7 +10,7 @@ package scala package collection import mutable.ArrayBuffer -import scala.annotation.migration +import scala.annotation.{tailrec, migration} import immutable.Stream /** The `Iterator` object provides various functions for creating specialized iterators. @@ -166,8 +166,10 @@ object Iterator { private[scala] final class ConcatIterator[+A](private[this] var current: Iterator[A], initial: Vector[() => Iterator[A]]) extends Iterator[A] { @deprecated def this(initial: Vector[() => Iterator[A]]) = this(Iterator.empty, initial) // for binary compatibility private[this] var queue: Vector[() => Iterator[A]] = initial + private[this] var currentHasNextChecked = false // Advance current to the next non-empty iterator // current is set to null when all iterators are exhausted + @tailrec private[this] def advance(): Boolean = { if (queue.isEmpty) { current = null @@ -176,20 +178,57 @@ object Iterator { else { current = queue.head() queue = queue.tail - current.hasNext || advance() + if (current.hasNext) { + currentHasNextChecked = true + true + } else advance() } } - def hasNext = (current ne null) && (current.hasNext || advance()) - def next() = if (hasNext) current.next() else Iterator.empty.next() + def hasNext = + if (currentHasNextChecked) true + else if (current eq null) false + else if (current.hasNext) { + currentHasNextChecked = true + true + } else advance() + def next() = + if (hasNext) { + currentHasNextChecked = false + current.next() + } else Iterator.empty.next() override def ++[B >: A](that: => GenTraversableOnce[B]): Iterator[B] = new ConcatIterator(current, queue :+ (() => that.toIterator)) } private[scala] final class JoinIterator[+A](lhs: Iterator[A], that: => GenTraversableOnce[A]) extends Iterator[A] { + private[this] var state = 0 // 0: lhs not checked, 1: lhs has next, 2: switched to rhs private[this] lazy val rhs: Iterator[A] = that.toIterator - def hasNext = lhs.hasNext || rhs.hasNext - def next() = if (lhs.hasNext) lhs.next() else rhs.next() + def hasNext = state match { + case 0 => + if (lhs.hasNext) { + state = 1 + true + } else { + state = 2 + rhs.hasNext + } + case 1 => true + case _ => rhs.hasNext + } + def next() = state match { + case 0 => + if (lhs.hasNext) lhs.next() + else { + state = 2 + rhs.next() + } + case 1 => + state = 0 + lhs.next() + case _ => + rhs.next() + } override def ++[B >: A](that: => GenTraversableOnce[B]) = new ConcatIterator(this, Vector(() => that.toIterator)) diff --git a/src/library/scala/collection/JavaConverters.scala b/src/library/scala/collection/JavaConverters.scala index 875f6e1c02..86e86d4584 100644 --- a/src/library/scala/collection/JavaConverters.scala +++ b/src/library/scala/collection/JavaConverters.scala @@ -52,7 +52,10 @@ import convert._ * - `scala.collection.Set` => `java.util.Set` * - `scala.collection.Map` => `java.util.Map` * - * @author Martin Odersky + * The following one way conversion is provided via `asScala`: + * + * - `java.util.Properties` => `scala.collection.mutable.Map` + * * @since 2.8.1 */ object JavaConverters extends DecorateAsJava with DecorateAsScala diff --git a/src/library/scala/collection/Searching.scala b/src/library/scala/collection/Searching.scala index b68124b3f8..25e8b5e253 100644 --- a/src/library/scala/collection/Searching.scala +++ b/src/library/scala/collection/Searching.scala @@ -36,12 +36,12 @@ object Searching { class SearchImpl[A, Repr](val coll: SeqLike[A, Repr]) { /** Search the sorted sequence for a specific element. If the sequence is an - * `IndexedSeq`, a binary search is used. Otherwise, a linear search is used. + * `IndexedSeqLike`, a binary search is used. Otherwise, a linear search is used. * * The sequence should be sorted with the same `Ordering` before calling; otherwise, * the results are undefined. * - * @see [[scala.collection.IndexedSeq]] + * @see [[scala.collection.IndexedSeqLike]] * @see [[scala.math.Ordering]] * @see [[scala.collection.SeqLike]], method `sorted` * @@ -54,18 +54,18 @@ object Searching { */ final def search[B >: A](elem: B)(implicit ord: Ordering[B]): SearchResult = coll match { - case _: IndexedSeq[A] => binarySearch(elem, 0, coll.length)(ord) + case _: IndexedSeqLike[A, Repr] => binarySearch(elem, 0, coll.length)(ord) case _ => linearSearch(coll.view, elem, 0)(ord) } /** Search within an interval in the sorted sequence for a specific element. If the - * sequence is an IndexedSeq, a binary search is used. Otherwise, a linear search + * sequence is an `IndexedSeqLike`, a binary search is used. Otherwise, a linear search * is used. * * The sequence should be sorted with the same `Ordering` before calling; otherwise, * the results are undefined. * - * @see [[scala.collection.IndexedSeq]] + * @see [[scala.collection.IndexedSeqLike]] * @see [[scala.math.Ordering]] * @see [[scala.collection.SeqLike]], method `sorted` * @@ -81,7 +81,7 @@ object Searching { final def search[B >: A](elem: B, from: Int, to: Int) (implicit ord: Ordering[B]): SearchResult = coll match { - case _: IndexedSeq[A] => binarySearch(elem, from, to)(ord) + case _: IndexedSeqLike[A, Repr] => binarySearch(elem, from, to)(ord) case _ => linearSearch(coll.view(from, to), elem, from)(ord) } diff --git a/src/library/scala/collection/TraversableLike.scala b/src/library/scala/collection/TraversableLike.scala index fa9a3a7482..d914f2e0ff 100644 --- a/src/library/scala/collection/TraversableLike.scala +++ b/src/library/scala/collection/TraversableLike.scala @@ -138,17 +138,6 @@ trait TraversableLike[+A, +Repr] extends Any result } - /** Tests whether this $coll is known to have a finite size. - * All strict collections are known to have finite size. For a non-strict - * collection such as `Stream`, the predicate returns `'''true'''` if all - * elements have been computed. It returns `'''false'''` if the stream is - * not yet evaluated to the end. - * - * Note: many collection methods will not work on collections of infinite sizes. - * - * @return `'''true'''` if this collection is known to have finite size, - * `'''false'''` otherwise. - */ def hasDefiniteSize = true def ++[B >: A, That](that: GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = { diff --git a/src/library/scala/collection/TraversableOnce.scala b/src/library/scala/collection/TraversableOnce.scala index 41362e8dd7..b87fcd166e 100644 --- a/src/library/scala/collection/TraversableOnce.scala +++ b/src/library/scala/collection/TraversableOnce.scala @@ -38,9 +38,10 @@ import scala.reflect.ClassTag * `Traversables`, such as folds, conversions, and other operations which * traverse some or all of the elements and return a derived value. * Directly subclassing `TraversableOnce` is not recommended - instead, - * consider declaring an `Iterator` with a `next` and `hasNext` method, - * creating an `Iterator` with one of the methods on the `Iterator` object, - * or declaring a subclass of `Traversable`. + * consider declaring an `Iterator` with a `next` and `hasNext` method or + * creating an `Iterator` with one of the methods on the `Iterator` object. + * Consider declaring a subclass of `Traversable` instead if the elements + * can be traversed repeatedly. * * @define coll traversable or iterator * @define orderDependent @@ -61,8 +62,8 @@ import scala.reflect.ClassTag trait TraversableOnce[+A] extends Any with GenTraversableOnce[A] { self => + //TODO 2.12: Remove these methods. They are already defined in GenTraversableOnce /* Self-documenting abstract methods. */ - def foreach[U](f: A => U): Unit def isEmpty: Boolean def hasDefiniteSize: Boolean diff --git a/src/library/scala/collection/convert/DecorateAsJava.scala b/src/library/scala/collection/convert/DecorateAsJava.scala index 6658b6feea..e6aa5da067 100644 --- a/src/library/scala/collection/convert/DecorateAsJava.scala +++ b/src/library/scala/collection/convert/DecorateAsJava.scala @@ -16,7 +16,7 @@ import WrapAsJava._ import scala.language.implicitConversions -/** A collection of decorators that allow to convert between +/** A collection of decorators that allow converting between * Scala and Java collections using `asScala` and `asJava` methods. * * The following conversions are supported via `asJava`, `asScala` @@ -38,8 +38,8 @@ import scala.language.implicitConversions * val sl2 : scala.collection.mutable.Buffer[Int] = jl.asScala * assert(sl eq sl2) * }}} - * The following conversions also are supported, but the - * direction Scala to Java is done my a more specifically named method: + * The following conversions are also supported, but the + * direction from Scala to Java is done by the more specifically named methods: * `asJavaCollection`, `asJavaEnumeration`, `asJavaDictionary`. * * - `scala.collection.Iterable` <=> `java.util.Collection` @@ -53,10 +53,12 @@ import scala.language.implicitConversions * - `scala.collection.Set` => `java.util.Set` * - `scala.collection.Map` => `java.util.Map` * - * @author Martin Odersky + * The following one way conversion is provided via `asScala`: + * + * - `java.util.Properties` => `scala.collection.mutable.Map` + * * @since 2.8.1 */ - trait DecorateAsJava { /** * Adds an `asJava` method that implicitly converts a Scala `Iterator` to a diff --git a/src/library/scala/collection/immutable/ListSet.scala b/src/library/scala/collection/immutable/ListSet.scala index a65e25ed6e..98b91f7c84 100644 --- a/src/library/scala/collection/immutable/ListSet.scala +++ b/src/library/scala/collection/immutable/ListSet.scala @@ -12,7 +12,7 @@ package immutable import generic._ import scala.annotation.tailrec -import mutable.Builder +import mutable.{Builder, ReusableBuilder} /** $factoryInfo * @define Coll immutable.ListSet @@ -32,8 +32,10 @@ object ListSet extends ImmutableSetFactory[ListSet] { * a time to a list backed set puts the "squared" in N^2. There is a * temporary space cost, but it's improbable a list backed set could * become large enough for this to matter given its pricy element lookup. + * + * This builder is reusable. */ - class ListSetBuilder[Elem](initial: ListSet[Elem]) extends Builder[Elem, ListSet[Elem]] { + class ListSetBuilder[Elem](initial: ListSet[Elem]) extends ReusableBuilder[Elem, ListSet[Elem]] { def this() = this(empty[Elem]) protected val elems = (new mutable.ListBuffer[Elem] ++= initial).reverse protected val seen = new mutable.HashSet[Elem] ++= initial diff --git a/src/library/scala/collection/immutable/Vector.scala b/src/library/scala/collection/immutable/Vector.scala index cd2d3f843b..539ae9c387 100644 --- a/src/library/scala/collection/immutable/Vector.scala +++ b/src/library/scala/collection/immutable/Vector.scala @@ -13,7 +13,7 @@ package immutable import scala.annotation.unchecked.uncheckedVariance import scala.compat.Platform import scala.collection.generic._ -import scala.collection.mutable.Builder +import scala.collection.mutable.{Builder, ReusableBuilder} import scala.collection.parallel.immutable.ParVector /** Companion object to the Vector class @@ -156,7 +156,7 @@ override def companion: GenericCompanion[Vector] = Vector override def take(n: Int): Vector[A] = { if (n <= 0) Vector.empty - else if (startIndex + n < endIndex) + else if (startIndex < endIndex - n) dropBack0(startIndex + n) else this @@ -165,7 +165,7 @@ override def companion: GenericCompanion[Vector] = Vector override def drop(n: Int): Vector[A] = { if (n <= 0) this - else if (startIndex + n < endIndex) + else if (startIndex < endIndex - n) dropFront0(startIndex + n) else Vector.empty @@ -704,8 +704,8 @@ extends AbstractIterator[A] } } - -final class VectorBuilder[A]() extends Builder[A,Vector[A]] with VectorPointer[A @uncheckedVariance] { +/** A class to build instances of `Vector`. This builder is reusable. */ +final class VectorBuilder[A]() extends ReusableBuilder[A,Vector[A]] with VectorPointer[A @uncheckedVariance] { // possible alternative: start with display0 = null, blockIndex = -32, lo = 32 // to avoid allocating initial array if the result will be empty anyways diff --git a/src/library/scala/collection/mutable/AnyRefMap.scala b/src/library/scala/collection/mutable/AnyRefMap.scala index af7600ad3d..2ed5bbea60 100644 --- a/src/library/scala/collection/mutable/AnyRefMap.scala +++ b/src/library/scala/collection/mutable/AnyRefMap.scala @@ -427,7 +427,11 @@ object AnyRefMap { def apply(): AnyRefMapBuilder[J, U] = new AnyRefMapBuilder[J, U] } - final class AnyRefMapBuilder[K <: AnyRef, V] extends Builder[(K, V), AnyRefMap[K, V]] { + /** A builder for instances of `AnyRefMap`. + * + * This builder can be reused to create multiple instances. + */ + final class AnyRefMapBuilder[K <: AnyRef, V] extends ReusableBuilder[(K, V), AnyRefMap[K, V]] { private[collection] var elems: AnyRefMap[K, V] = new AnyRefMap[K, V] def +=(entry: (K, V)): this.type = { elems += entry diff --git a/src/library/scala/collection/mutable/ArrayBuilder.scala b/src/library/scala/collection/mutable/ArrayBuilder.scala index ac78ab823b..549ffef565 100644 --- a/src/library/scala/collection/mutable/ArrayBuilder.scala +++ b/src/library/scala/collection/mutable/ArrayBuilder.scala @@ -18,7 +18,7 @@ import scala.reflect.ClassTag * * @tparam T the type of the elements for the builder. */ -abstract class ArrayBuilder[T] extends Builder[T, Array[T]] with Serializable +abstract class ArrayBuilder[T] extends ReusableBuilder[T, Array[T]] with Serializable /** A companion object for array builders. * @@ -49,6 +49,8 @@ object ArrayBuilder { /** A class for array builders for arrays of reference types. * + * This builder can be reused. + * * @tparam T type of elements for the array builder, subtype of `AnyRef` with a `ClassTag` context bound. */ @deprecatedInheritance("ArrayBuilder.ofRef is an internal implementation not intended for subclassing.", "2.11.0") @@ -98,12 +100,13 @@ object ArrayBuilder { super.++=(xs) } - def clear() { - size = 0 - } + def clear() { size = 0 } def result() = { - if (capacity != 0 && capacity == size) elems + if (capacity != 0 && capacity == size) { + capacity = 0 + elems + } else mkArray(size) } @@ -115,7 +118,7 @@ object ArrayBuilder { override def toString = "ArrayBuilder.ofRef" } - /** A class for array builders for arrays of `byte`s. */ + /** A class for array builders for arrays of `byte`s. It can be reused. */ @deprecatedInheritance("ArrayBuilder.ofByte is an internal implementation not intended for subclassing.", "2.11.0") class ofByte extends ArrayBuilder[Byte] { @@ -163,12 +166,13 @@ object ArrayBuilder { super.++=(xs) } - def clear() { - size = 0 - } + def clear() { size = 0 } def result() = { - if (capacity != 0 && capacity == size) elems + if (capacity != 0 && capacity == size) { + capacity = 0 + elems + } else mkArray(size) } @@ -180,7 +184,7 @@ object ArrayBuilder { override def toString = "ArrayBuilder.ofByte" } - /** A class for array builders for arrays of `short`s. */ + /** A class for array builders for arrays of `short`s. It can be reused. */ @deprecatedInheritance("ArrayBuilder.ofShort is an internal implementation not intended for subclassing.", "2.11.0") class ofShort extends ArrayBuilder[Short] { @@ -228,12 +232,13 @@ object ArrayBuilder { super.++=(xs) } - def clear() { - size = 0 - } + def clear() { size = 0 } def result() = { - if (capacity != 0 && capacity == size) elems + if (capacity != 0 && capacity == size) { + capacity = 0 + elems + } else mkArray(size) } @@ -245,7 +250,7 @@ object ArrayBuilder { override def toString = "ArrayBuilder.ofShort" } - /** A class for array builders for arrays of `char`s. */ + /** A class for array builders for arrays of `char`s. It can be reused. */ @deprecatedInheritance("ArrayBuilder.ofChar is an internal implementation not intended for subclassing.", "2.11.0") class ofChar extends ArrayBuilder[Char] { @@ -293,12 +298,13 @@ object ArrayBuilder { super.++=(xs) } - def clear() { - size = 0 - } + def clear() { size = 0 } def result() = { - if (capacity != 0 && capacity == size) elems + if (capacity != 0 && capacity == size) { + capacity = 0 + elems + } else mkArray(size) } @@ -310,7 +316,7 @@ object ArrayBuilder { override def toString = "ArrayBuilder.ofChar" } - /** A class for array builders for arrays of `int`s. */ + /** A class for array builders for arrays of `int`s. It can be reused. */ @deprecatedInheritance("ArrayBuilder.ofInt is an internal implementation not intended for subclassing.", "2.11.0") class ofInt extends ArrayBuilder[Int] { @@ -358,12 +364,13 @@ object ArrayBuilder { super.++=(xs) } - def clear() { - size = 0 - } + def clear() { size = 0 } def result() = { - if (capacity != 0 && capacity == size) elems + if (capacity != 0 && capacity == size) { + capacity = 0 + elems + } else mkArray(size) } @@ -375,7 +382,7 @@ object ArrayBuilder { override def toString = "ArrayBuilder.ofInt" } - /** A class for array builders for arrays of `long`s. */ + /** A class for array builders for arrays of `long`s. It can be reused. */ @deprecatedInheritance("ArrayBuilder.ofLong is an internal implementation not intended for subclassing.", "2.11.0") class ofLong extends ArrayBuilder[Long] { @@ -423,12 +430,13 @@ object ArrayBuilder { super.++=(xs) } - def clear() { - size = 0 - } + def clear() { size = 0 } def result() = { - if (capacity != 0 && capacity == size) elems + if (capacity != 0 && capacity == size) { + capacity = 0 + elems + } else mkArray(size) } @@ -440,7 +448,7 @@ object ArrayBuilder { override def toString = "ArrayBuilder.ofLong" } - /** A class for array builders for arrays of `float`s. */ + /** A class for array builders for arrays of `float`s. It can be reused. */ @deprecatedInheritance("ArrayBuilder.ofFloat is an internal implementation not intended for subclassing.", "2.11.0") class ofFloat extends ArrayBuilder[Float] { @@ -488,12 +496,13 @@ object ArrayBuilder { super.++=(xs) } - def clear() { - size = 0 - } + def clear() { size = 0 } def result() = { - if (capacity != 0 && capacity == size) elems + if (capacity != 0 && capacity == size) { + capacity = 0 + elems + } else mkArray(size) } @@ -505,7 +514,7 @@ object ArrayBuilder { override def toString = "ArrayBuilder.ofFloat" } - /** A class for array builders for arrays of `double`s. */ + /** A class for array builders for arrays of `double`s. It can be reused. */ @deprecatedInheritance("ArrayBuilder.ofDouble is an internal implementation not intended for subclassing.", "2.11.0") class ofDouble extends ArrayBuilder[Double] { @@ -553,12 +562,13 @@ object ArrayBuilder { super.++=(xs) } - def clear() { - size = 0 - } + def clear() { size = 0 } def result() = { - if (capacity != 0 && capacity == size) elems + if (capacity != 0 && capacity == size) { + capacity = 0 + elems + } else mkArray(size) } @@ -570,7 +580,7 @@ object ArrayBuilder { override def toString = "ArrayBuilder.ofDouble" } - /** A class for array builders for arrays of `boolean`s. */ + /** A class for array builders for arrays of `boolean`s. It can be reused. */ class ofBoolean extends ArrayBuilder[Boolean] { private var elems: Array[Boolean] = _ @@ -617,12 +627,13 @@ object ArrayBuilder { super.++=(xs) } - def clear() { - size = 0 - } + def clear() { size = 0 } def result() = { - if (capacity != 0 && capacity == size) elems + if (capacity != 0 && capacity == size) { + capacity = 0 + elems + } else mkArray(size) } @@ -634,65 +645,33 @@ object ArrayBuilder { override def toString = "ArrayBuilder.ofBoolean" } - /** A class for array builders for arrays of `Unit` type. */ + /** A class for array builders for arrays of `Unit` type. It can be reused. */ @deprecatedInheritance("ArrayBuilder.ofUnit is an internal implementation not intended for subclassing.", "2.11.0") class ofUnit extends ArrayBuilder[Unit] { - private var elems: Array[Unit] = _ - private var capacity: Int = 0 private var size: Int = 0 - private def mkArray(size: Int): Array[Unit] = { - val newelems = new Array[Unit](size) - if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size) - newelems - } - - private def resize(size: Int) { - elems = mkArray(size) - capacity = size - } - - override def sizeHint(size: Int) { - if (capacity < size) resize(size) - } - - private def ensureSize(size: Int) { - if (capacity < size || capacity == 0) { - var newsize = if (capacity == 0) 16 else capacity * 2 - while (newsize < size) newsize *= 2 - resize(newsize) - } - } - def +=(elem: Unit): this.type = { - ensureSize(size + 1) - elems(size) = elem size += 1 this } - override def ++=(xs: TraversableOnce[Unit]): this.type = xs match { - case xs: WrappedArray.ofUnit => - ensureSize(this.size + xs.length) - Array.copy(xs.array, 0, elems, this.size, xs.length) - size += xs.length - this - case _ => - super.++=(xs) + override def ++=(xs: TraversableOnce[Unit]): this.type = { + size += xs.size + this } - def clear() { - size = 0 - } + def clear() { size = 0 } def result() = { - if (capacity != 0 && capacity == size) elems - else mkArray(size) + val ans = new Array[Unit](size) + var i = 0 + while (i < size) { ans(i) = (); i += 1 } + ans } override def equals(other: Any): Boolean = other match { - case x: ofUnit => (size == x.size) && (elems == x.elems) + case x: ofUnit => (size == x.size) case _ => false } diff --git a/src/library/scala/collection/mutable/Builder.scala b/src/library/scala/collection/mutable/Builder.scala index 75560580cc..8d6a0ec69d 100644 --- a/src/library/scala/collection/mutable/Builder.scala +++ b/src/library/scala/collection/mutable/Builder.scala @@ -18,6 +18,14 @@ import generic._ * elements to the builder with `+=` and then converting to the required * collection type with `result`. * + * One cannot assume that a single `Builder` can build more than one + * instance of the desired collection. Particular subclasses may allow + * such behavior. Otherwise, `result` should be treated as a terminal + * operation: after it is called, no further methods should be called on + * the builder. Extend the [[collection.mutable.ReusableBuilder]] trait + * instead of `Builder` for builders that may be reused to build multiple + * instances. + * * @tparam Elem the type of elements that get added to the builder. * @tparam To the type of collection that it produced. * @@ -36,8 +44,10 @@ trait Builder[-Elem, +To] extends Growable[Elem] { */ def clear() - /** Produces a collection from the added elements. - * The builder's contents are undefined after this operation. + /** Produces a collection from the added elements. This is a terminal operation: + * the builder's contents are undefined after this operation, and no further + * methods should be called. + * * @return a collection containing the elements added to this builder. */ def result(): To @@ -112,6 +122,8 @@ trait Builder[-Elem, +To] extends Growable[Elem] { * @tparam NewTo the type of collection returned by `f`. * @return a new builder which is the same as the current builder except * that a transformation function is applied to this builder's result. + * + * @note The original builder should no longer be used after `mapResult` is called. */ def mapResult[NewTo](f: To => NewTo): Builder[Elem, NewTo] = new Builder[Elem, NewTo] with Proxy { diff --git a/src/library/scala/collection/mutable/GrowingBuilder.scala b/src/library/scala/collection/mutable/GrowingBuilder.scala index c4b5e546aa..27d554d98e 100644 --- a/src/library/scala/collection/mutable/GrowingBuilder.scala +++ b/src/library/scala/collection/mutable/GrowingBuilder.scala @@ -15,6 +15,8 @@ import generic._ /** The canonical builder for collections that are growable, i.e. that support an * efficient `+=` method which adds an element to the collection. * + * GrowableBuilders can produce only a single instance of the collection they are growing. + * * @author Paul Phillips * @version 2.8 * @since 2.8 @@ -25,6 +27,6 @@ import generic._ class GrowingBuilder[Elem, To <: Growable[Elem]](empty: To) extends Builder[Elem, To] { protected var elems: To = empty def +=(x: Elem): this.type = { elems += x; this } - def clear() { elems = empty } + def clear() { empty.clear } def result: To = elems } diff --git a/src/library/scala/collection/mutable/LazyBuilder.scala b/src/library/scala/collection/mutable/LazyBuilder.scala index ebee38b77f..f0a5e6971a 100644 --- a/src/library/scala/collection/mutable/LazyBuilder.scala +++ b/src/library/scala/collection/mutable/LazyBuilder.scala @@ -13,12 +13,14 @@ package mutable /** A builder that constructs its result lazily. Iterators or iterables to * be added to this builder with `++=` are not evaluated until `result` is called. * + * This builder can be reused. + * * @since 2.8 * * @tparam Elem type of the elements for this builder. * @tparam To type of the collection this builder builds. */ -abstract class LazyBuilder[Elem, +To] extends Builder[Elem, To] { +abstract class LazyBuilder[Elem, +To] extends ReusableBuilder[Elem, To] { /** The different segments of elements to be added to the builder, represented as iterators */ protected var parts = new ListBuffer[TraversableOnce[Elem]] def +=(x: Elem): this.type = { parts += List(x); this } diff --git a/src/library/scala/collection/mutable/ListBuffer.scala b/src/library/scala/collection/mutable/ListBuffer.scala index 0a483ceb86..02fcced3ac 100644 --- a/src/library/scala/collection/mutable/ListBuffer.scala +++ b/src/library/scala/collection/mutable/ListBuffer.scala @@ -46,7 +46,7 @@ final class ListBuffer[A] with Buffer[A] with GenericTraversableTemplate[A, ListBuffer] with BufferLike[A, ListBuffer[A]] - with Builder[A, List[A]] + with ReusableBuilder[A, List[A]] with SeqForwarder[A] with Serializable { @@ -297,6 +297,10 @@ final class ListBuffer[A] // Implementation of abstract method in Builder + /** Returns the accumulated `List`. + * + * This method may be called multiple times to obtain snapshots of the list in different stages of construction. + */ def result: List[A] = toList /** Converts this buffer to a list. Takes constant time. The buffer is diff --git a/src/library/scala/collection/mutable/LongMap.scala b/src/library/scala/collection/mutable/LongMap.scala index f39a6ba634..ecbb1952af 100644 --- a/src/library/scala/collection/mutable/LongMap.scala +++ b/src/library/scala/collection/mutable/LongMap.scala @@ -519,7 +519,11 @@ object LongMap { def apply(): LongMapBuilder[U] = new LongMapBuilder[U] } - final class LongMapBuilder[V] extends Builder[(Long, V), LongMap[V]] { + /** A builder for instances of `LongMap`. + * + * This builder can be reused to create multiple instances. + */ + final class LongMapBuilder[V] extends ReusableBuilder[(Long, V), LongMap[V]] { private[collection] var elems: LongMap[V] = new LongMap[V] def +=(entry: (Long, V)): this.type = { elems += entry diff --git a/src/library/scala/collection/mutable/MapBuilder.scala b/src/library/scala/collection/mutable/MapBuilder.scala index a5a6b12ea9..cfc3079f41 100644 --- a/src/library/scala/collection/mutable/MapBuilder.scala +++ b/src/library/scala/collection/mutable/MapBuilder.scala @@ -23,7 +23,7 @@ package mutable * @since 2.8 */ class MapBuilder[A, B, Coll <: scala.collection.GenMap[A, B] with scala.collection.GenMapLike[A, B, Coll]](empty: Coll) -extends Builder[(A, B), Coll] { +extends ReusableBuilder[(A, B), Coll] { protected var elems: Coll = empty def +=(x: (A, B)): this.type = { elems = (elems + x).asInstanceOf[Coll] diff --git a/src/library/scala/collection/mutable/ReusableBuilder.scala b/src/library/scala/collection/mutable/ReusableBuilder.scala new file mode 100644 index 0000000000..caab3071b6 --- /dev/null +++ b/src/library/scala/collection/mutable/ReusableBuilder.scala @@ -0,0 +1,51 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2016, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + + +package scala +package collection +package mutable + +import generic._ + +/** `ReusableBuilder` is a marker trait that indicates that a `Builder` + * can be reused to build more than one instance of a collection. In + * particular, calling `result` followed by `clear` will produce a + * collection and reset the builder to begin building a new collection + * of the same type. + * + * It is up to subclasses to implement this behavior, and to document any + * other behavior that varies from standard `ReusableBuilder` usage + * (e.g. operations being well-defined after a call to `result`, or allowing + * multiple calls to result to obtain different snapshots of a collection under + * construction). + * + * @tparam Elem the type of elements that get added to the builder. + * @tparam To the type of collection that it produced. + * + * @since 2.12 + */ +trait ReusableBuilder[-Elem, +To] extends Builder[Elem, To] { + /** Clears the contents of this builder. + * After execution of this method, the builder will contain no elements. + * + * If executed immediately after a call to `result`, this allows a new + * instance of the same type of collection to be built. + */ + override def clear(): Unit // Note: overriding for scaladoc only! + + /** Produces a collection from the added elements. + * + * After a call to `result`, the behavior of all other methods is undefined + * save for `clear`. If `clear` is called, then the builder is reset and + * may be used to build another instance. + * + * @return a collection containing the elements added to this builder. + */ + override def result(): To // Note: overriding for scaladoc only! +} diff --git a/src/library/scala/collection/mutable/SetBuilder.scala b/src/library/scala/collection/mutable/SetBuilder.scala index 01bfdc96ed..5d1e9ffc3a 100644 --- a/src/library/scala/collection/mutable/SetBuilder.scala +++ b/src/library/scala/collection/mutable/SetBuilder.scala @@ -17,7 +17,9 @@ package mutable * @param empty The empty element of the collection. * @since 2.8 */ -class SetBuilder[A, Coll <: scala.collection.Set[A] with scala.collection.SetLike[A, Coll]](empty: Coll) extends Builder[A, Coll] { +class SetBuilder[A, Coll <: scala.collection.Set[A] +with scala.collection.SetLike[A, Coll]](empty: Coll) +extends ReusableBuilder[A, Coll] { protected var elems: Coll = empty def +=(x: A): this.type = { elems = elems + x; this } def clear() { elems = empty } diff --git a/src/library/scala/collection/mutable/StringBuilder.scala b/src/library/scala/collection/mutable/StringBuilder.scala index c56d40786e..b5b9498374 100644 --- a/src/library/scala/collection/mutable/StringBuilder.scala +++ b/src/library/scala/collection/mutable/StringBuilder.scala @@ -33,7 +33,7 @@ final class StringBuilder(private val underlying: JavaStringBuilder) with java.lang.CharSequence with IndexedSeq[Char] with StringLike[StringBuilder] - with Builder[Char, String] + with ReusableBuilder[Char, String] with Serializable { override protected[this] def thisCollection: StringBuilder = this @@ -435,7 +435,11 @@ final class StringBuilder(private val underlying: JavaStringBuilder) */ override def mkString = toString - /** Returns the result of this Builder (a String) + /** Returns the result of this Builder (a String). + * + * If this method is called multiple times, each call will result in a snapshot of the buffer at that point in time. + * In particular, a `StringBuilder` can be used to build multiple independent strings by emptying the buffer with `clear` + * after each call to `result`. * * @return the string assembled by this StringBuilder */ diff --git a/src/library/scala/collection/mutable/WrappedArrayBuilder.scala b/src/library/scala/collection/mutable/WrappedArrayBuilder.scala index bfe95a11ab..c4781321d7 100644 --- a/src/library/scala/collection/mutable/WrappedArrayBuilder.scala +++ b/src/library/scala/collection/mutable/WrappedArrayBuilder.scala @@ -17,12 +17,14 @@ import scala.runtime.ScalaRunTime._ /** A builder class for arrays. * + * This builder can be reused. + * * @tparam A type of elements that can be added to this builder. * @param tag class tag for objects of type `A`. * * @since 2.8 */ -class WrappedArrayBuilder[A](tag: ClassTag[A]) extends Builder[A, WrappedArray[A]] { +class WrappedArrayBuilder[A](tag: ClassTag[A]) extends ReusableBuilder[A, WrappedArray[A]] { @deprecated("use tag instead", "2.10.0") val manifest: ClassTag[A] = tag @@ -73,12 +75,13 @@ class WrappedArrayBuilder[A](tag: ClassTag[A]) extends Builder[A, WrappedArray[A this } - def clear() { - size = 0 - } + def clear() { size = 0 } def result() = { - if (capacity != 0 && capacity == size) elems + if (capacity != 0 && capacity == size) { + capacity = 0 + elems + } else mkArray(size) } diff --git a/src/library/scala/concurrent/Future.scala b/src/library/scala/concurrent/Future.scala index f49536d351..b10aad0ecc 100644 --- a/src/library/scala/concurrent/Future.scala +++ b/src/library/scala/concurrent/Future.scala @@ -36,6 +36,8 @@ import scala.reflect.ClassTag * * @author Philipp Haller, Heather Miller, Aleksandar Prokopec, Viktor Klang * + * @see [[http://docs.scala-lang.org/overviews/core/futures.html Futures and Promises]] + * * @define multipleCallbacks * Multiple callbacks may be registered; there is no guarantee that they will be * executed in a particular order. @@ -243,7 +245,17 @@ trait Future[+T] extends Awaitable[T] { * this future. If this future is completed with an exception then the new * future will also contain this exception. * - * $forComprehensionExamples + * Example: + * + * {{{ + * val f = Future { "The future" } + * val g = f map { x: String => x + " is now!" } + * }}} + * + * Note that a for comprehension involving a `Future` + * may expand to include a call to `map` and or `flatMap` + * and `withFilter`. See [[scala.concurrent.Future#flatMap]] for an example of such a comprehension. + * * * @tparam S the type of the returned `Future` * @param f the function which will be applied to the successful result of this `Future` diff --git a/src/library/scala/concurrent/duration/Duration.scala b/src/library/scala/concurrent/duration/Duration.scala index 8b7d81d1c4..a905ef345c 100644 --- a/src/library/scala/concurrent/duration/Duration.scala +++ b/src/library/scala/concurrent/duration/Duration.scala @@ -94,7 +94,7 @@ object Duration { timeUnitLabels flatMap { case (unit, names) => expandLabels(names) map (_ -> unit) } toMap /** - * Extract length and time unit out of a string, where the format must match the description for [[Duration$.apply(String):Duration apply(String)]]. + * Extract length and time unit out of a string, where the format must match the description for [[Duration$.apply(s:String)* apply(String)]]. * The extractor will not match for malformed strings or non-finite durations. */ def unapply(s: String): Option[(Long, TimeUnit)] = @@ -355,7 +355,7 @@ object Duration { * - isomorphic to `java.lang.Double` when it comes to infinite or undefined values * * The conversion between Duration and Double is done using [[Duration.toUnit]] (with unit NANOSECONDS) - * and [[Duration$.fromNanos(Double):Duration Duration.fromNanos(Double)]]. + * and [[Duration$.fromNanos(nanos:Double)* Duration.fromNanos(Double)]] * * <h2>Ordering</h2> * diff --git a/src/library/scala/math/BigDecimal.scala b/src/library/scala/math/BigDecimal.scala index 371fd59a93..e769dfb8cb 100644 --- a/src/library/scala/math/BigDecimal.scala +++ b/src/library/scala/math/BigDecimal.scala @@ -397,7 +397,7 @@ object BigDecimal { * @version 1.1 */ final class BigDecimal(val bigDecimal: BigDec, val mc: MathContext) -extends ScalaNumber with ScalaNumericConversions with Serializable { +extends ScalaNumber with ScalaNumericConversions with Serializable with Ordered[BigDecimal] { def this(bigDecimal: BigDec) = this(bigDecimal, BigDecimal.defaultMathContext) import BigDecimal.RoundingMode._ import BigDecimal.{decimal, binary, exact} @@ -537,22 +537,6 @@ extends ScalaNumber with ScalaNumericConversions with Serializable { */ def compare (that: BigDecimal): Int = this.bigDecimal compareTo that.bigDecimal - /** Less-than-or-equals comparison of BigDecimals - */ - def <= (that: BigDecimal): Boolean = compare(that) <= 0 - - /** Greater-than-or-equals comparison of BigDecimals - */ - def >= (that: BigDecimal): Boolean = compare(that) >= 0 - - /** Less-than of BigDecimals - */ - def < (that: BigDecimal): Boolean = compare(that) < 0 - - /** Greater-than comparison of BigDecimals - */ - def > (that: BigDecimal): Boolean = compare(that) > 0 - /** Addition of BigDecimals */ def + (that: BigDecimal): BigDecimal = new BigDecimal(this.bigDecimal add that.bigDecimal, mc) diff --git a/src/library/scala/math/BigInt.scala b/src/library/scala/math/BigInt.scala index abc7371d9f..3ae3b9bf6c 100644 --- a/src/library/scala/math/BigInt.scala +++ b/src/library/scala/math/BigInt.scala @@ -109,7 +109,12 @@ object BigInt { * @author Martin Odersky * @version 1.0, 15/07/2003 */ -final class BigInt(val bigInteger: BigInteger) extends ScalaNumber with ScalaNumericConversions with Serializable { +final class BigInt(val bigInteger: BigInteger) + extends ScalaNumber + with ScalaNumericConversions + with Serializable + with Ordered[BigInt] +{ /** Returns the hash code for this BigInt. */ override def hashCode(): Int = if (isValidLong) unifiedPrimitiveHashcode() @@ -176,22 +181,6 @@ final class BigInt(val bigInteger: BigInteger) extends ScalaNumber with ScalaNum */ def compare (that: BigInt): Int = this.bigInteger.compareTo(that.bigInteger) - /** Less-than-or-equals comparison of BigInts - */ - def <= (that: BigInt): Boolean = compare(that) <= 0 - - /** Greater-than-or-equals comparison of BigInts - */ - def >= (that: BigInt): Boolean = compare(that) >= 0 - - /** Less-than of BigInts - */ - def < (that: BigInt): Boolean = compare(that) < 0 - - /** Greater-than comparison of BigInts - */ - def > (that: BigInt): Boolean = compare(that) > 0 - /** Addition of BigInts */ def + (that: BigInt): BigInt = new BigInt(this.bigInteger.add(that.bigInteger)) diff --git a/src/library/scala/math/package.scala b/src/library/scala/math/package.scala index b6593d6661..54c81ed613 100644 --- a/src/library/scala/math/package.scala +++ b/src/library/scala/math/package.scala @@ -58,7 +58,19 @@ package object math { * logarithms. */ def exp(x: Double): Double = java.lang.Math.exp(x) + + /** Returns the natural logarithm of a `double` value. + * + * @param x the number to take the natural logarithm of + * @return the value `logₑ(x)` where `e` is Eulers number + */ def log(x: Double): Double = java.lang.Math.log(x) + + /** Returns the square root of a `double` value. + * + * @param x the number to take the square root of + * @return the value √x + */ def sqrt(x: Double): Double = java.lang.Math.sqrt(x) def IEEEremainder(x: Double, y: Double): Double = java.lang.Math.IEEEremainder(x, y) diff --git a/src/library/scala/reflect/Manifest.scala b/src/library/scala/reflect/Manifest.scala index 4ff49c44d0..e099853463 100644 --- a/src/library/scala/reflect/Manifest.scala +++ b/src/library/scala/reflect/Manifest.scala @@ -157,6 +157,9 @@ object ManifestFactory { override def newArray(len: Int): Array[Unit] = new Array[Unit](len) override def newWrappedArray(len: Int): WrappedArray[Unit] = new WrappedArray.ofUnit(new Array[Unit](len)) override def newArrayBuilder(): ArrayBuilder[Unit] = new ArrayBuilder.ofUnit() + override protected def arrayClass[T](tp: Class[_]): Class[Array[T]] = + if (tp eq runtimeClass) classOf[Array[scala.runtime.BoxedUnit]].asInstanceOf[Class[Array[T]]] + else super.arrayClass(tp) private def readResolve(): Any = Manifest.Unit } diff --git a/src/library/scala/runtime/LambdaDeserialize.java b/src/library/scala/runtime/LambdaDeserialize.java new file mode 100644 index 0000000000..e239debf25 --- /dev/null +++ b/src/library/scala/runtime/LambdaDeserialize.java @@ -0,0 +1,29 @@ +package scala.runtime; + + +import java.lang.invoke.*; +import java.util.Arrays; +import java.util.HashMap; + +public final class LambdaDeserialize { + + private MethodHandles.Lookup lookup; + private final HashMap<String, MethodHandle> cache = new HashMap<>(); + private final LambdaDeserializer$ l = LambdaDeserializer$.MODULE$; + + private LambdaDeserialize(MethodHandles.Lookup lookup) { + this.lookup = lookup; + } + + public Object deserializeLambda(SerializedLambda serialized) { + return l.deserializeLambda(lookup, cache, serialized); + } + + public static CallSite bootstrap(MethodHandles.Lookup lookup, String invokedName, + MethodType invokedType) throws Throwable { + MethodType type = MethodType.fromMethodDescriptorString("(Ljava/lang/invoke/SerializedLambda;)Ljava/lang/Object;", lookup.getClass().getClassLoader()); + MethodHandle deserializeLambda = lookup.findVirtual(LambdaDeserialize.class, "deserializeLambda", type); + MethodHandle exact = deserializeLambda.bindTo(new LambdaDeserialize(lookup)).asType(invokedType); + return new ConstantCallSite(exact); + } +} diff --git a/src/library/scala/runtime/StructuralCallSite.java b/src/library/scala/runtime/StructuralCallSite.java new file mode 100644 index 0000000000..f73b4f08e6 --- /dev/null +++ b/src/library/scala/runtime/StructuralCallSite.java @@ -0,0 +1,43 @@ +package scala.runtime; + + +import java.lang.invoke.*; +import java.lang.ref.SoftReference; +import java.lang.reflect.Method; + +public final class StructuralCallSite { + + private Class<?>[] parameterTypes; + private SoftReference<MethodCache> cache = new SoftReference<>(new EmptyMethodCache()); + + private StructuralCallSite(MethodType callType) { + parameterTypes = callType.parameterArray(); + } + + public MethodCache get() { + MethodCache cache = this.cache.get(); + if (cache == null) { + cache = new EmptyMethodCache(); + this.cache = new SoftReference<>(cache); + } + return cache; + } + + public Method find(Class<?> receiver) { + return get().find(receiver); + } + + public Method add(Class<?> receiver, Method m) { + cache = new SoftReference<MethodCache>(get().add(receiver, m)); + return m; + } + public Class<?>[] parameterTypes() { + return parameterTypes; + } + + public static CallSite bootstrap(MethodHandles.Lookup lookup, String invokedName, + MethodType invokedType, MethodType reflectiveCallType) throws Throwable { + StructuralCallSite structuralCallSite = new StructuralCallSite(reflectiveCallType); + return new ConstantCallSite(MethodHandles.constant(StructuralCallSite.class, structuralCallSite)); + } +} diff --git a/src/library/scala/runtime/SymbolLiteral.java b/src/library/scala/runtime/SymbolLiteral.java new file mode 100644 index 0000000000..09a66c83d5 --- /dev/null +++ b/src/library/scala/runtime/SymbolLiteral.java @@ -0,0 +1,20 @@ +package scala.runtime; + +import java.lang.invoke.*; +import java.util.regex.Pattern; + +public final class SymbolLiteral { + private SymbolLiteral() { + } + + public static CallSite bootstrap(MethodHandles.Lookup lookup, String invokedName, + MethodType invokedType, + String value) throws Throwable { + ClassLoader classLoader = lookup.lookupClass().getClassLoader(); + MethodType type = MethodType.fromMethodDescriptorString("(Ljava/lang/Object;)Ljava/lang/Object;", classLoader); + Class<?> symbolClass = Class.forName("scala.Symbol", false, classLoader); + MethodHandle factoryMethod = lookup.findStatic(symbolClass, "apply", type); + Object symbolValue = factoryMethod.invokeWithArguments(value); + return new ConstantCallSite(MethodHandles.constant(symbolClass, symbolValue)); + } +} diff --git a/src/library/scala/util/MurmurHash.scala b/src/library/scala/util/MurmurHash.scala index e05fe0875b..1b6db5d6aa 100644 --- a/src/library/scala/util/MurmurHash.scala +++ b/src/library/scala/util/MurmurHash.scala @@ -10,7 +10,7 @@ package scala package util /** An implementation of Austin Appleby's MurmurHash 3.0 algorithm - * (32 bit version); reference: http://code.google.com/p/smhasher + * (32 bit version); reference: https://github.com/aappleby/smhasher * * This is the hash used by collections and case classes (including * tuples). diff --git a/src/library/scala/util/Properties.scala b/src/library/scala/util/Properties.scala index 6ee74acd6a..583f1539d1 100644 --- a/src/library/scala/util/Properties.scala +++ b/src/library/scala/util/Properties.scala @@ -105,7 +105,7 @@ private[scala] trait PropertiesTrait { * or "version (unknown)" if it cannot be determined. */ val versionString = "version " + scalaPropOrElse("version.number", "(unknown)") - val copyrightString = scalaPropOrElse("copyright.string", "Copyright 2002-2015, LAMP/EPFL") + val copyrightString = scalaPropOrElse("copyright.string", "Copyright 2002-2016, LAMP/EPFL") /** This is the encoding to use reading in source files, overridden with -encoding. * Note that it uses "prop" i.e. looks in the scala jar, not the system properties. diff --git a/src/library/scala/util/hashing/MurmurHash3.scala b/src/library/scala/util/hashing/MurmurHash3.scala index 4e5537954f..6a56910451 100644 --- a/src/library/scala/util/hashing/MurmurHash3.scala +++ b/src/library/scala/util/hashing/MurmurHash3.scala @@ -193,7 +193,7 @@ private[hashing] class MurmurHash3 { * to remedy some weaknesses and improve performance. This represents the * latest and supposedly final version of the algorithm (revision 136). * - * @see [[http://code.google.com/p/smhasher]] + * @see [[https://github.com/aappleby/smhasher]] */ object MurmurHash3 extends MurmurHash3 { final val arraySeed = 0x3c074a61 |