KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > constants > Enum


1 /*
2  * Copyright 2001-2004 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.axis.constants;
18
19 import org.apache.axis.components.logger.LogFactory;
20 import org.apache.axis.utils.Messages;
21 import org.apache.commons.logging.Log;
22
23 import java.util.Hashtable JavaDoc;
24
25
26 /**
27  * General support for 'enumerated' data types.
28  * Name searches are case insensitive.
29  *
30  * @author Richard Sitze (rsitze@apache.org)
31  */

32 public abstract class Enum implements java.io.Serializable JavaDoc {
33     private static final Hashtable JavaDoc types = new Hashtable JavaDoc(13);
34     
35     protected static Log log =
36         LogFactory.getLog(Enum JavaDoc.class.getName());
37
38     private final Type type;
39     public final int value;
40     public final String JavaDoc name;
41     
42     protected Enum(Type type, int value, String JavaDoc name) {
43         this.type = type;
44         this.value = value;
45         this.name = name.intern();
46     }
47     
48     public final int getValue() { return value; }
49     public final String JavaDoc getName() { return name; }
50     public final Type getType() { return type; }
51
52     public String JavaDoc toString() {
53         return name;
54     }
55
56     public final boolean equals(Object JavaDoc obj) {
57         return (obj != null && obj instanceof Enum JavaDoc)
58                ? _equals((Enum JavaDoc)obj)
59                : false;
60     }
61
62     public int hashCode() {
63         return value;
64     }
65     
66     public final boolean equals(Enum JavaDoc obj) {
67         return (obj != null) ? _equals(obj) : false;
68     }
69
70     /**
71      * The 'equals' logic assumes that there is a one-to-one
72      * relationship between value & name. If this isn't true,
73      * then expect to be confused when using this class with
74      * Collections.
75      */

76     private final boolean _equals(Enum JavaDoc obj) {
77         return (//obj.name == name && // names are internalized
78
obj.type == type &&
79                 obj.value == value);
80     }
81
82     public abstract static class Type implements java.io.Serializable JavaDoc {
83         private final String JavaDoc name;
84         private final Enum JavaDoc[] enums;
85         private Enum JavaDoc dephault = null;
86         
87         protected Type(String JavaDoc name, Enum JavaDoc[] enums) {
88             this.name = name.intern();
89             this.enums = enums;
90             synchronized (types) {
91                 types.put(name, this);
92             }
93         }
94         
95         public void setDefault(Enum JavaDoc dephault) {
96             this.dephault = dephault;
97         }
98         
99         public Enum JavaDoc getDefault() {
100             return dephault;
101         }
102         
103         public final String JavaDoc getName() {
104             return name;
105         }
106         
107         public final boolean isValid(String JavaDoc enumName) {
108             for (int enumElt = 0; enumElt < enums.length; enumElt++) {
109                 if (enums[enumElt].getName().equalsIgnoreCase(enumName))
110                     return true;
111             }
112             
113             return false;
114         }
115         
116         public final int size() {
117             return enums.length;
118         }
119         
120         /**
121          * Returns array of names for enumerated values
122          */

123         public final String JavaDoc[] getEnumNames() {
124             String JavaDoc[] nms = new String JavaDoc[ size() ];
125             
126             for (int idx = 0; idx < enums.length; idx++)
127                 nms[idx] = enums[idx].getName();
128     
129             return nms;
130         }
131         
132         /**
133          * Returns name of enumerated value
134          */

135         public final Enum JavaDoc getEnum(int enumElt) {
136             return (enumElt >= 0 && enumElt < enums.length) ? enums[enumElt] : null;
137         }
138         
139         /**
140          * Returns enumerated value of name
141          */

142         public final Enum JavaDoc getEnum(String JavaDoc enumName) {
143             Enum JavaDoc e = getEnum(enumName, null);
144             
145             if (e == null) {
146                 log.error(Messages.getMessage("badEnum02", name, enumName));
147             }
148     
149             return e;
150         }
151         
152         /**
153          * Returns enumerated value of name
154          *
155          * For large sets of enumerated values, a HashMap could
156          * be used to retrieve. It's not clear if there is any
157          * benefit for small (3 to 4) sets, as used now.
158          */

159         public final Enum JavaDoc getEnum(String JavaDoc enumName, Enum JavaDoc dephault) {
160             if (enumName != null && enumName.length() > 0) {
161                 for (int enumElt = 0; enumElt < enums.length; enumElt++) {
162                     Enum JavaDoc e = enums[enumElt];
163                     if (e.getName().equalsIgnoreCase(enumName))
164                         return e;
165                 }
166             }
167     
168             return dephault;
169         }
170
171         private Object JavaDoc readResolve() throws java.io.ObjectStreamException JavaDoc {
172             Object JavaDoc type = types.get(name);
173             if (type == null) {
174                 type = this;
175                 types.put(name, type);
176             }
177             return type;
178         }
179     }
180 }
181
Popular Tags