| Line | Hits | Note | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * $Id: IntUserTypeBase.java 1557 2009-10-08 19:07:58Z amandel $ | ||
| 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 | */ | ||
| 33 | package org.jcoderz.commons.util; | ||
| 34 | |||
| 35 | import java.sql.PreparedStatement; | ||
| 36 | import java.sql.ResultSet; | ||
| 37 | import java.sql.SQLException; | ||
| 38 | import java.sql.Types; | ||
| 39 | |||
| 40 | import 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 | */ | ||
| 48 | 0 | public abstract class IntUserTypeBase | |
| 49 | extends UserTypeBase | ||
| 50 | { | ||
| 51 | private static final long serialVersionUID = 1L; | ||
| 52 | private static final int SQL_TYPE = Types.INTEGER; | ||
| 53 | 0 | 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 | (1) | throws HibernateException, SQLException | |
| 61 | { | ||
| 62 | 0 | (2) | final int value = resultSet.getInt(types[0]); |
| 63 | final Object result; | ||
| 64 | 0 | if (resultSet.wasNull()) | |
| 65 | { | ||
| 66 | 0 | result = null; | |
| 67 | } | ||
| 68 | else | ||
| 69 | { | ||
| 70 | 0 | result = fromInt(value); | |
| 71 | } | ||
| 72 | 0 | 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 | (3) | throws HibernateException, SQLException | |
| 98 | { | ||
| 99 | 0 | if (value != null) | |
| 100 | { | ||
| 101 | 0 | statement.setInt(index, toInt(value)); | |
| 102 | } | ||
| 103 | else | ||
| 104 | { | ||
| 105 | 0 | statement.setNull(index, SQL_TYPE); | |
| 106 | } | ||
| 107 | 0 | } | |
| 108 | |||
| 109 | /** | ||
| 110 | * {@inheritDoc} | ||
| 111 | */ | ||
| 112 | public int[] sqlTypes () | ||
| 113 | { | ||
| 114 | 0 | return (int[]) SQL_TYPES.clone(); | |
| 115 | } | ||
| 116 | } |