Skip to content

Commit

Permalink
Merge pull request scala-js#176 from techaddict/java_arrays_copyto
Browse files Browse the repository at this point in the history
Fix scala-js#163 completely added for func's for AnyRef
  • Loading branch information
sjrd committed Jan 27, 2014
2 parents 431e837 + 09eb1eb commit 7c44f71
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
20 changes: 20 additions & 0 deletions javalib/source/src/java/util/Arrays.scala
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,24 @@ object Arrays {
throw new IllegalArgumentException()
}

def copyOf(original: Array[AnyRef], newLen: Int): Array[AnyRef] = {
if (newLen >= 0)
return copyOfRange(original, 0, newLen)
throw new NegativeArraySizeException();
}

def copyOfRange(original: Array[AnyRef], start: Int, end: Int): Array[AnyRef] = {
if (start <= end) {
if (0 <= start && start <= original.length) {
val retLength = end - start
val copyLength = Math.min(retLength, original.length - start)
val ret = new Array[AnyRef](retLength)
System.arraycopy(original, start, ret, 0, copyLength)
return ret
}
throw new ArrayIndexOutOfBoundsException()
}
throw new IllegalArgumentException()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,12 @@ object ArraysTest extends JasmineTest {
expect(boolscopy).toEqual(Array[Boolean](false, true, false, false, false))
}

it("should respond to `copyOf` with key for AnyRef") {
val anyrefs: Array[AnyRef] = Array("a", "b", "c")
val anyrefscopy = Arrays.copyOf(anyrefs, 5)
expect(anyrefscopy).toEqual(Array[AnyRef]("a", "b", "c", null, null))
}

}

}

0 comments on commit 7c44f71

Please sign in to comment.