From 8250d380953f455ef3f7c8649ea761f1764702da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Wed, 20 Feb 2008 18:09:04 +0000 Subject: [PATCH] refactored Operators --- .../com/mysema/query/grammar/BoOpImpl.java | 11 + .../com/mysema/query/grammar/Grammar.java | 129 +++++------ .../com/mysema/query/grammar/NumOpImpl.java | 11 + .../java/com/mysema/query/grammar/OpImpl.java | 13 ++ .../com/mysema/query/grammar/StrOpImpl.java | 11 + .../java/com/mysema/query/grammar/Types.java | 207 +++++++++--------- .../java/com/mysema/query/test/QueryTest.java | 2 +- .../com/mysema/query/test/domain/User.java | 2 - 8 files changed, 208 insertions(+), 178 deletions(-) create mode 100644 src/main/java/com/mysema/query/grammar/BoOpImpl.java create mode 100644 src/main/java/com/mysema/query/grammar/NumOpImpl.java create mode 100644 src/main/java/com/mysema/query/grammar/OpImpl.java create mode 100644 src/main/java/com/mysema/query/grammar/StrOpImpl.java diff --git a/src/main/java/com/mysema/query/grammar/BoOpImpl.java b/src/main/java/com/mysema/query/grammar/BoOpImpl.java new file mode 100644 index 000000000..e077ea13c --- /dev/null +++ b/src/main/java/com/mysema/query/grammar/BoOpImpl.java @@ -0,0 +1,11 @@ +package com.mysema.query.grammar; + +/** + * BoOpImpl provides + * + * @author tiwe + * @version $Id$ + */ +public class BoOpImpl implements Types.BoOp{ + +} diff --git a/src/main/java/com/mysema/query/grammar/Grammar.java b/src/main/java/com/mysema/query/grammar/Grammar.java index 75bc9c731..c1129b494 100644 --- a/src/main/java/com/mysema/query/grammar/Grammar.java +++ b/src/main/java/com/mysema/query/grammar/Grammar.java @@ -10,100 +10,77 @@ import com.mysema.query.grammar.Types.*; * @version $Id$ */ public class Grammar { - // order - public static OrderSpecifier asc(Expr target){ - return _asc(target); - } - public static OrderSpecifier desc(Expr target){ - return _desc(target); - } - - // boolean - - public static Expr and(Expr left, Expr right){ - return _bbe(BoOp.AND, left, right); - } - public static Expr not(Expr left){ - return _bue(BoOp.NE, left); - } - public static Expr or(Expr left, Expr right){ - return _bbe(BoOp.OR, left, right); - } - - // number compariosn - - public static Expr eq(Object left, Object right){ - return _bbe(BoOp.EQ, left, right); - } - public static Expr goe(Object left, Object right){ - return _bbe(BoOp.GOE, left, right); - } - public static Expr gt(Object left, Object right){ - return _bbe(BoOp.GT, left, right); - } - public static Expr loe(Object left, Object right){ - return _bbe(BoOp.LOE, left, right); - } - public static Expr lt(Object left, Object right){ - return _bbe(BoOp.LT, left, right); - } - public static Expr ne(Object left, Object right){ - return _bbe(BoOp.NE, left, right); - } - - // string comparison - - public static Expr like(Expr left, String right){ - return _bbe(BoOp.LIKE, left, right); - } - - public static Expr lower(Expr path){ - return null; - } - - // arithmetic operations - - // TODO : +,-,*,/,mod,div - - // order - - static OrderSpecifier _asc(Expr target) { + static OrderSpecifier _orderAsc(Expr target) { OrderSpecifier os = new OrderSpecifier(); os.order = Order.ASC; os.target = target; return os; } - static OrderSpecifier _desc(Expr target) { + static Expr _asConst(A obj){ + ConstantExpr e = new ConstantExpr(); + e.constant = obj; + return e; + } + + static OrderSpecifier _orderDesc(Expr target) { OrderSpecifier os = new OrderSpecifier(); os.order = Order.DESC; os.target = target; return os; } - // constants + public static Expr and(Expr left, Expr right){ + return _bbe(BoOp.AND, left, right); + } - static Expr _co(A obj){ - ConstantExpr e = new ConstantExpr(); - e.constant = obj; - return e; + public static OrderSpecifier asc(Expr target){ + return _orderAsc(target); + } + + public static OrderSpecifier desc(Expr target){ + return _orderDesc(target); + } + + public static Expr eq(Object left, Object right){ + return _bbe(NumOp.EQ, left, right); } - // boolean + public static Expr 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 gt(Object left, Object right){ + return _bbe(NumOp.GT, left, right); + } + + public static Expr like(Expr left, String right){ + return _bbe(StrOp.LIKE, left, right); } - static BooleanUnaryExpr _bue(BoOp type, Expr left){ - BooleanUnaryExpr bue = new BooleanUnaryExpr(); - bue.type = type; - bue.left = left; - return bue; + + public static Expr loe(Object left, Object right){ + return _bbe(NumOp.LOE, left, right); + } + + public static Expr lower(Expr path){ + return null; + } + + public static Expr lt(Object left, Object right){ + return _bbe(NumOp.LT, left, right); + } + + public static Expr ne(Object left, Object right){ + return _bbe(NumOp.NE, left, right); + } + + public static Expr not(Expr left){ + return _bue(BoOp.NE, left); + } + + public static Expr or(Expr left, Expr right){ + return _bbe(BoOp.OR, left, right); } diff --git a/src/main/java/com/mysema/query/grammar/NumOpImpl.java b/src/main/java/com/mysema/query/grammar/NumOpImpl.java new file mode 100644 index 000000000..bac4620fd --- /dev/null +++ b/src/main/java/com/mysema/query/grammar/NumOpImpl.java @@ -0,0 +1,11 @@ +package com.mysema.query.grammar; + +/** + * NumOpImpl provides + * + * @author tiwe + * @version $Id$ + */ +class NumOpImpl implements Types.NumOp { + +} diff --git a/src/main/java/com/mysema/query/grammar/OpImpl.java b/src/main/java/com/mysema/query/grammar/OpImpl.java new file mode 100644 index 000000000..09bba7326 --- /dev/null +++ b/src/main/java/com/mysema/query/grammar/OpImpl.java @@ -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 implements Op { + +} diff --git a/src/main/java/com/mysema/query/grammar/StrOpImpl.java b/src/main/java/com/mysema/query/grammar/StrOpImpl.java new file mode 100644 index 000000000..a5ca1ca5f --- /dev/null +++ b/src/main/java/com/mysema/query/grammar/StrOpImpl.java @@ -0,0 +1,11 @@ +package com.mysema.query.grammar; + +/** + * StrOpImpl provides + * + * @author tiwe + * @version $Id$ + */ +class StrOpImpl implements Types.StrOp{ + +} diff --git a/src/main/java/com/mysema/query/grammar/Types.java b/src/main/java/com/mysema/query/grammar/Types.java index b71ef4141..03bc9206e 100644 --- a/src/main/java/com/mysema/query/grammar/Types.java +++ b/src/main/java/com/mysema/query/grammar/Types.java @@ -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 extends Reference implements EntityPathExpr{ + public Reference from, to; + AsExpr(Reference from, Reference 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{ + public Expr left; + public Expr right; + public Op type; + } + + public static class BooleanProperty extends Reference{ + public BooleanProperty(String path) {super(path);} + } + + /** + * Unary boolean expression (expression with one argument and boolean type) + */ + public static class BooleanUnaryExpr implements Expr{ + public Expr left; + public Op type; + } + + /** + * Boolean operators (operators used with boolean operands) + */ + public interface BoOp extends Op{ + BoOp AND = new BoOpImpl(); + BoOp NOT = new BoOpImpl(); + BoOp OR = new BoOpImpl(); + BoOp XNOR = new BoOpImpl(); + BoOp XOR = new BoOpImpl(); + } + + public static class CharProperty extends Reference{ + public CharProperty(String path) {super(path);} + } + + public static class ConstantExpr implements Expr{ + public A constant; + } + + public static class DomainType extends Reference implements EntityPathExpr{ + protected DomainType(DomainType type, String path) { + super(type._path+"."+path); + } + protected DomainType(String path) {super(path);} + public EntityPathExpr as(DomainType to){ + return new AsExpr(this, to); + } + } + + public static interface EntityPathExpr extends Expr{} + + public interface Expr { } + + public static class NumberProperty extends Reference{ + public NumberProperty(String path) {super(path);} + } + + /** + * Numeric Operators (operators used with numeric operands) + */ + public interface NumOp extends Op{ + NumOp ADD = new NumOpImpl(); + NumOp DIV = new NumOpImpl(); + NumOp GOE = new NumOpImpl(); + NumOp GT = new NumOpImpl(); + NumOp LOE = new NumOpImpl(); + NumOp LT = new NumOpImpl(); + NumOp MOD = new NumOpImpl(); + NumOp MULT = new NumOpImpl(); + NumOp SUB = new NumOpImpl(); + } + + /** + * Operators (the return type is encoded in the generic parameter) + */ + public interface Op { + Op EQ = new OpImpl(); + Op NE = new OpImpl(); + } + + public enum Order{ ASC,DESC } public static class OrderSpecifier{ public Order order; public Expr target; } - // expressions - - public interface Expr { } - - // constant - - public static class ConstantExpr implements Expr{ - 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{ - public BoOp type; - public Expr left; - } - - public static class BooleanBinaryExpr implements Expr{ - public BoOp type; - public Expr left; public Expr right; - } - - // references - - public static interface EntityPathExpr extends Expr{} - public static class Reference implements Expr{ + 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{ - public BooleanProperty(String path) {super(path);} - } - public static class CharProperty extends Reference{ - public CharProperty(String path) {super(path);} - } - public static class NumberProperty extends Reference{ - public NumberProperty(String path) {super(path);} - } public static class StringProperty extends Reference{ public StringProperty(String path) {super(path);} } - public static class DomainType extends Reference implements EntityPathExpr{ - protected DomainType(String path) {super(path);} - protected DomainType(DomainType type, String path) { - super(type._path+"."+path); - } - public EntityPathExpr as(DomainType to){ - return new AsExpr(this, to); - } - } - public static class AsExpr extends Reference implements EntityPathExpr{ - AsExpr(Reference from, Reference to) { - super(to._path); - } - public Reference from, to; + /** + * String Operators (operators used with String operands) + */ + public interface StrOp extends Op{ + StrOp CONCAT = new StrOpImpl(); + StrOp LIKE = new StrOpImpl(); + StrOp LOWER = new StrOpImpl(); + StrOp SUBSTRING = new StrOpImpl(); + StrOp UPPER = new StrOpImpl(); } } diff --git a/src/test/java/com/mysema/query/test/QueryTest.java b/src/test/java/com/mysema/query/test/QueryTest.java index dadf4b94f..1dbdb6038 100644 --- a/src/test/java/com/mysema/query/test/QueryTest.java +++ b/src/test/java/com/mysema/query/test/QueryTest.java @@ -8,7 +8,7 @@ import org.junit.Test; /** - * DomainTest provides + * QueryTest provides * * @author tiwe * @version $Id$ diff --git a/src/test/java/com/mysema/query/test/domain/User.java b/src/test/java/com/mysema/query/test/domain/User.java index c051953ee..edd810ea3 100644 --- a/src/test/java/com/mysema/query/test/domain/User.java +++ b/src/test/java/com/mysema/query/test/domain/User.java @@ -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; /**