1 /*
2 * Hibernate, Relational Persistence for Idiomatic Java
3 *
4 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5 * indicated by the @author tags or express copyright attribution
6 * statements applied by the authors. All third-party contributions are
7 * distributed under license by Red Hat Middleware LLC.
8 *
9 * This copyrighted material is made available to anyone wishing to use, modify,
10 * copy, or redistribute it subject to the terms and conditions of the GNU
11 * Lesser General Public License, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this distribution; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301 USA
23 *
24 */
25 package org.hibernate.type;
26
27 import java.io.Serializable;
28 import java.sql.PreparedStatement;
29 import java.sql.ResultSet;
30 import java.sql.SQLException;
31 import java.sql.Types;
32 import java.util.Comparator;
33
34 import org.hibernate.util.ComparableComparator;
35 import org.hibernate.dialect.Dialect;
36 import org.hibernate.engine.SessionImplementor;
37
38 /**
39 * <tt>integer</tt>: A type that maps an SQL INT to a Java Integer.
40 * @author Gavin King
41 */
42 public class IntegerType extends PrimitiveType implements DiscriminatorType, VersionType {
43
44 private static final Integer ZERO = new Integer(0);
45
46 public Serializable getDefaultValue() {
47 return ZERO;
48 }
49
50 public Object get(ResultSet rs, String name) throws SQLException {
51 return new Integer( rs.getInt(name) );
52 }
53
54 public Class getPrimitiveClass() {
55 return int.class;
56 }
57
58 public Class getReturnedClass() {
59 return Integer.class;
60 }
61
62 public void set(PreparedStatement st, Object value, int index)
63 throws SQLException {
64 st.setInt( index, ( (Integer) value ).intValue() );
65 }
66
67 public int sqlType() {
68 return Types.INTEGER;
69 }
70
71 public String getName() { return "integer"; }
72
73 public String objectToSQLString(Object value, Dialect dialect) throws Exception {
74 return value.toString();
75 }
76
77 public Object stringToObject(String xml) throws Exception {
78 return new Integer(xml);
79 }
80
81 public Object next(Object current, SessionImplementor session) {
82 return new Integer( ( (Integer) current ).intValue() + 1 );
83 }
84
85 public Object seed(SessionImplementor session) {
86 return ZERO;
87 }
88
89 public Comparator getComparator() {
90 return ComparableComparator.INSTANCE;
91 }
92
93 public Object fromStringValue(String xml) {
94 return new Integer(xml);
95 }
96
97 }