"Fossies" - the Fresh Open Source Software Archive

Member "scala-js-1.3.1/javalib/src/main/scala/java/util/ArrayDeque.scala" (14 Nov 2020, 4017 Bytes) of package /linux/www/scala-js-1.3.1.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. See also the last Fossies "Diffs" side-by-side code changes report for "ArrayDeque.scala": 1.2.0_vs_1.3.0.

    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.js
   16 
   17 class ArrayDeque[E] private (private var inner: js.Array[E])
   18     extends AbstractCollection[E] with Deque[E] with Cloneable with Serializable {
   19   self =>
   20 
   21   private var status = 0
   22 
   23   def this(initialCapacity: Int) = {
   24     this(new js.Array[E])
   25 
   26     if (initialCapacity < 0)
   27       throw new IllegalArgumentException
   28   }
   29 
   30   def this() =
   31     this(16)
   32 
   33   def this(c: Collection[_ <: E]) = {
   34     this()
   35     addAll(c)
   36   }
   37 
   38   def addFirst(e: E): Unit =
   39     offerFirst(e)
   40 
   41   def addLast(e: E): Unit =
   42     offerLast(e)
   43 
   44   def offerFirst(e: E): Boolean = {
   45     if (e == null) {
   46       throw new NullPointerException()
   47     } else {
   48       inner = e +: inner
   49       status += 1
   50       true
   51     }
   52   }
   53 
   54   def offerLast(e: E): Boolean = {
   55     if (e == null) {
   56       throw new NullPointerException()
   57     } else {
   58       inner += e
   59       status += 1
   60       true
   61     }
   62   }
   63 
   64   def removeFirst(): E = {
   65     if (inner.isEmpty)
   66       throw new NoSuchElementException()
   67     else
   68       pollFirst()
   69   }
   70 
   71   def removeLast(): E = {
   72     if (inner.isEmpty)
   73       throw new NoSuchElementException()
   74     else
   75       pollLast()
   76   }
   77 
   78   def pollFirst(): E = {
   79     if (inner.isEmpty) null.asInstanceOf[E]
   80     else {
   81       val res = inner.remove(0)
   82       status += 1
   83       res
   84     }
   85   }
   86 
   87   def pollLast(): E = {
   88     if (inner.isEmpty) null.asInstanceOf[E]
   89     else inner.pop()
   90   }
   91 
   92   def getFirst(): E = {
   93     if (inner.isEmpty)
   94       throw new NoSuchElementException()
   95     else
   96       peekFirst()
   97   }
   98 
   99   def getLast(): E = {
  100     if (inner.isEmpty)
  101       throw new NoSuchElementException()
  102     else
  103       peekLast()
  104   }
  105 
  106   def peekFirst(): E = {
  107     if (inner.isEmpty) null.asInstanceOf[E]
  108     else inner.head
  109   }
  110 
  111   def peekLast(): E = {
  112     if (inner.isEmpty) null.asInstanceOf[E]
  113     else inner.last
  114   }
  115 
  116   def removeFirstOccurrence(o: Any): Boolean = {
  117     val index = inner.indexWhere(Objects.equals(_, o))
  118     if (index >= 0) {
  119       inner.remove(index)
  120       status += 1
  121       true
  122     } else
  123       false
  124   }
  125 
  126   def removeLastOccurrence(o: Any): Boolean = {
  127     val index = inner.lastIndexWhere(Objects.equals(_, o))
  128     if (index >= 0) {
  129       inner.remove(index)
  130       status += 1
  131       true
  132     } else
  133       false
  134   }
  135 
  136   override def add(e: E): Boolean = {
  137     addLast(e)
  138     true
  139   }
  140 
  141   def offer(e: E): Boolean = offerLast(e)
  142 
  143   def remove(): E = removeFirst()
  144 
  145   def poll(): E = pollFirst()
  146 
  147   def element(): E = getFirst()
  148 
  149   def peek(): E = peekFirst()
  150 
  151   def push(e: E): Unit = addFirst(e)
  152 
  153   def pop(): E = removeFirst()
  154 
  155   def size(): Int = inner.size
  156 
  157   private def failFastIterator(startIndex: Int, nex: (Int) => Int) = {
  158     new Iterator[E] {
  159       private def checkStatus() =
  160         if (self.status != actualStatus)
  161           throw new ConcurrentModificationException()
  162 
  163       private val actualStatus = self.status
  164 
  165       private var index: Int = startIndex
  166 
  167       def hasNext(): Boolean = {
  168         checkStatus()
  169         val n = nex(index)
  170         (n >= 0) && (n < inner.size)
  171       }
  172 
  173       def next(): E = {
  174         checkStatus()
  175         index = nex(index)
  176         inner(index)
  177       }
  178 
  179       override def remove(): Unit = {
  180         checkStatus()
  181         if (index < 0 || index >= inner.size) {
  182           throw new IllegalStateException()
  183         } else {
  184           inner.remove(index)
  185         }
  186       }
  187     }
  188   }
  189 
  190   def iterator(): Iterator[E] =
  191     failFastIterator(-1, x => (x + 1))
  192 
  193   def descendingIterator(): Iterator[E] =
  194     failFastIterator(inner.size, x => (x - 1))
  195 
  196   override def contains(o: Any): Boolean = inner.exists(Objects.equals(_, o))
  197 
  198   override def remove(o: Any): Boolean = removeFirstOccurrence(o)
  199 
  200   override def clear(): Unit = {
  201     if (!inner.isEmpty) status += 1
  202     inner.clear()
  203   }
  204 }