KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > enums > Enum


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.enums;
57
58 import org.jboss.axis.utils.Messages;
59 import org.jboss.logging.Logger;
60
61 import java.util.Hashtable JavaDoc;
62
63
64 /**
65  * General support for 'enumerated' data types.
66  * Name searches are case insensitive.
67  *
68  * @author Richard Sitze (rsitze@apache.org)
69  */

70 public abstract class Enum
71 {
72    private static final Hashtable JavaDoc types = new Hashtable JavaDoc(13);
73
74    private static Logger log = Logger.getLogger(Enum JavaDoc.class.getName());
75
76    private final Type type;
77    public final int value;
78    public final String JavaDoc name;
79
80    protected Enum(Type type, int value, String JavaDoc name)
81    {
82       this.type = type;
83       this.value = value;
84       this.name = name.intern();
85    }
86
87    public final int getValue()
88    {
89       return value;
90    }
91
92    public final String JavaDoc getName()
93    {
94       return name;
95    }
96
97    public final Type getType()
98    {
99       return type;
100    }
101
102    public String JavaDoc toString()
103    {
104       return name;
105    }
106
107    public final boolean equals(Object JavaDoc obj)
108    {
109       return (obj != null && obj instanceof Enum JavaDoc)
110               ? _equals((Enum JavaDoc)obj)
111               : false;
112    }
113
114    public final boolean equals(Enum JavaDoc obj)
115    {
116       return (obj != null) ? _equals(obj) : false;
117    }
118
119    /**
120     * The 'equals' logic assumes that there is a one-to-one
121     * relationship between value & name. If this isn't true,
122     * then expect to be confused when using this class with
123     * Collections.
124     */

125    private final boolean _equals(Enum JavaDoc obj)
126    {
127       return (//obj.name == name && // names are internalized
128
obj.type == type &&
129               obj.value == value);
130    }
131
132    public abstract static class Type
133    {
134       private final String JavaDoc name;
135       private final Enum JavaDoc[] enumArr;
136       private Enum JavaDoc dephault = null;
137
138       protected Type(String JavaDoc name, Enum JavaDoc[] enums)
139       {
140          this.name = name.intern();
141          this.enumArr = enums;
142          synchronized (types)
143          {
144             types.put(name, this);
145          }
146       }
147
148       protected void setDefault(Enum JavaDoc dephault)
149       {
150          this.dephault = dephault;
151       }
152
153       public Enum JavaDoc getDefault()
154       {
155          return dephault;
156       }
157
158       public final String JavaDoc getName()
159       {
160          return name;
161       }
162
163       public final boolean isValid(String JavaDoc enumName)
164       {
165          for (int en = 0; en < enumArr.length; en++)
166          {
167             if (enumArr[en].getName().equalsIgnoreCase(enumName))
168                return true;
169          }
170
171          return false;
172       }
173
174       public final int size()
175       {
176          return enumArr.length;
177       }
178
179       /**
180        * Returns array of names for enumerated values
181        */

182       public final String JavaDoc[] getEnumNames()
183       {
184          String JavaDoc[] nms = new String JavaDoc[size()];
185
186          for (int idx = 0; idx < enumArr.length; idx++)
187             nms[idx] = enumArr[idx].getName();
188
189          return nms;
190       }
191
192       /**
193        * Returns name of enumerated value
194        */

195       public final Enum JavaDoc getEnum(int en)
196       {
197          return (en >= 0 && en < enumArr.length) ? enumArr[en] : null;
198       }
199
200       /**
201        * Returns enumerated value of name
202        */

203       public final Enum JavaDoc getEnum(String JavaDoc enumName)
204       {
205          Enum JavaDoc e = getEnum(enumName, null);
206
207          if (e == null)
208          {
209             log.error(Messages.getMessage("badEnum02", name, enumName));
210          }
211
212          return e;
213       }
214
215       /**
216        * Returns enumerated value of name
217        * <p/>
218        * For large sets of enumerated values, a HashMap could
219        * be used to retrieve. It's not clear if there is any
220        * benefit for small (3 to 4) sets, as used now.
221        */

222       public final Enum JavaDoc getEnum(String JavaDoc enumName, Enum JavaDoc dephault)
223       {
224          if (enumName != null && enumName.length() > 0)
225          {
226             for (int en = 0; en < enumArr.length; en++)
227             {
228                Enum JavaDoc e = enumArr[en];
229                if (e.getName().equalsIgnoreCase(enumName))
230                   return e;
231             }
232          }
233
234          return dephault;
235       }
236    }
237 }
238
Popular Tags