001    /*
002     *  Copyright 2001-2011 Stephen Colebourne
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.joda.time.contrib.hibernate;
017    
018    import java.io.Serializable;
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.HibernateException;
025    import org.hibernate.type.StandardBasicTypes;
026    import org.hibernate.usertype.UserType;
027    
028    /**
029     * @author gjoseph
030     */
031    public abstract class AbstractStringBasedJodaType implements UserType, Serializable {
032    
033        private static final int[] SQL_TYPES = new int[] { Types.VARCHAR };
034    
035        public int[] sqlTypes() {
036            return SQL_TYPES;
037        }
038    
039        public Object nullSafeGet(ResultSet resultSet, String[] strings, Object object) throws HibernateException, SQLException {
040            String s = (String) StandardBasicTypes.STRING.nullSafeGet(resultSet, strings[0]);
041            if (s == null) {
042                return null;
043            }
044    
045            return fromNonNullString(s);
046        }
047    
048        protected abstract Object fromNonNullString(String s);
049    
050        public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
051            if (value == null) {
052                StandardBasicTypes.STRING.nullSafeSet(preparedStatement, null, index);
053            } else {
054                StandardBasicTypes.STRING.nullSafeSet(preparedStatement, toNonNullString(value), index);
055            }
056        }
057    
058        protected abstract String toNonNullString(Object value);
059    
060        public boolean equals(Object x, Object y) throws HibernateException {
061            if (x == y) {
062                return true;
063            }
064            if (x == null || y == null) {
065                return false;
066            }
067            return x.equals(y);
068        }
069    
070        public int hashCode(Object object) throws HibernateException {
071            return object.hashCode();
072        }
073    
074        public Object deepCopy(Object value) throws HibernateException {
075            return value;
076        }
077    
078        public boolean isMutable() {
079            return false;
080        }
081    
082        public Serializable disassemble(Object value) throws HibernateException {
083            return (Serializable) value;
084        }
085    
086        public Object assemble(Serializable cached, Object value) throws HibernateException {
087            return cached;
088        }
089    
090        public Object replace(Object original, Object target, Object owner) throws HibernateException {
091            return original;
092        }
093    
094    }