Skip to content

Commit 212a01f

Browse files
authored
Merge pull request scala-js#4680 from sjrd/more-javalib-cleanups
More javalib cleanups
2 parents 7d82c41 + d3dbd47 commit 212a01f

22 files changed

+336
-224
lines changed

javalib/src/main/scala/java/math/BigDecimal.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ class BigDecimal() extends Number with Comparable[BigDecimal] {
811811
val (q, l) = loop(1, q1.shiftRight(k), 0)
812812

813813
// If abs(q) != 1 then the quotient is periodic
814-
if (q.abs() != BigInteger.ONE) {
814+
if (!q.abs().equals(BigInteger.ONE)) {
815815
throw new ArithmeticException(
816816
"Non-terminating decimal expansion; no exact representable decimal result")
817817
}
@@ -1264,7 +1264,7 @@ class BigDecimal() extends Number with Comparable[BigDecimal] {
12641264
case that: BigDecimal =>
12651265
that._scale == this._scale && (
12661266
if (_bitLength < 64) that._smallValue == this._smallValue
1267-
else this._intVal == that._intVal)
1267+
else this._intVal.equals(that._intVal))
12681268
case _ => false
12691269
}
12701270

javalib/src/main/scala/java/math/Logical.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private[math] object Logical {
4444
// scalastyle:off return
4545
if (bi.sign == 0) {
4646
BigInteger.MINUS_ONE
47-
} else if (bi == BigInteger.MINUS_ONE) {
47+
} else if (bi.equals(BigInteger.MINUS_ONE)) {
4848
BigInteger.ZERO
4949
} else {
5050
val resDigits = new Array[Int](bi.numberLength + 1)
@@ -88,9 +88,9 @@ private[math] object Logical {
8888
def and(bi: BigInteger, that: BigInteger): BigInteger = {
8989
if (that.sign == 0 || bi.sign == 0)
9090
BigInteger.ZERO
91-
else if (that == BigInteger.MINUS_ONE)
91+
else if (that.equals(BigInteger.MINUS_ONE))
9292
bi
93-
else if (bi == BigInteger.MINUS_ONE)
93+
else if (bi.equals(BigInteger.MINUS_ONE))
9494
that
9595
else if (bi.sign > 0 && that.sign > 0)
9696
andPositive(bi, that)
@@ -235,9 +235,9 @@ private[math] object Logical {
235235
bi
236236
else if (bi.sign == 0)
237237
BigInteger.ZERO
238-
else if (bi == BigInteger.MINUS_ONE)
238+
else if (bi.equals(BigInteger.MINUS_ONE))
239239
that.not()
240-
else if (that == BigInteger.MINUS_ONE)
240+
else if (that.equals(BigInteger.MINUS_ONE))
241241
BigInteger.ZERO
242242
else if (bi.sign > 0 && that.sign > 0)
243243
andNotPositive(bi, that)
@@ -446,7 +446,7 @@ private[math] object Logical {
446446

447447
/** @see BigInteger#or(BigInteger) */
448448
def or(bi: BigInteger, that: BigInteger): BigInteger = {
449-
if (that == BigInteger.MINUS_ONE || bi == BigInteger.MINUS_ONE) {
449+
if (that.equals(BigInteger.MINUS_ONE) || bi.equals(BigInteger.MINUS_ONE)) {
450450
BigInteger.MINUS_ONE
451451
} else if (that.sign == 0) {
452452
bi
@@ -593,9 +593,9 @@ private[math] object Logical {
593593
bi
594594
} else if (bi.sign == 0) {
595595
that
596-
} else if (that == BigInteger.MINUS_ONE) {
596+
} else if (that.equals(BigInteger.MINUS_ONE)) {
597597
bi.not()
598-
} else if (bi == BigInteger.MINUS_ONE) {
598+
} else if (bi.equals(BigInteger.MINUS_ONE)) {
599599
that.not()
600600
} else if (bi.sign > 0) {
601601
if (that.sign > 0) {

javalib/src/main/scala/java/math/Primality.scala

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,15 @@ private[math] object Primality {
134134
Arrays.binarySearch(Primes, n.digits(0)) >= 0
135135
} else {
136136
// To check if 'n' is divisible by some prime of the table
137-
for (i <- 1 until Primes.length) {
137+
var i: Int = 1
138+
val primesLength = Primes.length
139+
while (i != primesLength) {
138140
if (Division.remainderArrayByInt(n.digits, n.numberLength, Primes(i)) == 0)
139141
return false
142+
i += 1
140143
}
141144

142145
// To set the number of iterations necessary for Miller-Rabin test
143-
var i: Int = 0
144146
val bitLength = n.bitLength()
145147
i = 2
146148
while (bitLength < Bits(i)) {
@@ -218,13 +220,15 @@ private[math] object Primality {
218220
}
219221
}
220222
// To execute Miller-Rabin for non-divisible numbers by all first primes
221-
for (j <- 0 until gapSize) {
223+
var j = 0
224+
while (j != gapSize) {
222225
if (!isDivisible(j)) {
223226
Elementary.inplaceAdd(probPrime, j)
224227
if (millerRabin(probPrime, certainty)) {
225228
return probPrime
226229
}
227230
}
231+
j += 1
228232
}
229233
Elementary.inplaceAdd(startPoint, gapSize)
230234
}
@@ -251,7 +255,9 @@ private[math] object Primality {
251255
val k = nMinus1.getLowestSetBit()
252256
val q = nMinus1.shiftRight(k)
253257
val rnd = new Random()
254-
for (i <- 0 until t) {
258+
259+
var i = 0
260+
while (i != t) {
255261
// To generate a witness 'x', first it use the primes of table
256262
if (i < Primes.length) {
257263
x = BiPrimes(i)
@@ -267,17 +273,21 @@ private[math] object Primality {
267273
}
268274

269275
y = x.modPow(q, n)
270-
if (!(y.isOne || y == nMinus1)) {
271-
for (j <- 1 until k) {
272-
if (y != nMinus1) {
276+
if (!(y.isOne || y.equals(nMinus1))) {
277+
var j = 1
278+
while (j != k) {
279+
if (!y.equals(nMinus1)) {
273280
y = y.multiply(y).mod(n)
274281
if (y.isOne)
275282
return false
276283
}
284+
j += 1
277285
}
278-
if (y != nMinus1)
286+
if (!y.equals(nMinus1))
279287
return false
280288
}
289+
290+
i += 1
281291
}
282292
true
283293
// scalastyle:on return

javalib/src/main/scala/java/net/URI.scala

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,20 +190,24 @@ final class URI(origStr: String) extends Serializable with Comparable[URI] {
190190
import scala.util.hashing.MurmurHash3._
191191
import URI.normalizeEscapes
192192

193+
def normalizeEscapesHash(str: String): Int =
194+
if (str == null) 0
195+
else normalizeEscapes(str).hashCode()
196+
193197
var acc = URI.uriSeed
194-
acc = mix(acc, if (_scheme == null) 0 else _scheme.toLowerCase.##) // scheme may not contain escapes
198+
acc = mix(acc, if (_scheme == null) 0 else _scheme.toLowerCase.hashCode()) // scheme may not contain escapes
195199
if (this.isOpaque()) {
196-
acc = mix(acc, normalizeEscapes(this._schemeSpecificPart).##)
200+
acc = mix(acc, normalizeEscapesHash(this._schemeSpecificPart))
197201
} else if (this._host != null) {
198-
acc = mix(acc, normalizeEscapes(this._userInfo).##)
199-
acc = mix(acc, this._host.toLowerCase.##)
200-
acc = mix(acc, this._port.##)
202+
acc = mix(acc, normalizeEscapesHash(this._userInfo))
203+
acc = mix(acc, this._host.toLowerCase.hashCode())
204+
acc = mix(acc, this._port.hashCode())
201205
} else {
202-
acc = mix(acc, normalizeEscapes(this._authority).##)
206+
acc = mix(acc, normalizeEscapesHash(this._authority))
203207
}
204-
acc = mix(acc, normalizeEscapes(this._path).##)
205-
acc = mix(acc, normalizeEscapes(this._query).##)
206-
acc = mixLast(acc, normalizeEscapes(this._fragment).##)
208+
acc = mix(acc, normalizeEscapesHash(this._path))
209+
acc = mix(acc, normalizeEscapesHash(this._query))
210+
acc = mixLast(acc, normalizeEscapesHash(this._fragment))
207211
finalizeHash(acc, 3)
208212
}
209213

javalib/src/main/scala/java/nio/Buffer.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
package java.nio
1414

15+
import java.util.internal.GenericArrayOps._
16+
1517
import scala.scalajs.js.typedarray._
1618

1719
abstract class Buffer private[nio] (val _capacity: Int) {
@@ -192,8 +194,9 @@ abstract class Buffer private[nio] (val _capacity: Int) {
192194
}
193195

194196
@inline private[nio] def validateArrayIndexRange(
195-
array: Array[_], offset: Int, length: Int): Unit = {
196-
if (offset < 0 || length < 0 || offset > array.length - length)
197+
array: Array[ElementType], offset: Int, length: Int)(
198+
implicit arrayOps: ArrayOps[ElementType]): Unit = {
199+
if (offset < 0 || length < 0 || offset > arrayOps.length(array) - length)
197200
throw new IndexOutOfBoundsException
198201
}
199202

javalib/src/main/scala/java/nio/GenBuffer.scala

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
package java.nio
1414

15+
import java.util.internal.GenericArrayOps._
16+
1517
private[nio] object GenBuffer {
1618
def apply[B <: Buffer](self: B): GenBuffer[B] =
1719
new GenBuffer(self)
@@ -49,8 +51,8 @@ private[nio] final class GenBuffer[B <: Buffer] private (val self: B)
4951
}
5052

5153
@inline
52-
def generic_get(dst: Array[ElementType],
53-
offset: Int, length: Int): BufferType = {
54+
def generic_get(dst: Array[ElementType], offset: Int, length: Int)(
55+
implicit arrayOps: ArrayOps[ElementType]): BufferType = {
5456
validateArrayIndexRange(dst, offset, length)
5557
load(getPosAndAdvanceRead(length), dst, offset, length)
5658
self
@@ -82,8 +84,8 @@ private[nio] final class GenBuffer[B <: Buffer] private (val self: B)
8284
}
8385

8486
@inline
85-
def generic_put(src: Array[ElementType],
86-
offset: Int, length: Int): BufferType = {
87+
def generic_put(src: Array[ElementType], offset: Int, length: Int)(
88+
implicit arrayOps: ArrayOps[ElementType]): BufferType = {
8789
ensureNotReadOnly()
8890
validateArrayIndexRange(src, offset, length)
8991
store(getPosAndAdvanceWrite(length), src, offset, length)
@@ -122,7 +124,7 @@ private[nio] final class GenBuffer[B <: Buffer] private (val self: B)
122124
var h = hashSeed
123125
var i = start
124126
while (i != end) {
125-
h = mix(h, load(i).##)
127+
h = mix(h, load(i).hashCode())
126128
i += 1
127129
}
128130
finalizeHash(h, end-start)
@@ -156,25 +158,27 @@ private[nio] final class GenBuffer[B <: Buffer] private (val self: B)
156158

157159
@inline
158160
def generic_load(startIndex: Int,
159-
dst: Array[ElementType], offset: Int, length: Int): Unit = {
161+
dst: Array[ElementType], offset: Int, length: Int)(
162+
implicit arrayOps: ArrayOps[ElementType]): Unit = {
160163
var selfPos = startIndex
161164
val endPos = selfPos + length
162165
var arrayIndex = offset
163166
while (selfPos != endPos) {
164-
dst(arrayIndex) = load(selfPos)
167+
arrayOps.set(dst, arrayIndex, load(selfPos))
165168
selfPos += 1
166169
arrayIndex += 1
167170
}
168171
}
169172

170173
@inline
171174
def generic_store(startIndex: Int,
172-
src: Array[ElementType], offset: Int, length: Int): Unit = {
175+
src: Array[ElementType], offset: Int, length: Int)(
176+
implicit arrayOps: ArrayOps[ElementType]): Unit = {
173177
var selfPos = startIndex
174178
val endPos = selfPos + length
175179
var arrayIndex = offset
176180
while (selfPos != endPos) {
177-
store(selfPos, src(arrayIndex))
181+
store(selfPos, arrayOps.get(src, arrayIndex))
178182
selfPos += 1
179183
arrayIndex += 1
180184
}

javalib/src/main/scala/java/nio/GenDataViewBuffer.scala

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,11 @@ private[nio] object GenDataViewBuffer {
3737
val viewCapacity =
3838
(byteBufferLimit - byteBufferPos) / newDataViewBuffer.bytesPerElem
3939
val byteLength = viewCapacity * newDataViewBuffer.bytesPerElem
40-
val dataView = newDataView(
40+
val dataView = new DataView(
4141
byteArray.buffer, byteArray.byteOffset + byteBufferPos, byteLength)
4242
newDataViewBuffer(dataView,
4343
0, viewCapacity, byteBuffer.isReadOnly(), byteBuffer.isBigEndian)
4444
}
45-
46-
/* Work around for https://github.com/joyent/node/issues/6051
47-
* node 0.10 does not like creating a DataView whose byteOffset is equal to
48-
* the buffer's length, even if byteLength == 0.
49-
*/
50-
@inline
51-
private def newDataView(buffer: ArrayBuffer, byteOffset: Int, byteLength: Int): DataView = {
52-
if (byteLength == 0)
53-
lit(buffer = buffer, byteOffset = byteOffset, byteLength = byteLength).asInstanceOf[DataView]
54-
else
55-
new DataView(buffer, byteOffset, byteLength)
56-
}
5745
}
5846

5947
/* The underlying `val self` is intentionally public because
@@ -65,8 +53,6 @@ private[nio] final class GenDataViewBuffer[B <: Buffer] private (val self: B)
6553

6654
import self._
6755

68-
import GenDataViewBuffer.newDataView
69-
7056
type NewThisDataViewBuffer = GenDataViewBuffer.NewDataViewBuffer[BufferType]
7157

7258
@inline
@@ -76,7 +62,7 @@ private[nio] final class GenDataViewBuffer[B <: Buffer] private (val self: B)
7662
val dataView = _dataView
7763
val pos = position()
7864
val newCapacity = limit() - pos
79-
val slicedDataView = newDataView(dataView.buffer,
65+
val slicedDataView = new DataView(dataView.buffer,
8066
dataView.byteOffset + bytesPerElem*pos, bytesPerElem*newCapacity)
8167
newDataViewBuffer(slicedDataView,
8268
0, newCapacity, isReadOnly(), isBigEndian)

javalib/src/main/scala/java/nio/GenHeapBuffer.scala

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
package java.nio
1414

15+
import java.util.internal.GenericArrayOps._
16+
1517
private[nio] object GenHeapBuffer {
1618
def apply[B <: Buffer](self: B): GenHeapBuffer[B] =
1719
new GenHeapBuffer(self)
@@ -25,8 +27,9 @@ private[nio] object GenHeapBuffer {
2527
def generic_wrap[BufferType <: Buffer, ElementType](
2628
array: Array[ElementType], arrayOffset: Int, capacity: Int,
2729
initialPosition: Int, initialLength: Int, isReadOnly: Boolean)(
28-
implicit newHeapBuffer: NewHeapBuffer[BufferType, ElementType]): BufferType = {
29-
if (arrayOffset < 0 || capacity < 0 || arrayOffset+capacity > array.length)
30+
implicit arrayOps: ArrayOps[ElementType],
31+
newHeapBuffer: NewHeapBuffer[BufferType, ElementType]): BufferType = {
32+
if (arrayOffset < 0 || capacity < 0 || arrayOffset+capacity > arrayOps.length(array))
3033
throw new IndexOutOfBoundsException
3134
val initialLimit = initialPosition + initialLength
3235
if (initialPosition < 0 || initialLength < 0 || initialLimit > capacity)
@@ -86,12 +89,12 @@ private[nio] final class GenHeapBuffer[B <: Buffer] private (val self: B)
8689
}
8790

8891
@inline
89-
def generic_load(index: Int): ElementType =
90-
_array(_arrayOffset + index)
92+
def generic_load(index: Int)(implicit arrayOps: ArrayOps[ElementType]): ElementType =
93+
arrayOps.get(_array, _arrayOffset + index)
9194

9295
@inline
93-
def generic_store(index: Int, elem: ElementType): Unit =
94-
_array(_arrayOffset + index) = elem
96+
def generic_store(index: Int, elem: ElementType)(implicit arrayOps: ArrayOps[ElementType]): Unit =
97+
arrayOps.set(_array, _arrayOffset + index, elem)
9598

9699
@inline
97100
def generic_load(startIndex: Int,

javalib/src/main/scala/java/nio/charset/Charset.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class Charset protected (canonicalName: String,
3636

3737
override final def toString(): String = name()
3838

39-
override final def hashCode(): Int = name().##
39+
override final def hashCode(): Int = name().hashCode()
4040

4141
override final def compareTo(that: Charset): Int =
4242
name().compareToIgnoreCase(that.name())

javalib/src/main/scala/java/nio/charset/UTF_16_Common.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private[charset] abstract class UTF_16_Common protected (
119119
UTF_16_Common.this, 2.0f,
120120
if (endianness == AutoEndian) 4.0f else 2.0f,
121121
// Character 0xfffd
122-
if (endianness == LittleEndian) Array(-3, -1) else Array(-1, -3)) {
122+
if (endianness == LittleEndian) Array(-3.toByte, -1.toByte) else Array(-1.toByte, -3.toByte)) {
123123

124124
private var needToWriteBOM: Boolean = endianness == AutoEndian
125125

0 commit comments

Comments
 (0)