"Fossies" - the Fresh Open Source Software Archive

Member "scala-js-1.0.0-RC1/javalib/src/main/scala/java/util/ArrayList.scala" (21 Nov 2019, 1974 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.scalajs._
   16 
   17 class ArrayList[E] private (private[ArrayList] val inner: js.Array[E])
   18     extends AbstractList[E] with RandomAccess with Cloneable with Serializable {
   19   self =>
   20 
   21   def this(initialCapacity: Int) = {
   22     this(new js.Array[E])
   23     if (initialCapacity < 0)
   24       throw new IllegalArgumentException
   25   }
   26 
   27   def this() =
   28     this(new js.Array[E])
   29 
   30   def this(c: Collection[_ <: E]) = {
   31     this()
   32     addAll(c)
   33   }
   34 
   35   def trimToSize(): Unit = {
   36     // We ignore this as js.Array doesn't support explicit pre-allocation
   37   }
   38 
   39   def ensureCapacity(minCapacity: Int): Unit = {
   40     // We ignore this as js.Array doesn't support explicit pre-allocation
   41   }
   42 
   43   def size(): Int =
   44     inner.length
   45 
   46   override def clone(): AnyRef =
   47     new ArrayList(inner.jsSlice(0))
   48 
   49   def get(index: Int): E = {
   50     checkIndexInBounds(index)
   51     inner(index)
   52   }
   53 
   54   override def set(index: Int, element: E): E = {
   55     val e = get(index)
   56     inner(index) = element
   57     e
   58   }
   59 
   60   override def add(e: E): Boolean = {
   61     inner += e
   62     true
   63   }
   64 
   65   override def add(index: Int, element: E): Unit = {
   66     checkIndexOnBounds(index)
   67     inner.insert(index, element)
   68   }
   69 
   70   override def remove(index: Int): E = {
   71     checkIndexInBounds(index)
   72     inner.remove(index)
   73   }
   74 
   75   override def clear(): Unit =
   76     inner.clear()
   77 
   78   override def addAll(index: Int, c: Collection[_ <: E]): Boolean = {
   79     c match {
   80       case other: ArrayList[_] =>
   81         inner.splice(index, 0, other.inner.toSeq: _*)
   82         other.size > 0
   83       case _ => super.addAll(index, c)
   84     }
   85   }
   86 
   87   override protected def removeRange(fromIndex: Int, toIndex: Int): Unit =
   88     inner.splice(fromIndex, toIndex - fromIndex)
   89 
   90 }