diff options
author | Hubert Plociniczak <hubert.plociniczak@epfl.ch> | 2011-01-04 10:38:41 +0000 |
---|---|---|
committer | Hubert Plociniczak <hubert.plociniczak@epfl.ch> | 2011-01-04 10:38:41 +0000 |
commit | a87d132bb752858dc5f8ac0d450a33f58dd12cba (patch) | |
tree | 32799c6c6515b752382f50d5ddf3b587e083f20f /src | |
parent | 1f4d528702ca32ed01e500ea2ef2e9b2ebbe07d1 (diff) | |
download | scala-a87d132bb752858dc5f8ac0d450a33f58dd12cba.tar.gz scala-a87d132bb752858dc5f8ac0d450a33f58dd12cba.tar.bz2 scala-a87d132bb752858dc5f8ac0d450a33f58dd12cba.zip |
Closes #3687, #3719, #3950, #3616.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/Enumeration.scala | 94 |
1 files changed, 16 insertions, 78 deletions
diff --git a/src/library/scala/Enumeration.scala b/src/library/scala/Enumeration.scala index 7039fec4bf..c73829bd6c 100644 --- a/src/library/scala/Enumeration.scala +++ b/src/library/scala/Enumeration.scala @@ -48,13 +48,12 @@ import java.lang.reflect.{ Modifier, Method => JMethod, Field => JField } * @param names The sequence of names to give to this enumeration's values. * * @author Matthias Zenger - * @version 1.0, 10/02/2004 */ @SerialVersionUID(8476000850333817230L) abstract class Enumeration(initial: Int, names: String*) extends Serializable { thisenum => - def this() = this(0, null) + def this() = this(0) def this(names: String*) = this(0, names: _*) /* Note that `readResolve` cannot be private, since otherwise @@ -81,7 +80,7 @@ abstract class Enumeration(initial: Int, names: String*) extends Serializable { */ def values: ValueSet = { if (!vsetDefined) { - vset = new ValueSet(immutable.BitSet.empty ++ (vmap.values map (_.id))) + vset = new ValueSet(immutable.SortedSet.empty[Int] ++ (vmap.values map (_.id))) vsetDefined = true } vset @@ -92,8 +91,8 @@ abstract class Enumeration(initial: Int, names: String*) extends Serializable { /** The string to use to name the next created value. */ protected var nextName = names.iterator - private def nextNameOrElse(orElse: => String) = - if (nextName.hasNext) nextName.next else orElse + private def nextNameOrNull = + if (nextName.hasNext) nextName.next else null /** The highest integer amongst those used to identify values in this * enumeration. */ @@ -133,13 +132,14 @@ abstract class Enumeration(initial: Int, names: String*) extends Serializable { * * @param i An integer that identifies this value at run-time. It must be * unique amongst all values of the enumeration. - * @return .. + * @return Fresh value identified by <code>i</code>. */ - protected final def Value(i: Int): Value = Value(i, nextNameOrElse(null)) + protected final def Value(i: Int): Value = Value(i, nextNameOrNull) /** Creates a fresh value, part of this enumeration, called <code>name</code>. * * @param name A human-readable name for that value. + * @return Fresh value called <code>name</code>. */ protected final def Value(name: String): Value = Value(nextId, name) @@ -149,14 +149,15 @@ abstract class Enumeration(initial: Int, names: String*) extends Serializable { * @param i An integer that identifies this value at run-time. It must be * unique amongst all values of the enumeration. * @param name A human-readable name for that value. - * @return .. + * @return Fresh value with the provided identifier <code>i</code> and name <code>name</code>. */ protected final def Value(i: Int, name: String): Value = new Val(i, name) private def populateNameMap() { // The list of possible Value methods: 0-args which return a conforming type - val methods = getClass.getMethods filter (m => m.getParameterTypes.isEmpty && classOf[Value].isAssignableFrom(m.getReturnType)) - + val methods = getClass.getMethods filter (m => m.getParameterTypes.isEmpty && + classOf[Value].isAssignableFrom(m.getReturnType) && + m.getDeclaringClass != classOf[Enumeration]) methods foreach { m => val name = m.getName // invoke method to obtain actual `Value` instance @@ -172,9 +173,7 @@ abstract class Enumeration(initial: Int, names: String*) extends Serializable { /* Obtains the name for the value with id `i`. If no name is cached * in `nmap`, it populates `nmap` using reflection. */ - private def nameOf(i: Int): String = synchronized { - nmap.getOrElse(i, { populateNameMap() ; nmap(i) }) - } + private def nameOf(i: Int): String = synchronized { nmap.getOrElse(i, { populateNameMap() ; nmap(i) }) } /** The type of the enumerated values. */ @SerialVersionUID(7091335633555234129L) @@ -190,23 +189,6 @@ abstract class Enumeration(initial: Int, names: String*) extends Serializable { case _ => false } override def hashCode: Int = id.## - - /** this enumeration value as an <code>Int</code> bit mask. - * @throws IllegalArgumentException if <code>id</code> is greater than 31 - */ - @deprecated("mask32 will be removed") - def mask32: Int = { - if (id >= 32) throw new IllegalArgumentException - 1 << id - } - /** this enumeration value as a <code>Long</code> bit mask. - * @throws IllegalArgumentException if <code>id</code> is greater than 63 - */ - @deprecated("mask64 will be removed") - def mask64: Long = { - if (id >= 64) throw new IllegalArgumentException - 1L << id - } } /** A class implementing the <a href="Enumeration.Value.html" @@ -216,7 +198,7 @@ abstract class Enumeration(initial: Int, names: String*) extends Serializable { */ @SerialVersionUID(0 - 3501153230598116017L) protected class Val(i: Int, name: String) extends Value with Serializable { - def this(i: Int) = this(i, nextNameOrElse(i.toString)) + def this(i: Int) = this(i, nextNameOrNull) def this(name: String) = this(nextId, name) def this() = this(nextId) @@ -240,9 +222,9 @@ abstract class Enumeration(initial: Int, names: String*) extends Serializable { /** A class for sets of values * Iterating through this set will yield values in increasing order of their ids. - * @param ids The set of ids of values, organized as a BitSet. + * @param ids The set of ids of values, organized as a SortedSet. */ - class ValueSet private[Enumeration] (val ids: immutable.BitSet) extends Set[Value] with SetLike[Value, ValueSet] { + class ValueSet private[Enumeration] (val ids: immutable.SortedSet[Int]) extends Set[Value] with SetLike[Value, ValueSet] { override def empty = ValueSet.empty def contains(v: Value) = ids contains (v.id) def + (value: Value) = new ValueSet(ids + value.id) @@ -257,7 +239,7 @@ abstract class Enumeration(initial: Int, names: String*) extends Serializable { import generic.CanBuildFrom /** The empty value set */ - val empty = new ValueSet(immutable.BitSet.empty) + val empty = new ValueSet(immutable.SortedSet.empty) /** A value set consisting of given elements */ def apply(elems: Value*): ValueSet = empty ++ elems /** A builder object for value sets */ @@ -269,48 +251,4 @@ abstract class Enumeration(initial: Int, names: String*) extends Serializable { def apply() = newBuilder } } - - /** The name of this enumeration. */ - @deprecated("use toString instead") def name = toString - - @deprecated("use withName instead") - def valueOf(s: String) = values.find(_.toString == s) - - /** A new iterator over all values of this enumeration. */ - @deprecated("use values.iterator instead") - final def iterator: Iterator[Value] = values.iterator - - /** Apply a function f to all values of this enumeration. */ - @deprecated("use values.foreach instead") - def foreach(f: Value => Unit): Unit = this.iterator foreach f - - /** Apply a predicate p to all values of this enumeration and return - * true, iff the predicate yields true for all values. - */ - @deprecated("use values.forall instead") - def forall(p: Value => Boolean): Boolean = this.iterator forall p - - /** Apply a predicate p to all values of this enumeration and return - * true, iff there is at least one value for which p yields true. - */ - @deprecated("use values.exists instead") - def exists(p: Value => Boolean): Boolean = this.iterator exists p - - /** Returns an iterator resulting from applying the given function f to each - * value of this enumeration. - */ - @deprecated("use values.map instead") - def map[B](f: Value => B): Iterator[B] = this.iterator map f - - /** Applies the given function f to each value of this enumeration, then - * concatenates the results. - */ - @deprecated("use values.flatMap instead") - def flatMap[B](f: Value => TraversableOnce[B]): Iterator[B] = this.iterator flatMap f - - /** Returns all values of this enumeration that satisfy the predicate p. - * The order of values is preserved. - */ - @deprecated("use values.filter instead") - def filter(p: Value => Boolean): Iterator[Value] = this.iterator filter p } |