diff --git a/src/java/org/apache/commons/lang/enum/Enum.java b/src/java/org/apache/commons/lang/enum/Enum.java
new file mode 100644
index 000000000..c41bc4d56
--- /dev/null
+++ b/src/java/org/apache/commons/lang/enum/Enum.java
@@ -0,0 +1,338 @@
+/* ====================================================================
+ * 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
+ * .
+ */
+package org.apache.commons.lang.enum;
+
+import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+/**
+ * Abstract superclass for type-safe enums.
+ *
+ * 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.
+ *
+ * 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);
+ * }
+ * }
+ *
+ *
+ * As shown, each enum has a name. This can be accessed using getName
.
+ *
+ * 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.
+ *
+ * NOTE: This class originated in the Jakarta Avalon project.
+ *
+ *
+ * @author Stephen Colebourne
+ * @version $Id: Enum.java,v 1.1 2002/08/11 23:17:54 scolebourne Exp $
+ */
+public abstract class Enum implements Comparable, Serializable {
+ /**
+ * Map, key of class name, value of Entry.
+ */
+ private static final Map cEnumClasses = new HashMap();
+ /**
+ * The string representation of the Enum.
+ */
+ private final String iName;
+
+ /**
+ * Enable the iterator to retain the source code order
+ */
+ private static class Entry {
+ final Map map = new HashMap(50);
+ final List list = new ArrayList(25);
+
+ private Entry() {
+ }
+ }
+
+ /**
+ * Constructor to add a new named item to the enumeration.
+ *
+ * @param name the name of the enum object
+ * @throws IllegalArgumentException if the name is null or a blank string
+ */
+ protected Enum(String name) {
+ super();
+ if (name == null || name.length() == 0) {
+ throw new IllegalArgumentException("The Enum name must not be empty");
+ }
+ iName = name;
+ Entry entry = (Entry) cEnumClasses.get(getClass().getName());
+ if (entry == null) {
+ entry = new Entry();
+ cEnumClasses.put(getClass().getName(), entry);
+ }
+ entry.map.put(name, this);
+ entry.list.add(this);
+ }
+
+ /**
+ * Handle the deserialization of the class to ensure that multiple
+ * copies are not wastefully created, or illegal enum types created.
+ */
+ protected Object readResolve() {
+ return Enum.getEnum(getClass(), getName());
+ }
+
+ /**
+ * Gets an Enum object by class and name.
+ *
+ * @param enumClass the class of the Enum to get
+ * @param name the name of the Enum to get, may be null
+ * @return the enum object, or null if the enum does not exist
+ * @throws IllegalArgumentException if the enum class is null
+ */
+ protected static Enum getEnum(Class enumClass, String name) {
+ if (enumClass == null) {
+ throw new IllegalArgumentException("The Enum Class must not be null");
+ }
+ Entry entry = (Entry) cEnumClasses.get(enumClass.getName());
+ if (entry == null) {
+ return null;
+ }
+ return (Enum) entry.map.get(name);
+ }
+
+ /**
+ * 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.
+ *
+ * @param enumClass the class of the Enum to get
+ * @return the enum object Map
+ * @throws IllegalArgumentException if the enum class is null
+ * @throws IllegalArgumentException if the enum class is not a subclass of Enum
+ */
+ protected static Map getEnumMap(Class enumClass) {
+ if (enumClass == null) {
+ throw new IllegalArgumentException("The Enum Class must not be null");
+ }
+ if (Enum.class.isAssignableFrom(enumClass) == false) {
+ throw new IllegalArgumentException("The Class must be a subclass of Enum");
+ }
+ Entry entry = (Entry) cEnumClasses.get(enumClass.getName());
+ if (entry == null) {
+ return Collections.EMPTY_MAP;
+ }
+ return Collections.unmodifiableMap(entry.map);
+ }
+
+ /**
+ * 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.
+ *
+ * @param enumClass the class of the Enum to get
+ * @return the enum object Map
+ * @throws IllegalArgumentException if the enum class is null
+ * @throws IllegalArgumentException if the enum class is not a subclass of Enum
+ */
+ protected static List getEnumList(Class enumClass) {
+ if (enumClass == null) {
+ throw new IllegalArgumentException("The Enum Class must not be null");
+ }
+ if (Enum.class.isAssignableFrom(enumClass) == false) {
+ throw new IllegalArgumentException("The Class must be a subclass of Enum");
+ }
+ Entry entry = (Entry) cEnumClasses.get(enumClass.getName());
+ if (entry == null) {
+ return Collections.EMPTY_LIST;
+ }
+ return Collections.unmodifiableList(entry.list);
+ }
+
+ /**
+ * 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.
+ *
+ * @param enumClass the class of the Enum to get
+ * @return an iterator of the Enum objects
+ * @throws IllegalArgumentException if the enum class is null
+ * @throws IllegalArgumentException if the enum class is not a subclass of Enum
+ */
+ protected static Iterator iterator(Class enumClass) {
+ return Enum.getEnumList(enumClass).iterator();
+ }
+
+ /**
+ * Retrieve the name of this Enum item, set in the constructor.
+ *
+ * @return the String
name of this Enum item
+ */
+ public final String getName() {
+ return iName;
+ }
+
+ /**
+ * 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.
+ *
+ * @param other the other object to compare for equality
+ */
+ public final boolean equals(Object other) {
+ if (other == this) {
+ return true;
+ } else if (other == null) {
+ return false;
+ } else if (other.getClass() == this.getClass()) {
+ // shouldn't happen, but...
+ return iName.equals(((Enum) other).iName);
+ } else if (other.getClass().getName().equals(this.getClass().getName())) {
+ // different classloaders
+ try {
+ // try to avoid reflection
+ return iName.equals(((Enum) other).iName);
+
+ } catch (ClassCastException ex) {
+ // use reflection
+ try {
+ Method mth = other.getClass().getMethod("getName", null);
+ String name = (String) mth.invoke(other, null);
+ return iName.equals(name);
+ } catch (NoSuchMethodException ex2) {
+ // ignore - should never happen
+ } catch (IllegalAccessException ex2) {
+ // ignore - should never happen
+ } catch (InvocationTargetException ex2) {
+ // ignore - should never happen
+ }
+ return false;
+ }
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Returns a suitable hashCode for the enumeration.
+ *
+ * @return a hashcode based on the name
+ */
+ public final int hashCode() {
+ return 7 + iName.hashCode();
+ }
+
+ /**
+ * Tests for order. The default ordering is alphabetic by name, but this
+ * can be overridden by subclasses.
+ *
+ * @see java.lang.Comparable#compareTo(Object)
+ * @param other the other object to compare to
+ * @return -ve if this is less than the other object, +ve if greater than, 0 of equal
+ * @throws ClassCastException if other is not an Enum
+ * @throws NullPointerException if other is null
+ */
+ public int compareTo(Object other) {
+ return iName.compareTo(((Enum) other).iName);
+ }
+
+ /**
+ * Human readable description of this Enum item. For use when debugging.
+ *
+ * @return String in the form type[name]
, for example:
+ * Color[Red]
. 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() + "]";
+ }
+}
diff --git a/src/java/org/apache/commons/lang/enum/EnumUtils.java b/src/java/org/apache/commons/lang/enum/EnumUtils.java
new file mode 100644
index 000000000..05ce62da1
--- /dev/null
+++ b/src/java/org/apache/commons/lang/enum/EnumUtils.java
@@ -0,0 +1,142 @@
+/* ====================================================================
+ * 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
+ * .
+ */
+package org.apache.commons.lang.enum;
+
+import java.io.Serializable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+/**
+ * Utility class for accessing and manipulating Enums.
+ *
+ * @see Enum
+ * @see ExtensibleEnum
+ * @see ValuedEnum
+ * @author Stephen Colebourne
+ * @version $Id: EnumUtils.java,v 1.1 2002/08/11 23:17:54 scolebourne Exp $
+ */
+public abstract class EnumUtils implements Comparable, Serializable {
+
+ /**
+ * Restricted constructor
+ */
+ private EnumUtils() {
+ }
+
+ /**
+ * Gets an Enum object by class and name.
+ *
+ * @param enumClass the class of the Enum to get
+ * @param name the name of the Enum to get, may be null
+ * @return the enum object
+ * @throws IllegalArgumentException if the enum class is null
+ */
+ public static Enum getEnum(Class enumClass, String name) {
+ return Enum.getEnum(enumClass, name);
+ }
+
+ /**
+ * Gets a ValuedEnum object by class and value.
+ *
+ * @param enumClass the class of the Enum to get
+ * @param value the value of the Enum to get
+ * @return the enum object, or null if the enum does not exist
+ * @throws IllegalArgumentException if the enum class is null
+ */
+ public static ValuedEnum getEnum(Class enumClass, int value) {
+ return (ValuedEnum) ValuedEnum.getEnum(enumClass, value);
+ }
+
+ /**
+ * 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.
+ *
+ * @param enumClass the class of the Enum to get
+ * @return the enum object Map
+ * @throws IllegalArgumentException if the enum class is null
+ * @throws IllegalArgumentException if the enum class is not a subclass of Enum
+ */
+ public static Map getEnumMap(Class enumClass) {
+ return Enum.getEnumMap(enumClass);
+ }
+
+ /**
+ * 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.
+ *
+ * @param enumClass the class of the Enum to get
+ * @return the enum object Map
+ * @throws IllegalArgumentException if the enum class is null
+ * @throws IllegalArgumentException if the enum class is not a subclass of Enum
+ */
+ public static List getEnumList(Class enumClass) {
+ return Enum.getEnumList(enumClass);
+ }
+
+ /**
+ * 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.
+ *
+ * @param enumClass the class of the Enum to get
+ * @return an iterator of the Enum objects
+ * @throws IllegalArgumentException if the enum class is null
+ * @throws IllegalArgumentException if the enum class is not a subclass of Enum
+ */
+ public static Iterator iterator(Class enumClass) {
+ return Enum.getEnumList(enumClass).iterator();
+ }
+
+}
diff --git a/src/java/org/apache/commons/lang/enum/ValuedEnum.java b/src/java/org/apache/commons/lang/enum/ValuedEnum.java
new file mode 100644
index 000000000..82edc7475
--- /dev/null
+++ b/src/java/org/apache/commons/lang/enum/ValuedEnum.java
@@ -0,0 +1,212 @@
+/* ====================================================================
+ * 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
+ * .
+ */
+package org.apache.commons.lang.enum;
+
+import java.util.Iterator;
+import java.util.List;
+/**
+ * Abstract superclass for type-safe enums with integer values.
+ *
+ * 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 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);
+ * }
+ * }
+ *
+ *
+ * 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 {@link EnumUtils} class.
+ *
+ * NOTE: This class originated in the Jakarta Avalon project.
+ *
+ *
+ * @author Stephen Colebourne
+ * @version $Id: ValuedEnum.java,v 1.1 2002/08/11 23:17:54 scolebourne Exp $
+ */
+public abstract class ValuedEnum extends Enum {
+ /**
+ * The value contained in enum.
+ */
+ private final int iValue;
+
+ /**
+ * Constructor for enum item.
+ *
+ * @param name the name of enum item.
+ * @param value the value of enum item.
+ */
+ protected ValuedEnum(String name, int value) {
+ super(name);
+ iValue = value;
+ }
+
+ /**
+ * 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 the class of the Enum to get
+ * @param value the value of the Enum to get
+ * @return the enum object, or null if the enum does not exist
+ * @throws IllegalArgumentException 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;
+ }
+
+ /**
+ * Get value of enum item.
+ *
+ * @return the enum item's value.
+ */
+ public final int getValue() {
+ return iValue;
+ }
+
+ /**
+ * 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 the other object to compare to
+ * @return -ve if this is less than the other object, +ve if greater than, 0 of equal
+ * @throws ClassCastException if other is not an Enum
+ * @throws NullPointerException if other is null
+ */
+ public int compareTo(Object other) {
+ return iValue - ((ValuedEnum) other).iValue;
+ }
+
+ /**
+ * Human readable description of this Enum item. For use when debugging.
+ *
+ * @return 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.
+ */
+ public String toString() {
+ String shortName = getClass().getName();
+ int pos = shortName.lastIndexOf('.');
+ if (pos != -1) {
+ shortName = shortName.substring(pos + 1);
+ }
+ return shortName + "[" + getName() + "=" + getValue() + "]";
+ }
+}
diff --git a/src/test/org/apache/commons/lang/enum/ColorEnum.java b/src/test/org/apache/commons/lang/enum/ColorEnum.java
new file mode 100644
index 000000000..3c3f6a0cd
--- /dev/null
+++ b/src/test/org/apache/commons/lang/enum/ColorEnum.java
@@ -0,0 +1,90 @@
+/* ====================================================================
+ * 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
+ * .
+ */
+package org.apache.commons.lang.enum;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+/**
+ * Color enumeration.
+ *
+ * @author Stephen Colebourne
+ * @version $Id: ColorEnum.java,v 1.1 2002/08/11 23:17:54 scolebourne Exp $
+ */
+
+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);
+ }
+}
diff --git a/src/test/org/apache/commons/lang/enum/EnumTest.java b/src/test/org/apache/commons/lang/enum/EnumTest.java
new file mode 100644
index 000000000..0ae49beda
--- /dev/null
+++ b/src/test/org/apache/commons/lang/enum/EnumTest.java
@@ -0,0 +1,146 @@
+/* ====================================================================
+ * 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
+ * .
+ */
+package org.apache.commons.lang.enum;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.commons.lang.SerializationUtils;
+/**
+ * Test cases for the {@link Enum} class.
+ *
+ * @author Stephen Colebourne
+ * @version $Id: EnumTest.java,v 1.1 2002/08/11 23:17:54 scolebourne Exp $
+ */
+
+public final class EnumTest extends TestCase {
+
+ public EnumTest(String name) {
+ super(name);
+ }
+
+ public void setUp() {
+ }
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(EnumTest.class);
+ suite.setName("Enum Tests");
+ return suite;
+ }
+
+ public void testName() {
+ assertEquals("Red", ColorEnum.RED.getName());
+ assertEquals("Green", ColorEnum.GREEN.getName());
+ assertEquals("Blue", ColorEnum.BLUE.getName());
+ }
+
+ public void testCompareTo() {
+ assertTrue(ColorEnum.BLUE.compareTo(ColorEnum.BLUE) == 0);
+ assertTrue(ColorEnum.RED.compareTo(ColorEnum.BLUE) > 0);
+ assertTrue(ColorEnum.BLUE.compareTo(ColorEnum.RED) < 0);
+ }
+
+ public void testEquals() {
+ assertSame(ColorEnum.RED, ColorEnum.RED);
+ assertSame(ColorEnum.getEnum("Red"), ColorEnum.RED);
+ }
+
+ public void testToString() {
+ assertEquals("ColorEnum[Red]", ColorEnum.RED.toString());
+ }
+
+ public void testIterator() {
+ Iterator it = ColorEnum.iterator();
+ assertSame(ColorEnum.RED, it.next());
+ assertSame(ColorEnum.GREEN, it.next());
+ assertSame(ColorEnum.BLUE, it.next());
+ }
+
+ public void testList() {
+ List list = ColorEnum.getEnumList();
+ Iterator it = list.iterator();
+ assertSame(ColorEnum.RED, it.next());
+ assertSame(ColorEnum.GREEN, it.next());
+ assertSame(ColorEnum.BLUE, it.next());
+ }
+
+ public void testMap() {
+ Map map = ColorEnum.getEnumMap();
+ assertTrue(map.containsValue(ColorEnum.RED));
+ assertTrue(map.containsValue(ColorEnum.GREEN));
+ assertTrue(map.containsValue(ColorEnum.BLUE));
+ assertSame(ColorEnum.RED, map.get("Red"));
+ assertSame(ColorEnum.GREEN, map.get("Green"));
+ assertSame(ColorEnum.BLUE, map.get("Blue"));
+ }
+
+ public void testGet() {
+ assertSame(ColorEnum.RED, ColorEnum.getEnum("Red"));
+ assertSame(ColorEnum.GREEN, ColorEnum.getEnum("Green"));
+ assertSame(ColorEnum.BLUE, ColorEnum.getEnum("Blue"));
+ assertSame(null, ColorEnum.getEnum("Pink"));
+ }
+
+ public void testSerialization() {
+ assertSame(ColorEnum.RED, SerializationUtils.clone(ColorEnum.RED));
+ assertSame(ColorEnum.GREEN, SerializationUtils.clone(ColorEnum.GREEN));
+ assertSame(ColorEnum.BLUE, SerializationUtils.clone(ColorEnum.BLUE));
+ }
+
+}
diff --git a/src/test/org/apache/commons/lang/enum/EnumTestSuite.java b/src/test/org/apache/commons/lang/enum/EnumTestSuite.java
new file mode 100644
index 000000000..898b8a089
--- /dev/null
+++ b/src/test/org/apache/commons/lang/enum/EnumTestSuite.java
@@ -0,0 +1,93 @@
+/* ====================================================================
+ * 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
+ * .
+ */
+package org.apache.commons.lang.enum;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+/**
+ * Test suite for the Enum package.
+ *
+ * @author Stephen Colebourne
+ * @version $Id: EnumTestSuite.java,v 1.1 2002/08/11 23:17:54 scolebourne Exp $
+ */
+public class EnumTestSuite extends TestCase {
+
+ /**
+ * Construct a new instance.
+ */
+ public EnumTestSuite(String name) {
+ super(name);
+ }
+
+ /**
+ * Command-line interface.
+ */
+ public static void main(String[] args) {
+ TestRunner.run(suite());
+ }
+
+ /**
+ * Get the suite of tests
+ */
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.setName("Commons-Lang-Enum Tests");
+ suite.addTest(EnumTest.suite());
+ suite.addTest(EnumUtilsTest.suite());
+ suite.addTest(ValuedEnumTest.suite());
+ return suite;
+ }
+}
diff --git a/src/test/org/apache/commons/lang/enum/EnumUtilsTest.java b/src/test/org/apache/commons/lang/enum/EnumUtilsTest.java
new file mode 100644
index 000000000..abb3ec92f
--- /dev/null
+++ b/src/test/org/apache/commons/lang/enum/EnumUtilsTest.java
@@ -0,0 +1,171 @@
+/* ====================================================================
+ * 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
+ * .
+ */
+package org.apache.commons.lang.enum;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.commons.lang.SerializationUtils;
+/**
+ * Test cases for the {@link Enum} class.
+ *
+ * @author Stephen Colebourne
+ * @version $Id: EnumUtilsTest.java,v 1.1 2002/08/11 23:17:54 scolebourne Exp $
+ */
+
+public final class EnumUtilsTest extends TestCase {
+
+ public EnumUtilsTest(String name) {
+ super(name);
+ }
+
+ public void setUp() {
+ }
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(EnumUtilsTest.class);
+ suite.setName("EnumUtils Tests");
+ return suite;
+ }
+
+ public void testIterator() {
+ Iterator it = EnumUtils.iterator(ColorEnum.class);
+ assertSame(ColorEnum.RED, it.next());
+ assertSame(ColorEnum.GREEN, it.next());
+ assertSame(ColorEnum.BLUE, it.next());
+ }
+
+ public void testIteratorEx() {
+ try {
+ EnumUtils.iterator(null);
+ } catch (IllegalArgumentException ex) {
+ return;
+ }
+ fail();
+ }
+
+ public void testList() {
+ List list = EnumUtils.getEnumList(ColorEnum.class);
+ Iterator it = list.iterator();
+ assertSame(ColorEnum.RED, it.next());
+ assertSame(ColorEnum.GREEN, it.next());
+ assertSame(ColorEnum.BLUE, it.next());
+ }
+
+ public void testListEx() {
+ try {
+ EnumUtils.getEnumList(null);
+ } catch (IllegalArgumentException ex) {
+ return;
+ }
+ fail();
+ }
+
+ public void testMap() {
+ Map map = EnumUtils.getEnumMap(ColorEnum.class);
+ assertTrue(map.containsValue(ColorEnum.RED));
+ assertTrue(map.containsValue(ColorEnum.GREEN));
+ assertTrue(map.containsValue(ColorEnum.BLUE));
+ assertSame(ColorEnum.RED, map.get("Red"));
+ assertSame(ColorEnum.GREEN, map.get("Green"));
+ assertSame(ColorEnum.BLUE, map.get("Blue"));
+ }
+
+ public void testMapEx() {
+ try {
+ EnumUtils.getEnumMap(null);
+ } catch (IllegalArgumentException ex) {
+ return;
+ }
+ fail();
+ }
+
+ public void testGet() {
+ assertSame(ColorEnum.RED, EnumUtils.getEnum(ColorEnum.class, "Red"));
+ assertSame(ColorEnum.GREEN, EnumUtils.getEnum(ColorEnum.class, "Green"));
+ assertSame(ColorEnum.BLUE, EnumUtils.getEnum(ColorEnum.class, "Blue"));
+ assertSame(null, EnumUtils.getEnum(ColorEnum.class, "Pink"));
+ }
+
+ public void testGetEx() {
+ try {
+ EnumUtils.getEnum(null, "");
+ } catch (IllegalArgumentException ex) {
+ return;
+ }
+ fail();
+ }
+
+ public void testGetValue() {
+ assertSame(ValuedColorEnum.RED, EnumUtils.getEnum(ValuedColorEnum.class, 1));
+ assertSame(ValuedColorEnum.GREEN, EnumUtils.getEnum(ValuedColorEnum.class, 2));
+ assertSame(ValuedColorEnum.BLUE, EnumUtils.getEnum(ValuedColorEnum.class, 3));
+ assertSame(null, EnumUtils.getEnum(ValuedColorEnum.class, 4));
+ }
+
+ public void testGetValueEx() {
+ try {
+ EnumUtils.getEnum(null, 0);
+ } catch (IllegalArgumentException ex) {
+ return;
+ }
+ fail();
+ }
+
+}
diff --git a/src/test/org/apache/commons/lang/enum/ValuedColorEnum.java b/src/test/org/apache/commons/lang/enum/ValuedColorEnum.java
new file mode 100644
index 000000000..34ec46619
--- /dev/null
+++ b/src/test/org/apache/commons/lang/enum/ValuedColorEnum.java
@@ -0,0 +1,94 @@
+/* ====================================================================
+ * 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
+ * .
+ */
+package org.apache.commons.lang.enum;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+/**
+ * Color enumeration.
+ *
+ * @author Stephen Colebourne
+ * @version $Id: ValuedColorEnum.java,v 1.1 2002/08/11 23:17:54 scolebourne Exp $
+ */
+
+public final class ValuedColorEnum extends ValuedEnum {
+ public static final ValuedColorEnum RED = new ValuedColorEnum("Red", 1);
+ public static final ValuedColorEnum GREEN = new ValuedColorEnum("Green", 2);
+ public static final ValuedColorEnum BLUE = new ValuedColorEnum("Blue", 3);
+
+ private ValuedColorEnum(String color, int value) {
+ super(color, value);
+ }
+
+ public static ValuedColorEnum getEnum(String color) {
+ return (ValuedColorEnum) getEnum(ValuedColorEnum.class, color);
+ }
+
+ public static ValuedColorEnum getEnum(int value) {
+ return (ValuedColorEnum) getEnum(ValuedColorEnum.class, value);
+ }
+
+ public static Map getEnumMap() {
+ return getEnumMap(ValuedColorEnum.class);
+ }
+
+ public static List getEnumList() {
+ return getEnumList(ValuedColorEnum.class);
+ }
+
+ public static Iterator iterator() {
+ return iterator(ValuedColorEnum.class);
+ }
+}
diff --git a/src/test/org/apache/commons/lang/enum/ValuedEnumTest.java b/src/test/org/apache/commons/lang/enum/ValuedEnumTest.java
new file mode 100644
index 000000000..4544bddf1
--- /dev/null
+++ b/src/test/org/apache/commons/lang/enum/ValuedEnumTest.java
@@ -0,0 +1,159 @@
+/* ====================================================================
+ * 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
+ * .
+ */
+package org.apache.commons.lang.enum;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.commons.lang.SerializationUtils;
+/**
+ * Test cases for the {@link Enum} class.
+ *
+ * @author Stephen Colebourne
+ * @version $Id: ValuedEnumTest.java,v 1.1 2002/08/11 23:17:54 scolebourne Exp $
+ */
+
+public final class ValuedEnumTest extends TestCase {
+
+ public ValuedEnumTest(String name) {
+ super(name);
+ }
+
+ public void setUp() {
+ }
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(ValuedEnumTest.class);
+ suite.setName("ValuedEnum Tests");
+ return suite;
+ }
+
+ public void testName() {
+ assertEquals("Red", ValuedColorEnum.RED.getName());
+ assertEquals("Green", ValuedColorEnum.GREEN.getName());
+ assertEquals("Blue", ValuedColorEnum.BLUE.getName());
+ }
+
+ public void testValue() {
+ assertEquals(1, ValuedColorEnum.RED.getValue());
+ assertEquals(2, ValuedColorEnum.GREEN.getValue());
+ assertEquals(3, ValuedColorEnum.BLUE.getValue());
+ }
+
+ public void testCompareTo() {
+ assertTrue(ValuedColorEnum.BLUE.compareTo(ValuedColorEnum.BLUE) == 0);
+ assertTrue(ValuedColorEnum.RED.compareTo(ValuedColorEnum.BLUE) < 0);
+ assertTrue(ValuedColorEnum.BLUE.compareTo(ValuedColorEnum.RED) > 0);
+ }
+
+ public void testEquals() {
+ assertSame(ValuedColorEnum.RED, ValuedColorEnum.RED);
+ assertSame(ValuedColorEnum.getEnum("Red"), ValuedColorEnum.RED);
+ }
+
+ public void testToString() {
+ assertEquals("ValuedColorEnum[Red=1]", ValuedColorEnum.RED.toString());
+ }
+
+ public void testIterator() {
+ Iterator it = ValuedColorEnum.iterator();
+ assertSame(ValuedColorEnum.RED, it.next());
+ assertSame(ValuedColorEnum.GREEN, it.next());
+ assertSame(ValuedColorEnum.BLUE, it.next());
+ }
+
+ public void testList() {
+ List list = ValuedColorEnum.getEnumList();
+ Iterator it = list.iterator();
+ assertSame(ValuedColorEnum.RED, it.next());
+ assertSame(ValuedColorEnum.GREEN, it.next());
+ assertSame(ValuedColorEnum.BLUE, it.next());
+ }
+
+ public void testMap() {
+ Map map = ValuedColorEnum.getEnumMap();
+ assertTrue(map.containsValue(ValuedColorEnum.RED));
+ assertTrue(map.containsValue(ValuedColorEnum.GREEN));
+ assertTrue(map.containsValue(ValuedColorEnum.BLUE));
+ assertSame(ValuedColorEnum.RED, map.get("Red"));
+ assertSame(ValuedColorEnum.GREEN, map.get("Green"));
+ assertSame(ValuedColorEnum.BLUE, map.get("Blue"));
+ }
+
+ public void testGet() {
+ assertSame(ValuedColorEnum.RED, ValuedColorEnum.getEnum("Red"));
+ assertSame(ValuedColorEnum.GREEN, ValuedColorEnum.getEnum("Green"));
+ assertSame(ValuedColorEnum.BLUE, ValuedColorEnum.getEnum("Blue"));
+ assertSame(null, ValuedColorEnum.getEnum("Pink"));
+ }
+
+ public void testGetValue() {
+ assertSame(ValuedColorEnum.RED, ValuedColorEnum.getEnum(1));
+ assertSame(ValuedColorEnum.GREEN, ValuedColorEnum.getEnum(2));
+ assertSame(ValuedColorEnum.BLUE, ValuedColorEnum.getEnum(3));
+ assertSame(null, ValuedColorEnum.getEnum(4));
+ }
+
+ public void testSerialization() {
+ assertSame(ValuedColorEnum.RED, SerializationUtils.clone(ValuedColorEnum.RED));
+ assertSame(ValuedColorEnum.GREEN, SerializationUtils.clone(ValuedColorEnum.GREEN));
+ assertSame(ValuedColorEnum.BLUE, SerializationUtils.clone(ValuedColorEnum.BLUE));
+ }
+
+}