alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Hibernate example source code file (AbstractManagedType.java)

This example Hibernate source code file (AbstractManagedType.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Hibernate tags/keywords

class, class, collectionattribute, e, e, io, mapattribute, pluralattribute, pluralattribute, setattribute, singularattribute, string, suppresswarnings, util, x, x

The Hibernate AbstractManagedType.java source code

/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
 * third-party contributors as indicated by either @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.ejb.metamodel;
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<X> 
		extends AbstractType<X>
		implements ManagedType<X>, Serializable {

	private final  AbstractManagedType<? super X> superType;

	private final Map<String,Attribute declaredAttributes
			= new HashMap<String, Attribute();
	private final Map<String, SingularAttribute declaredSingularAttributes
			= new HashMap<String, SingularAttribute();
	private final Map<String, PluralAttribute declaredPluralAttributes
			= new HashMap<String, PluralAttribute();

	protected AbstractManagedType(Class<X> javaType, AbstractManagedType superType) {
		super( javaType );
		this.superType = superType;
	}

	protected AbstractManagedType<? super X> getSupertype() {
		return superType;
	}

	private boolean locked = false;

	public Builder<X> getBuilder() {
		if ( locked ) {
			throw new IllegalStateException( "Type has been locked" );
		}
		return new Builder<X>() {
			public void addAttribute(Attribute<X,?> attribute) {
				declaredAttributes.put( attribute.getName(), attribute );
				final Bindable.BindableType bindableType = ( ( Bindable ) attribute ).getBindableType();
				switch ( bindableType ) {
					case SINGULAR_ATTRIBUTE : {
						declaredSingularAttributes.put( attribute.getName(), (SingularAttribute<X,?>) attribute );
						break;
					}
					case PLURAL_ATTRIBUTE : {
						declaredPluralAttributes.put(attribute.getName(), (PluralAttribute<X,?,?>) attribute );
						break;
					}
					default : {
						throw new AssertionFailure( "unknown bindable type: " + bindableType );
					}
				}
			}
		};
	}

	public void lock() {
		locked = true;
	}

	public static interface Builder<X> {
		public void addAttribute(Attribute<X,?> attribute);
	}


	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public Set<Attribute getAttributes() {
		HashSet attributes = new HashSet<Attribute( declaredAttributes.values() );
		if ( getSupertype() != null ) {
			attributes.addAll( getSupertype().getAttributes() );
		}
		return attributes;
	}

	/**
	 * {@inheritDoc}
	 */
	public Set<Attribute getDeclaredAttributes() {
		return new HashSet<Attribute( declaredAttributes.values() );
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public Attribute<? super X, ?> getAttribute(String name) {
		Attribute<? super X, ?> attribute = declaredAttributes.get( name );
		if ( attribute == null && getSupertype() != null ) {
			attribute = getSupertype().getAttribute( name );
		}
		return attribute;
	}

	/**
	 * {@inheritDoc}
	 */
	public Attribute<X, ?> getDeclaredAttribute(String name) {
		final Attribute<X, ?> attr = declaredSingularAttributes.get( name );
		checkNotNull( "Attribute ", attr, name );
		return attr;
	}

	private void checkNotNull(String attributeType, Attribute<?,?> attribute, String name) {
		if ( attribute == null ) {
			throw new IllegalArgumentException( attributeType + " named " + name + " is not present" );
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public Set<SingularAttribute getSingularAttributes() {
		HashSet attributes = new HashSet<SingularAttribute( declaredSingularAttributes.values() );
		if ( getSupertype() != null ) {
			attributes.addAll( getSupertype().getSingularAttributes() );
		}
		return attributes;
	}

	/**
	 * {@inheritDoc}
	 */
	public Set<SingularAttribute getDeclaredSingularAttributes() {
		return new HashSet<SingularAttribute( declaredSingularAttributes.values() );
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public SingularAttribute<? super X, ?> getSingularAttribute(String name) {
		SingularAttribute<? super X, ?> attribute = declaredSingularAttributes.get( name );
		if ( attribute == null && getSupertype() != null ) {
			attribute = getSupertype().getSingularAttribute( name );
		}
		return attribute;
	}

	/**
	 * {@inheritDoc}
	 */
	public SingularAttribute<X, ?> getDeclaredSingularAttribute(String name) {
		final SingularAttribute<X, ?> attr = declaredSingularAttributes.get( name );
		checkNotNull( "SingularAttribute ", attr, name );
		return attr;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public <Y> SingularAttribute getSingularAttribute(String name, Class type) {
		SingularAttribute<? super X, ?> attribute = declaredSingularAttributes.get( name );
		if ( attribute == null && getSupertype() != null ) {
			attribute = getSupertype().getSingularAttribute( name );
		}
		checkTypeForSingleAttribute( "SingularAttribute ", attribute, name, type );
		return ( SingularAttribute<? super X, Y> ) attribute;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings( "unchecked")
	public <Y> SingularAttribute getDeclaredSingularAttribute(String name, Class javaType) {
		final SingularAttribute<X, ?> attr = declaredSingularAttributes.get( name );
		checkTypeForSingleAttribute( "SingularAttribute ", attr, name, javaType );
		return ( SingularAttribute<X, Y> ) attr;
	}

	private <Y> void checkTypeForSingleAttribute(
			String attributeType,
			SingularAttribute<?,?> attribute,
			String name,
			Class<Y> 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 <Y> 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;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public Set<PluralAttribute getPluralAttributes() {
		HashSet attributes = new HashSet<PluralAttribute( declaredPluralAttributes.values() );
		if ( getSupertype() != null ) {
			attributes.addAll( getSupertype().getPluralAttributes() );
		}
		return attributes;
	}

	/**
	 * {@inheritDoc}
	 */
	public Set<PluralAttribute getDeclaredPluralAttributes() {
		return new HashSet<PluralAttribute( declaredPluralAttributes.values() );
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public CollectionAttribute<? super X, ?> getCollection(String name) {
		PluralAttribute<? super X, ?, ?> attribute = getPluralAttribute( name );
		if ( attribute == null && getSupertype() != null ) {
			attribute = getSupertype().getPluralAttribute( name );
		}
		basicCollectionCheck( attribute, name );
		return ( CollectionAttribute<X, ?> ) attribute;
	}

	private PluralAttribute<? super X, ?, ?> getPluralAttribute(String name) {
		return declaredPluralAttributes.get( name );
	}

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

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings( "unchecked")
	public CollectionAttribute<X, ?> getDeclaredCollection(String name) {
		final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
		basicCollectionCheck( attribute, name );
		return ( CollectionAttribute<X, ?> ) attribute;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public SetAttribute<? super X, ?> getSet(String name) {
		PluralAttribute<? super X, ?, ?> attribute = getPluralAttribute( name );
		if ( attribute == null && getSupertype() != null ) {
			attribute = getSupertype().getPluralAttribute( name );
		}
		basicSetCheck( attribute, name );
		return (SetAttribute<? super X, ?>) attribute;
	}

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

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings( "unchecked")
	public SetAttribute<X, ?> getDeclaredSet(String name) {
		final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
		basicSetCheck( attribute, name );
		return ( SetAttribute<X, ?> ) attribute;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public ListAttribute<? super X, ?> getList(String name) {
		PluralAttribute<? super X, ?, ?> attribute = getPluralAttribute( name );
		if ( attribute == null && getSupertype() != null ) {
			attribute = getSupertype().getPluralAttribute( name );
		}
		basicListCheck( attribute, name );
		return (ListAttribute<? super X, ?>) attribute;
	}

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

	/**
	 * {@inheritDoc}
	 */
	public ListAttribute<X, ?> getDeclaredList(String name) {
		final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
		basicListCheck( attribute, name );
		return ( ListAttribute<X, ?> ) attribute;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public MapAttribute<? super X, ?, ?> getMap(String name) {
		PluralAttribute<? super X, ?, ?> attribute = getPluralAttribute( name );
		if ( attribute == null && getSupertype() != null ) {
			attribute = getSupertype().getPluralAttribute( name );
		}
		basicMapCheck( attribute, name );
		return (MapAttribute<? super X, ?, ?>) attribute;
	}

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

	/**
	 * {@inheritDoc}
	 */
	public MapAttribute<X, ?, ?> getDeclaredMap(String name) {
		final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
		basicMapCheck( attribute, name );
		return ( MapAttribute<X,?,?> ) attribute;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public <E> CollectionAttribute getCollection(String name, Class elementType) {
		PluralAttribute<? super X, ?, ?> attribute = declaredPluralAttributes.get( name );
		if ( attribute == null && getSupertype() != null ) {
			attribute = getSupertype().getPluralAttribute( name );
		}
		checkCollectionElementType( attribute, name, elementType );
		return ( CollectionAttribute<? super X, E> ) attribute;
	}

	/**
	 * {@inheritDoc}
	 */
	public <E> CollectionAttribute getDeclaredCollection(String name, Class elementType) {
		final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
		checkCollectionElementType( attribute, name, elementType );
		return ( CollectionAttribute<X, E> ) attribute;
	}

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

	private <E> void checkTypeForPluralAttributes(
			String attributeType,
			PluralAttribute<?,?,?> attribute,
			String name,
			Class<E> 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"
			);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public <E> SetAttribute getSet(String name, Class elementType) {
		PluralAttribute<? super X, ?, ?> attribute = declaredPluralAttributes.get( name );
		if ( attribute == null && getSupertype() != null ) {
			attribute = getSupertype().getPluralAttribute( name );
		}
		checkSetElementType( attribute, name, elementType );
		return ( SetAttribute<? super X, E> ) attribute;
	}

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

	/**
	 * {@inheritDoc}
	 */
	public <E> SetAttribute getDeclaredSet(String name, Class elementType) {
		final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
		checkSetElementType( attribute, name, elementType );
		return ( SetAttribute<X, E> ) attribute;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public <E> ListAttribute getList(String name, Class elementType) {
		PluralAttribute<? super X, ?, ?> attribute = declaredPluralAttributes.get( name );
		if ( attribute == null && getSupertype() != null ) {
			attribute = getSupertype().getPluralAttribute( name );
		}
		checkListElementType( attribute, name, elementType );
		return ( ListAttribute<? super X, E> ) attribute;
	}

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

	/**
	 * {@inheritDoc}
	 */
	public <E> ListAttribute getDeclaredList(String name, Class elementType) {
		final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
		checkListElementType( attribute, name, elementType );
		return ( ListAttribute<X, E> ) attribute;
	}

	@SuppressWarnings({ "unchecked" })
	public <K, V> MapAttribute getMap(String name, Class keyType, Class valueType) {
		PluralAttribute<? super X, ?, ?> attribute = getPluralAttribute( name );
		if ( attribute == null && getSupertype() != null ) {
			attribute = getSupertype().getPluralAttribute( name );
		}
		checkMapValueType( attribute, name, valueType );
		final MapAttribute<? super X, K, V> mapAttribute = ( MapAttribute ) attribute;
		checkMapKeyType( mapAttribute, name, keyType );
		return mapAttribute;
	}

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

	private <K,V> 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 );
		}
	}

	public <K, V> MapAttribute getDeclaredMap(String name, Class keyType, Class valueType) {
		final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
		checkMapValueType( attribute, name, valueType );
		final MapAttribute<X, K, V> mapAttribute = ( MapAttribute ) attribute;
		checkMapKeyType( mapAttribute, name, keyType );
		return mapAttribute;
	}
}

Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate AbstractManagedType.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.