refactored Operators

This commit is contained in:
Timo Westkämper 2008-02-20 18:09:04 +00:00
parent 4b608a406f
commit 8250d38095
8 changed files with 208 additions and 178 deletions

View File

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

View File

@ -10,100 +10,77 @@ import com.mysema.query.grammar.Types.*;
* @version $Id$
*/
public class Grammar {
// order
public static <A> OrderSpecifier<A> asc(Expr<A> target){
return _asc(target);
}
public static <A> OrderSpecifier<A> desc(Expr<A> target){
return _desc(target);
}
// boolean
public static Expr<Boolean> and(Expr<Boolean> left, Expr<Boolean> right){
return _bbe(BoOp.AND, left, right);
}
public static Expr<Boolean> not(Expr<Boolean> left){
return _bue(BoOp.NE, left);
}
public static Expr<Boolean> or(Expr<Boolean> left, Expr<Boolean> right){
return _bbe(BoOp.OR, left, right);
}
// number compariosn
public static Expr<Boolean> eq(Object left, Object right){
return _bbe(BoOp.EQ, left, right);
}
public static Expr<Boolean> goe(Object left, Object right){
return _bbe(BoOp.GOE, left, right);
}
public static Expr<Boolean> gt(Object left, Object right){
return _bbe(BoOp.GT, left, right);
}
public static Expr<Boolean> loe(Object left, Object right){
return _bbe(BoOp.LOE, left, right);
}
public static Expr<Boolean> lt(Object left, Object right){
return _bbe(BoOp.LT, left, right);
}
public static Expr<Boolean> ne(Object left, Object right){
return _bbe(BoOp.NE, left, right);
}
// string comparison
public static Expr<Boolean> like(Expr<String> left, String right){
return _bbe(BoOp.LIKE, left, right);
}
public static Expr<String> lower(Expr<String> path){
return null;
}
// arithmetic operations
// TODO : +,-,*,/,mod,div
// order
static <A> OrderSpecifier<A> _asc(Expr<A> target) {
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> OrderSpecifier<A> _desc(Expr<A> target) {
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) {
OrderSpecifier<A> os = new OrderSpecifier<A>();
os.order = Order.DESC;
os.target = target;
return os;
}
// constants
public static Expr<Boolean> and(Expr<Boolean> left, Expr<Boolean> right){
return _bbe(BoOp.AND, left, right);
}
static <A> Expr<A> _co(A obj){
ConstantExpr<A> e = new ConstantExpr<A>();
e.constant = obj;
return e;
public static <A> OrderSpecifier<A> asc(Expr<A> target){
return _orderAsc(target);
}
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);
}
// boolean
public static Expr<Boolean> goe(Object left, Object right){
return _bbe(NumOp.GOE, left, right);
}
static BooleanBinaryExpr _bbe(BoOp type,Object left, Object right){
BooleanBinaryExpr bbe = new BooleanBinaryExpr();
bbe.type = type;
bbe.left = left instanceof Expr ? (Expr)left : _co(left);
bbe.right = right instanceof Expr ? (Expr)right : _co(left);
return bbe;
public static Expr<Boolean> gt(Object left, Object right){
return _bbe(NumOp.GT, left, right);
}
public static Expr<Boolean> like(Expr<String> left, String right){
return _bbe(StrOp.LIKE, left, right);
}
static BooleanUnaryExpr _bue(BoOp type, Expr<Boolean> left){
BooleanUnaryExpr bue = new BooleanUnaryExpr();
bue.type = type;
bue.left = left;
return bue;
public static Expr<Boolean> loe(Object left, Object right){
return _bbe(NumOp.LOE, left, right);
}
public static Expr<String> lower(Expr<String> path){
return null;
}
public static Expr<Boolean> lt(Object left, Object right){
return _bbe(NumOp.LT, left, right);
}
public static Expr<Boolean> ne(Object left, Object right){
return _bbe(NumOp.NE, left, right);
}
public static Expr<Boolean> not(Expr<Boolean> left){
return _bue(BoOp.NE, left);
}
public static Expr<Boolean> or(Expr<Boolean> left, Expr<Boolean> right){
return _bbe(BoOp.OR, left, right);
}

View File

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

View File

@ -0,0 +1,13 @@
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

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

View File

@ -3,131 +3,140 @@ package com.mysema.query.grammar;
/**
* Query provides
* Types provides
*
* @author tiwe
* @version $Id$
*/
public class Types {
// order
public static class AsExpr<D> extends Reference<D> implements EntityPathExpr<D>{
public Reference<D> from, to;
AsExpr(Reference<D> from, Reference<D> to) {
super(to._path);
}
}
public enum Order{ ASC,DESC }
/**
* 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 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)
*/
public interface BoOp<RT> extends Op<RT>{
BoOp<Boolean> AND = new BoOpImpl<Boolean>();
BoOp<Boolean> NOT = new BoOpImpl<Boolean>();
BoOp<Boolean> OR = new BoOpImpl<Boolean>();
BoOp<Boolean> XNOR = new BoOpImpl<Boolean>();
BoOp<Boolean> XOR = new BoOpImpl<Boolean>();
}
public static class CharProperty extends Reference<Character>{
public CharProperty(String path) {super(path);}
}
public static class ConstantExpr<A> implements Expr<A>{
public A constant;
}
public static class DomainType<D> extends Reference<D> implements EntityPathExpr<D>{
protected DomainType(DomainType<?> type, String path) {
super(type._path+"."+path);
}
protected DomainType(String path) {super(path);}
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 static class NumberProperty extends Reference<Number>{
public NumberProperty(String path) {super(path);}
}
/**
* Numeric Operators (operators used with numeric operands)
*/
public interface NumOp<RT> extends Op<RT>{
NumOp<Number> ADD = new NumOpImpl<Number>();
NumOp<Number> DIV = new NumOpImpl<Number>();
NumOp<Boolean> GOE = new NumOpImpl<Boolean>();
NumOp<Boolean> GT = new NumOpImpl<Boolean>();
NumOp<Boolean> LOE = new NumOpImpl<Boolean>();
NumOp<Boolean> LT = new NumOpImpl<Boolean>();
NumOp<Number> MOD = new NumOpImpl<Number>();
NumOp<Number> MULT = new NumOpImpl<Number>();
NumOp<Number> SUB = new NumOpImpl<Number>();
}
/**
* Operators (the return type is encoded in the generic parameter)
*/
public interface Op<RT> {
Op<Boolean> EQ = new OpImpl<Boolean>();
Op<Boolean> NE = new OpImpl<Boolean>();
}
public enum Order{ ASC,DESC }
public static class OrderSpecifier<A>{
public Order order;
public Expr<A> target;
}
// expressions
public interface Expr<A> { }
// constant
public static class ConstantExpr<A> implements Expr<A>{
public A constant;
}
public interface Op {}
// operators
public enum BoOp implements Op{
// boolean
AND,
NOT,
OR,
XOR,
XNOR,
// comparison
EQ,
NE,
GOE,
GT,
LOE,
LT,
// string comparison
LIKE,
}
public enum StrOp implements Op{
// string manipulation
LOWER,
UPPER,
}
public enum NumOp implements Op{
// arithmetic
PLUS,
MINUS,
MULT,
DIV,
MOD
}
public static class BooleanUnaryExpr implements Expr<Boolean>{
public BoOp type;
public Expr<Boolean> left;
}
public static class BooleanBinaryExpr implements Expr<Boolean>{
public BoOp type;
public Expr<Boolean> left; public Expr<Boolean> right;
}
// references
public static interface EntityPathExpr<T> extends Expr<T>{}
public static class Reference<T> implements Expr<T>{
public final String _path;
public Reference(String path) {
this._path = path;
}
protected CharProperty ch(String path) {
return new CharProperty(this._path+"."+path);
}
protected StringProperty str(String path) {
return new StringProperty(this._path+"."+path);
}
protected NumberProperty num(String path) {
return new NumberProperty(this._path+"."+path);
}
protected BooleanProperty bool(String path){
return new BooleanProperty(this._path+"."+_path);
}
public final String _path;
protected CharProperty ch(String path) {
return new CharProperty(this._path+"."+path);
}
protected NumberProperty num(String path) {
return new NumberProperty(this._path+"."+path);
}
protected StringProperty str(String path) {
return new StringProperty(this._path+"."+path);
}
}
public static class BooleanProperty extends Reference<Boolean>{
public BooleanProperty(String path) {super(path);}
}
public static class CharProperty extends Reference<Character>{
public CharProperty(String path) {super(path);}
}
public static class NumberProperty extends Reference<Number>{
public NumberProperty(String path) {super(path);}
}
public static class StringProperty extends Reference<String>{
public StringProperty(String path) {super(path);}
}
public static class DomainType<D> extends Reference<D> implements EntityPathExpr<D>{
protected DomainType(String path) {super(path);}
protected DomainType(DomainType<?> type, String path) {
super(type._path+"."+path);
}
public EntityPathExpr<D> as(DomainType<D> to){
return new AsExpr<D>(this, to);
}
}
public static class AsExpr<D> extends Reference<D> implements EntityPathExpr<D>{
AsExpr(Reference<D> from, Reference<D> to) {
super(to._path);
}
public Reference<D> from, to;
/**
* String Operators (operators used with String operands)
*/
public interface StrOp<RT> extends Op<RT>{
StrOp<String> CONCAT = new StrOpImpl<String>();
StrOp<Boolean> LIKE = new StrOpImpl<Boolean>();
StrOp<String> LOWER = new StrOpImpl<String>();
StrOp<String> SUBSTRING = new StrOpImpl<String>();
StrOp<String> UPPER = new StrOpImpl<String>();
}
}

View File

@ -8,7 +8,7 @@ import org.junit.Test;
/**
* DomainTest provides
* QueryTest provides
*
* @author tiwe
* @version $Id$

View File

@ -1,7 +1,5 @@
package com.mysema.query.test.domain;
import com.mysema.query.grammar.Types.NumberProperty;
import com.mysema.query.grammar.Types.StringProperty;
import com.mysema.query.test.domain.Domain.qCompany;
/**