com.blazebit.persistence.criteria.impl.path.AbstractPath Maven / Gradle / Ivy
/*
* Copyright 2014 - 2020 Blazebit.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.blazebit.persistence.criteria.impl.path;
import com.blazebit.persistence.criteria.BlazeExpression;
import com.blazebit.persistence.criteria.BlazePath;
import com.blazebit.persistence.criteria.impl.BlazeCriteriaBuilderImpl;
import com.blazebit.persistence.criteria.impl.RenderContext;
import com.blazebit.persistence.criteria.impl.expression.AbstractExpression;
import com.blazebit.persistence.criteria.impl.expression.PathTypeExpression;
import javax.persistence.criteria.Path;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.MapAttribute;
import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.SingularAttribute;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* @author Christian Beikov
* @since 1.2.0
*/
public abstract class AbstractPath extends AbstractExpression implements BlazePath {
private static final long serialVersionUID = 1L;
private final AbstractPath basePath;
private final BlazeExpression> typeExpression;
private Map> attributePathCache;
@SuppressWarnings({"unchecked"})
public AbstractPath(BlazeCriteriaBuilderImpl criteriaBuilder, Class javaType, AbstractPath basePath) {
super(criteriaBuilder, javaType);
this.basePath = basePath;
this.typeExpression = new PathTypeExpression(criteriaBuilder, getJavaType(), this);
}
public AbstractPath getBasePath() {
return basePath;
}
@Override
public AbstractPath getParentPath() {
return getBasePath();
}
public abstract AbstractPath treatAs(Class treatAsType);
protected final EntityType getTreatType(Class type) {
return criteriaBuilder.getEntityMetamodel().entity(type);
}
@Override
@SuppressWarnings({"unchecked"})
public BlazeExpression> type() {
return typeExpression;
}
public abstract Attribute getAttribute();
protected abstract Attribute findAttribute(String attributeName);
protected abstract boolean isDereferencable();
public String getPathExpression() {
return getBasePath().getPathExpression() + "." + getAttribute().getName();
}
private void checkGet(Attribute attribute) {
checkDereferenceAllowed();
if (attribute == null) {
throw new IllegalArgumentException("Null attribute");
}
}
protected final Path getAttributePath(String attributeName) {
return attributePathCache == null ? null : attributePathCache.get(attributeName);
}
protected final void putAttributePath(String attributeName, Path path) {
if (attributePathCache == null) {
attributePathCache = new HashMap>();
}
attributePathCache.put(attributeName, path);
}
@Override
@SuppressWarnings({"unchecked"})
public BlazePath get(SingularAttribute attribute) {
checkGet(attribute);
SingularAttributePath path = (SingularAttributePath) getAttributePath(attribute.getName());
if (path == null) {
path = new SingularAttributePath(criteriaBuilder, attribute.getJavaType(), this, attribute);
putAttributePath(attribute.getName(), path);
}
return path;
}
@Override
@SuppressWarnings({"unchecked"})
public > BlazeExpression get(PluralAttribute attribute) {
checkGet(attribute);
PluralAttributePath path = (PluralAttributePath) getAttributePath(attribute.getName());
if (path == null) {
path = new PluralAttributePath(criteriaBuilder, this, attribute);
putAttributePath(attribute.getName(), path);
}
return path;
}
@Override
@SuppressWarnings({"unchecked"})
public > BlazeExpression get(MapAttribute attribute) {
checkGet(attribute);
PluralAttributePath path = (PluralAttributePath) getAttributePath(attribute.getName());
if (path == null) {
path = new PluralAttributePath(criteriaBuilder, this, (PluralAttribute) attribute);
putAttributePath(attribute.getName(), path);
}
return path;
}
@Override
@SuppressWarnings({"unchecked"})
public BlazePath get(String attributeName) {
checkDereferenceAllowed();
final Attribute attribute = getAttribute(attributeName);
if (attribute.isCollection()) {
final PluralAttribute pluralAttribute = (PluralAttribute) attribute;
if (PluralAttribute.CollectionType.MAP.equals(pluralAttribute.getCollectionType())) {
return (PluralAttributePath) this.