01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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:
44: private static class PlusOperation extends OperationEnum {
45: private PlusOperation() {
46: super ("Plus");
47: }
48:
49: public int eval(int a, int b) {
50: return (a + b);
51: }
52: }
53:
54: public static final OperationEnum MINUS = new MinusOperation();
55:
56: private static class MinusOperation extends OperationEnum {
57: private MinusOperation() {
58: super ("Minus");
59: }
60:
61: public int eval(int a, int b) {
62: return (a - b);
63: }
64: }
65:
66: private OperationEnum(String name) {
67: super (name);
68: }
69:
70: public final Class getEnumClass() {
71: return OperationEnum.class;
72: }
73:
74: public abstract int eval(int a, int b);
75:
76: public static OperationEnum getEnum(String name) {
77: return (OperationEnum) getEnum(OperationEnum.class, name);
78: }
79:
80: public static Map getEnumMap() {
81: return getEnumMap(OperationEnum.class);
82: }
83:
84: public static List getEnumList() {
85: return getEnumList(OperationEnum.class);
86: }
87:
88: public static Iterator iterator() {
89: return iterator(OperationEnum.class);
90: }
91: }
|