"Fossies" - the Fresh Open Source Software Archive

Member "scala-js-1.0.0-RC1/test-suite/shared/src/test/scala/org/scalajs/testsuite/javalib/util/AbstractCollectionTest.scala" (21 Nov 2019, 2818 Bytes) of package /linux/www/scala-js-1.0.0-RC1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Scala source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * Scala.js (https://www.scala-js.org/)
    3  *
    4  * Copyright EPFL.
    5  *
    6  * Licensed under Apache License 2.0
    7  * (https://www.apache.org/licenses/LICENSE-2.0).
    8  *
    9  * See the NOTICE file distributed with this work for
   10  * additional information regarding copyright ownership.
   11  */
   12 
   13 package org.scalajs.testsuite.javalib.util
   14 
   15 import java.{util => ju}
   16 
   17 import scala.reflect.ClassTag
   18 
   19 class AbstractCollectionTest extends CollectionTest {
   20   def factory: AbstractCollectionFactory = new AbstractCollectionFactory
   21 }
   22 
   23 class AbstractCollectionFactory extends CollectionFactory {
   24   import AbstractCollectionFactory._
   25 
   26   override def implementationName: String =
   27     "java.util.AbstractCollection"
   28 
   29   override def empty[E: ClassTag]: ju.AbstractCollection[E] =
   30     new AbstractCollectionImpl[E]
   31 
   32 }
   33 
   34 object AbstractCollectionFactory {
   35   /* A mutable implementation of `java.util.Collection[E]` that relies on all
   36    * the default behaviors implemented in `j.u.AbstractCollection`.
   37    *
   38    * Every modification allocates a new internal `Array`. This property is used
   39    * to reliably detect concurrent modifications.
   40    */
   41   private final class AbstractCollectionImpl[E] extends ju.AbstractCollection[E] {
   42     private var inner = new Array[AnyRef](0)
   43 
   44     override def add(elem: E): Boolean = {
   45       inner = ju.Arrays.copyOf(inner, inner.length + 1)
   46       inner(inner.length - 1) = elem.asInstanceOf[AnyRef]
   47       true
   48     }
   49 
   50     def size(): Int =
   51       inner.length
   52 
   53     def iterator(): ju.Iterator[E] =
   54       new AbstractCollectionImplIterator(inner)
   55 
   56     private final class AbstractCollectionImplIterator[E](
   57         private var iterInner: Array[AnyRef])
   58         extends ju.Iterator[E] {
   59 
   60       private[this] var nextIndex: Int = 0
   61       private[this] var canRemove: Boolean = false
   62 
   63       def hasNext(): Boolean = {
   64         checkConcurrentModification()
   65         nextIndex != inner.length
   66       }
   67 
   68       def next(): E = {
   69         checkConcurrentModification()
   70         if (nextIndex == inner.length)
   71           throw new ju.NoSuchElementException()
   72 
   73         val elem = inner(nextIndex).asInstanceOf[E]
   74         nextIndex += 1
   75         canRemove = true
   76         elem
   77       }
   78 
   79       override def remove(): Unit = {
   80         checkConcurrentModification()
   81         if (!canRemove)
   82           throw new IllegalStateException("remove() called before next()")
   83 
   84         nextIndex -= 1
   85         val newInner = ju.Arrays.copyOf(iterInner, iterInner.length - 1)
   86         var i = nextIndex
   87         while (i != newInner.length) {
   88           newInner(i) = iterInner(i + 1)
   89           i += 1
   90         }
   91         inner = newInner
   92         iterInner = newInner
   93         canRemove = false
   94       }
   95 
   96       private def checkConcurrentModification(): Unit = {
   97         if (inner ne iterInner)
   98           throw new ju.ConcurrentModificationException()
   99       }
  100     }
  101   }
  102 }