Skip to content

Commit

Permalink
WIP: Annotate & IOU-test j.u.List default methods
Browse files Browse the repository at this point in the history
This PR builds upon merged PR scala-native#1937 by adding the JavaDefaultMethod
annotation to the trait

IOU: The discerning reader will notice that there is no ListTest.scala
     in this WIP.

     Before this PR can move out of WIP state the corresponding
     Scala.js ListTest.scala should be ported. Currently ScalaNative
     has no ListTest.

     Scala.js ListTest has a number of prerequisites, few of which
     are currently ported (such as TestMainBase).  I believe
     porting those items in a series of separate PRs. Tedious, but
     it will leave a better audit trail.

See Issue scala-native#1972 for background & discussion of JavaDefaultMethod
annotation.

This PR is WIP because it will fail to build until after PR scala-native#1997
is merged and this PR re-based upon it.
  • Loading branch information
LeeTibbert committed Nov 17, 2020
1 parent 3197bf3 commit 98da2d2
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions javalib/src/main/scala/java/util/List.scala
@@ -1,21 +1,51 @@
// Ported from Scala.js commit: ad7d82f dated: 2020-10-05

package java.util

import java.util.function.UnaryOperator

import scala.scalanative.annotation.JavaDefaultMethod

trait List[E] extends Collection[E] {
@JavaDefaultMethod
def replaceAll(operator: UnaryOperator[E]): Unit = {
val iter = listIterator()
while (iter.hasNext())
iter.set(operator.apply(iter.next()))
}

@JavaDefaultMethod
def sort(c: Comparator[_ >: E]): Unit = {
val arrayBuf = toArray()
Arrays.sort[AnyRef with E](arrayBuf.asInstanceOf[Array[AnyRef with E]], c)

val len = arrayBuf.length

if (this.isInstanceOf[RandomAccess]) {
var i = 0
while (i != len) {
set(i, arrayBuf(i).asInstanceOf[E])
i += 1
}
} else {
var i = 0
val iter = listIterator()
while (i != len) {
iter.next()
iter.set(arrayBuf(i).asInstanceOf[E])
i += 1
}
}
}

def get(index: Int): E
def set(index: Int, element: E): E
def add(element: E): Boolean
def add(index: Int, element: E): Unit
def remove(index: Int): E
def indexOf(o: Any): Int
def lastIndexOf(o: Any): Int
def listIterator(): ListIterator[E]
def listIterator(index: Int): ListIterator[E]
def subList(fromIndex: Int, toIndex: Int): List[E]
def addAll(c: Collection[_ <: E]): Boolean
def addAll(index: Int, c: Collection[_ <: E]): Boolean
def clear(): Unit
def isEmpty(): Boolean
def iterator(): Iterator[E]
def contains(o: Any): Boolean
def size(): Int
}

0 comments on commit 98da2d2

Please sign in to comment.