org.apache.commons.lang.enum
クラス ValuedEnum

java.lang.Object
  拡張org.apache.commons.lang.enum.Enum
      拡張org.apache.commons.lang.enum.ValuedEnum
すべての実装インタフェース:
Comparable, Serializable

public abstract class ValuedEnum
extends Enum

Abstract superclass for type-safe enums with integer values suitable for use in switch statements.

NOTE:Due to the way in which Java ClassLoaders work, comparing Enum objects should always be done using the equals() method, not ==. The equals() method will try == first so in most cases the effect is the same.

To use this class, it must be subclassed. For example:

 public final class JavaVersionEnum extends ValuedEnum {
   //standard enums for version of JVM
   public static final int  JAVA1_0_VALUE  = 100;
   public static final int  JAVA1_1_VALUE  = 110;
   public static final int  JAVA1_2_VALUE  = 120;
   public static final int  JAVA1_3_VALUE  = 130;
   public static final JavaVersionEnum  JAVA1_0  = new JavaVersionEnum( "Java 1.0", JAVA1_0_VALUE );
   public static final JavaVersionEnum  JAVA1_1  = new JavaVersionEnum( "Java 1.1", JAVA1_1_VALUE );
   public static final JavaVersionEnum  JAVA1_2  = new JavaVersionEnum( "Java 1.2", JAVA1_2_VALUE );
   public static final JavaVersionEnum  JAVA1_3  = new JavaVersionEnum( "Java 1.3", JAVA1_3_VALUE );

   private JavaVersionEnum(String name, int value) {
     super( name, value );
   }
 
   public static JavaVersionEnum getEnum(String javaVersion) {
     return (JavaVersionEnum) getEnum(JavaVersionEnum.class, javaVersion);
   }
 
   public static JavaVersionEnum getEnum(int javaVersion) {
     return (JavaVersionEnum) getEnum(JavaVersionEnum.class, javaVersion);
   }
 
   public static Map getEnumMap() {
     return getEnumMap(JavaVersionEnum.class);
   }
 
   public static List getEnumList() {
     return getEnumList(JavaVersionEnum.class);
   }
 
   public static Iterator iterator() {
     return iterator(JavaVersionEnum.class);
   }
 }
 

The above class could then be used as follows:

 public void doSomething(JavaVersion ver) {
   switch (ver.getValue()) {
     case JAVA1_0_VALUE:
       // ...
       break;
     case JAVA1_1_VALUE:
       // ...
       break;
     //...
   }
 }
 

As shown, each enum has a name and a value. These can be accessed using getName and getValue.

The getEnum and iterator methods are recommended. Unfortunately, Java restrictions require these to be coded as shown in each subclass. An alternative choice is to use the EnumUtils class.

導入されたバージョン:
1.0
バージョン:
$Id: ValuedEnum.java,v 1.1.1.1 2004/02/13 10:02:06 hioki Exp $
作成者:
Apache Avalon project, Stephen Colebourne
関連項目:
直列化された形式

フィールドの概要
 
クラス org.apache.commons.lang.enum.Enum から継承したフィールド
iToString
 
コンストラクタの概要
protected ValuedEnum(String name, int value)
          Constructor for enum item.
 
メソッドの概要
 int compareTo(Object other)
          Tests for order.
protected static Enum getEnum(Class enumClass, int value)
          Gets an Enum object by class and value.
 int getValue()
          Get value of enum item.
 String toString()
          Human readable description of this Enum item.
 
クラス org.apache.commons.lang.enum.Enum から継承したメソッド
equals, getEnum, getEnumClass, getEnumList, getEnumMap, getName, hashCode, iterator, readResolve
 
クラス java.lang.Object から継承したメソッド
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

コンストラクタの詳細

ValuedEnum

protected ValuedEnum(String name,
                     int value)
Constructor for enum item.

パラメータ:
name - the name of enum item
value - the value of enum item
メソッドの詳細

getEnum

protected static Enum getEnum(Class enumClass,
                              int value)

Gets an Enum object by class and value.

This method loops through the list of Enum, thus if there are many Enums this will be slow.

パラメータ:
enumClass - the class of the Enum to get
value - the value of the Enum to get
戻り値:
the enum object, or null if the enum does not exist
例外:
IllegalArgumentException - if the enum class is null

getValue

public final int getValue()

Get value of enum item.

戻り値:
the enum item's value.

compareTo

public int compareTo(Object other)

Tests for order.

The default ordering is numeric by value, but this can be overridden by subclasses.

定義:
インタフェース Comparable 内の compareTo
オーバーライド:
クラス Enum 内の compareTo
パラメータ:
other - the other object to compare to
戻り値:
-ve if this is less than the other object, +ve if greater than, 0 of equal
例外:
ClassCastException - if other is not an Enum
NullPointerException - if other is null
関連項目:
Comparable.compareTo(Object)

toString

public String toString()

Human readable description of this Enum item.

オーバーライド:
クラス Enum 内の toString
戻り値:
String in the form type[name=value], for example: JavaVersion[Java 1.0=100]. Note that the package name is stripped from the type name.


このドキュメントは、Ja-Jakartaにより訳されました。 コメントがある場合は report@jajakarta.orgまでお願いします。
Translated into Japanese by jajakarta.org. The original page is here.
Copyright (c) 2002-2003 - Apache Software Foundation