Author: steve.ebersole(a)jboss.com
Date: 2009-08-11 15:16:08 -0400 (Tue, 11 Aug 2009)
New Revision: 17264
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaSubqueryImpl.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryBuilderImpl.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/BinaryArithmeticOperation.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/UnaryArithmeticOperation.java
Log:
EJB-447 : Implement JPA 2.0 criteria apis
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaSubqueryImpl.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaSubqueryImpl.java 2009-08-11
18:00:25 UTC (rev 17263)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaSubqueryImpl.java 2009-08-11
19:16:08 UTC (rev 17264)
@@ -38,7 +38,8 @@
import org.hibernate.ejb.criteria.expression.ExpressionImpl;
/**
- * TODO : javadoc
+ * The Hibernate implementation of the JPA {@link Subquery} contract. Mostlty a set of
delegation to its internal
+ * {@link QueryStructure}.
*
* @author Steve Ebersole
*/
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryBuilderImpl.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryBuilderImpl.java 2009-08-11
18:00:25 UTC (rev 17263)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryBuilderImpl.java 2009-08-11
19:16:08 UTC (rev 17264)
@@ -813,9 +813,11 @@
* {@inheritDoc}
*/
public <N extends Number> Expression<N> sum(Expression<? extends N>
expression1, Expression<? extends N> expression2) {
+ Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType(
(Class)Number.class, (Expression)expression1 );
+ type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type,
(Expression)expression2 );
return new BinaryArithmeticOperation<N>(
this,
- (Class<N>) expression1.getJavaType(),
+ type,
BinaryArithmeticOperation.Operation.ADD,
expression1,
expression2
@@ -826,9 +828,11 @@
* {@inheritDoc}
*/
public <N extends Number> Expression<N> prod(Expression<? extends N>
expression1, Expression<? extends N> expression2) {
+ Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType(
(Class)Number.class, (Expression)expression1 );
+ type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type,
(Expression)expression2 );
return new BinaryArithmeticOperation<N>(
this,
- (Class<N>) expression1.getJavaType(),
+ type,
BinaryArithmeticOperation.Operation.MULTIPLY,
expression1,
expression2
@@ -839,6 +843,8 @@
* {@inheritDoc}
*/
public <N extends Number> Expression<N> diff(Expression<? extends N>
expression1, Expression<? extends N> expression2) {
+ Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType(
(Class)Number.class, (Expression)expression1 );
+ type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type,
(Expression)expression2 );
return new BinaryArithmeticOperation<N>(
this,
(Class<N>) expression1.getJavaType(),
@@ -852,9 +858,11 @@
* {@inheritDoc}
*/
public <N extends Number> Expression<N> sum(Expression<? extends N>
expression, N n) {
+ Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType(
(Class)Number.class, (Expression)expression );
+ type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type, n );
return new BinaryArithmeticOperation<N>(
this,
- (Class<N>) expression.getJavaType(),
+ type,
BinaryArithmeticOperation.Operation.ADD,
expression,
n
@@ -865,9 +873,11 @@
* {@inheritDoc}
*/
public <N extends Number> Expression<N> prod(Expression<? extends N>
expression, N n) {
+ Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType(
(Class)Number.class, (Expression)expression );
+ type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type, n );
return new BinaryArithmeticOperation<N>(
this,
- (Class<N>) expression.getJavaType(),
+ type,
BinaryArithmeticOperation.Operation.MULTIPLY,
expression,
n
@@ -878,9 +888,11 @@
* {@inheritDoc}
*/
public <N extends Number> Expression<N> diff(Expression<? extends N>
expression, N n) {
+ Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType(
(Class)Number.class, (Expression)expression );
+ type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type, n );
return new BinaryArithmeticOperation<N>(
this,
- (Class<N>) expression.getJavaType(),
+ type,
BinaryArithmeticOperation.Operation.SUBTRACT,
expression,
n
@@ -891,9 +903,11 @@
* {@inheritDoc}
*/
public <N extends Number> Expression<N> sum(N n, Expression<? extends
N> expression) {
+ Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType(
(Class)Number.class, (Expression)expression );
+ type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type, n );
return new BinaryArithmeticOperation<N>(
this,
- (Class<N>) expression.getJavaType(),
+ type,
BinaryArithmeticOperation.Operation.ADD,
n,
expression
@@ -904,9 +918,11 @@
* {@inheritDoc}
*/
public <N extends Number> Expression<N> prod(N n, Expression<? extends
N> expression) {
+ Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType(
(Class)Number.class, (Expression)expression );
+ type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type, n );
return new BinaryArithmeticOperation<N>(
this,
- (Class<N>) expression.getJavaType(),
+ type,
BinaryArithmeticOperation.Operation.MULTIPLY,
n,
expression
@@ -917,9 +933,11 @@
* {@inheritDoc}
*/
public <N extends Number> Expression<N> diff(N n, Expression<? extends
N> expression) {
+ Class<N> type = (Class<N>)BinaryArithmeticOperation.determineReturnType(
(Class)Number.class, (Expression)expression );
+ type = (Class<N>)BinaryArithmeticOperation.determineReturnType( type, n );
return new BinaryArithmeticOperation<N>(
this,
- (Class<N>) expression.getJavaType(),
+ type,
BinaryArithmeticOperation.Operation.SUBTRACT,
n,
expression
@@ -930,24 +948,39 @@
* {@inheritDoc}
*/
public Expression<Number> quot(Expression<? extends Number> expression1,
Expression<? extends Number> expression2) {
- // TODO : still open question whether this should be a quotient (integer division) or
division
- throw new UnsupportedOperationException( "Not yet implemented!" );
+ return new BinaryArithmeticOperation<Number>(
+ this,
+ Number.class,
+ BinaryArithmeticOperation.Operation.DIVIDE,
+ expression1,
+ expression2
+ );
}
/**
* {@inheritDoc}
*/
public Expression<Number> quot(Expression<? extends Number> expression,
Number number) {
- // TODO : still open question whether this should be a quotient (integer division) or
division
- throw new UnsupportedOperationException( "Not yet implemented!" );
+ return new BinaryArithmeticOperation<Number>(
+ this,
+ Number.class,
+ BinaryArithmeticOperation.Operation.DIVIDE,
+ expression,
+ number
+ );
}
/**
* {@inheritDoc}
*/
public Expression<Number> quot(Number number, Expression<? extends Number>
expression) {
- // TODO : still open question whether this should be a quotient (integer division) or
division
- throw new UnsupportedOperationException( "Not yet implemented!" );
+ return new BinaryArithmeticOperation<Number>(
+ this,
+ Number.class,
+ BinaryArithmeticOperation.Operation.DIVIDE,
+ number,
+ expression
+ );
}
/**
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/BinaryArithmeticOperation.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/BinaryArithmeticOperation.java 2009-08-11
18:00:25 UTC (rev 17263)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/BinaryArithmeticOperation.java 2009-08-11
19:16:08 UTC (rev 17264)
@@ -23,7 +23,6 @@
import javax.persistence.criteria.Expression;
-import org.hibernate.ejb.criteria.ParameterContainer;
import org.hibernate.ejb.criteria.ParameterRegistry;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
@@ -44,18 +43,66 @@
private final Expression<? extends N> rhs;
private final Expression<? extends N> lhs;
+ /**
+ * Helper for determining the appropriate operation return type based on one of the
operands as an expression.
+ *
+ * @param defaultType The default return type to use if we cannot determine the java
type of 'expression' operand.
+ * @param expression The operand.
+ *
+ * @return The appropriate return type.
+ */
+ public static Class<? extends Number> determineReturnType(
+ Class<? extends Number> defaultType,
+ Expression<? extends Number> expression) {
+ return expression == null || expression.getJavaType() == null
+ ? defaultType
+ : expression.getJavaType();
+ }
+
+ /**
+ * Helper for determining the appropriate operation return type based on one of the
operands as a literal.
+ *
+ * @param defaultType The default return type to use if we cannot determine the java
type of 'numberLiteral' operand.
+ * @param numberLiteral The operand.
+ *
+ * @return The appropriate return type.
+ */
+ public static Class<? extends Number> determineReturnType(
+ Class<? extends Number> defaultType,
+ Number numberLiteral) {
+ return numberLiteral == null ? defaultType : numberLiteral.getClass();
+ }
+
+ /**
+ * Creates an arithmethic operation based on 2 expressions.
+ *
+ * @param queryBuilder The builder for query components.
+ * @param resultType The operation result type
+ * @param operator The operator (type of operation).
+ * @param rhs The right-hand operand
+ * @param lhs The left-hand operand.
+ */
public BinaryArithmeticOperation(
QueryBuilderImpl queryBuilder,
- Class<N> javaType,
+ Class<N> resultType,
Operation operator,
Expression<? extends N> rhs,
Expression<? extends N> lhs) {
- super( queryBuilder, javaType );
+ super( queryBuilder, resultType );
this.operator = operator;
this.rhs = rhs;
this.lhs = lhs;
}
+ /**
+ * Creates an arithmethic operation based on an expression and a literal.
+ *
+ * @param queryBuilder The builder for query components.
+ * @param resultType The operation result type
+ * @param operator The operator (type of operation).
+ * @param rhs The right-hand operand
+ * @param lhs The left-hand operand (the literal).
+ */
public BinaryArithmeticOperation(
QueryBuilderImpl queryBuilder,
Class<N> javaType,
@@ -68,6 +115,15 @@
this.lhs = new LiteralExpression<N>( queryBuilder, lhs );
}
+ /**
+ * Creates an arithmethic operation based on an expression and a literal.
+ *
+ * @param queryBuilder The builder for query components.
+ * @param resultType The operation result type
+ * @param operator The operator (type of operation).
+ * @param rhs The right-hand operand (the literal).
+ * @param lhs The left-hand operand
+ */
public BinaryArithmeticOperation(
QueryBuilderImpl queryBuilder,
Class<N> javaType,
@@ -79,19 +135,27 @@
this.rhs = new LiteralExpression<N>( queryBuilder, rhs );
this.lhs = lhs;
}
-
public Operation getOperator() {
return operator;
}
+ /**
+ * {@inheritDoc}
+ */
public Expression<? extends N> getRightHandOperand() {
return rhs;
}
+ /**
+ * {@inheritDoc}
+ */
public Expression<? extends N> getLeftHandOperand() {
return lhs;
}
+ /**
+ * {@inheritDoc}
+ */
public void registerParameters(ParameterRegistry registry) {
Helper.possibleParameter( getRightHandOperand(), registry );
Helper.possibleParameter( getLeftHandOperand(), registry );
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/UnaryArithmeticOperation.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/UnaryArithmeticOperation.java 2009-08-11
18:00:25 UTC (rev 17263)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/UnaryArithmeticOperation.java 2009-08-11
19:16:08 UTC (rev 17264)
@@ -1,12 +1,11 @@
package org.hibernate.ejb.criteria.expression;
import javax.persistence.criteria.Expression;
-import org.hibernate.ejb.criteria.ParameterContainer;
import org.hibernate.ejb.criteria.ParameterRegistry;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
- * Models arithmetc operations with two operands.
+ * Models unary arithmetic operation (unary plus and unary minus).
*
* @author Steve Ebersole
*/
@@ -30,14 +29,20 @@
this.operand = operand;
}
+ public Operation getOperation() {
+ return operation;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public Expression<T> getOperand() {
return operand;
}
- public Operation getOperation() {
- return operation;
- }
-
+ /**
+ * {@inheritDoc}
+ */
public void registerParameters(ParameterRegistry registry) {
Helper.possibleParameter( getOperand(), registry );
}