Skip to content

Commit 331b6d1

Browse files
committed
reduce the complexity of Enumeration lookups
1 parent c8cee34 commit 331b6d1

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

library/src/scala/Enumeration.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
153153
* @throws NoSuchElementException if no `Value` with a matching
154154
* name is in this `Enumeration`
155155
*/
156-
final def withName(s: String): Value = values.find(_.toString == s).getOrElse(
156+
final def withName(s: String): Value = values.byName.getOrElse(s,
157157
throw new NoSuchElementException(s"No value found for '$s'"))
158158

159159
/** Creates a fresh value, part of this enumeration. */
@@ -200,7 +200,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
200200
val value = m.invoke(this).asInstanceOf[Value]
201201
// verify that outer points to the correct Enumeration: ticket #3616.
202202
if (value.outerEnum eq thisenum) {
203-
val id = Int.unbox(classOf[Val] getMethod "id" invoke value)
203+
val id: Int = value.id
204204
nmap += ((id, name))
205205
}
206206
}
@@ -294,6 +294,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
294294
/** Creates a bit mask for the zero-adjusted ids in this set as a
295295
* new array of longs */
296296
def toBitMask: Array[Long] = nnIds.toBitMask
297+
private[Enumeration] lazy val byName: Map[String, Value] = iterator.map( v => v.toString -> v).toMap
297298
}
298299

299300
/** A factory object for value sets */

0 commit comments

Comments
 (0)