All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.internal.util.type.PrimitiveWrapperHelper Maven / Gradle / Ivy

There is a newer version: 7.0.0.Alpha1
Show newest version
/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * Copyright (c) 2013, Red Hat Inc. or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Red Hat Inc.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy