001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.orm.hibernate;
016    
017    import java.io.Serializable;
018    
019    import java.sql.PreparedStatement;
020    import java.sql.ResultSet;
021    import java.sql.SQLException;
022    import java.sql.Types;
023    
024    import org.hibernate.Hibernate;
025    import org.hibernate.HibernateException;
026    import org.hibernate.usertype.UserType;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class FloatType implements Serializable, UserType {
032    
033            public static final float DEFAULT_VALUE = 0.0F;
034    
035            public static final int[] SQL_TYPES = new int[] {Types.FLOAT};
036    
037            public Object assemble(Serializable cached, Object owner) {
038                    return cached;
039            }
040    
041            public Object deepCopy(Object obj) {
042                    return obj;
043            }
044    
045            public Serializable disassemble(Object value) {
046                    return (Serializable)value;
047            }
048    
049            public boolean equals(Object x, Object y) {
050                    if (x == y) {
051                            return true;
052                    }
053                    else if (x == null || y == null) {
054                            return false;
055                    }
056                    else {
057                            return x.equals(y);
058                    }
059            }
060    
061            public int hashCode(Object x) {
062                    return x.hashCode();
063            }
064    
065            public boolean isMutable() {
066                    return false;
067            }
068    
069            public Object nullSafeGet(ResultSet rs, String[] names, Object obj)
070                    throws HibernateException, SQLException {
071    
072                    Float value = (Float)Hibernate.FLOAT.nullSafeGet(rs, names[0]);
073    
074                    if (value == null) {
075                            return new Float(DEFAULT_VALUE);
076                    }
077                    else {
078                            return value;
079                    }
080            }
081    
082            public void nullSafeSet(PreparedStatement ps, Object obj, int index)
083                    throws HibernateException, SQLException {
084    
085                    if (obj == null) {
086                            obj = new Float(DEFAULT_VALUE);
087                    }
088    
089                    Hibernate.FLOAT.nullSafeSet(ps, obj, index);
090            }
091    
092            public Object replace(Object original, Object target, Object owner) {
093                    return original;
094            }
095    
096            public Class<Float> returnedClass() {
097                    return Float.class;
098            }
099    
100            public int[] sqlTypes() {
101                    return SQL_TYPES;
102            }
103    
104    }