org.hibernate.metamodel.internal.AbstractManagedType 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.metamodel.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;
/**
* Defines commonality for the JPA {@link ManagedType} hierarchy of interfaces.
*
* @author Steve Ebersole
*/
public abstract class AbstractManagedType
extends AbstractType
implements ManagedType, Serializable {
private final AbstractManagedType superType;
private final Map> declaredAttributes
= new HashMap>();
private final Map> declaredSingularAttributes
= new HashMap>();
private final Map> declaredPluralAttributes
= new HashMap>();
protected AbstractManagedType(Class javaType, String typeName, AbstractManagedType superType) {
super( javaType, typeName );
this.superType = superType;
}
protected AbstractManagedType getSupertype() {
return superType;
}
private boolean locked;
public Builder getBuilder() {
if ( locked ) {
throw new IllegalStateException( "Type has been locked" );
}
return new Builder() {
@Override
@SuppressWarnings("unchecked")
public void addAttribute(Attribute attribute) {
declaredAttributes.put( attribute.getName(), attribute );
final Bindable.BindableType bindableType = ( ( Bindable ) attribute ).getBindableType();
switch ( bindableType ) {
case SINGULAR_ATTRIBUTE : {
declaredSingularAttributes.put( attribute.getName(), (SingularAttribute) attribute );
break;
}
case PLURAL_ATTRIBUTE : {
declaredPluralAttributes.put(attribute.getName(), (PluralAttribute) attribute );
break;
}
default : {
throw new AssertionFailure( "unknown bindable type: " + bindableType );
}
}
}
};
}
public void lock() {
locked = true;
}
public static interface Builder {
public void addAttribute(Attribute attribute);
}
@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 Attribute getAttribute(String name) {
Attribute attribute = declaredAttributes.get( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getAttribute( name );
}
checkNotNull( "Attribute ", attribute, name );
return attribute;
}
@Override
public Attribute getDeclaredAttribute(String name) {
Attribute 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 SingularAttribute 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 ( SingularAttribute ) attribute;
}
@Override
@SuppressWarnings( "unchecked")
public SingularAttribute getDeclaredSingularAttribute(String name, Class javaType) {
final SingularAttribute attr = declaredSingularAttributes.get( name );
checkTypeForSingleAttribute( "SingularAttribute ", attr, name, javaType );
return ( SingularAttribute ) 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) {
PluralAttribute attribute = getPluralAttribute( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getPluralAttribute( name );
}
basicCollectionCheck( attribute, name );
return ( CollectionAttribute ) attribute;
}
private PluralAttribute 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 SetAttribute getSet(String name) {
PluralAttribute attribute = getPluralAttribute( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getPluralAttribute( name );
}
basicSetCheck( attribute, name );
return (SetAttribute) 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 SetAttribute getDeclaredSet(String name) {
final PluralAttribute attribute = declaredPluralAttributes.get( name );
basicSetCheck( attribute, name );
return ( SetAttribute ) attribute;
}
@Override
@SuppressWarnings({ "unchecked" })
public ListAttribute getList(String name) {
PluralAttribute attribute = getPluralAttribute( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getPluralAttribute( name );
}
basicListCheck( attribute, name );
return (ListAttribute) 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 ListAttribute getDeclaredList(String name) {
final PluralAttribute attribute = declaredPluralAttributes.get( name );
basicListCheck( attribute, name );
return ( ListAttribute ) attribute;
}
@Override
@SuppressWarnings({ "unchecked" })
public MapAttribute getMap(String name) {
PluralAttribute attribute = getPluralAttribute( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getPluralAttribute( name );
}
basicMapCheck( attribute, name );
return (MapAttribute) 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 MapAttribute getDeclaredMap(String name) {
final PluralAttribute attribute = declaredPluralAttributes.get( name );
basicMapCheck( attribute, name );
return ( MapAttribute ) attribute;
}
@Override
@SuppressWarnings({ "unchecked" })
public CollectionAttribute getCollection(String name, Class elementType) {
PluralAttribute attribute = declaredPluralAttributes.get( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getPluralAttribute( name );
}
checkCollectionElementType( attribute, name, elementType );
return ( CollectionAttribute ) 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;
}
}