diff --git a/src/java/org/apache/commons/lang/enum/Enum.java b/src/java/org/apache/commons/lang/enum/Enum.java
index 048f427e5..0f348b52d 100644
--- a/src/java/org/apache/commons/lang/enum/Enum.java
+++ b/src/java/org/apache/commons/lang/enum/Enum.java
@@ -63,6 +63,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.StringUtils;
/**
@@ -203,12 +204,15 @@ import org.apache.commons.lang.StringUtils;
* @author Chris Webb
* @author Mike Bowler
* @since 1.0
- * @version $Id: Enum.java,v 1.15 2003/07/20 15:49:58 scolebourne Exp $
+ * @version $Id: Enum.java,v 1.16 2003/07/30 23:13:09 scolebourne Exp $
*/
public abstract class Enum implements Comparable, Serializable {
+
+ /** Serialization id */
+ static final long serialVersionUID = -487045951170455942L;
+
// After discussion, the default size for HashMaps is used, as the
// sizing algorithm changes across the JDK versions
-
/**
* An empty Map
, as JDK1.2 didn't have an empty map.
*/
@@ -221,6 +225,14 @@ public abstract class Enum implements Comparable, Serializable {
* The string representation of the Enum.
*/
private final String iName;
+ /**
+ * The hashcode representation of the Enum.
+ */
+ private transient final int iHashCode;
+ /**
+ * The toString representation of the Enum.
+ */
+ protected transient String iToString = null;
/**
*
Enable the iterator to retain the source code order.
@@ -268,6 +280,9 @@ public abstract class Enum implements Comparable, Serializable { } entry.map.put(name, this); entry.list.add(this); + + iHashCode = 7 + enumClass.hashCode() + 3 * name.hashCode(); + // cannot create toString here as subclasses may want to include other data } /** @@ -490,7 +505,7 @@ public abstract class Enum implements Comparable, Serializable { * @return a hashcode based on the name */ public final int hashCode() { - return 7 + iName.hashCode(); + return iHashCode; } /** @@ -507,25 +522,26 @@ public abstract class Enum implements Comparable, Serializable { * @throws NullPointerException if other isnull
*/
public int compareTo(Object other) {
+ if (other == this) {
+ return 0;
+ }
return iName.compareTo(((Enum) other).iName);
}
/**
* Human readable description of this Enum item.
- * - *For use when debugging.
* * @return String in the formtype[name]
, for example:
* Color[Red]
. Note that the package name is stripped from
* the type name.
*/
public String toString() {
- String shortName = Enum.getEnumClass(getClass()).getName();
- int pos = shortName.lastIndexOf('.');
- if (pos != -1) {
- shortName = shortName.substring(pos + 1);
+ if (iToString == null) {
+ Class cls = Enum.getEnumClass(getClass());
+ String shortName = ClassUtils.getShortClassName(cls);
+ iToString = shortName + "[" + getName() + "]";
}
- shortName = shortName.replace('$', '.');
- return shortName + "[" + getName() + "]";
+ return iToString;
}
+
}
diff --git a/src/java/org/apache/commons/lang/enum/ValuedEnum.java b/src/java/org/apache/commons/lang/enum/ValuedEnum.java
index 398cc67a5..333a4b3ef 100644
--- a/src/java/org/apache/commons/lang/enum/ValuedEnum.java
+++ b/src/java/org/apache/commons/lang/enum/ValuedEnum.java
@@ -56,6 +56,8 @@ package org.apache.commons.lang.enum;
import java.util.Iterator;
import java.util.List;
+import org.apache.commons.lang.ClassUtils;
+
/**
* Abstract superclass for type-safe enums with integer values suitable
* for use in switch
statements.
Human readable description of this Enum
item.
For use when debugging.
- * * @return String in the formtype[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 = Enum.getEnumClass(getClass()).getName();
- int pos = shortName.lastIndexOf('.');
- if (pos != -1) {
- shortName = shortName.substring(pos + 1);
+ if (iToString == null) {
+ Class cls = Enum.getEnumClass(getClass());
+ String shortName = ClassUtils.getShortClassName(cls);
+ iToString = shortName + "[" + getName() + "=" + getValue() + "]";
}
- shortName = shortName.replace('$', '.');
- return shortName + "[" + getName() + "=" + getValue() + "]";
+ return iToString;
}
}
diff --git a/src/test/org/apache/commons/lang/enum/DummyEnum.java b/src/test/org/apache/commons/lang/enum/DummyEnum.java
new file mode 100644
index 000000000..63c4d3c92
--- /dev/null
+++ b/src/test/org/apache/commons/lang/enum/DummyEnum.java
@@ -0,0 +1,68 @@
+/* ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2002-2003 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
+ *