Package systole.persistence.userTypes

Source Code of systole.persistence.userTypes.SegmentType

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package systole.persistence.userTypes;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.StringTokenizer;
import org.hibernate.Hibernate;

import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
import systole.domain.signals.Segment;

/**
*
* @author jmj
*/
public class SegmentType implements UserType {

    private static final int[] SQL_TYPES = {Types.CLOB};

    private String segmentToString(Segment segment) {
        String result = "";
        if (segment != null) {
            int i = 0;
            while (segment.size() > i) {
                result = result.concat(segment.get(i).toString() + "|");
                i++;
            }
        }
        return result;
    }

    private Segment stringToSegment(String points) {
        Segment segment = new Segment();
        if (points != null) {
            StringTokenizer parser = new StringTokenizer(points, "|", false);
            while (parser.hasMoreElements()) {
                segment.add(new BigDecimal(parser.nextToken()));
            }
        }
        return segment;
    }

    @Override
    public Object assemble(Serializable arg0, Object arg1)
            throws HibernateException {
        return null;
    }

    @Override
    public Object deepCopy(Object o) throws HibernateException {
        if (o == null) {
            return null;
        }
        Segment segment = (Segment) o;
        Segment clone = new Segment(segment.toDoubleArray());
        return clone;
    }

    @Override
    public Serializable disassemble(Object arg0) throws HibernateException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean equals(Object x, Object y) throws HibernateException {
        if (x == null || y == null) {
            return false;
        }
        return x.equals(y);
    }

    @Override
    public int hashCode(Object x) throws HibernateException {
        if (x != null) {
            return x.hashCode();
        }
        return 0;
    }

    @Override
    public boolean isMutable() {
        return false;
    }

    @Override
    public Object nullSafeGet(ResultSet rs, String[] strings, Object o)
            throws HibernateException, SQLException {
        String val = (String) Hibernate.TEXT.nullSafeGet(rs, strings[0]);
        return this.stringToSegment(val);
    }

    @Override
    public void nullSafeSet(PreparedStatement ps, Object o, int i)
            throws HibernateException, SQLException {
        String val = (String) this.segmentToString((Segment) o);
        ps.setString(i, val);
    }

    @Override
    public Object replace(Object arg0, Object arg1, Object arg2)
            throws HibernateException {
        return null;
    }

    @Override
    public Class returnedClass() {
        return Segment.class;
    }

    @Override
    public int[] sqlTypes() {
        return SegmentType.SQL_TYPES;
    }
}
TOP

Related Classes of systole.persistence.userTypes.SegmentType

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.