refactorings

This commit is contained in:
Timo Westkämper 2008-02-20 19:50:55 +00:00
parent 8250d38095
commit e2a8d69bb5
8 changed files with 142 additions and 100 deletions

View File

@ -1,11 +0,0 @@
package com.mysema.query.grammar;
/**
* BoOpImpl provides
*
* @author tiwe
* @version $Id$
*/
public class BoOpImpl<RT> implements Types.BoOp<RT>{
}

View File

@ -11,17 +11,25 @@ import com.mysema.query.grammar.Types.*;
*/
public class Grammar {
static <RT,L,R> Operation<RT> _binOp(Op<RT> type, Expr<L> left, Expr<R> right) {
BinaryOperation<RT,L,R> op = new BinaryOperation<RT,L,R>();
op.type = type;
op.left = left;
op.right = right;
return op;
}
static <A> Expr<A> _const(A obj){
ConstantExpr<A> e = new ConstantExpr<A>();
e.constant = obj;
return e;
}
static <A> OrderSpecifier<A> _orderAsc(Expr<A> target) {
OrderSpecifier<A> os = new OrderSpecifier<A>();
os.order = Order.ASC;
os.target = target;
return os;
}
static <A> Expr<A> _asConst(A obj){
ConstantExpr<A> e = new ConstantExpr<A>();
e.constant = obj;
return e;
}
static <A> OrderSpecifier<A> _orderDesc(Expr<A> target) {
@ -30,58 +38,109 @@ public class Grammar {
os.target = target;
return os;
}
public static Expr<Boolean> and(Expr<Boolean> left, Expr<Boolean> right){
return _bbe(BoOp.AND, left, right);
static <RT,F,S,T> Operation<RT> _terOp(Op<RT> type, Expr<F> fst, Expr<S> snd, Expr<T> trd){
TertiaryOperation<RT,F,S,T> op = new TertiaryOperation<RT,F,S,T>();
op.type = type;
op.first = fst;
op.second = snd;
op.third = trd;
return op;
}
static <RT,A> Operation<RT> _unOp(Op<RT> type, Expr<A> left) {
UnaryOperation<RT,A> op = new UnaryOperation<RT,A>();
op.type = type;
op.left = left;
return op;
}
public static Expr<Boolean> and(Expr<Boolean> left, Expr<Boolean> right){
return _binOp(BoOp.AND, left, right);
}
public static <A> OrderSpecifier<A> asc(Expr<A> target){
return _orderAsc(target);
}
public static Expr<String> concat(Expr<String> left, Expr<String> right){
return _binOp(StrOp.CONCAT, left, right);
}
public static <A> OrderSpecifier<A> desc(Expr<A> target){
return _orderDesc(target);
}
public static Expr<Boolean> eq(Object left, Object right){
return _bbe(NumOp.EQ, left, right);
}
public static Expr<Boolean> goe(Object left, Object right){
return _bbe(NumOp.GOE, left, right);
public static <A> Expr<Boolean> eq(Expr<A> left, Expr<A> right){
return _binOp(NumOp.EQ, left, right);
}
public static Expr<Boolean> gt(Object left, Object right){
return _bbe(NumOp.GT, left, right);
}
public static <A> Expr<Boolean> eq(Expr<A> left, A right){
return _binOp(NumOp.EQ, left, _const(right));
}
public static <A> Expr<Boolean> goe(Expr<A> left, A right){
return _binOp(NumOp.GOE, left, _const(right));
}
public static <A> Expr<Boolean> goe(Expr<A> left, Expr<A> right){
return _binOp(NumOp.GOE, left, right);
}
public static <A> Expr<Boolean> gt(Expr<A> left, A right){
return _binOp(NumOp.GT, left, _const(right));
}
public static <A> Expr<Boolean> gt(Expr<A> left, Expr<A> right){
return _binOp(NumOp.GT, left, right);
}
public static Expr<Boolean> like(Expr<String> left, String right){
return _bbe(StrOp.LIKE, left, right);
return _binOp(StrOp.LIKE, left, _const(right));
}
public static Expr<Boolean> loe(Object left, Object right){
return _bbe(NumOp.LOE, left, right);
public static <A> Expr<Boolean> loe(Expr<A> left, Expr<A> right){
return _binOp(NumOp.LOE, left, right);
}
public static Expr<String> lower(Expr<String> path){
return null;
public static Expr<String> lower(Expr<String> left){
return _unOp(StrOp.LOWER, left);
}
public static Expr<Boolean> lt(Object left, Object right){
return _bbe(NumOp.LT, left, right);
public static <A> Expr<Boolean> lt(Expr<A> left, A right){
return _binOp(NumOp.LT, left, _const(right));
}
public static Expr<Boolean> ne(Object left, Object right){
return _bbe(NumOp.NE, left, right);
public static <A> Expr<Boolean> lt(Expr<A> left, Expr<A> right){
return _binOp(NumOp.LT, left, right);
}
public static <A> Expr<Boolean> ne(Expr<A> left, A right){
return _binOp(NumOp.NE, left, _const(right));
}
public static <A> Expr<Boolean> ne(Expr<A> left, Expr<A> right){
return _binOp(NumOp.NE, left, right);
}
public static Expr<Boolean> not(Expr<Boolean> left){
return _bue(BoOp.NE, left);
}
return _unOp(BoOp.NOT, left);
}
public static Expr<Boolean> or(Expr<Boolean> left, Expr<Boolean> right){
return _bbe(BoOp.OR, left, right);
return _binOp(BoOp.OR, left, right);
}
public static Expr<String> substr(Expr<String> left, int start){
return _binOp(StrOp.SUBSTRING, left, _const(start));
}
public static Expr<String> substr(Expr<String> left, int start, int offset){
return _terOp(StrOp.SUBSTRING, left, _const(start), _const(offset));
}
public static Expr<String> upper(Expr<String> left){
return _unOp(StrOp.UPPER, left);
}
}

View File

@ -1,11 +0,0 @@
package com.mysema.query.grammar;
/**
* NumOpImpl provides
*
* @author tiwe
* @version $Id$
*/
class NumOpImpl<A> implements Types.NumOp<A> {
}

View File

@ -1,13 +0,0 @@
package com.mysema.query.grammar;
import com.mysema.query.grammar.Types.Op;
/**
* OpImpl provides
*
* @author tiwe
* @version $Id$
*/
public class OpImpl<RT> implements Op<RT> {
}

View File

@ -1,11 +0,0 @@
package com.mysema.query.grammar;
/**
* StrOpImpl provides
*
* @author tiwe
* @version $Id$
*/
class StrOpImpl<RT> implements Types.StrOp<RT>{
}

View File

@ -17,27 +17,19 @@ public class Types {
}
}
/**
* Binary boolean expression (expression with two arguments and boolean type)
*/
public static class BooleanBinaryExpr implements Expr<Boolean>{
public Expr<Boolean> left;
public Expr<Boolean> right;
public Op<Boolean> type;
public static class BinaryOperation<RT,L,R> implements Operation<RT>{
/**
* arguments don't need to be of same type as return type
*/
public Expr<L> left;
public Expr<R> right;
public Op<RT> type;
}
public static class BooleanProperty extends Reference<Boolean>{
public BooleanProperty(String path) {super(path);}
}
/**
* Unary boolean expression (expression with one argument and boolean type)
*/
public static class BooleanUnaryExpr implements Expr<Boolean>{
public Expr<Boolean> left;
public Op<Boolean> type;
}
/**
* Boolean operators (operators used with boolean operands)
*/
@ -49,6 +41,8 @@ public class Types {
BoOp<Boolean> XOR = new BoOpImpl<Boolean>();
}
static class BoOpImpl<RT> implements BoOp<RT>{}
public static class CharProperty extends Reference<Character>{
public CharProperty(String path) {super(path);}
}
@ -65,16 +59,16 @@ public class Types {
public EntityPathExpr<D> as(DomainType<D> to){
return new AsExpr<D>(this, to);
}
}
}
public static interface EntityPathExpr<T> extends Expr<T>{}
public interface Expr<A> { }
public interface Expr<A> { }
public static class NumberProperty extends Reference<Number>{
public NumberProperty(String path) {super(path);}
}
/**
* Numeric Operators (operators used with numeric operands)
*/
@ -90,15 +84,20 @@ public class Types {
NumOp<Number> SUB = new NumOpImpl<Number>();
}
static class NumOpImpl<A> implements Types.NumOp<A> {}
/**
* Operators (the return type is encoded in the generic parameter)
* Operators (the return type is encoded in the 1st generic parameter)
*/
public interface Op<RT> {
Op<Boolean> EQ = new OpImpl<Boolean>();
Op<Boolean> NE = new OpImpl<Boolean>();
}
public interface Operation<RT> extends Expr<RT> {}
static class OpImpl<RT> implements Op<RT> {}
public enum Order{ ASC,DESC }
public enum Order{ ASC,DESC }
public static class OrderSpecifier<A>{
public Order order;
@ -122,7 +121,7 @@ public class Types {
protected StringProperty str(String path) {
return new StringProperty(this._path+"."+path);
}
}
}
public static class StringProperty extends Reference<String>{
public StringProperty(String path) {super(path);}
@ -138,5 +137,25 @@ public class Types {
StrOp<String> SUBSTRING = new StrOpImpl<String>();
StrOp<String> UPPER = new StrOpImpl<String>();
}
static class StrOpImpl<RT> implements StrOp<RT>{}
public static class TertiaryOperation<RT,F,S,T> implements Operation<RT>{
/**
* arguments don't need to be of same type as return type
*/
public Expr<F> first;
public Expr<S> second;
public Expr<T> third;
public Op<RT> type;
}
public static class UnaryOperation<RT,A> implements Operation<RT>{
/**
* argument doesn't need to be of same type as return type
*/
public Expr<A> left;
public Op<RT> type;
}
}

View File

@ -9,7 +9,7 @@ import com.mysema.query.grammar.Types.*;
* @version $Id$
*/
public class QueryBase implements ExtQuery<QueryBase> {
public QueryBase from(EntityPathExpr<?>... objects) {
return this;
}
@ -27,6 +27,7 @@ public class QueryBase implements ExtQuery<QueryBase> {
}
public QueryBase where(Expr<Boolean>... objects) {
return this;
}

View File

@ -15,6 +15,15 @@ import org.junit.Test;
*/
public class QueryTest extends QueryBase{
@Test
public void testSimple(){
from(cat);
from(cat,cust).where(gt(cat.name,cust.name().firstName));
select(lower(cat.name)).from(cat).where(eq(substr(cat.name,0,2),"Mi"));
select(upper(cat.name)).from(cat);
select(concat(lower(cat.name),cat.mate().name)).from(cat);
}
// cats
@Test