scala

class Enumeration

[source: scala/Enumeration.scala]

@serializable

@SerialVersionUID(8476000850333817230L)

abstract class Enumeration(initial : Int, names : java.lang.String*)
extends AnyRef

Defines a finite set of values specific to the enumeration. Typically these values enumerate all possible forms something can take and provide a lightweight alternative to case classes.

Each call to a Value method adds a new unique value to the enumeration. To be accessible, these values are usually defined as val members of the evaluation.

All values in an enumeration share a common, unique type defined as the Value type member of the enumeration (Value selected on the stable identifier path of the enumeration instance).

Example use

  object Main extends Application {

    object WeekDay extends Enumeration {
      type WeekDay = Value
      val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
    }
    import WeekDay._

    def isWorkingDay(d: WeekDay) = ! (d == Sat || d == Sun)
 
    WeekDay.iterator filter isWorkingDay foreach println
  }
Parameters
initial - The initial value from which to count the integers that identifies values at run-time.
names - The sequence of names to give to this enumeration's values.
Author
Matthias Zenger
Version
1.0, 10/02/2004
Direct Known Subclasses:
Alignment, BorderPanel.Position, FileChooser.Result, FileChooser.SelectionMode, FlowPanel.Alignment, FormattedTextField.FocusLostBehavior, GridBagPanel.Fill, GridBagPanel.Anchor, ListView.IntervalMode, Orientation, Dialog.Message, Dialog.Options, Dialog.Result, ScrollPane.BarPolicy, TabbedPane.Layout, Table.AutoResizeMode, Table.IntervalMode, Table.ElementMode, BigDecimal.RoundingMode, Key, Key.Location

Additional Constructor Summary
def this : Enumeration
def this (names : java.lang.String*) : Enumeration
Value Summary
protected var nextId : Int
The integer to use to identify the next created value.
protected var nextName : Iterator[java.lang.String]
The string to use to name the next created value.
Method Summary
protected final def Value (i : Int, name : java.lang.String) : Value
Creates a fresh value, part of this enumeration, called name and identified by the integer i.
protected final def Value (name : java.lang.String) : Value
Creates a fresh value, part of this enumeration, called name.
protected final def Value (i : Int) : Value
Creates a fresh value, part of this enumeration, identified by the integer i.
protected final def Value : Value
Creates a fresh value, part of this enumeration.
final def apply (x : Int) : Value
The value of this enumeration with given id `x`
def exists (p : (Value) => Boolean) : Boolean
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.
def filter (p : (Value) => Boolean) : Iterator[Value]
Returns all values of this enumeration that satisfy the predicate p. The order of values is preserved.
def flatMap [B](f : (Value) => Iterator[B]) : Iterator[B]
Applies the given function f to each value of this enumeration, then concatenates the results.
def forall (p : (Value) => Boolean) : Boolean
Apply a predicate p to all values of this enumeration and return true, iff the predicate yields true for all values.
def foreach (f : (Value) => Unit) : Unit
Apply a function f to all values of this enumeration.
final def iterator : Iterator[Value]
A new iterator over all values of this enumeration.
def map [B](f : (Value) => B) : Iterator[B]
Returns an iterator resulting from applying the given function f to each value of this enumeration.
final def maxId : Int
The highest integer amongst those used to identify values in this enumeration.
def name : java.lang.String
The name of this enumeration.
override def toString : java.lang.String
The name of this enumeration.
def valueOf (s : java.lang.String) : Option[Value]
def values : ValueSet
The values of this enumeration as a set.
final def withName (s : java.lang.String) : Value
Returns a Value from this Enumeration whose name matches the argument s. You can pass a String* set of names to the constructor, or initialize each Enumeration with Value(String). Otherwise, the names are determined automatically through reflection. Note the change here wrt 2.7 is intentional. You should know whether a name is in an Enumeration beforehand. If not, just use find on values.
Methods inherited from AnyRef
getClass, hashCode, equals, clone, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Class Summary
protected class Val (i : Int, name : java.lang.String) extends Value
A class implementing the Value type. This class can be overriden to change the enumeration's naming and integer identification behaviour.
abstract class Value extends Ordered[Value]
The type of the enumerated values.
class ValueSet (val ids : BitSet) extends Set[Value] with SetLike[Value, ValueSet]
A class for sets of values Iterating through this set will yield values in increasing order of their ids.
Object Summary
object ValueSet extends AnyRef
A factory object for value sets
Additional Constructor Details
def this : Enumeration

def this(names : java.lang.String*) : Enumeration

Value Details
protected var nextId : Int
The integer to use to identify the next created value.

protected var nextName : Iterator[java.lang.String]
The string to use to name the next created value.

Method Details
override def toString : java.lang.String
The name of this enumeration.

def values : ValueSet
The values of this enumeration as a set.

final def maxId : Int
The highest integer amongst those used to identify values in this enumeration.

final def apply(x : Int) : Value
The value of this enumeration with given id `x`

final def withName(s : java.lang.String) : Value
Returns a Value from this Enumeration whose name matches the argument s. You can pass a String* set of names to the constructor, or initialize each Enumeration with Value(String). Otherwise, the names are determined automatically through reflection. Note the change here wrt 2.7 is intentional. You should know whether a name is in an Enumeration beforehand. If not, just use find on values.
Parameters
s - an Enumeration name
Returns
the Value of this Enumeration if its name matches s
Throws
java.util.NoSuchElementException - if no Value with a matching name is in this Enumeration

protected final def Value : Value
Creates a fresh value, part of this enumeration.

protected final def Value(i : Int) : Value
Creates a fresh value, part of this enumeration, identified by the integer i.
Parameters
i - An integer that identifies this value at run-time. It must be unique amongst all values of the enumeration.
Returns
..

protected final def Value(name : java.lang.String) : Value
Creates a fresh value, part of this enumeration, called name.
Parameters
name - A human-readable name for that value.

protected final def Value(i : Int, name : java.lang.String) : Value
Creates a fresh value, part of this enumeration, called name and identified by the integer i.
Parameters
i - An integer that identifies this value at run-time. It must be unique amongst all values of the enumeration.
name - A human-readable name for that value.
Returns
..

@deprecated("use toString instead")

def name : java.lang.String
The name of this enumeration.

@deprecated("use withName instead")

def valueOf(s : java.lang.String) : Option[Value]

@deprecated("use values.iterator instead")

final def iterator : Iterator[Value]
A new iterator over all values of this enumeration.

@deprecated("use values.foreach instead")

def foreach(f : (Value) => Unit) : Unit
Apply a function f to all values of this enumeration.

@deprecated("use values.forall instead")

def forall(p : (Value) => Boolean) : Boolean
Apply a predicate p to all values of this enumeration and return true, iff the predicate yields true for all values.

@deprecated("use values.exists instead")

def exists(p : (Value) => Boolean) : Boolean
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.map instead")

def map[B](f : (Value) => B) : Iterator[B]
Returns an iterator resulting from applying the given function f to each value of this enumeration.

@deprecated("use values.flatMap instead")

def flatMap[B](f : (Value) => Iterator[B]) : Iterator[B]
Applies the given function f to each value of this enumeration, then concatenates the results.

@deprecated("use values.filter instead")

def filter(p : (Value) => Boolean) : Iterator[Value]
Returns all values of this enumeration that satisfy the predicate p. The order of values is preserved.