"Fossies" - the Fresh Open Source Software Archive

Member "scala-js-1.0.0-RC1/javalib/src/main/scala/java/util/AbstractCollection.scala" (21 Nov 2019, 2231 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 java.util
   14 
   15 import scala.annotation.tailrec
   16 
   17 import ScalaOps._
   18 
   19 import java.lang.{reflect => jlr}
   20 
   21 abstract class AbstractCollection[E] protected () extends Collection[E] {
   22   def iterator(): Iterator[E]
   23   def size(): Int
   24 
   25   def isEmpty(): Boolean = size == 0
   26 
   27   def contains(o: Any): Boolean =
   28     this.scalaOps.exists(Objects.equals(o, _))
   29 
   30   def toArray(): Array[AnyRef] =
   31     toArray(new Array[AnyRef](size))
   32 
   33   def toArray[T <: AnyRef](a: Array[T]): Array[T] = {
   34     val toFill: Array[T] =
   35       if (a.length >= size) a
   36       else jlr.Array.newInstance(a.getClass.getComponentType, size).asInstanceOf[Array[T]]
   37 
   38     val iter = iterator
   39     for (i <- 0 until size)
   40       toFill(i) = iter.next().asInstanceOf[T]
   41     if (toFill.length > size)
   42       toFill(size) = null.asInstanceOf[T]
   43     toFill
   44   }
   45 
   46   def add(e: E): Boolean =
   47     throw new UnsupportedOperationException()
   48 
   49   def remove(o: Any): Boolean = {
   50     @tailrec
   51     def findAndRemove(iter: Iterator[E]): Boolean = {
   52       if (iter.hasNext) {
   53         if (Objects.equals(iter.next(), o)) {
   54           iter.remove()
   55           true
   56         } else {
   57           findAndRemove(iter)
   58         }
   59       } else {
   60         false
   61       }
   62     }
   63     findAndRemove(iterator())
   64   }
   65 
   66   def containsAll(c: Collection[_]): Boolean =
   67     c.scalaOps.forall(this.contains(_))
   68 
   69   def addAll(c: Collection[_ <: E]): Boolean =
   70     c.scalaOps.foldLeft(false)((prev, elem) => add(elem) || prev)
   71 
   72   def removeAll(c: Collection[_]): Boolean =
   73     removeWhere(c.contains(_))
   74 
   75   def retainAll(c: Collection[_]): Boolean =
   76     removeWhere(!c.contains(_))
   77 
   78   def clear(): Unit =
   79     removeWhere(_ => true)
   80 
   81   private def removeWhere(p: Any => Boolean): Boolean = {
   82     val iter = iterator()
   83     var changed = false
   84     while (iter.hasNext) {
   85       if (p(iter.next())) {
   86         iter.remove()
   87         changed = true
   88       }
   89     }
   90     changed
   91   }
   92 
   93   override def toString(): String =
   94     this.scalaOps.mkString("[", ", ", "]")
   95 }