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

java.lang.Object
  拡張org.apache.commons.lang.enum.Enum
すべての実装インタフェース:
Comparable, Serializable
直系の既知のサブクラス:
ValuedEnum

public abstract class Enum
extends Object
implements Comparable, Serializable

タイプセーフな enum のスーパークラスとなる抽象クラスです。

Abstract superclass for type-safe enums.

C言語にあって Java に欠けている機能の一つが列挙型です。 int をベースにした C の実装は不十分で誤用を招きます。 オリジナルの Java の勧告とほとんどのJDKは int を不変の値として使用します。 しかしより強固でタイプセーフなクラスをベースにされた解決策がデザインされています。 このクラスは基本的なタイプセーフな列挙型のパターンを提供します。

One feature of the C programming language lacking in Java is enumerations. The C implementation based on ints was poor and open to abuse. The original Java recommendation and most of the JDK also uses int constants. It has been recognised however that a more robust type-safe class-based solution can be designed. This class follows the basic Java type-safe enumeration pattern.

注: Java クラスローダの処理に依存して結果が変わるため Enum オブジェクトの比較には == ではなく equals() メソッドを使う必要があります。 The equals() メソッドは初めに == を実行するため、たいていの場合は同じ結果になります。

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 ColorEnum extends Enum {
   public static final ColorEnum RED = new ColorEnum("Red");
   public static final ColorEnum GREEN = new ColorEnum("Green");
   public static final ColorEnum BLUE = new ColorEnum("Blue");

   private ColorEnum(String color) {
     super(color);
   }
 
   public static ColorEnum getEnum(String color) {
     return (ColorEnum) getEnum(ColorEnum.class, color);
   }
 
   public static Map getEnumMap() {
     return getEnumMap(ColorEnum.class);
   }
 
   public static List getEnumList() {
     return getEnumList(ColorEnum.class);
   }
 
   public static Iterator iterator() {
     return iterator(ColorEnum.class);
   }
 }
 

上に見られるように各 enum は名前をもっています。 これには getName を使用することによりアクセスできます。

As shown, each enum has a name. This can be accessed using getName.

getEnumiterator のメソッドの使用をお勧めします。 不幸なことに、Javaの制限のために各サブクラス毎にこのようなコードが必要となってしまいます。 この代案は EnumUtils クラスの使用です。

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 {@link EnumUtils} class.

注: このクラスのオリジナルは Jakarta Avalon プロジェクト内にあります。

NOTE: This class originated in the Jakarta Avalon project.

バージョン:
$Id: Enum.java,v 1.1.1.1 2004/02/13 10:02:04 hioki Exp $
作成者:
Stephen Colebourne
関連項目:
直列化された形式
翻訳者:
日置 聡
翻訳状況:
初稿(校正者募集中)
翻訳更新日:
2003/08/05

入れ子クラスの概要
private static class Enum.Entry
          ソースコードの順序を保持してイテレータを使用可能とします。
 
フィールドの概要
private static Map cEnumClasses
          キーがクラス名、値がエントリーの Map。
private static Map EMPTY_MAP
          空のマップ(JDK1.2 は空のマップを持っていないので)。
private  String iName
          Enum を表現する名称。
 
コンストラクタの概要
protected Enum(String name)
          列挙型に追加する新しい名称のアイテムを生成します。
 
メソッドの概要
 int compareTo(Object other)
          順序をチェックします。
 boolean equals(Object other)
          等しいかどうかのチェックを行います。
protected static Enum getEnum(Class enumClass, String name)
          クラスと名称から Enum を取得します。
protected static List getEnumList(Class enumClass)
          Enum クラスを使用して Enum オブジェクトのリストを取得します。
protected static Map getEnumMap(Class enumClass)
          Enum クラスの名称から Enum オブジェクトの Map を取得します。
 String getName()
          コンストラクタで設定された Enum アイテムの名称を取得します。
 int hashCode()
          適切な列挙型のハッシュコードを返します。
protected static Iterator iterator(Class enumClass)
          Enum クラス内の Enum オブジェクトを走査するイテレータを取得します。.
protected  Object readResolve()
          複数のコピーが無駄に生成される、または不正な enum 型が生成される事のないようにクラスの直列化復元を処理します。
 String toString()
          Enum アイテムを読みやすい形で返します。
 
クラス java.lang.Object から継承したメソッド
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

フィールドの詳細

EMPTY_MAP

private static final Map EMPTY_MAP
空のマップ(JDK1.2 は空のマップを持っていないので)。
An empty map, as JDK1.2 didn't have an empty map


cEnumClasses

private static final Map cEnumClasses
キーがクラス名、値がエントリーの Map。
Map, key of class name, value of Entry.


iName

private final String iName
Enum を表現する名称。
The string representation of the Enum.

コンストラクタの詳細

Enum

protected Enum(String name)
列挙型に追加する新しい名称のアイテムを生成します。
Constructor to add a new named item to the enumeration.

パラメータ:
name - enum オブジェクトの名称
the name of the enum object
例外:
IllegalArgumentException - 名称が null または空の文字列だった場合
if the name is null or a blank string
メソッドの詳細

readResolve

protected Object readResolve()
複数のコピーが無駄に生成される、または不正な enum 型が生成される事のないようにクラスの直列化復元を処理します。
Handle the deserialization of the class to ensure that multiple copies are not wastefully created, or illegal enum types created.

戻り値:
解決されたオブジェクト
the resolved object

getEnum

protected static Enum getEnum(Class enumClass,
                              String name)
クラスと名称から Enum を取得します。
Gets an Enum object by class and name.

パラメータ:
enumClass - 取得元となる Enum クラス
the class of the Enum to get
name - Enum の名称、または null
the name of the Enum to get, may be null
戻り値:
enum オブジェクト、該当する enum が存在しない場合には null
the enum object, or null if the enum does not exist
例外:
IllegalArgumentException - enum クラスが null だった場合
if the enum class is null

getEnumMap

protected static Map getEnumMap(Class enumClass)
Enum クラスの名称から Enum オブジェクトの Map を取得します。 要求されたクラスが Enum オブジェクトを持っていなかった場合には空の Map を返します。
Gets the Map of Enum objects by name using the Enum class. If the requested class has no enum objects an empty Map is returned.

パラメータ:
enumClass - 取得元となる Enum クラス
enumClass the class of the Enum to get
戻り値:
enum オブジェクトの Map
the enum object Map
例外:
IllegalArgumentException - enum クラスが null だった場合
if the enum class is null
IllegalArgumentException - enum クラスが Enum のサブクラスでなかった場合
if the enum class is not a subclass of Enum

getEnumList

protected static List getEnumList(Class enumClass)
Enum クラスを使用して Enum オブジェクトのリストを取得します。 このリスト内はオブジェクトの生成された順番に並んでいます(ソースコード内の順番)。 指定されたクラスが enum オブジェクトを持っていない場合には空の List を返します。
Gets the List of Enum objects using the Enum class. The list is in the order that the objects were created (source code order). If the requested class has no enum objects an empty List is returned.

パラメータ:
enumClass - 取得元となる Enum クラス
the class of the Enum to get
戻り値:
enum オブジェクトの List
the enum object List
例外:
IllegalArgumentException - enum クラスが null だった場合
if the enum class is null
IllegalArgumentException - enum クラスが Enum のサブクラスでなかった場合
if the enum class is not a subclass of Enum

iterator

protected static Iterator iterator(Class enumClass)
Enum クラス内の Enum オブジェクトを走査するイテレータを取得します。. このイテレータはオブジェクトの生成された順番に並んでいます(ソースコード内の順番)。 指定されたクラスが enum オブジェクトを持っていない場合には空の Iterator を返します。
Gets an iterator over the Enum objects in an Enum class. The iterator is in the order that the objects were created (source code order). If the requested class has no enum objects an empty Iterator is returned.

パラメータ:
enumClass - 取得元となる Enum クラス
enumClass the class of the Enum to get
戻り値:
an enum オブジェクトの イテレータ
an iterator of the Enum objects
例外:
IllegalArgumentException - enum クラスが null だった場合
if the enum class is null
IllegalArgumentException - enum クラスが Enum のサブクラスでなかった場合
if the enum class is not a subclass of Enum

getName

public final String getName()
コンストラクタで設定された Enum アイテムの名称を取得します。
Retrieve the name of this Enum item, set in the constructor.

戻り値:
Enum アイテムの名称
the String name of this Enum item

equals

public final boolean equals(Object other)
等しいかどうかのチェックを行います。 2つの Enum オブジェクト の判定は、クラス名と名称が等しいかどうかにより行われます。 同一であるかのチェック(==の事)が最初に行われるため、たいていの場合このメソッドは高速に動作します。
Tests for equality. Two Enum objects are considered equal if they have the same class names and the same names. Identity is tested for first, so this method usually runs fast.

パラメータ:
other - 等しいかどうかの比較対照となるオブジェクト
the other object to compare for equality
戻り値:
Enums が等しい場合 true
if the Enums are equal

hashCode

public final int hashCode()
適切な列挙型のハッシュコードを返します。
Returns a suitable hashCode for the enumeration.

戻り値:
名称をベースとするハッシュコード
a hashcode based on the name

compareTo

public int compareTo(Object other)
順序をチェックします。 デフォルトの順番は名称のアルファベット順となりますが、これはサブクラスによってオーバライドされます。
Tests for order. The default ordering is alphabetic by name, but this can be overridden by subclasses.

定義:
インタフェース Comparable 内の compareTo
パラメータ:
other - 比較対照となるオブジェクト
the other object to compare to
戻り値:
比較対照より小さかった場合 -ve(マイナス)、比較対照より大きかった場合 +ve(プラス)、等しかった場合 0
-ve if this is less than the other object, +ve if greater than, 0 of equal
例外:
ClassCastException - 比較対照が Enum でなかった場合
if other is not an Enum
NullPointerException - 比較対照が null だった場合
if other is null
関連項目:
Comparable.compareTo(Object)

toString

public String toString()
Enum アイテムを読みやすい形で返します。 デバッグの際に使用されます。
Human readable description of this Enum item. For use when debugging.

戻り値:
type[name] のフォームの文字列、例: Color[Red]、 注 型の名前の中のパッケージ名は省略されます。
String in the form type[name], for example: Color[Red]. 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