KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > tools > db > Descriptor


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: Descriptor.java,v 1.1 2004/11/26 01:51:15 tanderson Exp $
44  */

45
46 package org.exolab.jms.tools.db;
47
48 import java.lang.reflect.Field JavaDoc;
49 import java.lang.reflect.Modifier JavaDoc;
50 import java.sql.Types JavaDoc;
51 import java.util.HashMap JavaDoc;
52
53
54 /**
55  * This class is a helper class for converting from string values to their
56  * corresponding {@link java.sql.Types}
57  *
58  * @version $Revision: 1.1 $ $Date: 2004/11/26 01:51:15 $
59  * @author <a HREF="mailto:tima@intalio.com">Tim Anderson</a>
60  */

61 class Descriptor {
62
63     /**
64      * The type identifier, corresponding to one in java.sql.Types
65      */

66     private final int _type;
67
68     /**
69      * The name of the type
70      */

71     private final String JavaDoc _name;
72
73     /**
74      * A map of type identifiers to their names
75      */

76     private static HashMap JavaDoc TYPE_MAP = null;
77
78     /**
79      * A map of names to their corresponding type identifiers
80      */

81     private static HashMap JavaDoc NAME_MAP = null;
82
83     /**
84      * Construct a new descriptor
85      *
86      * @param type the type identifier
87      * @param name the name of the type
88      */

89     private Descriptor(int type, String JavaDoc name) {
90         _type = type;
91         _name = name;
92     }
93
94     /**
95      * Returns the type identifier
96      *
97      * @return the type identifier, corresponding to one in
98      * {@link java.sql.Types}
99      */

100     public int getType() {
101         return _type;
102     }
103
104     /**
105      * Returns the type name
106      */

107     public String JavaDoc getName() {
108         return _name;
109     }
110
111     /**
112      * Returns the descriptor for a given type identifier
113      *
114      * @return the descriptor corresponding to the type identifier, or null
115      * if it doesn't exist
116      */

117     public static Descriptor getDescriptor(int type) {
118         return (Descriptor) TYPE_MAP.get(new Integer JavaDoc(type));
119     }
120
121     /**
122      * Returns the descriptor for a given type name
123      *
124      * @return the descriptor corresponding to the type name, or null
125      * if it doesn't exist
126      */

127     public static Descriptor getDescriptor(String JavaDoc name) {
128         return (Descriptor) NAME_MAP.get(name.toUpperCase());
129     }
130
131     /**
132      * Initialise the maps
133      */

134     static {
135         TYPE_MAP = new HashMap JavaDoc();
136         NAME_MAP = new HashMap JavaDoc();
137         try {
138             Field JavaDoc[] fields = Types JavaDoc.class.getFields();
139             for (int i = 0; i < fields.length; ++i) {
140                 Field JavaDoc field = fields[i];
141                 if (Modifier.isStatic(field.getModifiers())) {
142                     int type = ((Integer JavaDoc) field.get(null)).intValue();
143                     String JavaDoc name = field.getName().toUpperCase();
144                     Descriptor descriptor = new Descriptor(type, name);
145                     TYPE_MAP.put(new Integer JavaDoc(type), descriptor);
146                     NAME_MAP.put(name, descriptor);
147                 }
148             }
149         } catch (IllegalAccessException JavaDoc exception) {
150             throw new RuntimeException JavaDoc(exception.getMessage());
151         }
152     }
153
154 } //-- Descriptor
155
Popular Tags