Rework Enum JDK1.2 solution to avoid needing to store Class

in Serialized object.
Added more tests, improved performance
bug 19030


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137571 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-08-05 00:24:02 +00:00
parent 8b1992ee2f
commit 70d0151452
9 changed files with 718 additions and 63 deletions

View File

@ -163,7 +163,7 @@ import org.apache.commons.lang.StringUtils;
* <h4>Functional Enums</h4>
*
* <p>The enums can have functionality by defining subclasses and
* changing the <code>super()</code> call:</p>
* overriding the <code>getEnumClass()</code> method:</p>
*
* <pre>
* public static final OperationEnum PLUS = new PlusOperation();
@ -186,9 +186,13 @@ import org.apache.commons.lang.StringUtils;
* }
*
* private OperationEnum(String color) {
* super(color, OperationEnum.class); // NOTE: super() changed!
* super(color);
* }
*
* public final Class getEnumClass() { // NOTE: new method!
* return OperationEnum.class;
* }
*
* public abstract double eval(double a, double b);
*
* public static OperationEnum getEnum(String name) {
@ -216,7 +220,7 @@ import org.apache.commons.lang.StringUtils;
* @author Chris Webb
* @author Mike Bowler
* @since 1.0
* @version $Id: Enum.java,v 1.18 2003/08/04 23:52:27 scolebourne Exp $
* @version $Id: Enum.java,v 1.19 2003/08/05 00:24:02 scolebourne Exp $
*/
public abstract class Enum implements Comparable, Serializable {
@ -237,10 +241,6 @@ public abstract class Enum implements Comparable, Serializable {
* The string representation of the Enum.
*/
private final String iName;
/**
* The Enum class.
*/
private final Class iEnumClass;
/**
* The hashcode representation of the Enum.
*/
@ -258,10 +258,18 @@ public abstract class Enum implements Comparable, Serializable {
* Map of Enum name to Enum.
*/
final Map map = new HashMap();
/**
* Map of Enum name to Enum.
*/
final Map unmodifiableMap = Collections.unmodifiableMap(map);
/**
* List of Enums in source code order.
*/
final List list = new ArrayList(25);
/**
* Map of Enum name to Enum.
*/
final List unmodifiableList = Collections.unmodifiableList(list);
/**
* <p>Restrictive constructor.</p>
@ -277,38 +285,14 @@ public abstract class Enum implements Comparable, Serializable {
* must not be empty or <code>null</code>
* @throws IllegalArgumentException if the name is <code>null</code>
* or an empty string
* @throws IllegalArgumentException if the getEnumClass() method returns
* a null or invalid Class
*/
protected Enum(String name) {
super();
init(name, getClass());
init(name);
iName = name;
iEnumClass = getClass();
iHashCode = 7 + iEnumClass.hashCode() + 3 * name.hashCode();
// cannot create toString here as subclasses may want to include other data
}
/**
* <p>Constructor to add a new named item to the enumeration.</p>
*
* <p>This constructor is used when a subclass wants to allow further
* subclasses to add values to the enumeration. The class specifies
* which class they are all to be tied to.</p>
*
* @param name the name of the enum object,
* must not be empty or <code>null</code>
* @param enumClass the enum class,
* must not be null and must be this class or a superclass
* @throws IllegalArgumentException if the name is <code>null</code>
* or an empty string
* @throws IllegalArgumentException if the enumClass is <code>null</code>
* or invalid
*/
protected Enum(String name, Class enumClass) {
super();
init(name, enumClass);
iName = name;
iEnumClass = enumClass;
iHashCode = 7 + enumClass.hashCode() + 3 * name.hashCode();
iHashCode = 7 + getEnumClass().hashCode() + 3 * name.hashCode();
// cannot create toString here as subclasses may want to include other data
}
@ -316,14 +300,32 @@ public abstract class Enum implements Comparable, Serializable {
* Initializes the enumeration.
*
* @param name the enum name
* @param enumClass the enum class
* @throws IllegalArgumentException if the name is null or empty
* @throws IllegalArgumentException if the name is null or empty or duplicate
* @throws IllegalArgumentException if the enumClass is null or invalid
*/
private void init(String name, Class enumClass) {
private void init(String name) {
if (StringUtils.isEmpty(name)) {
throw new IllegalArgumentException("The Enum name must not be empty or null");
}
Class enumClass = getEnumClass();
if (enumClass == null) {
throw new IllegalArgumentException("getEnumClass() must not be null");
}
Class cls = getClass();
boolean ok = false;
while (cls != null && cls != Enum.class && cls != ValuedEnum.class) {
if (cls == enumClass) {
ok = true;
break;
}
cls = cls.getSuperclass();
}
if (ok == false) {
throw new IllegalArgumentException("getEnumClass() must return a superclass of this class");
}
// create entry
Entry entry = (Entry) cEnumClasses.get(enumClass);
if (entry == null) {
entry = createEntry(enumClass);
@ -343,7 +345,7 @@ public abstract class Enum implements Comparable, Serializable {
* @return the resolved object
*/
protected Object readResolve() {
Entry entry = (Entry) cEnumClasses.get(iEnumClass);
Entry entry = (Entry) cEnumClasses.get(getEnumClass());
if (entry == null) {
return null;
}
@ -389,7 +391,7 @@ public abstract class Enum implements Comparable, Serializable {
if (entry == null) {
return EMPTY_MAP;
}
return Collections.unmodifiableMap(entry.map);
return entry.unmodifiableMap;
}
/**
@ -411,7 +413,7 @@ public abstract class Enum implements Comparable, Serializable {
if (entry == null) {
return Collections.EMPTY_LIST;
}
return Collections.unmodifiableList(entry.list);
return entry.unmodifiableList;
}
/**
@ -487,12 +489,13 @@ public abstract class Enum implements Comparable, Serializable {
* <p>Retrieves the Class of this Enum item, set in the constructor.</p>
*
* <p>This is normally the same as <code>getClass()</code>, but for
* advanced Enums may be different.</p>
* advanced Enums may be different. If overridden, it must return a
* constant value.</p>
*
* @return the <code>String</code> name of this Enum item
* @return the <code>Class</code> of the enum
*/
public final Class getEnumClass() {
return iEnumClass;
public Class getEnumClass() {
return getClass();
}
/**
@ -513,7 +516,7 @@ public abstract class Enum implements Comparable, Serializable {
} else if (other.getClass() == this.getClass()) {
// shouldn't happen, but...
return iName.equals(((Enum) other).iName);
} else if (((Enum) other).iEnumClass.getName().equals(iEnumClass.getName())) {
} else if (((Enum) other).getEnumClass().getName().equals(getEnumClass().getName())) {
// different classloaders
try {
// try to avoid reflection
@ -577,7 +580,7 @@ public abstract class Enum implements Comparable, Serializable {
*/
public String toString() {
if (iToString == null) {
String shortName = ClassUtils.getShortClassName(iEnumClass);
String shortName = ClassUtils.getShortClassName(getEnumClass());
iToString = shortName + "[" + getName() + "]";
}
return iToString;

View File

@ -133,7 +133,7 @@ import org.apache.commons.lang.ClassUtils;
* @author Apache Avalon project
* @author Stephen Colebourne
* @since 1.0
* @version $Id: ValuedEnum.java,v 1.11 2003/08/04 23:52:27 scolebourne Exp $
* @version $Id: ValuedEnum.java,v 1.12 2003/08/05 00:24:02 scolebourne Exp $
*/
public abstract class ValuedEnum extends Enum {
@ -156,18 +156,6 @@ public abstract class ValuedEnum extends Enum {
iValue = value;
}
/**
* Constructor for enum item.
*
* @param name the name of enum item
* @param enumClass the enum class
* @param value the value of enum item
*/
protected ValuedEnum(String name, Class enumClass, int value) {
super(name, enumClass);
iValue = value;
}
/**
* <p>Gets an <code>Enum</code> object by class and value.</p>
*

View File

@ -0,0 +1,123 @@
/* ====================================================================
* 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
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.enum;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Broken Operator enumeration, null class.
*
* @author Stephen Colebourne
* @version $Id: Broken1OperationEnum.java,v 1.1 2003/08/05 00:24:02 scolebourne Exp $
*/
public abstract class Broken1OperationEnum extends Enum {
// This syntax works for JDK 1.3 and upwards:
// public static final OperationEnum PLUS = new OperationEnum("Plus") {
// public int eval(int a, int b) {
// return (a + b);
// }
// };
// public static final OperationEnum MINUS = new OperationEnum("Minus") {
// public int eval(int a, int b) {
// return (a - b);
// }
// };
// This syntax works for JDK 1.2 and upwards:
public static final Broken1OperationEnum PLUS = new PlusOperation();
private static class PlusOperation extends Broken1OperationEnum {
private PlusOperation() {
super("Plus");
}
public int eval(int a, int b) {
return (a + b);
}
}
public static final Broken1OperationEnum MINUS = new MinusOperation();
private static class MinusOperation extends Broken1OperationEnum {
private MinusOperation() {
super("Minus");
}
public int eval(int a, int b) {
return (a - b);
}
}
private Broken1OperationEnum(String name) {
super(name);
}
public final Class getEnumClass() {
return null;
}
public abstract int eval(int a, int b);
public static Broken1OperationEnum getEnum(String name) {
return (Broken1OperationEnum) getEnum(Broken1OperationEnum.class, name);
}
public static Map getEnumMap() {
return getEnumMap(Broken1OperationEnum.class);
}
public static List getEnumList() {
return getEnumList(Broken1OperationEnum.class);
}
public static Iterator iterator() {
return iterator(Broken1OperationEnum.class);
}
}

View File

@ -0,0 +1,123 @@
/* ====================================================================
* 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
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.enum;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Broken Operator enumeration, getEnumClass() not superclass.
*
* @author Stephen Colebourne
* @version $Id: Broken2OperationEnum.java,v 1.1 2003/08/05 00:24:02 scolebourne Exp $
*/
public abstract class Broken2OperationEnum extends Enum {
// This syntax works for JDK 1.3 and upwards:
// public static final OperationEnum PLUS = new OperationEnum("Plus") {
// public int eval(int a, int b) {
// return (a + b);
// }
// };
// public static final OperationEnum MINUS = new OperationEnum("Minus") {
// public int eval(int a, int b) {
// return (a - b);
// }
// };
// This syntax works for JDK 1.2 and upwards:
public static final Broken2OperationEnum PLUS = new PlusOperation();
private static class PlusOperation extends Broken2OperationEnum {
private PlusOperation() {
super("Plus");
}
public int eval(int a, int b) {
return (a + b);
}
}
public static final Broken2OperationEnum MINUS = new MinusOperation();
private static class MinusOperation extends Broken2OperationEnum {
private MinusOperation() {
super("Minus");
}
public int eval(int a, int b) {
return (a - b);
}
}
private Broken2OperationEnum(String name) {
super(name);
}
public final Class getEnumClass() {
return ColorEnum.class;
}
public abstract int eval(int a, int b);
public static Broken2OperationEnum getEnum(String name) {
return (Broken2OperationEnum) getEnum(Broken2OperationEnum.class, name);
}
public static Map getEnumMap() {
return getEnumMap(Broken2OperationEnum.class);
}
public static List getEnumList() {
return getEnumList(Broken2OperationEnum.class);
}
public static Iterator iterator() {
return iterator(Broken2OperationEnum.class);
}
}

View File

@ -0,0 +1,123 @@
/* ====================================================================
* 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
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.enum;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Broken Operator enumeration, getEnumClass() is Enum.class.
*
* @author Stephen Colebourne
* @version $Id: Broken3OperationEnum.java,v 1.1 2003/08/05 00:24:02 scolebourne Exp $
*/
public abstract class Broken3OperationEnum extends Enum {
// This syntax works for JDK 1.3 and upwards:
// public static final OperationEnum PLUS = new OperationEnum("Plus") {
// public int eval(int a, int b) {
// return (a + b);
// }
// };
// public static final OperationEnum MINUS = new OperationEnum("Minus") {
// public int eval(int a, int b) {
// return (a - b);
// }
// };
// This syntax works for JDK 1.2 and upwards:
public static final Broken3OperationEnum PLUS = new PlusOperation();
private static class PlusOperation extends Broken3OperationEnum {
private PlusOperation() {
super("Plus");
}
public int eval(int a, int b) {
return (a + b);
}
}
public static final Broken3OperationEnum MINUS = new MinusOperation();
private static class MinusOperation extends Broken3OperationEnum {
private MinusOperation() {
super("Minus");
}
public int eval(int a, int b) {
return (a - b);
}
}
private Broken3OperationEnum(String name) {
super(name);
}
public final Class getEnumClass() {
return Enum.class;
}
public abstract int eval(int a, int b);
public static Broken3OperationEnum getEnum(String name) {
return (Broken3OperationEnum) getEnum(Broken3OperationEnum.class, name);
}
public static Map getEnumMap() {
return getEnumMap(Broken3OperationEnum.class);
}
public static List getEnumList() {
return getEnumList(Broken3OperationEnum.class);
}
public static Iterator iterator() {
return iterator(Broken3OperationEnum.class);
}
}

View File

@ -0,0 +1,123 @@
/* ====================================================================
* 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
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.enum;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Broken Operator enumeration, getEnumClass() is ValuedEnum.class.
*
* @author Stephen Colebourne
* @version $Id: Broken4OperationEnum.java,v 1.1 2003/08/05 00:24:02 scolebourne Exp $
*/
public abstract class Broken4OperationEnum extends Enum {
// This syntax works for JDK 1.3 and upwards:
// public static final OperationEnum PLUS = new OperationEnum("Plus") {
// public int eval(int a, int b) {
// return (a + b);
// }
// };
// public static final OperationEnum MINUS = new OperationEnum("Minus") {
// public int eval(int a, int b) {
// return (a - b);
// }
// };
// This syntax works for JDK 1.2 and upwards:
public static final Broken4OperationEnum PLUS = new PlusOperation();
private static class PlusOperation extends Broken4OperationEnum {
private PlusOperation() {
super("Plus");
}
public int eval(int a, int b) {
return (a + b);
}
}
public static final Broken4OperationEnum MINUS = new MinusOperation();
private static class MinusOperation extends Broken4OperationEnum {
private MinusOperation() {
super("Minus");
}
public int eval(int a, int b) {
return (a - b);
}
}
private Broken4OperationEnum(String name) {
super(name);
}
public final Class getEnumClass() {
return ValuedEnum.class;
}
public abstract int eval(int a, int b);
public static Broken4OperationEnum getEnum(String name) {
return (Broken4OperationEnum) getEnum(Broken4OperationEnum.class, name);
}
public static Map getEnumMap() {
return getEnumMap(Broken4OperationEnum.class);
}
public static List getEnumList() {
return getEnumList(Broken4OperationEnum.class);
}
public static Iterator iterator() {
return iterator(Broken4OperationEnum.class);
}
}

View File

@ -0,0 +1,123 @@
/* ====================================================================
* 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
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.enum;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Broken Operator enumeration, getEnumClass() is not an Enum class.
*
* @author Stephen Colebourne
* @version $Id: Broken5OperationEnum.java,v 1.1 2003/08/05 00:24:02 scolebourne Exp $
*/
public abstract class Broken5OperationEnum extends Enum {
// This syntax works for JDK 1.3 and upwards:
// public static final OperationEnum PLUS = new OperationEnum("Plus") {
// public int eval(int a, int b) {
// return (a + b);
// }
// };
// public static final OperationEnum MINUS = new OperationEnum("Minus") {
// public int eval(int a, int b) {
// return (a - b);
// }
// };
// This syntax works for JDK 1.2 and upwards:
public static final Broken5OperationEnum PLUS = new PlusOperation();
private static class PlusOperation extends Broken5OperationEnum {
private PlusOperation() {
super("Plus");
}
public int eval(int a, int b) {
return (a + b);
}
}
public static final Broken5OperationEnum MINUS = new MinusOperation();
private static class MinusOperation extends Broken5OperationEnum {
private MinusOperation() {
super("Minus");
}
public int eval(int a, int b) {
return (a - b);
}
}
private Broken5OperationEnum(String name) {
super(name);
}
public final Class getEnumClass() {
return String.class;
}
public abstract int eval(int a, int b);
public static Broken5OperationEnum getEnum(String name) {
return (Broken5OperationEnum) getEnum(Broken5OperationEnum.class, name);
}
public static Map getEnumMap() {
return getEnumMap(Broken5OperationEnum.class);
}
public static List getEnumList() {
return getEnumList(Broken5OperationEnum.class);
}
public static Iterator iterator() {
return iterator(Broken5OperationEnum.class);
}
}

View File

@ -67,7 +67,7 @@ import org.apache.commons.lang.SerializationUtils;
* Test cases for the {@link Enum} class.
*
* @author Stephen Colebourne
* @version $Id: EnumTest.java,v 1.9 2003/07/31 22:36:39 scolebourne Exp $
* @version $Id: EnumTest.java,v 1.10 2003/08/05 00:24:02 scolebourne Exp $
*/
public final class EnumTest extends TestCase {
@ -203,6 +203,51 @@ public final class EnumTest extends TestCase {
}
}
public void testBroken1Operation() {
try {
Broken1OperationEnum.PLUS.getName();
fail();
} catch (ExceptionInInitializerError ex) {
assertTrue(ex.getException() instanceof IllegalArgumentException);
}
}
public void testBroken2Operation() {
try {
Broken2OperationEnum.PLUS.getName();
fail();
} catch (ExceptionInInitializerError ex) {
assertTrue(ex.getException() instanceof IllegalArgumentException);
}
}
public void testBroken3Operation() {
try {
Broken3OperationEnum.PLUS.getName();
fail();
} catch (ExceptionInInitializerError ex) {
assertTrue(ex.getException() instanceof IllegalArgumentException);
}
}
public void testBroken4Operation() {
try {
Broken4OperationEnum.PLUS.getName();
fail();
} catch (ExceptionInInitializerError ex) {
assertTrue(ex.getException() instanceof IllegalArgumentException);
}
}
public void testBroken5Operation() {
try {
Broken5OperationEnum.PLUS.getName();
fail();
} catch (ExceptionInInitializerError ex) {
assertTrue(ex.getException() instanceof IllegalArgumentException);
}
}
public void testOperationGet() {
assertSame(OperationEnum.PLUS, OperationEnum.getEnum("Plus"));
assertSame(OperationEnum.MINUS, OperationEnum.getEnum("Minus"));

View File

@ -61,7 +61,7 @@ import java.util.Map;
* Operator enumeration.
*
* @author Stephen Colebourne
* @version $Id: OperationEnum.java,v 1.4 2003/08/04 23:52:27 scolebourne Exp $
* @version $Id: OperationEnum.java,v 1.5 2003/08/05 00:24:02 scolebourne Exp $
*/
public abstract class OperationEnum extends Enum {
// This syntax works for JDK 1.3 and upwards:
@ -96,7 +96,11 @@ public abstract class OperationEnum extends Enum {
}
private OperationEnum(String name) {
super(name, OperationEnum.class);
super(name);
}
public final Class getEnumClass() {
return OperationEnum.class;
}
public abstract int eval(int a, int b);