"Fossies" - the Fresh Open Source Software Archive

Member "scala-js-1.3.1/javalib/src/main/scala/java/util/AbstractMap.scala" (14 Nov 2020, 4797 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 "AbstractMap.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.annotation.tailrec
   16 
   17 import ScalaOps._
   18 
   19 object AbstractMap {
   20 
   21   private def entryEquals[K, V](entry: Map.Entry[K, V], other: Any): Boolean = {
   22     other match {
   23       case other: Map.Entry[_, _] =>
   24         Objects.equals(entry.getKey(), other.getKey()) &&
   25         Objects.equals(entry.getValue(), other.getValue())
   26       case _ => false
   27     }
   28   }
   29 
   30   private def entryHashCode[K, V](entry: Map.Entry[K, V]): Int =
   31     Objects.hashCode(entry.getKey()) ^ Objects.hashCode(entry.getValue())
   32 
   33   class SimpleEntry[K, V](private var key: K, private var value: V)
   34       extends Map.Entry[K, V] with Serializable {
   35 
   36     def this(entry: Map.Entry[_ <: K, _ <: V]) =
   37       this(entry.getKey(), entry.getValue())
   38 
   39     def getKey(): K = key
   40 
   41     def getValue(): V = value
   42 
   43     def setValue(value: V): V = {
   44       val oldValue = this.value
   45       this.value = value
   46       oldValue
   47     }
   48 
   49     override def equals(o: Any): Boolean =
   50       entryEquals(this, o)
   51 
   52     override def hashCode(): Int =
   53       entryHashCode(this)
   54 
   55     override def toString(): String =
   56       "" + getKey() + "=" + getValue()
   57   }
   58 
   59   class SimpleImmutableEntry[K, V](key: K, value: V)
   60       extends Map.Entry[K, V] with Serializable {
   61 
   62     def this(entry: Map.Entry[_ <: K, _ <: V]) =
   63       this(entry.getKey(), entry.getValue())
   64 
   65     def getKey(): K = key
   66 
   67     def getValue(): V = value
   68 
   69     def setValue(value: V): V =
   70       throw new UnsupportedOperationException()
   71 
   72     override def equals(o: Any): Boolean =
   73       entryEquals(this, o)
   74 
   75     override def hashCode(): Int =
   76       entryHashCode(this)
   77 
   78     override def toString(): String =
   79       "" + getKey() + "=" + getValue()
   80   }
   81 }
   82 
   83 abstract class AbstractMap[K, V] protected () extends java.util.Map[K, V] {
   84   self =>
   85 
   86   def size(): Int = entrySet().size()
   87 
   88   def isEmpty(): Boolean = size() == 0
   89 
   90   def containsValue(value: Any): Boolean =
   91     entrySet().scalaOps.exists(entry => Objects.equals(value, entry.getValue()))
   92 
   93   def containsKey(key: Any): Boolean =
   94     entrySet().scalaOps.exists(entry => Objects.equals(key, entry.getKey()))
   95 
   96   def get(key: Any): V = {
   97     entrySet().scalaOps.find(entry => Objects.equals(key, entry.getKey())).fold[V] {
   98       null.asInstanceOf[V]
   99     } { entry =>
  100       entry.getValue()
  101     }
  102   }
  103 
  104   def put(key: K, value: V): V =
  105     throw new UnsupportedOperationException()
  106 
  107   def remove(key: Any): V = {
  108     @tailrec
  109     def findAndRemove(iter: Iterator[Map.Entry[K, V]]): V = {
  110       if (iter.hasNext()) {
  111         val item = iter.next()
  112         if (Objects.equals(key, item.getKey())) {
  113           iter.remove()
  114           item.getValue()
  115         } else
  116           findAndRemove(iter)
  117       } else
  118         null.asInstanceOf[V]
  119     }
  120     findAndRemove(entrySet().iterator())
  121   }
  122 
  123   def putAll(m: Map[_ <: K, _ <: V]): Unit =
  124     m.entrySet().scalaOps.foreach(e => put(e.getKey(), e.getValue()))
  125 
  126   def clear(): Unit =
  127     entrySet().clear()
  128 
  129   def keySet(): Set[K] = {
  130     new AbstractSet[K] {
  131       override def size(): Int = self.size()
  132 
  133       def iterator(): Iterator[K] = {
  134         new Iterator[K] {
  135           val iter = entrySet().iterator()
  136 
  137           def hasNext(): Boolean = iter.hasNext()
  138 
  139           def next(): K = iter.next().getKey()
  140 
  141           override def remove(): Unit = iter.remove()
  142         }
  143       }
  144     }
  145   }
  146 
  147   def values(): Collection[V] = {
  148     new AbstractCollection[V] {
  149       override def size(): Int = self.size()
  150 
  151       def iterator(): Iterator[V] = {
  152         new Iterator[V] {
  153           val iter = entrySet().iterator()
  154 
  155           def hasNext(): Boolean = iter.hasNext()
  156 
  157           def next(): V = iter.next().getValue()
  158 
  159           override def remove(): Unit = iter.remove()
  160         }
  161       }
  162     }
  163   }
  164 
  165   def entrySet(): Set[Map.Entry[K, V]]
  166 
  167   override def equals(o: Any): Boolean = {
  168     if (o.asInstanceOf[AnyRef] eq this) true
  169     else {
  170       o match {
  171         case m: Map[_, _] =>
  172           self.size() == m.size() &&
  173           entrySet().scalaOps.forall(item => Objects.equals(m.get(item.getKey()), item.getValue()))
  174         case _ => false
  175       }
  176     }
  177   }
  178 
  179   override def hashCode(): Int =
  180     entrySet().scalaOps.foldLeft(0)((prev, item) => item.hashCode + prev)
  181 
  182   override def toString(): String = {
  183     var result = "{"
  184     var first = true
  185     val iter = entrySet().iterator()
  186     while (iter.hasNext()) {
  187       val entry = iter.next()
  188       if (first)
  189         first = false
  190       else
  191         result += ", "
  192       result = result + entry.getKey() + "=" + entry.getValue()
  193     }
  194     result + "}"
  195   }
  196 }