org.hibernate.internal.util.type.PrimitiveWrapperHelper 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.internal.util.type;
/**
* Helper for primitive/wrapper utilities.
*
* @author Steve Ebersole
*/
public final class PrimitiveWrapperHelper {
private PrimitiveWrapperHelper() {
}
/**
* Describes a particular primitive/wrapper combo
*/
public static interface PrimitiveWrapperDescriptor {
public Class getPrimitiveClass();
public Class getWrapperClass();
}
public static class BooleanDescriptor implements PrimitiveWrapperDescriptor {
public static final BooleanDescriptor INSTANCE = new BooleanDescriptor();
private BooleanDescriptor() {
}
@Override
public Class getPrimitiveClass() {
return boolean.class;
}
@Override
public Class getWrapperClass() {
return Boolean.class;
}
}
public static class CharacterDescriptor implements PrimitiveWrapperDescriptor {
public static final CharacterDescriptor INSTANCE = new CharacterDescriptor();
private CharacterDescriptor() {
}
@Override
public Class getPrimitiveClass() {
return char.class;
}
@Override
public Class getWrapperClass() {
return Character.class;
}
}
public static class ByteDescriptor implements PrimitiveWrapperDescriptor {
public static final ByteDescriptor INSTANCE = new ByteDescriptor();
private ByteDescriptor() {
}
@Override
public Class getPrimitiveClass() {
return byte.class;
}
@Override
public Class getWrapperClass() {
return Byte.class;
}
}
public static class ShortDescriptor implements PrimitiveWrapperDescriptor {
public static final ShortDescriptor INSTANCE = new ShortDescriptor();
private ShortDescriptor() {
}
@Override
public Class getPrimitiveClass() {
return short.class;
}
@Override
public Class getWrapperClass() {
return Short.class;
}
}
public static class IntegerDescriptor implements PrimitiveWrapperDescriptor {
public static final IntegerDescriptor INSTANCE = new IntegerDescriptor();
private IntegerDescriptor() {
}
@Override
public Class getPrimitiveClass() {
return int.class;
}
@Override
public Class getWrapperClass() {
return Integer.class;
}
}
public static class LongDescriptor implements PrimitiveWrapperDescriptor {
public static final LongDescriptor INSTANCE = new LongDescriptor();
private LongDescriptor() {
}
@Override
public Class getPrimitiveClass() {
return long.class;
}
@Override
public Class getWrapperClass() {
return Long.class;
}
}
public static class FloatDescriptor implements PrimitiveWrapperDescriptor {
public static final FloatDescriptor INSTANCE = new FloatDescriptor();
private FloatDescriptor() {
}
@Override
public Class getPrimitiveClass() {
return float.class;
}
@Override
public Class getWrapperClass() {
return Float.class;
}
}
public static class DoubleDescriptor implements PrimitiveWrapperDescriptor {
public static final DoubleDescriptor INSTANCE = new DoubleDescriptor();
private DoubleDescriptor() {
}
@Override
public Class getPrimitiveClass() {
return double.class;
}
@Override
public Class getWrapperClass() {
return Double.class;
}
}
@SuppressWarnings("unchecked")
public static PrimitiveWrapperDescriptor getDescriptorByPrimitiveType(Class primitiveClazz) {
if ( ! primitiveClazz.isPrimitive() ) {
throw new IllegalArgumentException( "Given class is not a primitive type : " + primitiveClazz.getName() );
}
if ( boolean.class == primitiveClazz ) {
return (PrimitiveWrapperDescriptor) BooleanDescriptor.INSTANCE;
}
if ( char.class == primitiveClazz ) {
return (PrimitiveWrapperDescriptor) CharacterDescriptor.INSTANCE;
}
if ( byte.class == primitiveClazz ) {
return (PrimitiveWrapperDescriptor) ByteDescriptor.INSTANCE;
}
if ( short.class == primitiveClazz ) {
return (PrimitiveWrapperDescriptor) ShortDescriptor.INSTANCE;
}
if ( int.class == primitiveClazz ) {
return (PrimitiveWrapperDescriptor) IntegerDescriptor.INSTANCE;
}
if ( long.class == primitiveClazz ) {
return (PrimitiveWrapperDescriptor) LongDescriptor.INSTANCE;
}
if ( float.class == primitiveClazz ) {
return (PrimitiveWrapperDescriptor) FloatDescriptor.INSTANCE;
}
if ( double.class == primitiveClazz ) {
return (PrimitiveWrapperDescriptor) DoubleDescriptor.INSTANCE;
}
if ( void.class == primitiveClazz ) {
throw new IllegalArgumentException( "void, as primitive type, has no wrapper equivalent" );
}
throw new IllegalArgumentException( "Unrecognized primitive type class : " + primitiveClazz.getName() );
}
@SuppressWarnings("unchecked")
public static PrimitiveWrapperDescriptor getDescriptorByWrapperType(Class wrapperClass) {
if ( wrapperClass.isPrimitive() ) {
throw new IllegalArgumentException( "Given class is a primitive type : " + wrapperClass.getName() );
}
if ( Boolean.class.equals( wrapperClass ) ) {
return (PrimitiveWrapperDescriptor) BooleanDescriptor.INSTANCE;
}
if ( Character.class == wrapperClass ) {
return (PrimitiveWrapperDescriptor) CharacterDescriptor.INSTANCE;
}
if ( Byte.class == wrapperClass ) {
return (PrimitiveWrapperDescriptor) ByteDescriptor.INSTANCE;
}
if ( Short.class == wrapperClass ) {
return (PrimitiveWrapperDescriptor) ShortDescriptor.INSTANCE;
}
if ( Integer.class == wrapperClass ) {
return (PrimitiveWrapperDescriptor) IntegerDescriptor.INSTANCE;
}
if ( Long.class == wrapperClass ) {
return (PrimitiveWrapperDescriptor) LongDescriptor.INSTANCE;
}
if ( Float.class == wrapperClass ) {
return (PrimitiveWrapperDescriptor) FloatDescriptor.INSTANCE;
}
if ( Double.class == wrapperClass ) {
return (PrimitiveWrapperDescriptor) DoubleDescriptor.INSTANCE;
}
// most likely void.class, which we can't really handle here
throw new IllegalArgumentException( "Unrecognized wrapper type class : " + wrapperClass.getName() );
}
public static boolean isWrapper(Class clazz) {
try {
getDescriptorByWrapperType( clazz );
return true;
}
catch (Exception e) {
return false;
}
}
public static boolean arePrimitiveWrapperEquivalents(Class converterDefinedType, Class propertyType) {
if ( converterDefinedType.isPrimitive() ) {
return getDescriptorByPrimitiveType( converterDefinedType ).getWrapperClass().equals( propertyType );
}
else if ( propertyType.isPrimitive() ) {
return getDescriptorByPrimitiveType( propertyType ).getWrapperClass().equals( converterDefinedType );
}
return false;
}
}