KickJava   Java API By Example, From Geeks To Geeks.

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


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: Type.java,v 1.1 2004/11/26 01:51:16 tanderson Exp $
44  */

45
46 package org.exolab.jms.tools.db;
47
48 import org.exolab.jms.persistence.PersistenceException;
49
50
51 /**
52  * This class is a helper class for converting from string values to their
53  * corresponding <code>java.sql.Types</code>
54  *
55  * @version $Revision: 1.1 $ $Date: 2004/11/26 01:51:16 $
56  * @author <a HREF="mailto:tima@intalio.com">Tim Anderson</a>
57  */

58 public class Type {
59
60     /**
61      * The type descriptor
62      */

63     private final Descriptor _descriptor;
64
65     /**
66      * The type name
67      */

68     private final String JavaDoc _name;
69
70     /**
71      * The precision of the type
72      */

73     private final long _precision;
74
75     /**
76      * If true, denotes that the type takes parameters
77      */

78     private final boolean _parameters;
79
80     /**
81      * Construct an instance, using the default name for the type
82      *
83      * @param type a type corresponding to one in <code>java.sql.Types</code>
84      * @param precision the precision of the type. A precision &lt;= 0
85      * indicates that the type has no precision
86      * @param parameters if true, denotes that the type takes parameters
87      * @throws IllegalArgumentException if type is invalid
88      */

89     public Type(int type, long precision, boolean parameters) {
90         _descriptor = Descriptor.getDescriptor(type);
91         if (_descriptor == null) {
92             throw new IllegalArgumentException JavaDoc("Type id=" + type +
93                 " is not a valid type");
94         }
95         _name = _descriptor.getName();
96         _precision = precision;
97         _parameters = parameters;
98     }
99
100     /**
101      * Construct an instance specifying the database specific type name
102      *
103      * @param type a type corresponding to one in <code>java.sql.Types</code>
104      * @param name the RDBMS name of the type
105      * @param precision the precision of the type. A precision &lt;= 0
106      * indicates that the type has no precision
107      * @param parameters if true, denotes that the type takes parameters
108      * @throws IllegalArgumentException if type is invalid
109      */

110     public Type(int type, String JavaDoc name, long precision, boolean parameters) {
111         _descriptor = Descriptor.getDescriptor(type);
112         if (_descriptor == null) {
113             throw new IllegalArgumentException JavaDoc("Type id=" + type +
114                 " is not a valid type");
115         }
116         _name = name;
117         _precision = precision;
118         _parameters = parameters;
119     }
120
121     /**
122      * Returns the type identifier
123      *
124      * @return the type identifier, corresponding to one in
125      * <code>java.sql.Types</code>
126      */

127     public int getType() {
128         return _descriptor.getType();
129     }
130
131     /**
132      * Returns the name of the type
133      *
134      * @return the type name
135      */

136     public String JavaDoc getName() {
137         return _name;
138     }
139
140     /**
141      * Returns the precision of the type
142      *
143      * @return the type precision
144      */

145     public long getPrecision() {
146         return _precision;
147     }
148
149     /**
150      * Returns if the type takes parameters when created
151      *
152      * @return true if the type takes parameters when created, false otherwise
153      */

154     public boolean getParameters() {
155         return _parameters;
156     }
157
158     /**
159      * Returns a symbolic representation of the type
160      *
161      * @return a symbolic representation of the type
162      */

163     public String JavaDoc getSymbolicType() {
164         String JavaDoc result = _descriptor.getName();
165         if (_parameters && _precision > 0) {
166             result += "(" + _precision + ")";
167         }
168         return result;
169     }
170
171     /**
172      * Returns an SQL representation of the type
173      *
174      * @return an SQL string representation of the type
175      */

176     public String JavaDoc getSQL() {
177         String JavaDoc result = _name;
178         if (_parameters && _precision > 0) {
179             result += "(" + _precision + ")";
180         }
181         return result;
182     }
183
184     /**
185      * Returns a string representation of the type, for debugging purposes
186      */

187     public String JavaDoc toString() {
188         return "type=" + _descriptor.getName() + ", name=" + _name +
189             ", precision=" + _precision + ", parameters=" +
190             _parameters;
191     }
192
193     /**
194      * Returns a new type corresponding to its string representation
195      *
196      * @param type the string representation of the type
197      * @return the type corresponding to the string
198      * @throws PersistenceException if the string is invalid
199      */

200     public static Type getType(String JavaDoc type) throws PersistenceException {
201         int start = type.indexOf('(');
202         String JavaDoc name = type;
203         long precision = -1;
204         boolean parameters = false;
205         if (start != -1) {
206             name = type.substring(0, start);
207             int end = type.indexOf(')', start);
208             if (end == -1) {
209                 throw new PersistenceException("Illegal type: " + type);
210             }
211             precision = Long.parseLong(type.substring(start + 1, end));
212             parameters = true;
213         }
214
215         Descriptor descriptor = Descriptor.getDescriptor(name.trim());
216         if (descriptor == null) {
217             throw new PersistenceException("Type name=" + type +
218                 " is not a valid type");
219         }
220         return new Type(descriptor.getType(), precision, parameters);
221     }
222
223 } //-- Type
224
Popular Tags