KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > catalog > types > DefaultInfoImpl


1 /*
2
3    Derby - Class org.apache.derby.catalog.types.DefaultInfoImpl
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.catalog.types;
23
24 import org.apache.derby.iapi.services.io.StoredFormatIds;
25 import org.apache.derby.iapi.services.io.Formatable;
26
27 import org.apache.derby.iapi.types.DataValueDescriptor;
28
29 import org.apache.derby.iapi.sql.depend.ProviderInfo;
30
31 import org.apache.derby.iapi.reference.SQLState;
32
33 import org.apache.derby.catalog.DefaultInfo;
34
35 import java.io.ObjectOutput JavaDoc;
36 import java.io.ObjectInput JavaDoc;
37 import java.io.IOException JavaDoc;
38
39 public class DefaultInfoImpl implements DefaultInfo, Formatable
40 {
41     /********************************************************
42     **
43     ** This class implements Formatable. That means that it
44     ** can write itself to and from a formatted stream. If
45     ** you add more fields to this class, make sure that you
46     ** also write/read them with the writeExternal()/readExternal()
47     ** methods.
48     **
49     ** If, inbetween releases, you add more fields to this class,
50     ** then you should bump the version number emitted by the getTypeFormatId()
51     ** method.
52     **
53     ********************************************************/

54
55     private DataValueDescriptor defaultValue;
56     private String JavaDoc defaultText;
57     private int type;
58
59     final private static int BITS_MASK_IS_DEFAULTVALUE_AUTOINC = 0x1 << 0;
60
61     /**
62      * Public niladic constructor. Needed for Formatable interface to work.
63      *
64      */

65     public DefaultInfoImpl() {}
66
67     /**
68      * Constructor for use with numeric types
69      *
70      * @param defaultText The text of the default.
71      */

72     public DefaultInfoImpl(boolean isDefaultValueAutoinc,
73         String JavaDoc defaultText,
74         DataValueDescriptor defaultValue)
75     {
76         this.type = calcType(isDefaultValueAutoinc);
77         this.defaultText = defaultText;
78         this.defaultValue = defaultValue;
79     }
80
81     /**
82      * @see DefaultInfo#getDefaultText
83      */

84     public String JavaDoc getDefaultText()
85     {
86         return defaultText;
87     }
88
89     public String JavaDoc toString()
90     {
91         if(isDefaultValueAutoinc()){
92             return "GENERATED_BY_DEFAULT";
93         }
94         return defaultText;
95     }
96
97     // Formatable methods
98

99     /**
100      * Read this object from a stream of stored objects.
101      *
102      * @param in read this.
103      *
104      * @exception IOException thrown on error
105      * @exception ClassNotFoundException thrown on error
106      */

107     public void readExternal( ObjectInput JavaDoc in )
108          throws IOException JavaDoc, ClassNotFoundException JavaDoc
109     {
110         defaultText = (String JavaDoc) in.readObject();
111         defaultValue = (DataValueDescriptor) in.readObject();
112         type = in.readInt();
113     }
114
115     /**
116      * Write this object to a stream of stored objects.
117      *
118      * @param out write bytes here.
119      *
120      * @exception IOException thrown on error
121      */

122     public void writeExternal( ObjectOutput JavaDoc out )
123          throws IOException JavaDoc
124     {
125         out.writeObject( defaultText );
126         out.writeObject( defaultValue );
127         out.writeInt(type);
128     }
129  
130     /**
131      * Get the formatID which corresponds to this class.
132      *
133      * @return the formatID of this class
134      */

135     public int getTypeFormatId() { return StoredFormatIds.DEFAULT_INFO_IMPL_V01_ID; }
136
137     /**
138      * Get the default value.
139      * (NOTE: This returns null if
140      * the default is not a constant.)
141      *
142      * @return The default value.
143      */

144     public DataValueDescriptor getDefaultValue()
145     {
146         return defaultValue;
147     }
148
149     /**
150      * Set the default value.
151      *
152      * @param defaultValue The default value.
153      */

154     public void setDefaultValue(DataValueDescriptor defaultValue)
155     {
156         this.defaultValue = defaultValue;
157     }
158     
159     /**
160      * @see DefaultInfo#isDefaultValueAutoinc
161      */

162     public boolean isDefaultValueAutoinc(){
163         return (type & BITS_MASK_IS_DEFAULTVALUE_AUTOINC ) != 0;
164     }
165     
166     /**
167      * This function returns stored value for flags and so on.
168      */

169     private static int calcType(boolean isDefaultValueAutoinc){
170
171         int value = 0;
172
173         if(isDefaultValueAutoinc){
174             value |= BITS_MASK_IS_DEFAULTVALUE_AUTOINC;
175         }
176
177         return value;
178     }
179     
180 }
181
Popular Tags