View Javadoc

1   /*
2    * Copyright 2004,2007 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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