From cf10e28a54aaeab124f9a939f71da7e09d299bcb Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Tue, 14 Feb 2017 11:17:53 +0100 Subject: Change enumeration members. Based on the discussion in #1970, enumeration objects now have three public members: - valueOf: Map[Int, E] - withName: Map[String, E] - values: Iterable[E] Also, the variance of case type parameters is now the same as in the corresponding type parameter of the enum class. --- library/src/scala/runtime/EnumValues.scala | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'library') diff --git a/library/src/scala/runtime/EnumValues.scala b/library/src/scala/runtime/EnumValues.scala index 6d2e56cf3..6f9d907b3 100644 --- a/library/src/scala/runtime/EnumValues.scala +++ b/library/src/scala/runtime/EnumValues.scala @@ -1,18 +1,21 @@ package scala.runtime -import scala.collection.immutable.Seq -import scala.collection.mutable.ResizableArray +import scala.collection.immutable.Map + +class EnumValues[E <: Enum] { + private var myMap: Map[Int, E] = Map() + private var fromNameCache: Map[String, E] = null -class EnumValues[E <: Enum] extends ResizableArray[E] { - private var valuesCache: List[E] = Nil def register(v: E) = { - ensureSize(v.enumTag + 1) - size0 = size0 max (v.enumTag + 1) - array(v.enumTag) = v - valuesCache = null + require(!myMap.contains(v.enumTag)) + myMap = myMap.updated(v.enumTag, v) + fromNameCache = null } - def values: Seq[E] = { - if (valuesCache == null) valuesCache = array.filter(_ != null).toList.asInstanceOf[scala.List[E]] - valuesCache + + def fromInt: Map[Int, E] = myMap + def fromName: Map[String, E] = { + if (fromNameCache == null) fromNameCache = myMap.values.map(v => v.toString -> v).toMap + fromNameCache } + def values: Iterable[E] = myMap.values } -- cgit v1.2.3