KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > ColumnElement


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.dbschema;
21
22 import java.sql.Types JavaDoc;
23
24 import org.netbeans.modules.dbschema.util.SQLTypeUtil;
25
26 /** Describes a column in a table.
27  */

28 public class ColumnElement extends DBMemberElement {
29     /** Create a new column element represented in memory.
30      */

31     public ColumnElement () {
32         this(new Memory(), null);
33     }
34
35     /** Creates a new column element.
36      * @param impl the pluggable implementation
37      * @param declaringTable declaring table of this column, or <code>null</code>
38      */

39     public ColumnElement (Impl impl, TableElement declaringTable) {
40         super(impl, declaringTable);
41     }
42
43     /** Indicates whether some other object is "equal to" this one.
44      * @param obj the reference object with which to compare.
45      * @return true if this object is the same as the obj argument; false otherwise.
46      */

47     public boolean equals(Object JavaDoc obj) {
48         Integer JavaDoc iThis, iArg;
49
50         if(!(obj instanceof ColumnElement))
51             return false;
52
53         ColumnElement ce = (ColumnElement) obj;
54         if(!getName().getFullName().equals(ce.getName().getFullName()))
55             return false;
56
57         if(getType() != ce.getType())
58             return false;
59
60         if(isNullable() != ce.isNullable())
61             return false;
62             
63         // handle length
64
iThis = getLength();
65         iArg = ce.getLength();
66         if (iThis != null ^ iArg != null)
67             // return false, if one length is null and the other is not null
68
return false;
69
70         if (iThis != null && iArg != null && iThis.compareTo(iArg) != 0)
71             // return false, if both lengths are defined but do not compare equal
72
return false;
73         
74         // handle scale
75
iThis = getScale();
76         iArg = ce.getScale();
77         if (iThis != null ^ iArg != null)
78             // return false, if one scale is null and the other is not null
79
return false;
80
81         if (iThis != null && iArg != null && iThis.compareTo(iArg) != 0)
82             // return false, if both scales are defined but do not compare equal
83
return false;
84
85         // handle precision
86
iThis = getPrecision();
87         iArg = ce.getPrecision();
88         if (iThis != null ^ iArg != null)
89             // return false, if one precision is null and the other is not null
90
return false;
91
92         if (iThis != null && iArg != null && iThis.compareTo(iArg) != 0)
93             // return false, if both precisions are defined but do not compare equal
94
return false;
95
96         return true;
97     }
98
99     /** Clone the column element.
100      * @return a new element that has the same values as the original but is represented in memory
101      */

102     public Object JavaDoc clone () {
103         return new ColumnElement(new Memory(this), null);
104     }
105
106     /** Returns the implementation for the column.
107      * @return implementation for the column
108      */

109     final Impl getColumnImpl() {
110         return (Impl)getElementImpl();
111     }
112
113     /** Get the value type of the column.
114      * @return the type
115      */

116     public int getType () {
117         return getColumnImpl().getType();
118     }
119
120     /** Set the value type of the column.
121      * @param type the type
122      * @throws DBException if impossible
123      */

124     public void setType (int type) throws DBException {
125         getColumnImpl().setType(type);
126     }
127
128     //convenience methods
129

130     /** Returns whether the data type is numeric.
131      * @return true if tha data type is numeric; false otherwise.
132      */

133     public boolean isNumericType () {
134         return SQLTypeUtil.isNumeric(getType());
135     }
136
137     /** Returns whether the data type is character.
138      * @return true if tha data type is character; false otherwise.
139      */

140     public boolean isCharacterType () {
141         return SQLTypeUtil.isCharacter(getType());
142     }
143     
144     /** Returns whether the data type is blob type.
145      * @return true if tha data type is blob type; false otherwise.
146      */

147     public boolean isBlobType () {
148         return SQLTypeUtil.isBlob(getType());
149     }
150     //end convenience methods
151

152     /** Returns whether the column is nullable.
153      * @return a flag representing whether the column is nullable
154      */

155     public boolean isNullable () {
156         return getColumnImpl().isNullable();
157     }
158
159     /** Set whether the column is nullable.
160      * @param flag flag representing whether the column is nullable
161      * @throws DBException if impossible
162      */

163     public void setNullable (boolean flag) throws DBException {
164         getColumnImpl().setNullable(flag);
165     }
166
167     /** Get the length of the column - for character type fields only.
168      * @return the length, <code>null</code> if it is not a character type
169      * field or there is no length.
170      */

171     public Integer JavaDoc getLength () {
172         if (isCharacterType() || isBlobType())
173             return getColumnImpl().getLength();
174         else
175             return null;
176   }
177
178     /** Set the length of the column - for character type fields only.
179      * @param length the length for the column if it a character type
180      * @throws DBException if impossible
181      */

182     public void setLength (Integer JavaDoc length) throws DBException {
183         if (isCharacterType() || isBlobType())
184             getColumnImpl().setLength(length);
185     }
186
187     /** Get the precision of the column - for numeric type fields only.
188      * @return the precision, <code>null</code> if it is not a numeric type
189      * field or there is no precision.
190      */

191     public Integer JavaDoc getPrecision () {
192         if (isNumericType())
193             return getColumnImpl().getPrecision();
194         else
195             return null;
196     }
197
198     /** Set the precision of the column - for numeric type fields only.
199      * @param precision the precision for the column if it a numeric type
200      * @throws DBException if impossible
201      */

202     public void setPrecision (Integer JavaDoc precision) throws DBException {
203         if (isNumericType())
204             getColumnImpl().setPrecision(precision);
205     }
206
207     /** Get the scale of the column - for numeric type fields only.
208      * @return the scale, <code>null</code> if it is not a numeric type
209      * field or there is no scale.
210      */

211     public Integer JavaDoc getScale () {
212         if (isNumericType())
213             return getColumnImpl().getScale();
214         else
215             return null;
216     }
217
218     /** Set the scale of the column - for numeric type fields only.
219      * @param scale the scale for the column if it a numeric type
220      * @throws DBException if impossible
221      */

222     public void setScale (Integer JavaDoc scale) throws DBException {
223         if (isNumericType())
224             getColumnImpl().setScale(scale);
225     }
226
227     /** Returns a string representation of the object.
228      * @return a string representation of the object.
229      */

230     public String JavaDoc toString() {
231         return getName().toString();
232     }
233
234     /** Implementation of a column element.
235      * @see ColumnElement
236      */

237     public interface Impl extends DBMemberElement.Impl {
238         /** Get the value type of the column.
239          * @return the type
240          */

241         public int getType ();
242
243         /** Set the value type of the column.
244         * @param type the type
245         * @throws DBException if impossible
246         */

247         public void setType (int type) throws DBException;
248
249         /** Returns whether the column is nullable.
250          * @return a flag representing whether the column is nullable
251          */

252         public boolean isNullable ();
253
254         /** Set whether the column is nullable.
255          * @param flag flag representing whether the column is nullable
256          * @throws DBException if impossible
257          */

258         public void setNullable (boolean flag) throws DBException;
259
260         /** Get the length of the column - for character type fields only.
261          * @return the length, <code>null</code> if it is not a character type
262          * field or there is no length.
263          */

264         public Integer JavaDoc getLength ();
265
266         /** Set the length of the column - for character type fields only.
267          * @param length the length for the column if it a character type
268          * @throws DBException if impossible
269          */

270         public void setLength (Integer JavaDoc length) throws DBException;
271
272         /** Get the precision of the column - for numeric type fields only.
273          * @return the precision, <code>null</code> if it is not a numeric type
274          * field or there is no precision.
275          */

276         public Integer JavaDoc getPrecision ();
277
278         /** Set the precision of the column - for numeric type fields only.
279          * @param precision the precision for the column if it a numeric type
280          * @throws DBException if impossible
281          */

282         public void setPrecision (Integer JavaDoc precision) throws DBException;
283
284         /** Get the scale of the column - for numeric type fields only.
285          * @return the scale, <code>null</code> if it is not a numeric type
286          * field or there is no scale.
287          */

288         public Integer JavaDoc getScale ();
289
290         /** Set the scale of the column - for numeric type fields only.
291          * @param scale the scale for the column if it a numeric type
292          * @throws DBException if impossible
293          */

294         public void setScale (Integer JavaDoc scale) throws DBException;
295     }
296
297     static class Memory extends DBMemberElement.Memory implements Impl {
298         /** Type of column */
299         private int _type;
300
301         /** Nullability flag */
302         private boolean _nullable;
303
304         /** Length of column */
305         private Integer JavaDoc _length;
306
307         /** Precision of column */
308         private Integer JavaDoc _precision;
309
310         /** Scale of column */
311         private Integer JavaDoc _scale;
312
313         /** Default constructor.
314          */

315         Memory () {
316             super();
317             _type = Types.NULL;
318         }
319
320         /** Copy constructor.
321         * @param column the object from which to read values
322         */

323         Memory (ColumnElement column) {
324             super(column);
325             _type = column.getType();
326             _nullable = column.isNullable();
327             _length = column.getLength();
328             _precision = column.getPrecision();
329             _scale = column.getScale();
330         }
331
332         /** Type of the column.
333         * @return the type
334         */

335         public int getType () {
336             return _type;
337         }
338
339         /** Setter for type of the column.
340         * @param type the column type
341         */

342         public void setType (int type) {
343             int old = _type;
344
345             _type = type;
346             firePropertyChange (PROP_TYPE, new Integer JavaDoc(old), new Integer JavaDoc(type));
347         }
348
349         /** Returns whether the column is nullable.
350          * @return a flag representing whether the column is nullable
351          */

352         public boolean isNullable () {
353             return _nullable;
354         }
355
356         /** Set whether the column is nullable.
357          * @param flag flag representing whether the column is nullable
358          * @throws DBException if impossible
359          */

360         public void setNullable (boolean flag) throws DBException {
361             boolean old = _nullable;
362
363             _nullable = flag;
364             firePropertyChange (PROP_NULLABLE, Boolean.valueOf(old), Boolean.valueOf(flag));
365         }
366
367         /** Get the length of the column - for character type fields only.
368          * @return the length, <code>null</code> if it is not a character type
369          * field or there is no length.
370          */

371         public Integer JavaDoc getLength () {
372             return _length;
373         }
374
375         /** Set the length of the column - for character type fields only.
376          * @param length the length for the column if it a character type
377          * @throws DBException if impossible
378          */

379         public void setLength (Integer JavaDoc length) throws DBException {
380             Integer JavaDoc old = _length;
381
382             _length = length;
383             firePropertyChange (PROP_LENGTH, old, length);
384         }
385
386         /** Get the precision of the column - for numeric type fields only.
387          * @return the precision, <code>null</code> if it is not a numeric type
388          * field or there is no precision.
389          */

390         public Integer JavaDoc getPrecision () {
391             return _precision;
392         }
393
394         /** Set the precision of the column - for numeric type fields only.
395          * @param precision the precision for the column if it a numeric type
396          * @throws DBException if impossible
397          */

398         public void setPrecision (Integer JavaDoc precision) throws DBException {
399             Integer JavaDoc old = _precision;
400
401             _precision = precision;
402             firePropertyChange (PROP_PRECISION, old, precision);
403         }
404
405         /** Get the scale of the column - for numeric type fields only.
406          * @return the scale, <code>null</code> if it is not a numeric type
407          * field or there is no scale.
408          */

409         public Integer JavaDoc getScale () {
410             return _scale;
411         }
412
413         /** Set the scale of the column - for numeric type fields only.
414          * @param scale the scale for the column if it a numeric type
415          * @throws DBException if impossible
416          */

417         public void setScale (Integer JavaDoc scale) throws DBException {
418             Integer JavaDoc old = _scale;
419
420             _scale = scale;
421             firePropertyChange (PROP_SCALE, old, scale);
422         }
423     }
424 }
425
Popular Tags