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

org.hibernate.metamodel.model.domain.internal.AbstractManagedType Maven / Gradle / Ivy

There is a newer version: 7.0.0.Alpha1
Show newest version
/*
 * 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 http://www.gnu.org/licenses/lgpl-2.1.html
 */
package org.hibernate.metamodel.model.domain.internal;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.Bindable;
import javax.persistence.metamodel.CollectionAttribute;
import javax.persistence.metamodel.ListAttribute;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.MapAttribute;
import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.SetAttribute;
import javax.persistence.metamodel.SingularAttribute;

import org.hibernate.annotations.common.AssertionFailure;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.graph.internal.SubGraphImpl;
import org.hibernate.graph.spi.SubGraphImplementor;
import org.hibernate.metamodel.model.domain.spi.PersistentAttributeDescriptor;
import org.hibernate.metamodel.model.domain.spi.BagPersistentAttribute;
import org.hibernate.metamodel.model.domain.spi.DomainModelHelper;
import org.hibernate.metamodel.model.domain.spi.ListPersistentAttribute;
import org.hibernate.metamodel.model.domain.spi.ManagedTypeDescriptor;
import org.hibernate.metamodel.model.domain.spi.MapPersistentAttribute;
import org.hibernate.metamodel.model.domain.spi.PluralPersistentAttribute;
import org.hibernate.metamodel.model.domain.spi.SetPersistentAttribute;
import org.hibernate.metamodel.model.domain.spi.SingularPersistentAttribute;

/**
 * Defines commonality for the JPA {@link ManagedType} hierarchy of interfaces.
 *
 * @author Steve Ebersole
 */
public abstract class AbstractManagedType
		extends AbstractType
		implements ManagedTypeDescriptor, Serializable {

	private final SessionFactoryImplementor sessionFactory;

	private final ManagedTypeDescriptor superType;

	private final Map> declaredAttributes = new HashMap<>();
	private final Map> declaredSingularAttributes = new HashMap<>();
	private final Map> declaredPluralAttributes = new HashMap<>();

	private transient InFlightAccess inFlightAccess;

	protected AbstractManagedType(
			Class javaType,
			String typeName,
			ManagedTypeDescriptor superType,
			SessionFactoryImplementor sessionFactory) {
		super( javaType, typeName );
		this.superType = superType;
		this.sessionFactory = sessionFactory;

		this.inFlightAccess = createInFlightAccess();
	}

	protected InFlightAccess createInFlightAccess() {
		return new InFlightAccessImpl();
	}

	@Override
	public String getName() {
		return getTypeName();
	}

	@Override
	public ManagedTypeDescriptor getSuperType() {
		return superType;
	}

	protected SessionFactoryImplementor sessionFactory() {
		return sessionFactory;
	}

	@Override
	public InFlightAccess getInFlightAccess() {
		if ( inFlightAccess == null ) {
			throw new IllegalStateException( "Type has been locked" );
		}

		return inFlightAccess;
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public Set> getAttributes() {
		HashSet attributes = new HashSet>( declaredAttributes.values() );
		if ( getSuperType() != null ) {
			attributes.addAll( getSuperType().getAttributes() );
		}
		return attributes;
	}

	@Override
	public Set> getDeclaredAttributes() {
		return new HashSet<>( declaredAttributes.values() );
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public PersistentAttributeDescriptor getAttribute(String name) {
		PersistentAttributeDescriptor attribute = declaredAttributes.get( name );
		if ( attribute == null && getSuperType() != null ) {
			attribute = getSuperType().getAttribute( name );
		}
		checkNotNull( "Attribute ", attribute, name );
		return attribute;
	}

	@Override
	public PersistentAttributeDescriptor findDeclaredAttribute(String name) {
		return declaredAttributes.get( name );
	}

	@Override
	public PersistentAttributeDescriptor findAttribute(String name) {
		PersistentAttributeDescriptor attribute = findDeclaredAttribute( name );
		if ( attribute == null && getSuperType() != null ) {
			attribute = getSuperType().findAttribute( name );
		}
		return attribute;
	}

	@Override
	public PersistentAttributeDescriptor getDeclaredAttribute(String name) {
		PersistentAttributeDescriptor attr = declaredAttributes.get( name );
		checkNotNull( "Attribute ", attr, name );
		return attr;
	}

	private void checkNotNull(String attributeType, Attribute attribute, String name) {

		if ( attribute == null ) {
			throw new IllegalArgumentException(
					String.format(
							"Unable to locate %s with the the given name [%s] on this ManagedType [%s]",
							attributeType,
							name,
							getTypeName()
					)
			);
		}
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public Set> getSingularAttributes() {
		HashSet attributes = new HashSet>( declaredSingularAttributes.values() );
		if ( getSuperType() != null ) {
			attributes.addAll( getSuperType().getSingularAttributes() );
		}
		return attributes;
	}

	@Override
	public Set> getDeclaredSingularAttributes() {
		return new HashSet>( declaredSingularAttributes.values() );
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public SingularAttribute getSingularAttribute(String name) {
		SingularAttribute attribute = declaredSingularAttributes.get( name );
		if ( attribute == null && getSuperType() != null ) {
			attribute = getSuperType().getSingularAttribute( name );
		}
		checkNotNull( "SingularAttribute ", attribute, name );
		return attribute;
	}

	@Override
	public SingularAttribute getDeclaredSingularAttribute(String name) {
		final SingularAttribute attr = declaredSingularAttributes.get( name );
		checkNotNull( "SingularAttribute ", attr, name );
		return attr;
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public  SingularPersistentAttribute getSingularAttribute(String name, Class type) {
		SingularAttribute attribute = declaredSingularAttributes.get( name );
		if ( attribute == null && getSuperType() != null ) {
			attribute = getSuperType().getSingularAttribute( name );
		}
		checkTypeForSingleAttribute( "SingularAttribute ", attribute, name, type );
		return (SingularPersistentAttribute) attribute;
	}

	@Override
	@SuppressWarnings( "unchecked")
	public  SingularPersistentAttribute getDeclaredSingularAttribute(String name, Class javaType) {
		final SingularAttribute attr = declaredSingularAttributes.get( name );
		checkTypeForSingleAttribute( "SingularAttribute ", attr, name, javaType );
		return (SingularPersistentAttribute) attr;
	}

	private  void checkTypeForSingleAttribute(
			String attributeType,
			SingularAttribute attribute,
			String name,
			Class javaType) {
		if ( attribute == null || ( javaType != null && !attribute.getBindableJavaType().equals( javaType ) ) ) {
			if ( isPrimitiveVariant( attribute, javaType ) ) {
				return;
			}
			throw new IllegalArgumentException(
					attributeType + " named " + name
					+ ( javaType != null ? " and of type " + javaType.getName() : "" )
					+ " is not present"
			);
		}
	}

	@SuppressWarnings({ "SimplifiableIfStatement" })
	protected  boolean isPrimitiveVariant(SingularAttribute attribute, Class javaType) {
		if ( attribute == null ) {
			return false;
		}
		Class declaredType = attribute.getBindableJavaType();

		if ( declaredType.isPrimitive() ) {
			return ( Boolean.class.equals( javaType ) && Boolean.TYPE.equals( declaredType ) )
					|| ( Character.class.equals( javaType ) && Character.TYPE.equals( declaredType ) )
					|| ( Byte.class.equals( javaType ) && Byte.TYPE.equals( declaredType ) )
					|| ( Short.class.equals( javaType ) && Short.TYPE.equals( declaredType ) )
					|| ( Integer.class.equals( javaType ) && Integer.TYPE.equals( declaredType ) )
					|| ( Long.class.equals( javaType ) && Long.TYPE.equals( declaredType ) )
					|| ( Float.class.equals( javaType ) && Float.TYPE.equals( declaredType ) )
					|| ( Double.class.equals( javaType ) && Double.TYPE.equals( declaredType ) );
		}

		if ( javaType.isPrimitive() ) {
			return ( Boolean.class.equals( declaredType ) && Boolean.TYPE.equals( javaType ) )
					|| ( Character.class.equals( declaredType ) && Character.TYPE.equals( javaType ) )
					|| ( Byte.class.equals( declaredType ) && Byte.TYPE.equals( javaType ) )
					|| ( Short.class.equals( declaredType ) && Short.TYPE.equals( javaType ) )
					|| ( Integer.class.equals( declaredType ) && Integer.TYPE.equals( javaType ) )
					|| ( Long.class.equals( declaredType ) && Long.TYPE.equals( javaType ) )
					|| ( Float.class.equals( declaredType ) && Float.TYPE.equals( javaType ) )
					|| ( Double.class.equals( declaredType ) && Double.TYPE.equals( javaType ) );
		}

		return false;
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public Set> getPluralAttributes() {
		HashSet attributes = new HashSet>( declaredPluralAttributes.values() );
		if ( getSuperType() != null ) {
			attributes.addAll( getSuperType().getPluralAttributes() );
		}
		return attributes;
	}

	@Override
	public Set> getDeclaredPluralAttributes() {
		return new HashSet>( declaredPluralAttributes.values() );
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public CollectionAttribute getCollection(String name) {
		PluralPersistentAttribute attribute = getPluralAttribute( name );
		if ( attribute == null && getSuperType() != null ) {
			attribute = getSuperType().getPluralAttribute( name );
		}
		basicCollectionCheck( attribute, name );
		return ( CollectionAttribute ) attribute;
	}

	@Override
	@SuppressWarnings("unchecked")
	public PluralPersistentAttribute getPluralAttribute(String name) {
		return declaredPluralAttributes.get( name );
	}

	private void basicCollectionCheck(PluralAttribute attribute, String name) {
		checkNotNull( "CollectionAttribute", attribute, name );
		if ( ! CollectionAttribute.class.isAssignableFrom( attribute.getClass() ) ) {
			throw new IllegalArgumentException( name + " is not a CollectionAttribute: " + attribute.getClass() );
		}
	}

	@Override
	@SuppressWarnings( "unchecked")
	public CollectionAttribute getDeclaredCollection(String name) {
		final PluralAttribute attribute = declaredPluralAttributes.get( name );
		basicCollectionCheck( attribute, name );
		return ( CollectionAttribute ) attribute;
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public SetPersistentAttribute getSet(String name) {
		PluralAttribute attribute = getPluralAttribute( name );
		if ( attribute == null && getSuperType() != null ) {
			attribute = getSuperType().getPluralAttribute( name );
		}
		basicSetCheck( attribute, name );
		return (SetPersistentAttribute) attribute;
	}

	private void basicSetCheck(PluralAttribute attribute, String name) {
		checkNotNull( "SetAttribute", attribute, name );
		if ( ! SetAttribute.class.isAssignableFrom( attribute.getClass() ) ) {
			throw new IllegalArgumentException( name + " is not a SetAttribute: " + attribute.getClass() );
		}
	}

	@Override
	@SuppressWarnings( "unchecked")
	public SetPersistentAttribute getDeclaredSet(String name) {
		final PluralAttribute attribute = declaredPluralAttributes.get( name );
		basicSetCheck( attribute, name );
		return (SetPersistentAttribute) attribute;
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public ListPersistentAttribute getList(String name) {
		PluralAttribute attribute = getPluralAttribute( name );
		if ( attribute == null && getSuperType() != null ) {
			attribute = getSuperType().getPluralAttribute( name );
		}
		basicListCheck( attribute, name );
		return (ListPersistentAttribute) attribute;
	}

	private void basicListCheck(PluralAttribute attribute, String name) {
		checkNotNull( "ListAttribute", attribute, name );
		if ( ! ListAttribute.class.isAssignableFrom( attribute.getClass() ) ) {
			throw new IllegalArgumentException( name + " is not a ListAttribute: " + attribute.getClass() );
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public ListPersistentAttribute getDeclaredList(String name) {
		final PluralAttribute attribute = declaredPluralAttributes.get( name );
		basicListCheck( attribute, name );
		return (ListPersistentAttribute) attribute;
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public MapPersistentAttribute getMap(String name) {
		PluralAttribute attribute = getPluralAttribute( name );
		if ( attribute == null && getSuperType() != null ) {
			attribute = getSuperType().getPluralAttribute( name );
		}
		basicMapCheck( attribute, name );
		return (MapPersistentAttribute) attribute;
	}

	private void basicMapCheck(PluralAttribute attribute, String name) {
		checkNotNull( "MapAttribute", attribute, name );
		if ( ! MapAttribute.class.isAssignableFrom( attribute.getClass() ) ) {
			throw new IllegalArgumentException( name + " is not a MapAttribute: " + attribute.getClass() );
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public MapPersistentAttribute getDeclaredMap(String name) {
		final PluralAttribute attribute = declaredPluralAttributes.get( name );
		basicMapCheck( attribute, name );
		return (MapPersistentAttribute) attribute;
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public  BagPersistentAttribute getCollection(String name, Class elementType) {
		PluralAttribute attribute = declaredPluralAttributes.get( name );
		if ( attribute == null && getSuperType() != null ) {
			attribute = getSuperType().getPluralAttribute( name );
		}
		checkCollectionElementType( attribute, name, elementType );
		return (BagPersistentAttribute) attribute;
	}

	@Override
	@SuppressWarnings("unchecked")
	public  CollectionAttribute getDeclaredCollection(String name, Class elementType) {
		final PluralAttribute attribute = declaredPluralAttributes.get( name );
		checkCollectionElementType( attribute, name, elementType );
		return (CollectionAttribute) attribute;
	}

	private  void checkCollectionElementType(PluralAttribute attribute, String name, Class elementType) {
		checkTypeForPluralAttributes( "CollectionAttribute", attribute, name, elementType, PluralAttribute.CollectionType.COLLECTION );
	}

	private  void checkTypeForPluralAttributes(
			String attributeType,
			PluralAttribute attribute,
			String name,
			Class elementType,
			PluralAttribute.CollectionType collectionType) {
		if ( attribute == null
				|| ( elementType != null && !attribute.getBindableJavaType().equals( elementType ) )
				|| attribute.getCollectionType() != collectionType ) {
			throw new IllegalArgumentException(
					attributeType + " named " + name
					+ ( elementType != null ? " and of element type " + elementType : "" )
					+ " is not present"
			);
		}
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public  SetAttribute getSet(String name, Class elementType) {
		PluralAttribute attribute = declaredPluralAttributes.get( name );
		if ( attribute == null && getSuperType() != null ) {
			attribute = getSuperType().getPluralAttribute( name );
		}
		checkSetElementType( attribute, name, elementType );
		return ( SetAttribute ) attribute;
	}

	private  void checkSetElementType(PluralAttribute attribute, String name, Class elementType) {
		checkTypeForPluralAttributes( "SetAttribute", attribute, name, elementType, PluralAttribute.CollectionType.SET );
	}

	@Override
	@SuppressWarnings("unchecked")
	public  SetAttribute getDeclaredSet(String name, Class elementType) {
		final PluralAttribute attribute = declaredPluralAttributes.get( name );
		checkSetElementType( attribute, name, elementType );
		return ( SetAttribute ) attribute;
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public  ListAttribute getList(String name, Class elementType) {
		PluralAttribute attribute = declaredPluralAttributes.get( name );
		if ( attribute == null && getSuperType() != null ) {
			attribute = getSuperType().getPluralAttribute( name );
		}
		checkListElementType( attribute, name, elementType );
		return ( ListAttribute ) attribute;
	}

	private  void checkListElementType(PluralAttribute attribute, String name, Class elementType) {
		checkTypeForPluralAttributes( "ListAttribute", attribute, name, elementType, PluralAttribute.CollectionType.LIST );
	}

	@Override
	@SuppressWarnings("unchecked")
	public  ListAttribute getDeclaredList(String name, Class elementType) {
		final PluralAttribute attribute = declaredPluralAttributes.get( name );
		checkListElementType( attribute, name, elementType );
		return ( ListAttribute ) attribute;
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public  MapAttribute getMap(String name, Class keyType, Class valueType) {
		PluralAttribute attribute = getPluralAttribute( name );
		if ( attribute == null && getSuperType() != null ) {
			attribute = getSuperType().getPluralAttribute( name );
		}
		checkMapValueType( attribute, name, valueType );
		final MapAttribute mapAttribute = ( MapAttribute ) attribute;
		checkMapKeyType( mapAttribute, name, keyType );
		return mapAttribute;
	}

	private  void checkMapValueType(PluralAttribute attribute, String name, Class valueType) {
		checkTypeForPluralAttributes( "MapAttribute", attribute, name, valueType, PluralAttribute.CollectionType.MAP);
	}

	private  void checkMapKeyType(MapAttribute mapAttribute, String name, Class keyType) {
		if ( mapAttribute.getKeyJavaType() != keyType ) {
			throw new IllegalArgumentException( "MapAttribute named " + name + " does not support a key of type " + keyType );
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public  MapAttribute getDeclaredMap(String name, Class keyType, Class valueType) {
		final PluralAttribute attribute = declaredPluralAttributes.get( name );
		checkMapValueType( attribute, name, valueType );
		final MapAttribute mapAttribute = ( MapAttribute ) attribute;
		checkMapKeyType( mapAttribute, name, keyType );
		return mapAttribute;
	}


	@Override
	public SubGraphImplementor makeSubGraph() {
		return new SubGraphImpl<>( this, true, sessionFactory );
	}

	@Override
	public  ManagedTypeDescriptor findSubType(String subTypeName) {
		return DomainModelHelper.resolveSubType( this, subTypeName, sessionFactory() );
	}

	@Override
	public  ManagedTypeDescriptor findSubType(Class subType) {
		return DomainModelHelper.resolveSubType( this, subType, sessionFactory() );
	}

	protected class InFlightAccessImpl implements InFlightAccess {
		@Override
		@SuppressWarnings("unchecked")
		public void addAttribute(PersistentAttributeDescriptor attribute) {
			// put it in the collective group of attributes
			declaredAttributes.put( attribute.getName(), attribute );

			// additionally classify as singular or plural attribute
			final Bindable.BindableType bindableType = ( ( Bindable ) attribute ).getBindableType();
			switch ( bindableType ) {
				case SINGULAR_ATTRIBUTE : {
					declaredSingularAttributes.put( attribute.getName(), (SingularPersistentAttribute) attribute );
					break;
				}
				case PLURAL_ATTRIBUTE : {
					declaredPluralAttributes.put(attribute.getName(), (PluralPersistentAttribute) attribute );
					break;
				}
				default : {
					throw new AssertionFailure( "unknown bindable type: " + bindableType );
				}
			}
		}

		@Override
		public void finishUp() {
			inFlightAccess = null;
		}
	}
}