/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2002 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */
package org.apache.commons.lang.enum;

import java.util.Iterator;
import java.util.List;
/**
 * タイプセーフな int の値を使用した enum のスーパークラスとなる抽象クラスです。
 * {@primary Abstract superclass for type-safe enums with integer values.}
 * <p>
 * <em>注:</em> Java クラスローダの処理に依存して結果が変わるため
 * Enum オブジェクトの比較には == ではなく equals() メソッドを使う必要があります。
 * The equals() メソッドは初めに == を実行するため、たいていの場合は同じ結果になります。
 * {@primary <em>NOTE:</em>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.}
 * <p>
 * このクラスは継承して使用する必要があります。 例えば:
 * {@primary To use this class, it must be subclassed. For example:}
 *
 * <pre>
 * public final class JavaVersion 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);
 *   }
 * }
 * </pre>
 *
 * The above class could then be used as follows:
 * <pre>
 * public void doSomething(JavaVersion ver) {
 *   switch (ver.getValue()) {
 *     case JAVA1_0_VALUE:
 *       // ...
 *       break;
 *     case JAVA1_1_VALUE:
 *       // ...
 *       break;
 *     //...
 *   }
 * }
 * </pre>
 * <p>
 * 上に見られるように各 enum は名前と値をもっています。 
 * これには <code>getName</code> と <code>getValue</code> を使用することによりアクセスできます。
 * {@primary As shown, each enum has a name and a value. These can be accessed using 
 * <code>getName</code> and <code>getValue</code>.}
 * <p>
 * <code>getEnum</code> と <code>iterator</code> のメソッドの使用をお勧めします。
 * 不幸なことに、Javaの制限のために各サブクラス毎にこのようなコードが必要となってしまいます。
 * この代案は {@link EnumUtils} クラスの使用です
 * {@primary The <code>getEnum</code> and <code>iterator</code> 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.}
 * <p>
 * <em>注:</em> このクラスのオリジナルは Jakarta Avalon プロジェクト内にあります。
 * {@primary <em>NOTE:</em> This class originated in the Jakarta Avalon project.}
 * </p>
 *
 * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
 * @translator 日置 聡
 * @status firstdraft
 * @update 2003/08/05
 * @version $Id: ValuedEnum.java,v 1.1.1.1 2004/02/13 10:02:04 hioki Exp $
 */
public abstract class ValuedEnum extends Enum {
    /**
     * enum の持つ値。
     * {@primary The value contained in enum.}
     */
    private final int iValue;

    /**
     * enum アイテムを生成します。
     * {@primary Constructor for enum item.}
     *
     * @param name enum アイテムの名称
     * {@primary the name of enum item.}
     * @param value enum アイテムの値
     * {@primary the value of enum item.}
     */
    protected ValuedEnum(String name, int value) {
        super(name);
        iValue = value;
    }

    /**
     * クラスと値から Enum を取得します。
     * このメソッドは Enum のリストをループして走査するため、多くの
     * Enum が登録されている場合、その処理は遅くなります。
     * {@primary Gets an Enum object by class and value.
     * This method loops through the list of Enums, thus if there
     * are many Enums this will be slow.}
     * 
     * @param enumClass  取得元となる Enum クラス
     * {@primary the class of the Enum to get}
     * @param value  取得する Enum の値
     * {@primary the value of the Enum to get}
     * @return enum オブジェクト、該当する enum 画存在しない場合 null
     * {@primary the enum object, or null if the enum does not exist}
     * @throws IllegalArgumentException enum クラスが null だった場合
     * {@primary if the enum class is null}
     */
    protected static Enum getEnum(Class enumClass, int value) {
        if (enumClass == null) {
            throw new IllegalArgumentException("The Enum Class must not be null");
        }
        List list = Enum.getEnumList(enumClass);
        for (Iterator it = list.iterator(); it.hasNext();) {
            ValuedEnum enum = (ValuedEnum) it.next();
            if (enum.getValue() == value) {
                return enum;
            }
        }
        return null;
    }

    /**
     * enum アイテムの値を取得します。
     * {@primary Get value of enum item.}
     *
     * @return enum アイテムの値
     * {@primary the enum item's value.}
     */
    public final int getValue() {
        return iValue;
    }

    /**
     * 順序をチェックします。 
     * デフォルトの順番は値のの数値順となりますが、これはサブクラスによってオーバライドされます。
     * {@primary Tests for order. The default ordering is numeric by value, but this
     * can be overridden by subclasses.}
     * 
     * @see java.lang.Comparable#compareTo(Object)
     * @param other  比較対照となるオブジェクト
     * {@primary the other object to compare to}
     * @return 比較対照より小さかった場合 -ve(マイナス)、比較対照より大きかった場合 +ve(プラス)、等しかった場合 0
     * {@primary -ve if this is less than the other object, +ve if greater than, 0 of equal}
     * @throws ClassCastException 比較対照が Enum でなかった場合
     * {@primary if other is not an Enum}
     * @throws NullPointerException 比較対照が null だった場合
     * {@primary if other is null}
     */
    public int compareTo(Object other) {
        return iValue - ((ValuedEnum) other).iValue;
    }

    /**
     * Enum アイテムを読みやすい形で返します。 デバッグの際に使用されます。
     * {@primary Human readable description of this Enum item. For use when debugging.}
     * 
     * @return <code>type[name=value]</code> のフォームの文字列、例:
     * <code>JavaVersion[Java 1.0=100]</code>、 注 型の名前の中のパッケージ名は省略されます。
     * {@primary String in the form <code>type[name=value]</code>, for example:
     * <code>JavaVersion[Java 1.0=100]</code>. Note that the package name is
     * stripped from the type name.}
     */
    public String toString() {
        String shortName = getClass().getName();
        int pos = shortName.lastIndexOf('.');
        if (pos != -1) {
            shortName = shortName.substring(pos + 1);
        }
        return shortName + "[" + getName() + "=" + getValue() + "]";
    }
}
