org.hibernate.type.descriptor.java.internal.DoubleJavaDescriptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-core Show documentation
Show all versions of hibernate-core Show documentation
Hibernate's core ORM functionality
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.type.descriptor.java.internal;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Types;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.descriptor.java.spi.AbstractNumericJavaDescriptor;
import org.hibernate.type.descriptor.java.spi.Primitive;
import org.hibernate.type.descriptor.spi.SqlTypeDescriptorIndicators;
import org.hibernate.type.descriptor.sql.spi.SqlTypeDescriptor;
/**
* Descriptor for {@link Double} handling.
*
* @author Steve Ebersole
*/
public class DoubleJavaDescriptor extends AbstractNumericJavaDescriptor implements Primitive {
public static final DoubleJavaDescriptor INSTANCE = new DoubleJavaDescriptor();
public static final Double ZERO = 0.0;
public DoubleJavaDescriptor() {
super( Double.class );
}
@Override
public SqlTypeDescriptor getJdbcRecommendedSqlType(SqlTypeDescriptorIndicators context) {
return context.getTypeConfiguration().getSqlTypeDescriptorRegistry().getDescriptor( Types.DOUBLE );
}
@Override
public String toString(Double value) {
return value == null ? null : value.toString();
}
@Override
public Double fromString(String string) {
return Double.valueOf( string );
}
@SuppressWarnings({ "unchecked" })
@Override
public X unwrap(Double value, Class type, SharedSessionContractImplementor session) {
if ( value == null ) {
return null;
}
if ( Double.class.isAssignableFrom( type ) ) {
return (X) value;
}
if ( Byte.class.isAssignableFrom( type ) ) {
return (X) Byte.valueOf( value.byteValue() );
}
if ( Short.class.isAssignableFrom( type ) ) {
return (X) Short.valueOf( value.shortValue() );
}
if ( Integer.class.isAssignableFrom( type ) ) {
return (X) Integer.valueOf( value.intValue() );
}
if ( Long.class.isAssignableFrom( type ) ) {
return (X) Long.valueOf( value.longValue() );
}
if ( Float.class.isAssignableFrom( type ) ) {
return (X) Float.valueOf( value.floatValue() );
}
if ( BigInteger.class.isAssignableFrom( type ) ) {
return (X) BigInteger.valueOf( value.longValue() );
}
if ( BigDecimal.class.isAssignableFrom( type ) ) {
return (X) BigDecimal.valueOf( value );
}
if ( String.class.isAssignableFrom( type ) ) {
return (X) value.toString();
}
throw unknownUnwrap( type );
}
@Override
public Double wrap(X value, SharedSessionContractImplementor session) {
if ( value == null ) {
return null;
}
if ( Double.class.isInstance( value ) ) {
return (Double) value;
}
if ( Number.class.isInstance( value ) ) {
return ( (Number) value ).doubleValue();
}
else if ( String.class.isInstance( value ) ) {
return Double.valueOf( ( (String) value ) );
}
throw unknownWrap( value.getClass() );
}
@Override
public Class getPrimitiveClass() {
return double.class;
}
@Override
public Double getDefaultValue() {
return ZERO;
}
}