1 /**
2 * Redistribution and use of this software and associated documentation ("Software"), with or
3 * without modification, are permitted provided that the following conditions are met:
4 *
5 * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6 * must also contain a copy of this document.
7 *
8 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9 * conditions and the following disclaimer in the documentation and/or other materials provided with
10 * the distribution.
11 *
12 * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13 * without prior written permission of Intalio, Inc. For written permission, please contact
14 * info@exolab.org.
15 *
16 * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17 * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18 * Intalio, Inc.
19 *
20 * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21 *
22 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Copyright 1999-2003 (C) Intalio, Inc. All Rights Reserved.
32 *
33 * $Id$
34 */
35
36 package org.exolab.castor.mapping.loader;
37
38 import org.exolab.castor.mapping.TypeConvertor;
39 import org.exolab.castor.mapping.CollectionHandler;
40
41 /**
42 * Type information passed on creation of a {@link FieldHandlerImpl}.
43 *
44 *
45 * @author <a href="arkin@intalio.com">Assaf Arkin</a>
46 * @version $Revision$ $Date: 2005-03-05 06:42:06 -0700 (Sat, 05 Mar 2005) $
47 */
48 public class TypeInfo<T> {
49
50 /**
51 * The field type.
52 */
53 private Class<T> fieldType;
54
55 /**
56 * Convertor to the field type from external type.
57 */
58 private TypeConvertor convertorTo;
59
60 /**
61 * Convertor from the field type to external type.
62 */
63 private TypeConvertor convertorFrom;
64
65 /**
66 * True if the field type is immutable.
67 */
68 private boolean immutable = false;
69
70 /**
71 * True if the field is required.
72 */
73 private boolean required = false;
74
75 /**
76 * The default value of the field.
77 */
78 private Object _default;
79
80 /**
81 * The collection handler of the field.
82 */
83 private CollectionHandler<T> _colHandler;
84
85 /**
86 * Construct new type information for a field. This field requires no type conversion, and has no
87 * default value.
88 *
89 * @param fieldType The field type
90 **/
91 public TypeInfo(Class<T> fieldType) {
92 this(fieldType, null, null, false, null, null, true);
93 } // -- TypeInfo
94
95 /**
96 * Construct new type information for the field.
97 *
98 * @param fieldType The field type
99 * @param convertorTo Convertor to the field type from external type, or null if no conversion is
100 * required
101 * @param convertorFrom Convertor from the field type to external type, or null if no conversion
102 * is required
103 * @param required True if the field is required
104 * @param defaultValue The default value of the field, null to use the known Java defaults
105 * @param colHandler The collection handler for this field, or null if field is singular
106 */
107 public TypeInfo(Class<T> fieldType, TypeConvertor convertorTo, TypeConvertor convertorFrom,
108 boolean required, Object defaultValue, CollectionHandler<T> colHandler) {
109 this(fieldType, convertorTo, convertorFrom, required, defaultValue, colHandler, true);
110 }
111
112 /**
113 * Construct new type information for the field.
114 *
115 * @param fieldType The field type
116 * @param convertorTo Convertor to the field type from external type, or null if no conversion is
117 * required
118 * @param convertorFrom Convertor from the field type to external type, or null if no conversion
119 * is required
120 * @param required True if the field is required
121 * @param defaultValue The default value of the field, null to use the known Java defaults
122 * @param colHandler The collection handler for this field, or null if field is singular
123 */
124 public TypeInfo(Class<T> fieldType, TypeConvertor convertorTo, TypeConvertor convertorFrom,
125 boolean required, Object defaultValue, CollectionHandler<T> colHandler,
126 boolean checkForCollection) {
127 if ((colHandler == null) && checkForCollection) {
128
129 if (fieldType.isArray()) {
130 // -- byte[] should not use a CollectionHandler since it
131 // -- needs to be base64 encoded/decoded.
132 if (fieldType.getComponentType() != Byte.TYPE) {
133 try {
134 colHandler = CollectionHandlers.getHandler(Object[].class);
135 } catch (Exception e) {
136 // -- If we make it here, there was probably something wrong
137 // -- with loading the J1CollectionHandlers class...
138 throw new NullPointerException("Impossible to locate CollectionHandler for array.");
139 }
140 }
141
142 } else {
143 try {
144 colHandler = CollectionHandlers.getHandler(fieldType);
145 } catch (Exception e) {
146 // NOOP : It just mean that there is not handler for the
147 // collection
148 // and that this fieldType is not a collection
149 }
150 }
151 }
152
153 setFieldType(fieldType);
154 setConvertorTo(convertorTo);
155 setConvertorFrom(convertorFrom);
156 setImmutable(Types.isImmutable(fieldType));
157 setRequired(required);
158 // Note: must be called with fieldType (might be primitive) and not
159 // _fieldType (never primitive) to get the proper default value
160 setDefault((defaultValue == null ? Types.getDefault(fieldType) : defaultValue));
161 setColHandler(colHandler);
162 }
163
164 /**
165 * Returns the field type.
166 *
167 * @return The field type
168 */
169 public Class<?> getFieldType() {
170 return fieldType;
171 }
172
173 /**
174 * Returns the convertor to the field type from an external type.
175 *
176 * @return Convertor to field type
177 */
178 public TypeConvertor getConvertorTo() {
179 return convertorTo;
180 }
181
182 /**
183 * Returns the convertor from the field type to an external type.
184 *
185 * @return Convertor from field type
186 */
187 public TypeConvertor getConvertorFrom() {
188 return convertorFrom;
189 }
190
191 /**
192 * Returns true if field type is immutable.
193 *
194 * @return True if type is immutable
195 */
196 public boolean isImmutable() {
197 return immutable;
198 }
199
200 /**
201 * Returns true if field type is required.
202 *
203 * @return True if field is required
204 */
205 public boolean isRequired() {
206 return required;
207 }
208
209 /**
210 * Returns the default value for the field.
211 *
212 * @return The default value
213 */
214 public Object getDefaultValue() {
215 return getDefault();
216 }
217
218 /**
219 * Return the collection handler of this field.
220 *
221 * @return The collection handler of this field
222 */
223 public CollectionHandler<T> getCollectionHandler() {
224 return getColHandler();
225 }
226
227 /**
228 * Sets a flag indictating if the field is required.
229 *
230 * @param required the value of the flag. Should be true if the field is required, false
231 * otherwise.
232 */
233 public void setRequired(boolean required) {
234 this.required = required;
235 } // -- setRequired
236
237 /**
238 * Sets the CollectionHandler to use for the field described by this TypeInfo.
239 *
240 * @param handler the CollectionHandler, or null if no CollectionHandler should be used.
241 */
242 public void setCollectionHandler(CollectionHandler<T> handler) {
243 setColHandler(handler);
244 }
245
246 /**
247 * Sets whether or not the type is immutable
248 *
249 * @param immutable a boolean that when true indicates the type is immutable
250 */
251 void setImmutable(boolean immutable) {
252 this.immutable = immutable;
253 }
254
255 private void setFieldType(Class<T> fieldType) {
256 this.fieldType = fieldType;
257 }
258
259 private void setConvertorTo(TypeConvertor convertorTo) {
260 this.convertorTo = convertorTo;
261 }
262
263 private void setConvertorFrom(TypeConvertor convertorFrom) {
264 this.convertorFrom = convertorFrom;
265 }
266
267 private void setDefault(Object _default) {
268 this._default = _default;
269 }
270
271 private Object getDefault() {
272 return _default;
273 }
274
275 private void setColHandler(CollectionHandler<T> colHandler) {
276 _colHandler = colHandler;
277 }
278
279 private CollectionHandler<T> getColHandler() {
280 return _colHandler;
281 }
282
283 }