1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ws.commons.schema.constants;
18
19 public abstract class Enum {
20
21 public static String NULL = "NULL";
22
23 protected Enum(String value) {
24 setValue(value);
25 }
26
27 protected Enum() {
28 this(NULL);
29 }
30
31 protected abstract String[] getValues();
32
33 protected String value = NULL;
34
35 public void setValue(String value) {
36 if (value.equals(Enum.NULL))
37 this.value = Enum.NULL;
38 else {
39 String values[] = getValues();
40 for (int i = 0; i < values.length; i++) {
41 if (values[i].equals(value)) {
42 this.value = value;
43 break;
44 }
45 if (i == values.length - 1)
46 throw new EnumValueException("Bad Enumeration value '" + value + "'");
47 }
48 }
49 }
50
51 public String getValue() {
52 return value;
53 }
54
55 public String toString() {
56 return value;
57 }
58
59 public boolean equals(Object what) {
60 return what.getClass().equals(this.getClass()) &&
61 ((Enum) what).getValue().equals(this.getValue());
62 }
63
64 public static class EnumValueException extends RuntimeException {
65 public EnumValueException(String mesg) {
66 super(mesg);
67 }
68 }
69
70 protected static final int index(String value, String values[]) {
71 for (int i = 0; i < values.length; i++) {
72 if (value.equals(values[i]))
73 return i;
74 }
75 return -1;
76 }
77 }
78