1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang.enums;
18  
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Map;
22  
23  /**
24   * Operator enumeration.
25   *
26   * @author Stephen Colebourne
27   * @version $Id: OperationEnum.java 437554 2006-08-28 06:21:41Z bayard $
28   */
29  public abstract class OperationEnum extends Enum {
30      // This syntax works for JDK 1.3 and upwards:
31  //    public static final OperationEnum PLUS = new OperationEnum("Plus") {
32  //        public int eval(int a, int b) {
33  //            return (a + b);
34  //        }
35  //    };
36  //    public static final OperationEnum MINUS = new OperationEnum("Minus") {
37  //        public int eval(int a, int b) {
38  //            return (a - b);
39  //        }
40  //    };
41      // This syntax works for JDK 1.2 and upwards:
42      public static final OperationEnum PLUS = new PlusOperation();
43      private static class PlusOperation extends OperationEnum {
44          private PlusOperation() {
45              super("Plus");
46          }
47          public int eval(int a, int b) {
48              return (a + b);
49          }
50      }
51      public static final OperationEnum MINUS = new MinusOperation();
52      private static class MinusOperation extends OperationEnum {
53          private MinusOperation() {
54              super("Minus");
55          }
56          public int eval(int a, int b) {
57              return (a - b);
58          }
59      }
60  
61      private OperationEnum(String name) {
62          super(name);
63      }
64      
65      public final Class getEnumClass() {
66          return OperationEnum.class;
67      }
68  
69      public abstract int eval(int a, int b);
70  
71      public static OperationEnum getEnum(String name) {
72          return (OperationEnum) getEnum(OperationEnum.class, name);
73      }
74  
75      public static Map getEnumMap() {
76          return getEnumMap(OperationEnum.class);
77      }
78  
79      public static List getEnumList() {
80          return getEnumList(OperationEnum.class);
81      }
82  
83      public static Iterator iterator() {
84          return iterator(OperationEnum.class);
85      }
86  }