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

Hibernate example source code file (CriteriaBuilderImpl.java)

This example Hibernate source code file (CriteriaBuilderImpl.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, comparisonpredicate, expression, expression, illegalargumentexception, io, math, n, number, number, predicate, predicate, suppresswarnings, t, util, y, y

The Hibernate CriteriaBuilderImpl.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.criteria;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.Tuple;
import javax.persistence.criteria.CompoundSelection;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Selection;
import javax.persistence.criteria.Subquery;
import org.hibernate.ejb.EntityManagerFactoryImpl;
import org.hibernate.ejb.criteria.expression.BinaryArithmeticOperation;
import org.hibernate.ejb.criteria.expression.CoalesceExpression;
import org.hibernate.ejb.criteria.expression.CompoundSelectionImpl;
import org.hibernate.ejb.criteria.expression.ConcatExpression;
import org.hibernate.ejb.criteria.expression.LiteralExpression;
import org.hibernate.ejb.criteria.expression.NullLiteralExpression;
import org.hibernate.ejb.criteria.expression.NullifExpression;
import org.hibernate.ejb.criteria.expression.ParameterExpressionImpl;
import org.hibernate.ejb.criteria.expression.SearchedCaseExpression;
import org.hibernate.ejb.criteria.expression.SimpleCaseExpression;
import org.hibernate.ejb.criteria.expression.SizeOfCollectionExpression;
import org.hibernate.ejb.criteria.expression.SubqueryComparisonModifierExpression;
import org.hibernate.ejb.criteria.expression.UnaryArithmeticOperation;
import org.hibernate.ejb.criteria.expression.function.AbsFunction;
import org.hibernate.ejb.criteria.expression.function.AggregationFunction;
import org.hibernate.ejb.criteria.expression.function.BasicFunctionExpression;
import org.hibernate.ejb.criteria.expression.function.CurrentDateFunction;
import org.hibernate.ejb.criteria.expression.function.CurrentTimeFunction;
import org.hibernate.ejb.criteria.expression.function.CurrentTimestampFunction;
import org.hibernate.ejb.criteria.expression.function.LengthFunction;
import org.hibernate.ejb.criteria.expression.function.LocateFunction;
import org.hibernate.ejb.criteria.expression.function.LowerFunction;
import org.hibernate.ejb.criteria.expression.function.ParameterizedFunctionExpression;
import org.hibernate.ejb.criteria.expression.function.SqrtFunction;
import org.hibernate.ejb.criteria.expression.function.SubstringFunction;
import org.hibernate.ejb.criteria.expression.function.TrimFunction;
import org.hibernate.ejb.criteria.expression.function.UpperFunction;
import org.hibernate.ejb.criteria.path.PluralAttributePath;
import org.hibernate.ejb.criteria.predicate.BetweenPredicate;
import org.hibernate.ejb.criteria.predicate.BooleanAssertionPredicate;
import org.hibernate.ejb.criteria.predicate.BooleanExpressionPredicate;
import org.hibernate.ejb.criteria.predicate.BooleanStaticAssertionPredicate;
import org.hibernate.ejb.criteria.predicate.ComparisonPredicate;
import org.hibernate.ejb.criteria.predicate.ComparisonPredicate.ComparisonOperator;
import org.hibernate.ejb.criteria.predicate.CompoundPredicate;
import org.hibernate.ejb.criteria.predicate.ExistsPredicate;
import org.hibernate.ejb.criteria.predicate.ImplicitNumericExpressionTypeDeterminer;
import org.hibernate.ejb.criteria.predicate.InPredicate;
import org.hibernate.ejb.criteria.predicate.IsEmptyPredicate;
import org.hibernate.ejb.criteria.predicate.LikePredicate;
import org.hibernate.ejb.criteria.predicate.MemberOfPredicate;
import org.hibernate.ejb.criteria.predicate.NullnessPredicate;

/**
 * Hibernate implementation of the JPA {@link CriteriaBuilder} contract.
 *
 * @author Steve Ebersole
 */
public class CriteriaBuilderImpl implements CriteriaBuilder, Serializable {
	private final EntityManagerFactoryImpl entityManagerFactory;

	public CriteriaBuilderImpl(EntityManagerFactoryImpl entityManagerFactory) {
		this.entityManagerFactory = entityManagerFactory;
	}

	/**
	 * Provides protected access to the underlying {@link EntityManagerFactoryImpl}.
	 *
	 * @return The underlying {@link EntityManagerFactoryImpl}
	 */
	public  EntityManagerFactoryImpl getEntityManagerFactory() {
		return entityManagerFactory;
	}

	/**
	 * {@inheritDoc}
	 */
	public CriteriaQuery<Object> createQuery() {
		return new CriteriaQueryImpl<Object>( this, Object.class );
	}

	/**
	 * {@inheritDoc}
	 */
	public <T> CriteriaQuery createQuery(Class resultClass) {
		return new CriteriaQueryImpl<T>( this, resultClass );
	}

	/**
	 * {@inheritDoc}
	 */
	public CriteriaQuery<Tuple> createTupleQuery() {
		return new CriteriaQueryImpl<Tuple>( this, Tuple.class );
	}

	/**
	 * Package-protected method to centralize checking of criteria query
	 * multiselects as defined by the
	 * {@link CriteriaQuery#multiselect(List)}  method.
	 *
	 * @param selections The selection varargs to check
	 *
	 * @throws IllegalArgumentException If, as per
	 * {@link CriteriaQuery#multiselect(List)} documentation,
	 * <i>"An argument to the multiselect method must not be a tuple-
     * or array-valued compound selection item."</i>
	 */
	void checkMultiselect(List<Selection selections) {
		for ( Selection<?> selection : selections ) {
			if ( selection.isCompoundSelection() ) {
				if ( selection.getJavaType().isArray() ) {
					throw new IllegalArgumentException(
							"multiselect selections cannot contain " +
									"compound array-valued elements"
					);
				}
				if ( Tuple.class.isAssignableFrom( selection.getJavaType() ) ) {
					throw new IllegalArgumentException(
							"multiselect selections cannot contain " +
									"compound tuple-valued elements"
					);
				}
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public CompoundSelection<Tuple> tuple(Selection... selections) {
		return tuple( Arrays.asList( selections ) );
	}

	/**
	 * Version of {@link #tuple(Selection[])} taking a list.
	 *
	 * @param selections List of selections.
	 *
	 * @return The tuple compound selection
	 */
	public CompoundSelection<Tuple> tuple(List> selections) {
		checkMultiselect( selections );
		return new CompoundSelectionImpl<Tuple>( this, Tuple.class, selections );
	}

	/**
	 * {@inheritDoc}
	 */
	public CompoundSelection<Object[]> array(Selection... selections) {
		return array( Arrays.asList( selections ) );
	}

	/**
	 * Version of {@link #array(Selection[])} taking a list of selections.
	 *
	 * @param selections List of selections.
	 *
	 * @return The array compound selection
	 */
	public CompoundSelection<Object[]> array(List> selections) {
		return array( Object[].class, selections );
	}

	/**
	 * Version of {@link #array(Selection[])} taking a list of selections,
	 * as well as the type of array.
	 *
	 * @param type The type of array
	 * @param selections List of selections.
	 *
	 * @return The array compound selection
	 */
	public <Y> CompoundSelection array(Class type, List> selections) {
		checkMultiselect( selections );
		return new CompoundSelectionImpl<Y>( this, type, selections );
	}

	/**
	 * {@inheritDoc}
	 */
	public <Y> CompoundSelection construct(Class result, Selection... selections) {
		return construct( result, Arrays.asList( selections ) );
	}

	/**
	 * Version of {@link #construct(Class,Selection[])} taking the
	 * to-be-constructed type as well as a list of selections.
	 *
	 * @param result The result class to be constructed.
	 * @param selections The selections to use in the constructor call.
	 *
	 * @return The <b>view compound selection.
	 */
	public <Y> CompoundSelection construct(Class result, List> selections) {
		checkMultiselect( selections );
		return new CompoundSelectionImpl<Y>( this, result, selections );
	}


	// ordering ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	/**
	 * {@inheritDoc}
	 */
	public Order asc(Expression<?> x) {
		return new OrderImpl( x, true );
	}

	/**
	 * {@inheritDoc}
	 */
	public Order desc(Expression<?> x) {
		return new OrderImpl( x, false );
	}


	// predicates ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	public Predicate wrap(Expression<Boolean> expression) {
		if ( Predicate.class.isInstance( expression ) ) {
			return ( (Predicate) expression );
		}
		else if ( PathImplementor.class.isInstance( expression ) ) {
			return new BooleanAssertionPredicate( this, expression, Boolean.TRUE );
		}
		else {
			return new BooleanExpressionPredicate( this, expression );
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate not(Expression<Boolean> expression) {
		return wrap( expression ).not();
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate and(Expression<Boolean> x, Expression y) {
		return new CompoundPredicate( this, Predicate.BooleanOperator.AND, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate or(Expression<Boolean> x, Expression y) {
		return new CompoundPredicate( this, Predicate.BooleanOperator.OR, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate and(Predicate... restrictions) {
		return new CompoundPredicate( this, Predicate.BooleanOperator.AND, restrictions );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate or(Predicate... restrictions) {
		return new CompoundPredicate( this, Predicate.BooleanOperator.OR, restrictions );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate conjunction() {
		return new CompoundPredicate( this, Predicate.BooleanOperator.AND );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate disjunction() {
		return new CompoundPredicate( this, Predicate.BooleanOperator.OR );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate isTrue(Expression<Boolean> expression) {
		if ( CompoundPredicate.class.isInstance( expression ) ) {
			final CompoundPredicate predicate = (CompoundPredicate) expression;
			if ( predicate.getExpressions().size() == 0 ) {
				return new BooleanStaticAssertionPredicate(
						this,
						predicate.getOperator() == Predicate.BooleanOperator.AND
				);
			}
			return predicate;
		}
		else if ( Predicate.class.isInstance( expression ) ) {
			return (Predicate) expression;
		}
		return new BooleanAssertionPredicate( this, expression, Boolean.TRUE );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate isFalse(Expression<Boolean> expression) {
		if ( CompoundPredicate.class.isInstance( expression ) ) {
			final CompoundPredicate predicate = (CompoundPredicate) expression;
			if ( predicate.getExpressions().size() == 0 ) {
				return new BooleanStaticAssertionPredicate(
						this,
						predicate.getOperator() == Predicate.BooleanOperator.OR
				);
			}
			predicate.not();
			return predicate;
		}
		else if ( Predicate.class.isInstance( expression ) ) {
			final Predicate predicate = (Predicate) expression;
			predicate.not();
			return predicate;
		}
		return new BooleanAssertionPredicate( this, expression, Boolean.FALSE );
	}

	/**s
	 * {@inheritDoc}
	 */
	public Predicate isNull(Expression<?> x) {
		return new NullnessPredicate( this, x );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate isNotNull(Expression<?> x) {
		return isNull( x ).not();
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate equal(Expression<?> x, Expression y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.EQUAL, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate notEqual(Expression<?> x, Expression y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.NOT_EQUAL, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate equal(Expression<?> x, Object y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.EQUAL, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate notEqual(Expression<?> x, Object y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.NOT_EQUAL, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public <Y extends Comparable Predicate greaterThan(Expression x, Expression y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.GREATER_THAN, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public <Y extends Comparable Predicate lessThan(
			Expression<? extends Y> x,
			Expression<? extends Y> y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.LESS_THAN, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public <Y extends Comparable Predicate greaterThanOrEqualTo(
			Expression<? extends Y> x,
			Expression<? extends Y> y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.GREATER_THAN_OR_EQUAL, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public <Y extends Comparable Predicate lessThanOrEqualTo(
			Expression<? extends Y> x,
			Expression<? extends Y> y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.LESS_THAN_OR_EQUAL, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public <Y extends Comparable Predicate greaterThan(
			Expression<? extends Y> x,
			Y y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.GREATER_THAN, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public <Y extends Comparable Predicate lessThan(
			Expression<? extends Y> x,
			Y y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.LESS_THAN, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public <Y extends Comparable Predicate greaterThanOrEqualTo(
			Expression<? extends Y> x,
			Y y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.GREATER_THAN_OR_EQUAL, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public<Y extends Comparable Predicate lessThanOrEqualTo(
			Expression<? extends Y> x,
			Y y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.LESS_THAN_OR_EQUAL, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate gt(Expression<? extends Number> x, Expression y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.GREATER_THAN, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate lt(Expression<? extends Number> x, Expression y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.LESS_THAN, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate ge(Expression<? extends Number> x, Expression y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.GREATER_THAN_OR_EQUAL, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate le(Expression<? extends Number> x, Expression y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.LESS_THAN_OR_EQUAL, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate gt(Expression<? extends Number> x, Number y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.GREATER_THAN, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate lt(Expression<? extends Number> x, Number y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.LESS_THAN, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate ge(Expression<? extends Number> x, Number y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.GREATER_THAN_OR_EQUAL, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public Predicate le(Expression<? extends Number> x, Number y) {
		//noinspection SuspiciousNameCombination
		return new ComparisonPredicate( this, ComparisonOperator.LESS_THAN_OR_EQUAL, x, y );
	}

	/**
	 * {@inheritDoc}
	 */
	public <Y extends Comparable Predicate between(
			Expression<? extends Y> expression,
			Y lowerBound,
			Y upperBound) {
		return new BetweenPredicate<Y>( this, expression, lowerBound, upperBound );
	}

	/**
	 * {@inheritDoc}
	 */
	public <Y extends Comparable Predicate between(
			Expression<? extends Y> expression,
			Expression<? extends Y> lowerBound,
			Expression<? extends Y> upperBound) {
		return new BetweenPredicate<Y>( this, expression, lowerBound, upperBound );
	}

	/**
	 * {@inheritDoc}
	 */
	public <T> In in(Expression expression) {
		return new InPredicate<T>( this, expression );
	}

	public <T> In in(Expression expression, Expression... values) {
		return new InPredicate<T>( this, expression, values );
	}

	public <T> In in(Expression expression, T... values) {
		return new InPredicate<T>( this, expression, values );
	}

	public <T> In in(Expression expression, Collection values) {
		return new InPredicate<T>( this, expression, values );
	}

	public Predicate like(Expression<String> matchExpression, Expression pattern) {
		return new LikePredicate( this, matchExpression, pattern );
	}

	public Predicate like(Expression<String> matchExpression, Expression pattern, Expression escapeCharacter) {
		return new LikePredicate( this, matchExpression, pattern, escapeCharacter );
	}

	public Predicate like(Expression<String> matchExpression, Expression pattern, char escapeCharacter) {
		return new LikePredicate( this, matchExpression, pattern, escapeCharacter );
	}

	public Predicate like(Expression<String> matchExpression, String pattern) {
		return new LikePredicate( this, matchExpression, pattern );
	}

	public Predicate like(Expression<String> matchExpression, String pattern, Expression escapeCharacter) {
		return new LikePredicate( this, matchExpression, pattern, escapeCharacter );
	}

	public Predicate like(Expression<String> matchExpression, String pattern, char escapeCharacter) {
		return new LikePredicate( this, matchExpression, pattern, escapeCharacter );
	}

	public Predicate notLike(Expression<String> matchExpression, Expression pattern) {
		return like( matchExpression, pattern ).not();
	}

	public Predicate notLike(Expression<String> matchExpression, Expression pattern, Expression escapeCharacter) {
		return like( matchExpression, pattern, escapeCharacter ).not();
	}

	public Predicate notLike(Expression<String> matchExpression, Expression pattern, char escapeCharacter) {
		return like( matchExpression, pattern, escapeCharacter ).not();
	}

	public Predicate notLike(Expression<String> matchExpression, String pattern) {
		return like( matchExpression, pattern ).not();
	}

	public Predicate notLike(Expression<String> matchExpression, String pattern, Expression escapeCharacter) {
		return like( matchExpression, pattern, escapeCharacter ).not();
	}

	public Predicate notLike(Expression<String> matchExpression, String pattern, char escapeCharacter) {
		return like( matchExpression, pattern, escapeCharacter ).not();
	}


	// parameters ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	/**
	 * {@inheritDoc}
	 */
	public <T> ParameterExpression parameter(Class paramClass) {
		return new ParameterExpressionImpl<T>( this, paramClass );
	}

	/**
	 * {@inheritDoc}
	 */
	public <T> ParameterExpression parameter(Class paramClass, String name) {
		return new ParameterExpressionImpl<T>( this, paramClass, name );
	}

	/**
	 * {@inheritDoc}
	 */
	public <T> Expression literal(T value) {
		if ( value == null ) {
			throw new IllegalArgumentException( "literal value cannot be null" );
		}
		return new LiteralExpression<T>( this, value );
	}

	/**
	 * {@inheritDoc}
	 */
	public <T> Expression nullLiteral(Class resultClass) {
		return new NullLiteralExpression<T>( this, resultClass );
	}


	// aggregate functions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	/**
	 * {@inheritDoc}
	 */
	public <N extends Number> Expression avg(Expression x) {
		return new AggregationFunction.AVG( this, x );
	}

	/**
	 * {@inheritDoc}
	 */
	public <N extends Number> Expression sum(Expression x) {
		return new AggregationFunction.SUM<N>( this, x );
	}

	/**
	 * {@inheritDoc}
	 */
	public Expression<Long> sumAsLong(Expression x) {
		return new AggregationFunction.SUM<Long>( this, x, Long.class );
	}

	/**
	 * {@inheritDoc}
	 */
	public Expression<Double> sumAsDouble(Expression x) {
		return new AggregationFunction.SUM<Double>( this, x, Double.class );
	}

	/**
	 * {@inheritDoc}
	 */
	public <N extends Number> Expression max(Expression x) {
		return new AggregationFunction.MAX<N>( this, x );
	}

	/**
	 * {@inheritDoc}
	 */
	public <N extends Number> Expression min(Expression x) {
		return new AggregationFunction.MIN<N>( this, x );
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public <X extends Comparable Expression greatest(Expression x) {
		return new AggregationFunction.GREATEST( this, x );
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public <X extends Comparable Expression least(Expression x) {
		return new AggregationFunction.LEAST( this, x );
	}

	/**
	 * {@inheritDoc}
	 */
	public Expression<Long> count(Expression x) {
		return new AggregationFunction.COUNT( this, x, false );
	}

	/**
	 * {@inheritDoc}
	 */
	public Expression<Long> countDistinct(Expression x) {
		return new AggregationFunction.COUNT( this, x, true );
	}


	// other functions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	/**
	 * {@inheritDoc}
	 */
	public <T> Expression function(String name, Class returnType, Expression... arguments) {
		return new ParameterizedFunctionExpression<T>( this, returnType, name, arguments );
	}

	/**
	 * Create a reference to a function taking no params.
	 *
	 * @param name The function name.
	 * @param returnType The return type.
	 *
	 * @return The function expression
	 */
	public <T> Expression function(String name, Class returnType) {
		return new BasicFunctionExpression<T>( this, returnType, name );
	}

	/**
	 * {@inheritDoc}
	 */
	public <N extends Number> Expression abs(Expression expression) {
		return new AbsFunction<N>( this, expression );
	}

	/**
	 * {@inheritDoc}
	 */
	public Expression<Double> sqrt(Expression expression) {
		return new SqrtFunction( this, expression );
	}

	public Expression<java.sql.Date> currentDate() {
		return new CurrentDateFunction( this );
	}

	public Expression<java.sql.Timestamp> currentTimestamp() {
		return new CurrentTimestampFunction( this );
	}

	public Expression<java.sql.Time> currentTime() {
		return new CurrentTimeFunction( this );
	}

	public Expression<String> substring(Expression value, Expression start) {
		return new SubstringFunction( this, value, start );
	}

	public Expression<String> substring(Expression value, int start) {
		return new SubstringFunction( this, value, start );
	}

	public Expression<String> substring(Expression value, Expression start, Expression length) {
		return new SubstringFunction( this, value, start, length );
	}

	public Expression<String> substring(Expression value, int start, int length) {
		return new SubstringFunction( this, value, start, length );
	}

	public Expression<String> trim(Expression trimSource ) {
		return new TrimFunction( this, trimSource );
	}

	public Expression<String> trim(Trimspec trimspec, Expression trimSource) {
		return new TrimFunction( this, trimspec, trimSource );
	}

	public Expression<String> trim(Expression trimCharacter, Expression trimSource) {
		return new TrimFunction( this, trimCharacter, trimSource );
	}

	public Expression<String> trim(Trimspec trimspec, Expression trimCharacter, Expression trimSource) {
		return new TrimFunction( this, trimspec, trimCharacter, trimSource );
	}

	public Expression<String> trim(char trimCharacter, Expression trimSource) {
		return new TrimFunction( this, trimCharacter, trimSource );
	}

	public Expression<String> trim(Trimspec trimspec, char trimCharacter, Expression trimSource) {
		return new TrimFunction( this, trimspec, trimCharacter, trimSource );
	}

	public Expression<String> lower(Expression value) {
		return new LowerFunction( this, value );
	}

	public Expression<String> upper(Expression value) {
		return new UpperFunction( this, value );
	}

	public Expression<Integer> length(Expression value) {
		return new LengthFunction( this, value );
	}

	public Expression<Integer> locate(Expression string, Expression pattern) {
		return new LocateFunction( this, pattern, string );
	}

	public Expression<Integer> locate(Expression string, Expression pattern, Expression start) {
		return new LocateFunction( this, pattern, string, start );
	}

	public Expression<Integer> locate(Expression string, String pattern) {
		return new LocateFunction( this, pattern, string );
	}

	public Expression<Integer> locate(Expression string, String pattern, int start) {
		return new LocateFunction( this, pattern, string, start );
	}


	// arithmetic operations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	@Override
	public <N extends Number> Expression neg(Expression expression) {
		return new UnaryArithmeticOperation<N>(
				this,
				UnaryArithmeticOperation.Operation.UNARY_MINUS,
				expression
		);
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public <N extends Number> Expression sum(Expression expression1, Expression expression2) {
		if ( expression1 == null || expression2 == null ) {
			throw new IllegalArgumentException( "arguments to sum() cannot be null" );
		}

		final Class resultType = BinaryArithmeticOperation.determineResultType( expression1.getJavaType(), expression2.getJavaType() );

		return new BinaryArithmeticOperation<N>(
				this,
				resultType,
				BinaryArithmeticOperation.Operation.ADD,
				expression1,
				expression2
		);
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public <N extends Number> Expression prod(Expression expression1, Expression expression2) {
		if ( expression1 == null || expression2 == null ) {
			throw new IllegalArgumentException( "arguments to prod() cannot be null" );
		}

		final Class resultType = BinaryArithmeticOperation.determineResultType( expression1.getJavaType(), expression2.getJavaType() );

		return new BinaryArithmeticOperation<N>(
				this,
				resultType,
				BinaryArithmeticOperation.Operation.MULTIPLY,
				expression1,
				expression2
		);
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public <N extends Number> Expression diff(Expression expression1, Expression expression2) {
		if ( expression1 == null || expression2 == null ) {
			throw new IllegalArgumentException( "arguments to diff() cannot be null" );
		}

		final Class resultType = BinaryArithmeticOperation.determineResultType( expression1.getJavaType(), expression2.getJavaType() );

		return new BinaryArithmeticOperation<N>(
				this,
				resultType,
				BinaryArithmeticOperation.Operation.SUBTRACT,
				expression1,
				expression2
		);
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public <N extends Number> Expression sum(Expression expression, N n) {
		if ( expression == null || n == null ) {
			throw new IllegalArgumentException( "arguments to sum() cannot be null" );
		}

		final Class resultType = BinaryArithmeticOperation.determineResultType( expression.getJavaType(), n.getClass() );

		return new BinaryArithmeticOperation<N>(
				this,
				resultType,
				BinaryArithmeticOperation.Operation.ADD,
				expression,
				n
		);
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public <N extends Number> Expression prod(Expression expression, N n) {
		if ( expression == null || n == null ) {
			throw new IllegalArgumentException( "arguments to prod() cannot be null" );
		}

		final Class resultType = BinaryArithmeticOperation.determineResultType( expression.getJavaType(), n.getClass() );

		return new BinaryArithmeticOperation<N>(
				this,
				resultType,
				BinaryArithmeticOperation.Operation.MULTIPLY,
				expression,
				n
		);
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public <N extends Number> Expression diff(Expression expression, N n) {
		if ( expression == null || n == null ) {
			throw new IllegalArgumentException( "arguments to diff() cannot be null" );
		}

		final Class resultType = BinaryArithmeticOperation.determineResultType( expression.getJavaType(), n.getClass() );

		return new BinaryArithmeticOperation<N>(
				this,
				resultType,
				BinaryArithmeticOperation.Operation.SUBTRACT,
				expression,
				n
		);
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public <N extends Number> Expression sum(N n, Expression expression) {
		if ( expression == null || n == null ) {
			throw new IllegalArgumentException( "arguments to sum() cannot be null" );
		}

		final Class resultType = BinaryArithmeticOperation.determineResultType( n.getClass(), expression.getJavaType() );

		return new BinaryArithmeticOperation<N>(
				this,
				resultType,
				BinaryArithmeticOperation.Operation.ADD,
				n,
				expression
		);
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public <N extends Number> Expression prod(N n, Expression expression) {
		if ( n == null || expression == null ) {
			throw new IllegalArgumentException( "arguments to prod() cannot be null" );
		}

		final Class resultType = BinaryArithmeticOperation.determineResultType( n.getClass(), expression.getJavaType() );

		return (BinaryArithmeticOperation<N>) new BinaryArithmeticOperation(
				this,
				resultType,
				BinaryArithmeticOperation.Operation.MULTIPLY,
				n,
				expression
		);
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public <N extends Number> Expression diff(N n, Expression expression) {
		if ( n == null || expression == null ) {
			throw new IllegalArgumentException( "arguments to diff() cannot be null" );
		}

		final Class resultType = BinaryArithmeticOperation.determineResultType( n.getClass(), expression.getJavaType() );

		return new BinaryArithmeticOperation<N>(
				this,
				resultType,
				BinaryArithmeticOperation.Operation.SUBTRACT,
				n,
				expression
		);
	}

	@Override
	@SuppressWarnings( {"unchecked"})
	public Expression<Number> quot(Expression expression1, Expression expression2) {
		if ( expression1 == null || expression2 == null ) {
			throw new IllegalArgumentException( "arguments to quot() cannot be null" );
		}

		final Class resultType = BinaryArithmeticOperation.determineResultType( expression1.getJavaType(), expression2.getJavaType(), true );

		return new BinaryArithmeticOperation<Number>(
				this,
				resultType,
				BinaryArithmeticOperation.Operation.DIVIDE,
				expression1,
				expression2
		);
	}

	@Override
	@SuppressWarnings( {"unchecked"})
	public Expression<Number> quot(Expression expression, Number number) {
		if ( expression == null || number == null ) {
			throw new IllegalArgumentException( "arguments to quot() cannot be null" );
		}

		final Class resultType = BinaryArithmeticOperation.determineResultType( expression.getJavaType(), number.getClass(), true );

		return new BinaryArithmeticOperation<Number>(
				this,
				resultType,
				BinaryArithmeticOperation.Operation.DIVIDE,
				expression,
				number
		);
	}

	@Override
	@SuppressWarnings( {"unchecked"})
	public Expression<Number> quot(Number number, Expression expression) {
		if ( expression == null || number == null ) {
			throw new IllegalArgumentException( "arguments to quot() cannot be null" );
		}

		final Class resultType = BinaryArithmeticOperation.determineResultType( number.getClass(), expression.getJavaType(), true );

		return new BinaryArithmeticOperation<Number>(
				this,
				resultType,
				BinaryArithmeticOperation.Operation.DIVIDE,
				number,
				expression
		);
	}

	@Override
	public Expression<Integer> mod(Expression expression1, Expression expression2) {
		if ( expression1 == null || expression2 == null ) {
			throw new IllegalArgumentException( "arguments to mod() cannot be null" );
		}

		return new BinaryArithmeticOperation<Integer>(
				this,
				Integer.class,
				BinaryArithmeticOperation.Operation.MOD,
				expression1,
				expression2
		);
	}

	@Override
	public Expression<Integer> mod(Expression expression, Integer integer) {
		if ( expression == null || integer == null ) {
			throw new IllegalArgumentException( "arguments to mod() cannot be null" );
		}

		return new BinaryArithmeticOperation<Integer>(
				this,
				Integer.class,
				BinaryArithmeticOperation.Operation.MOD,
				expression,
				integer
		);
	}

	@Override
	public Expression<Integer> mod(Integer integer, Expression expression) {
		if ( integer == null || expression == null ) {
			throw new IllegalArgumentException( "arguments to mod() cannot be null" );
		}

		return new BinaryArithmeticOperation<Integer>(
				this,
				Integer.class,
				BinaryArithmeticOperation.Operation.MOD,
				integer,
				expression
		);
	}


	// casting ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	/**
	 * {@inheritDoc}
	 */
	public ExpressionImplementor<Long> toLong(Expression expression) {
		return ( (ExpressionImplementor<? extends Number>) expression ).asLong();
	}

	/**
	 * {@inheritDoc}
	 */
	public ExpressionImplementor<Integer> toInteger(Expression expression) {
		return ( (ExpressionImplementor<? extends Number>) expression ).asInteger();
	}

	/**
	 * {@inheritDoc}
	 */
	public ExpressionImplementor<Float> toFloat(Expression expression) {
		return ( (ExpressionImplementor<? extends Number>) expression ).asFloat();
	}

	/**
	 * {@inheritDoc}
	 */
	public ExpressionImplementor<Double> toDouble(Expression expression) {
		return ( (ExpressionImplementor<? extends Number>) expression ).asDouble();
	}

	/**
	 * {@inheritDoc}
	 */
	public ExpressionImplementor<BigDecimal> toBigDecimal(Expression expression) {
		return ( (ExpressionImplementor<? extends Number>) expression ).asBigDecimal();
	}

	/**
	 * {@inheritDoc}
	 */
	public ExpressionImplementor<BigInteger> toBigInteger(Expression expression) {
		return ( (ExpressionImplementor<? extends Number>) expression ).asBigInteger();
	}

	/**
	 * {@inheritDoc}
	 */
	public ExpressionImplementor<String> toString(Expression characterExpression) {
		return ( (ExpressionImplementor<Character>) characterExpression ).asString();
	}


	// subqueries ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	/**
	 * {@inheritDoc}
	 */
	public Predicate exists(Subquery<?> subquery) {
		return new ExistsPredicate( this, subquery );
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public <Y> Expression all(Subquery subquery) {
		return new SubqueryComparisonModifierExpression<Y>(
				this,
				(Class<Y>) subquery.getJavaType(),
				subquery,
				SubqueryComparisonModifierExpression.Modifier.ALL
		);
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public <Y> Expression some(Subquery subquery) {
		return new SubqueryComparisonModifierExpression<Y>(
				this,
				(Class<Y>) subquery.getJavaType(),
				subquery,
				SubqueryComparisonModifierExpression.Modifier.SOME
		);
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public <Y> Expression any(Subquery subquery) {
		return new SubqueryComparisonModifierExpression<Y>(
				this,
				(Class<Y>) subquery.getJavaType(),
				subquery,
				SubqueryComparisonModifierExpression.Modifier.ANY
		);
	}


	// miscellaneous expressions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "RedundantCast" })
	public <Y> Expression coalesce(Expression exp1, Expression exp2) {
		return coalesce( (Class<Y>) null, exp1, exp2 );
	}

	public <Y> Expression coalesce(Class type, Expression exp1, Expression exp2) {
		return new CoalesceExpression<Y>( this, type ).value( exp1 ).value( exp2 );
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "RedundantCast" })
	public <Y> Expression coalesce(Expression exp1, Y exp2) {
		return coalesce( (Class<Y>) null, exp1, exp2 );
	}

	public <Y> Expression coalesce(Class type, Expression exp1, Y exp2) {
		return new CoalesceExpression<Y>( this, type ).value( exp1 ).value( exp2 );
	}

	/**
	 * {@inheritDoc}
	 */
	public <T> Coalesce coalesce() {
		return coalesce( (Class<T>)null );
	}

	public <T> Coalesce coalesce(Class type) {
		return new CoalesceExpression<T>( this, type );
	}

	/**
	 * {@inheritDoc}
	 */
	public Expression<String> concat(Expression string1, Expression string2) {
		return new ConcatExpression( this, string1, string2 );
	}

	/**
	 * {@inheritDoc}
	 */
	public Expression<String> concat(Expression string1, String string2) {
		return new ConcatExpression( this, string1, string2 );
	}

	/**
	 * {@inheritDoc}
	 */
	public Expression<String> concat(String string1, Expression string2) {
		return new ConcatExpression( this, string1, string2 );
	}

	/**
	 * {@inheritDoc}
	 */
	public <Y> Expression nullif(Expression exp1, Expression exp2) {
		return nullif( null, exp1, exp2 );
	}

	public <Y> Expression nullif(Class type, Expression exp1, Expression exp2) {
		return new NullifExpression<Y>( this, type, exp1, exp2 );
	}

	/**
	 * {@inheritDoc}
	 */
	public <Y> Expression nullif(Expression exp1, Y exp2) {
		return nullif( null, exp1, exp2 );
	}

	public <Y> Expression nullif(Class type, Expression exp1, Y exp2) {
		return new NullifExpression<Y>( this, type, exp1, exp2 );
	}

	/**
	 * {@inheritDoc}
	 */
	public <C, R> SimpleCase selectCase(Expression expression) {
		return selectCase( (Class<R>)null, expression );
	}

	public <C, R> SimpleCase selectCase(Class type, Expression expression) {
		return new SimpleCaseExpression<C, R>( this, type, expression );
	}

	/**
	 * {@inheritDoc}
	 */
	public <R> Case selectCase() {
		return selectCase( (Class<R>)null );
	}

	public <R> Case selectCase(Class type) {
		return new SearchedCaseExpression<R>( this, type );
	}

	/**
	 * {@inheritDoc}
	 */
	public <C extends Collection Expression size(C c) {
		int size = c == null ? 0 : c.size();
		return new LiteralExpression<Integer>(this, Integer.class, size);
	}

	/**
	 * {@inheritDoc}
	 */
	public <C extends Collection Expression size(Expression exp) {
		if ( LiteralExpression.class.isInstance(exp) ) {
			return size( ( (LiteralExpression<C>) exp ).getLiteral() );
		}
		else if ( PluralAttributePath.class.isInstance(exp) ) {
			return new SizeOfCollectionExpression<C>(this, (PluralAttributePath) exp );
		}
		// TODO : what other specific types?  any?
		throw new IllegalArgumentException("unknown collection expression type [" + exp.getClass().getName() + "]" );
	}

	/**
	 * {@inheritDoc}
	 */
	public <V, M extends Map Expression> values(M map) {
		return new LiteralExpression<Collection( this, map.values() );
	}

	/**
	 * {@inheritDoc}
	 */
	public <K, M extends Map Expression> keys(M map) {
		return new LiteralExpression<Set( this, map.keySet() );
	}


	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings({ "unchecked" })
	public <C extends Collection Predicate isEmpty(Expression collectionExpression) {
		if ( PluralAttributePath.class.isInstance(collectionExpression) ) {
			return new IsEmptyPredicate( this, (PluralAttributePath<C>) collectionExpression );
		}
		// TODO : what other specific types?  any?
		throw new IllegalArgumentException(
				"unknown collection expression type [" + collectionExpression.getClass().getName() + "]"
		);
	}

	/**
	 * {@inheritDoc}
	 */
	public <C extends Collection Predicate isNotEmpty(Expression collectionExpression) {
		return isEmpty( collectionExpression ).not();
	}

	/**
	 * {@inheritDoc}
	 */
	public <E, C extends Collection Predicate isMember(E e, Expression collectionExpression) {
		if ( ! PluralAttributePath.class.isInstance( collectionExpression ) ) {
			throw new IllegalArgumentException(
					"unknown collection expression type [" + collectionExpression.getClass().getName() + "]"
			);
		}
		return new MemberOfPredicate<E, C>(
				this,
				e,
				(PluralAttributePath<C>)collectionExpression
		);
	}

	/**
	 * {@inheritDoc}
	 */
	public <E, C extends Collection Predicate isNotMember(E e, Expression cExpression) {
		return isMember(e, cExpression).not();
	}

	/**
	 * {@inheritDoc}
	 */
	public <E, C extends Collection Predicate isMember(Expression elementExpression, Expression collectionExpression) {
		if ( ! PluralAttributePath.class.isInstance( collectionExpression ) ) {
			throw new IllegalArgumentException(
					"unknown collection expression type [" + collectionExpression.getClass().getName() + "]"
			);
		}
		return new MemberOfPredicate<E, C>(
				this,
				elementExpression,
				(PluralAttributePath<C>)collectionExpression
		);
	}

	/**
	 * {@inheritDoc}
	 */
	public <E, C extends Collection Predicate isNotMember(Expression eExpression, Expression cExpression) {
		return isMember(eExpression, cExpression).not();
	}
}

Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate CriteriaBuilderImpl.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.