001    /**
002     * Copyright (c) 2000-2012 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    
023    import org.hibernate.engine.SessionImplementor;
024    import org.hibernate.type.StandardBasicTypes;
025    import org.hibernate.type.Type;
026    import org.hibernate.usertype.CompositeUserType;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class FloatType implements CompositeUserType, Serializable {
032    
033            public static final Float DEFAULT_VALUE = Float.valueOf(0);
034    
035            public Object assemble(
036                    Serializable cached, SessionImplementor session, Object owner) {
037    
038                    return cached;
039            }
040    
041            public Object deepCopy(Object obj) {
042                    return obj;
043            }
044    
045            public Serializable disassemble(Object value, SessionImplementor session) {
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 String[] getPropertyNames() {
062                    return new String[0];
063            }
064    
065            public Type[] getPropertyTypes() {
066                    return new Type[] {StandardBasicTypes.FLOAT};
067            }
068    
069            public Object getPropertyValue(Object component, int property) {
070                    return component;
071            }
072    
073            public int hashCode(Object x) {
074                    return x.hashCode();
075            }
076    
077            public boolean isMutable() {
078                    return false;
079            }
080    
081            public Object nullSafeGet(
082                            ResultSet rs, String[] names, SessionImplementor session,
083                            Object owner)
084                    throws SQLException {
085    
086                    Float value = StandardBasicTypes.FLOAT.nullSafeGet(
087                            rs, names[0], session);
088    
089                    if (value == null) {
090                            return DEFAULT_VALUE;
091                    }
092                    else {
093                            return value;
094                    }
095            }
096    
097            public void nullSafeSet(
098                            PreparedStatement ps, Object target, int index,
099                            SessionImplementor session)
100                    throws SQLException {
101    
102                    if (target == null) {
103                            target = DEFAULT_VALUE;
104                    }
105    
106                    ps.setFloat(index, (Float)target);
107            }
108    
109            public Object replace(
110                    Object original, Object target, SessionImplementor session,
111                    Object owner) {
112    
113                    return original;
114            }
115    
116            public Class<Float> returnedClass() {
117                    return Float.class;
118            }
119    
120            public void setPropertyValue(Object component, int property, Object value) {
121            }
122    
123    }