Skip to content

Commit 252e3b5

Browse files
committed
Minor updates to ArraySeq
1 parent d7332af commit 252e3b5

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/main/scala-2.11_2.12/scala/collection/immutable/ArraySeq.scala

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,19 @@ object ArraySeq {
6464
private val EmptyArraySeq = new ofRef[AnyRef](new Array[AnyRef](0))
6565
def empty[T <: AnyRef]: ArraySeq[T] = EmptyArraySeq.asInstanceOf[ArraySeq[T]]
6666

67-
// If make is called explicitly we use whatever we're given, even if it's
68-
// empty. This may be unnecessary (if ArraySeq is to honor the collections
69-
// contract all empty ones must be equal, so discriminating based on the reference
70-
// equality of an empty array should not come up) but we may as well be
71-
// conservative since wrapRefArray contributes most of the unnecessary allocations.
72-
def unsafeWrapArray[T](x: AnyRef): ArraySeq[T] = (x match {
67+
/**
68+
* Wrap an existing `Array` into an `ArraySeq` of the proper primitive specialization type
69+
* without copying.
70+
*
71+
* Note that an array containing boxed primitives can be wrapped in an `ArraySeq` without
72+
* copying. For example, `val a: Array[Any] = Array(1)` is an array of `Object` at runtime,
73+
* containing `Integer`s. An `ArraySeq[Int]` can be obtained with a cast:
74+
* `ArraySeq.unsafeWrapArray(a).asInstanceOf[ArraySeq[Int]]`. The values are still
75+
* boxed, the resulting instance is an [[ArraySeq.ofRef]]. Writing
76+
* `ArraySeq.unsafeWrapArray(a.asInstanceOf[Array[Int]])` does not work, it throws a
77+
* `ClassCastException` at runtime.
78+
*/
79+
def unsafeWrapArray[T](x: Array[T]): ArraySeq[T] = (x.asInstanceOf[Array[_]] match {
7380
case null => null
7481
case x: Array[AnyRef] => new ofRef[AnyRef](x)
7582
case x: Array[Int] => new ofInt(x)
@@ -91,6 +98,7 @@ object ArraySeq {
9198
ArrayBuilder.make[T]()(m) mapResult ArraySeq.unsafeWrapArray[T]
9299
}
93100

101+
@SerialVersionUID(3L)
94102
final class ofRef[T <: AnyRef](val unsafeArray: Array[T]) extends ArraySeq[T] with Serializable {
95103
lazy val elemTag = ClassTag[T](unsafeArray.getClass.getComponentType)
96104
def length: Int = unsafeArray.length
@@ -103,6 +111,7 @@ object ArraySeq {
103111
}
104112
}
105113

114+
@SerialVersionUID(3L)
106115
final class ofByte(val unsafeArray: Array[Byte]) extends ArraySeq[Byte] with Serializable {
107116
def elemTag = ClassTag.Byte
108117
def length: Int = unsafeArray.length
@@ -115,6 +124,7 @@ object ArraySeq {
115124
}
116125
}
117126

127+
@SerialVersionUID(3L)
118128
final class ofShort(val unsafeArray: Array[Short]) extends ArraySeq[Short] with Serializable {
119129
def elemTag = ClassTag.Short
120130
def length: Int = unsafeArray.length
@@ -127,6 +137,7 @@ object ArraySeq {
127137
}
128138
}
129139

140+
@SerialVersionUID(3L)
130141
final class ofChar(val unsafeArray: Array[Char]) extends ArraySeq[Char] with Serializable {
131142
def elemTag = ClassTag.Char
132143
def length: Int = unsafeArray.length
@@ -139,6 +150,7 @@ object ArraySeq {
139150
}
140151
}
141152

153+
@SerialVersionUID(3L)
142154
final class ofInt(val unsafeArray: Array[Int]) extends ArraySeq[Int] with Serializable {
143155
def elemTag = ClassTag.Int
144156
def length: Int = unsafeArray.length
@@ -151,6 +163,7 @@ object ArraySeq {
151163
}
152164
}
153165

166+
@SerialVersionUID(3L)
154167
final class ofLong(val unsafeArray: Array[Long]) extends ArraySeq[Long] with Serializable {
155168
def elemTag = ClassTag.Long
156169
def length: Int = unsafeArray.length
@@ -163,6 +176,7 @@ object ArraySeq {
163176
}
164177
}
165178

179+
@SerialVersionUID(3L)
166180
final class ofFloat(val unsafeArray: Array[Float]) extends ArraySeq[Float] with Serializable {
167181
def elemTag = ClassTag.Float
168182
def length: Int = unsafeArray.length
@@ -175,6 +189,7 @@ object ArraySeq {
175189
}
176190
}
177191

192+
@SerialVersionUID(3L)
178193
final class ofDouble(val unsafeArray: Array[Double]) extends ArraySeq[Double] with Serializable {
179194
def elemTag = ClassTag.Double
180195
def length: Int = unsafeArray.length
@@ -187,6 +202,7 @@ object ArraySeq {
187202
}
188203
}
189204

205+
@SerialVersionUID(3L)
190206
final class ofBoolean(val unsafeArray: Array[Boolean]) extends ArraySeq[Boolean] with Serializable {
191207
def elemTag = ClassTag.Boolean
192208
def length: Int = unsafeArray.length
@@ -199,6 +215,7 @@ object ArraySeq {
199215
}
200216
}
201217

218+
@SerialVersionUID(3L)
202219
final class ofUnit(val unsafeArray: Array[Unit]) extends ArraySeq[Unit] with Serializable {
203220
def elemTag = ClassTag.Unit
204221
def length: Int = unsafeArray.length

0 commit comments

Comments
 (0)