KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > types > BooleanType


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

19
20 package org.apache.cayenne.access.types;
21
22 import java.sql.CallableStatement JavaDoc;
23 import java.sql.PreparedStatement JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.Types JavaDoc;
26
27 import org.apache.cayenne.map.DbAttribute;
28 import org.apache.cayenne.validation.ValidationResult;
29
30 /**
31  * Handles <code>java.lang.Boolean</code> mapping. Note that "materialize*" methods
32  * return either Boolean.TRUE or Boolean.FALSE, instead of creating new Boolean instances
33  * using contructor. This makes possible identity comparison such as
34  * <code>object.getBooleanProperty() == Boolean.TRUE</code>.
35  *
36  * @since 1.2
37  * @author Andrus Adamchik
38  */

39 public class BooleanType implements ExtendedType {
40
41     public String JavaDoc getClassName() {
42         return Boolean JavaDoc.class.getName();
43     }
44
45     /**
46      * @deprecated since 3.0 as validation should not be done at the DataNode level.
47      */

48     public boolean validateProperty(
49             Object JavaDoc source,
50             String JavaDoc property,
51             Object JavaDoc value,
52             DbAttribute dbAttribute,
53             ValidationResult validationResult) {
54         return AbstractType.validateNull(
55                 source,
56                 property,
57                 value,
58                 dbAttribute,
59                 validationResult);
60     }
61
62     public void setJdbcObject(
63             PreparedStatement JavaDoc st,
64             Object JavaDoc val,
65             int pos,
66             int type,
67             int precision) throws Exception JavaDoc {
68
69         if (val == null) {
70             st.setNull(pos, type);
71         }
72         else if (type == Types.BIT || type == Types.BOOLEAN) {
73             boolean flag = Boolean.TRUE.equals(val);
74             st.setBoolean(pos, flag);
75         }
76         else {
77             st.setObject(pos, val, type);
78         }
79     }
80
81     public Object JavaDoc materializeObject(ResultSet JavaDoc rs, int index, int type) throws Exception JavaDoc {
82         boolean b = rs.getBoolean(index);
83         return (rs.wasNull()) ? null : b ? Boolean.TRUE : Boolean.FALSE;
84     }
85
86     public Object JavaDoc materializeObject(CallableStatement JavaDoc st, int index, int type)
87             throws Exception JavaDoc {
88         boolean b = st.getBoolean(index);
89         return (st.wasNull()) ? null : b ? Boolean.TRUE : Boolean.FALSE;
90     }
91 }
92
Popular Tags