root/trunk/src/java/org/jcoderz/commons/util/IntUserTypeBase.java

Revision 1557, 3.5 kB (checked in by amandel, 3 years ago)

- add serial version uids
- support from arrays / base type arrays in toString
- add data of nested loggables

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.commons.util;
34
35import java.sql.PreparedStatement;
36import java.sql.ResultSet;
37import java.sql.SQLException;
38import java.sql.Types;
39
40import org.hibernate.HibernateException;
41
42/**
43 *
44 * This class is used by the Hibernate binding for numeric strong types.
45 *
46 * @author Andreas Mandel
47 */
48public abstract class IntUserTypeBase
49    extends UserTypeBase
50{
51    private static final long serialVersionUID = 1L;
52    private static final int SQL_TYPE = Types.INTEGER;
53    private static final int[] SQL_TYPES = {SQL_TYPE};
54
55    /**
56     * {@inheritDoc}
57     */
58    public Object nullSafeGet (ResultSet resultSet, String[] types,
59        Object owner)
60        throws HibernateException, SQLException
61    {
62        final int value = resultSet.getInt(types[0]);
63        final Object result;
64        if (resultSet.wasNull())
65        {
66            result = null;
67        }
68        else
69        {
70            result = fromInt(value);
71        }
72        return result;
73    }
74
75    /**
76     * Implement this method for the particular StrongType.
77     *
78     * @param value the int representation of the mapped class
79     * @return the instance of the mapped class
80     */
81    public abstract Object fromInt (int value);
82
83    /**
84     * Implement this method for the particular StrongType.
85     *
86     * @param value the StrongType to be converted into its
87     *    numeric representation.
88     * @return the int representation of the value given.
89     */
90    public abstract int toInt (Object value);
91
92    /**
93     * {@inheritDoc}
94     */
95    public void nullSafeSet (PreparedStatement statement, Object value,
96        int index)
97        throws HibernateException, SQLException
98    {
99        if (value != null)
100        {
101            statement.setInt(index, toInt(value));
102        }
103        else
104        {
105            statement.setNull(index, SQL_TYPE);
106        }
107    }
108
109    /**
110     * {@inheritDoc}
111     */
112    public int[] sqlTypes ()
113    {
114      return (int[]) SQL_TYPES.clone();
115    }
116}
Note: See TracBrowser for help on using the browser.