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 [EMAIL PROTECTED]
   *
   * 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);
      }
  }
  
  
  
  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 [EMAIL PROTECTED]
   *
   * 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);
      }
  }
  
  
  
  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 [EMAIL PROTECTED]
   *
   * 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);
      }
  }
  
  
  
  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 [EMAIL PROTECTED]
   *
   * 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);
      }
  }
  
  
  
  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 [EMAIL PROTECTED]
   *
   * 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);
      }
  }
  
  
  
  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 @@
    * <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 @@
    *   }
    *
    *   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);
   
           /**
            * <p>Restrictive constructor.</p>
  @@ -277,38 +285,14 @@
        *  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());
  -        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);
  +        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 @@
        * <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 @@
           } 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;
  -    }
  -
  -    /**
        * <p>Gets an <code>Enum</code> object by class and value.</p>
        *
        * <p>This method loops through the list of <code>Enum</code>,
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to