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

org.hibernate.metamodel.model.domain.internal.SingularAttributeImpl 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.lang.reflect.Member;
import java.util.function.Supplier;

import org.hibernate.graph.spi.GraphHelper;
import org.hibernate.metamodel.model.domain.spi.ManagedTypeDescriptor;
import org.hibernate.metamodel.model.domain.spi.SimpleTypeDescriptor;
import org.hibernate.metamodel.model.domain.spi.SingularPersistentAttribute;

/**
 * @author Emmanuel Bernard
 * @author Steve Ebersole
 */
public class SingularAttributeImpl
		extends AbstractAttribute
		implements SingularPersistentAttribute, Serializable {
	private final boolean isIdentifier;
	private final boolean isVersion;
	private final boolean isOptional;

	private final SimpleTypeDescriptor attributeType;

	// NOTE : delay access for timing reasons
	private final DelayedKeyTypeAccess graphKeyTypeAccess = new DelayedKeyTypeAccess();

	public SingularAttributeImpl(
			ManagedTypeDescriptor declaringType,
			String name,
			PersistentAttributeType attributeNature,
			SimpleTypeDescriptor attributeType,
			Member member,
			boolean isIdentifier,
			boolean isVersion,
			boolean isOptional) {
		super( declaringType, name, attributeNature, attributeType, member );
		this.isIdentifier = isIdentifier;
		this.isVersion = isVersion;
		this.isOptional = isOptional;

		this.attributeType = attributeType;
	}

	@Override
	public SimpleTypeDescriptor getValueGraphType() {
		return attributeType;
	}

	@Override
	public SimpleTypeDescriptor getKeyGraphType() {
		return graphKeyTypeAccess.get();
	}



	/**
	 * Subclass used to simplify instantiation of singular attributes representing an entity's
	 * identifier.
	 */
	public static class Identifier extends SingularAttributeImpl {
		public Identifier(
				ManagedTypeDescriptor declaringType,
				String name,
				SimpleTypeDescriptor attributeType,
				Member member,
				PersistentAttributeType attributeNature) {
			super( declaringType, name, attributeNature, attributeType, member, true, false, false );
		}
	}

	/**
	 * Subclass used to simply instantiation of singular attributes representing an entity's
	 * version.
	 */
	public static class Version extends SingularAttributeImpl {
		public Version(
				ManagedTypeDescriptor declaringType,
				String name,
				PersistentAttributeType attributeNature,
				SimpleTypeDescriptor attributeType,
				Member member) {
			super( declaringType, name, attributeNature, attributeType, member, false, true, false );
		}
	}

	@Override
	public boolean isId() {
		return isIdentifier;
	}

	@Override
	public boolean isVersion() {
		return isVersion;
	}

	@Override
	public boolean isOptional() {
		return isOptional;
	}

	@Override
	public SimpleTypeDescriptor getType() {
		return attributeType;
	}

	@Override
	public boolean isAssociation() {
		return getPersistentAttributeType() == PersistentAttributeType.MANY_TO_ONE
				|| getPersistentAttributeType() == PersistentAttributeType.ONE_TO_ONE;
	}

	@Override
	public boolean isCollection() {
		return false;
	}

	@Override
	public BindableType getBindableType() {
		return BindableType.SINGULAR_ATTRIBUTE;
	}

	@Override
	public Class getBindableJavaType() {
		return attributeType.getJavaType();
	}

	private class DelayedKeyTypeAccess implements Supplier>, Serializable {
		private boolean resolved;
		private SimpleTypeDescriptor type;

		@Override
		public SimpleTypeDescriptor get() {
			if ( ! resolved ) {
				type = GraphHelper.resolveKeyTypeDescriptor( SingularAttributeImpl.this );
				resolved = true;
			}
			return type;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy