Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 16082 invoked from network); 5 Aug 2003 00:23:50 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 5 Aug 2003 00:23:50 -0000 Received: (qmail 24591 invoked by uid 97); 5 Aug 2003 00:26:35 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@nagoya.betaversion.org Received: (qmail 24584 invoked from network); 5 Aug 2003 00:26:35 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 5 Aug 2003 00:26:35 -0000 Received: (qmail 15877 invoked by uid 500); 5 Aug 2003 00:23:48 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 15862 invoked by uid 500); 5 Aug 2003 00:23:47 -0000 Received: (qmail 15857 invoked from network); 5 Aug 2003 00:23:47 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 5 Aug 2003 00:23:47 -0000 Received: (qmail 18237 invoked by uid 1529); 5 Aug 2003 00:24:03 -0000 Date: 5 Aug 2003 00:24:02 -0000 Message-ID: <20030805002402.18236.qmail@minotaur.apache.org> From: scolebourne@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/enum Enum.java ValuedEnum.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N scolebourne 2003/08/04 17:24:02 Modified: lang/src/test/org/apache/commons/lang/enum EnumTest.java OperationEnum.java lang/src/java/org/apache/commons/lang/enum Enum.java ValuedEnum.java Added: lang/src/test/org/apache/commons/lang/enum Broken1OperationEnum.java Broken4OperationEnum.java Broken5OperationEnum.java Broken3OperationEnum.java Broken2OperationEnum.java Log: Rework Enum JDK1.2 solution to avoid needing to store Class in Serialized object. Added more tests, improved performance bug 19030 Revision Changes Path 1.10 +46 -1 jakarta-commons/lang/src/test/org/apache/commons/lang/enum/EnumTest.java Index: EnumTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/enum/EnumTest.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- EnumTest.java 31 Jul 2003 22:36:39 -0000 1.9 +++ EnumTest.java 5 Aug 2003 00:24:02 -0000 1.10 @@ -203,6 +203,51 @@ } } + 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")); 1.5 +6 -2 jakarta-commons/lang/src/test/org/apache/commons/lang/enum/OperationEnum.java Index: OperationEnum.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/enum/OperationEnum.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- OperationEnum.java 4 Aug 2003 23:52:27 -0000 1.4 +++ OperationEnum.java 5 Aug 2003 00:24:02 -0000 1.5 @@ -96,7 +96,11 @@ } 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); 1.1 jakarta-commons/lang/src/test/org/apache/commons/lang/enum/Broken1OperationEnum.java Index: Broken1OperationEnum.java =================================================================== /* ==================================================================== * 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 * . */ 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); } } 1.1 jakarta-commons/lang/src/test/org/apache/commons/lang/enum/Broken4OperationEnum.java Index: Broken4OperationEnum.java =================================================================== /* ==================================================================== * 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 * . */ 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); } } 1.1 jakarta-commons/lang/src/test/org/apache/commons/lang/enum/Broken5OperationEnum.java Index: Broken5OperationEnum.java =================================================================== /* ==================================================================== * 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 * . */ 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); } } 1.1 jakarta-commons/lang/src/test/org/apache/commons/lang/enum/Broken3OperationEnum.java Index: Broken3OperationEnum.java =================================================================== /* ==================================================================== * 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 * . */ 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); } } 1.1 jakarta-commons/lang/src/test/org/apache/commons/lang/enum/Broken2OperationEnum.java Index: Broken2OperationEnum.java =================================================================== /* ==================================================================== * 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 * . */ 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); } } 1.19 +50 -47 jakarta-commons/lang/src/java/org/apache/commons/lang/enum/Enum.java Index: Enum.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/enum/Enum.java,v retrieving revision 1.18 retrieving revision 1.19 diff -u -r1.18 -r1.19 --- Enum.java 4 Aug 2003 23:52:27 -0000 1.18 +++ Enum.java 5 Aug 2003 00:24:02 -0000 1.19 @@ -163,7 +163,7 @@ *

Functional Enums

* *

The enums can have functionality by defining subclasses and - * changing the super() call:

+ * overriding the getEnumClass() method:

* *
    *   public static final OperationEnum PLUS = new PlusOperation();
  @@ -186,9 +186,13 @@
    *   }
    *
    *   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) {
  @@ -238,10 +242,6 @@
        */
       private final String iName;
       /**
  -     * The Enum class.
  -     */
  -    private final Class iEnumClass;
  -    /**
        * The hashcode representation of the Enum.
        */
       private transient final int iHashCode;
  @@ -259,9 +259,17 @@
            */
           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);
   
           /**
            * 

Restrictive constructor.

@@ -277,38 +285,14 @@ * must not be empty or null * @throws IllegalArgumentException if the name is null * or an empty string + * @throws IllegalArgumentException if the getEnumClass() method returns + * a null or invalid Class */ protected Enum(String name) { super(); - init(name, getClass()); - iName = name; - iEnumClass = getClass(); - iHashCode = 7 + iEnumClass.hashCode() + 3 * name.hashCode(); - // cannot create toString here as subclasses may want to include other data - } - - /** - *

Constructor to add a new named item to the enumeration.

- * - *

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.

- * - * @param name the name of the enum object, - * must not be empty or null - * @param enumClass the enum class, - * must not be null and must be this class or a superclass - * @throws IllegalArgumentException if the name is null - * or an empty string - * @throws IllegalArgumentException if the enumClass is null - * or invalid - */ - protected Enum(String name, Class enumClass) { - super(); - init(name, enumClass); + init(name); 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 @@ * 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 @@ * @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 @@ if (entry == null) { return EMPTY_MAP; } - return Collections.unmodifiableMap(entry.map); + return entry.unmodifiableMap; } /** @@ -411,7 +413,7 @@ if (entry == null) { return Collections.EMPTY_LIST; } - return Collections.unmodifiableList(entry.list); + return entry.unmodifiableList; } /** @@ -487,12 +489,13 @@ *

Retrieves the Class of this Enum item, set in the constructor.

* *

This is normally the same as getClass(), but for - * advanced Enums may be different.

+ * advanced Enums may be different. If overridden, it must return a + * constant value.

* - * @return the String name of this Enum item + * @return the Class of the enum */ - public final Class getEnumClass() { - return iEnumClass; + public Class getEnumClass() { + return getClass(); } /** @@ -513,7 +516,7 @@ } 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 String toString() { if (iToString == null) { - String shortName = ClassUtils.getShortClassName(iEnumClass); + String shortName = ClassUtils.getShortClassName(getEnumClass()); iToString = shortName + "[" + getName() + "]"; } return iToString; 1.12 +1 -13 jakarta-commons/lang/src/java/org/apache/commons/lang/enum/ValuedEnum.java Index: ValuedEnum.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/enum/ValuedEnum.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- ValuedEnum.java 4 Aug 2003 23:52:27 -0000 1.11 +++ ValuedEnum.java 5 Aug 2003 00:24:02 -0000 1.12 @@ -157,18 +157,6 @@ } /** - * 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; - } - - /** *

Gets an Enum object by class and value.

* *

This method loops through the list of Enum, --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org