summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntonio Cunei <antonio.cunei@epfl.ch>2011-09-07 15:58:09 +0000
committerAntonio Cunei <antonio.cunei@epfl.ch>2011-09-07 15:58:09 +0000
commit33321156c4f7768c8a85365239898ca220580d10 (patch)
treee676fa39ad17d3ddd708e94bf4c892c899dcceb0
parent58a8c69efd60eb688088fbebc3069c206c3aff5b (diff)
downloadscala-33321156c4f7768c8a85365239898ca220580d10.tar.gz
scala-33321156c4f7768c8a85365239898ca220580d10.tar.bz2
scala-33321156c4f7768c8a85365239898ca220580d10.zip
Backport of r23893.
-rw-r--r--src/library/scala/Enumeration.scala33
-rw-r--r--test/files/run/t3687.check2
-rw-r--r--test/files/run/t3687.scala6
-rw-r--r--test/files/run/t3719.check4
-rw-r--r--test/files/run/t3719.scala35
-rw-r--r--test/files/run/t3950.check3
-rw-r--r--test/files/run/t3950.scala17
7 files changed, 83 insertions, 17 deletions
diff --git a/src/library/scala/Enumeration.scala b/src/library/scala/Enumeration.scala
index 871de3714d..c1fe45c8c1 100644
--- a/src/library/scala/Enumeration.scala
+++ b/src/library/scala/Enumeration.scala
@@ -48,14 +48,13 @@ 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
*/
@serializable
@SerialVersionUID(8476000850333817230L)
abstract class Enumeration(initial: Int, names: String*) {
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
@@ -82,7 +81,7 @@ abstract class Enumeration(initial: Int, names: String*) {
*/
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
@@ -93,8 +92,8 @@ abstract class Enumeration(initial: Int, names: String*) {
/** 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. */
@@ -134,13 +133,14 @@ abstract class Enumeration(initial: Int, names: String*) {
*
* @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)
@@ -150,14 +150,15 @@ abstract class Enumeration(initial: Int, names: String*) {
* @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
@@ -173,9 +174,7 @@ abstract class Enumeration(initial: Int, names: String*) {
/* 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. */
@serializable
@@ -219,7 +218,7 @@ abstract class Enumeration(initial: Int, names: String*) {
@serializable
@SerialVersionUID(0 - 3501153230598116017L)
protected class Val(i: Int, name: String) extends Value {
- 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)
@@ -243,9 +242,9 @@ abstract class Enumeration(initial: Int, names: String*) {
/** 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)
@@ -260,7 +259,7 @@ abstract class Enumeration(initial: Int, names: String*) {
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 */
diff --git a/test/files/run/t3687.check b/test/files/run/t3687.check
new file mode 100644
index 0000000000..0f35862645
--- /dev/null
+++ b/test/files/run/t3687.check
@@ -0,0 +1,2 @@
+t.ValueSet(a, b)
+t.ValueSet(a, b) \ No newline at end of file
diff --git a/test/files/run/t3687.scala b/test/files/run/t3687.scala
new file mode 100644
index 0000000000..25141f8a32
--- /dev/null
+++ b/test/files/run/t3687.scala
@@ -0,0 +1,6 @@
+object t extends Enumeration { val a, b = Value }
+
+object Test extends Application {
+ println(t.values)
+ println(t.values)
+}
diff --git a/test/files/run/t3719.check b/test/files/run/t3719.check
new file mode 100644
index 0000000000..111fc7fd63
--- /dev/null
+++ b/test/files/run/t3719.check
@@ -0,0 +1,4 @@
+List(Mon, Tue, Wed, Thu, Fri, Sat, Sun)
+Mon
+Tue
+Mon \ No newline at end of file
diff --git a/test/files/run/t3719.scala b/test/files/run/t3719.scala
new file mode 100644
index 0000000000..2436e0cdf6
--- /dev/null
+++ b/test/files/run/t3719.scala
@@ -0,0 +1,35 @@
+object Days extends Enumeration {
+ type Day = DayValue
+ val Mon, Tue, Wed, Thu, Fri, Sat, Sun = new DayValue // DayValue
+
+ protected class DayValue extends Val {
+ def isWeekday: Boolean =
+ this match {
+ case Sun => false
+ case Sat => false
+ case _ => true
+ }
+ }
+}
+
+object Test extends Application {
+ def dayElementsShouldBeNamed(): List[String] =
+ Days.values.toList.sorted.map(x => x.toString)
+
+ def nameOfMon(): String = {
+ import Days._
+ val d: Day = Mon
+ d.toString
+ }
+
+ def nameOfTue(): String = {
+ import Days._
+ val d: Day = Tue
+ d.toString
+ }
+
+ println(dayElementsShouldBeNamed())
+ println(nameOfMon())
+ println(nameOfTue())
+ println(nameOfMon())
+}
diff --git a/test/files/run/t3950.check b/test/files/run/t3950.check
new file mode 100644
index 0000000000..10f81c51ad
--- /dev/null
+++ b/test/files/run/t3950.check
@@ -0,0 +1,3 @@
+minus
+zero
+plus \ No newline at end of file
diff --git a/test/files/run/t3950.scala b/test/files/run/t3950.scala
new file mode 100644
index 0000000000..fe99a7cc6f
--- /dev/null
+++ b/test/files/run/t3950.scala
@@ -0,0 +1,17 @@
+
+object NegativeId extends Enumeration {
+ val Negative = Value(-1, "minus")
+ val Zero = Value(0, "zero")
+ val Positive = Value(1, "plus")
+
+ def fromInt(id: Int) = values find (_.id == id) match {
+ case Some(v) => v
+ case None => null
+ }
+}
+
+object Test extends Application {
+ println(NegativeId.fromInt(-1))
+ println(NegativeId.fromInt(0))
+ println(NegativeId.fromInt(1))
+}