mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-16 21:01:10 +08:00
#300 further improvements to template handling
This commit is contained in:
parent
a13a475e1a
commit
56554b25c4
@ -20,11 +20,13 @@ import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.types.ConstantImpl;
|
||||
import com.mysema.query.types.expr.StringExpression;
|
||||
|
||||
@Ignore
|
||||
public class PathMetadataTest {
|
||||
|
||||
@Before
|
||||
@ -36,7 +38,7 @@ public class PathMetadataTest {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
@Test
|
||||
public void test() throws Exception{
|
||||
Field field = ConstantImpl.class.getDeclaredField("STRINGS");
|
||||
field.setAccessible(true);
|
||||
|
||||
@ -126,12 +126,7 @@ public final class ColQuerySerializer extends SerializerBase<ColQuerySerializer>
|
||||
} else if (element.isAsString()) {
|
||||
append(args.get(element.getIndex()).toString());
|
||||
} else {
|
||||
Object elem = args.get(element.getIndex());
|
||||
if (elem instanceof Expression) {
|
||||
handle((Expression)elem);
|
||||
} else {
|
||||
visitConstant(elem);
|
||||
}
|
||||
handle(args.get(element.getIndex()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -120,29 +120,29 @@ public final class Expressions {
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> SimpleExpression<T> template(Class<T> cl, String template, Expression<?>... args) {
|
||||
public static <T> SimpleExpression<T> template(Class<T> cl, String template, Object... args) {
|
||||
return SimpleTemplate.create(cl, template, args);
|
||||
}
|
||||
|
||||
public static <T> DslExpression<T> dslTemplate(Class<T> cl, String template, Expression<?>... args) {
|
||||
public static <T> DslExpression<T> dslTemplate(Class<T> cl, String template, Object... args) {
|
||||
return DslTemplate.create(cl, template, args);
|
||||
}
|
||||
|
||||
public static <T extends Comparable<?>> ComparableExpression<T> comparableTemplate(Class<T> cl,
|
||||
String template, Expression<?>... args) {
|
||||
String template, Object... args) {
|
||||
return ComparableTemplate.create(cl, template, args);
|
||||
}
|
||||
|
||||
public static <T extends Number & Comparable<?>> NumberExpression<T> numberTemplate(Class<T> cl,
|
||||
String template, Expression<?>... args) {
|
||||
String template, Object... args) {
|
||||
return NumberTemplate.create(cl, template, args);
|
||||
}
|
||||
|
||||
public static StringExpression stringTemplate(String template, Expression<?>... args) {
|
||||
public static StringExpression stringTemplate(String template, Object... args) {
|
||||
return StringTemplate.create(template, args);
|
||||
}
|
||||
|
||||
public static BooleanExpression booleanTemplate(String template, Expression<?>... args) {
|
||||
public static BooleanExpression booleanTemplate(String template, Object... args) {
|
||||
return BooleanTemplate.create(template, args);
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,83 @@
|
||||
package com.mysema.query.support;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public final class Normalization {
|
||||
|
||||
private static final String WS = "\\s*";
|
||||
|
||||
private static final String START = "\\b";
|
||||
|
||||
private static final String NUMBER = "([+\\-]?\\d+\\.?\\d*)";
|
||||
|
||||
private static final Pattern OPERATOR = Pattern.compile(WS + "[+\\-/*]" + WS);
|
||||
|
||||
private static final Pattern OPERATION = Pattern.compile(START + NUMBER + OPERATOR.pattern() + NUMBER);
|
||||
|
||||
private static final Pattern ADDITION = Pattern.compile(START + NUMBER + WS + "\\+" + WS + NUMBER);
|
||||
|
||||
private static final Pattern SUBTRACTION = Pattern.compile(START + NUMBER + WS + "\\-" + WS + NUMBER);
|
||||
|
||||
private static final Pattern DIVISION = Pattern.compile(START + NUMBER + WS + "/" + WS + NUMBER);
|
||||
|
||||
private static final Pattern MULTIPLICATION = Pattern.compile(START + NUMBER + WS + "\\*" + WS + NUMBER);
|
||||
|
||||
public static final String normalize(String queryString) {
|
||||
StringBuilder rv = null;
|
||||
Matcher m = OPERATION.matcher(queryString);
|
||||
int end = 0;
|
||||
while (m.find()) {
|
||||
if (rv == null) {
|
||||
rv = new StringBuilder(queryString.length());
|
||||
}
|
||||
if (m.start() > end) {
|
||||
rv.append(queryString.subSequence(end, m.start()));
|
||||
}
|
||||
String str = queryString.substring(m.start(), m.end());
|
||||
boolean add = ADDITION.matcher(str).matches();
|
||||
boolean subtract = SUBTRACTION.matcher(str).matches();
|
||||
boolean divide = DIVISION.matcher(str).matches();
|
||||
boolean multiply = MULTIPLICATION.matcher(str).matches();
|
||||
Matcher matcher = OPERATION.matcher(str);
|
||||
matcher.matches();
|
||||
BigDecimal first = new BigDecimal(matcher.group(1));
|
||||
BigDecimal second = new BigDecimal(matcher.group(2));
|
||||
String result = null;
|
||||
if (multiply) {
|
||||
result = first.multiply(second).toString();
|
||||
} else if (divide) {
|
||||
result = first.divide(second, 10, RoundingMode.HALF_UP).toString();
|
||||
} else if (subtract) {
|
||||
result = first.subtract(second).toString();
|
||||
} else if (add) {
|
||||
result = first.add(second).toString();
|
||||
} else {
|
||||
throw new IllegalStateException("Unsupported expression " + str);
|
||||
}
|
||||
while (result.contains(".") && (result.endsWith("0") || result.endsWith("."))) {
|
||||
result = result.substring(0, result.length()-1);
|
||||
}
|
||||
rv.append(result);
|
||||
end = m.end();
|
||||
}
|
||||
if (end > 0) {
|
||||
if (end < queryString.length()) {
|
||||
rv.append(queryString.substring(end));
|
||||
}
|
||||
if (rv.toString().equals(queryString)) {
|
||||
return rv.toString();
|
||||
} else {
|
||||
return normalize(rv.toString());
|
||||
}
|
||||
} else {
|
||||
return queryString;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Normalization(){}
|
||||
|
||||
}
|
||||
@ -13,15 +13,11 @@
|
||||
*/
|
||||
package com.mysema.query.support;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.mysema.commons.lang.Assert;
|
||||
import com.mysema.query.JoinFlag;
|
||||
@ -48,25 +44,7 @@ import com.mysema.query.types.Visitor;
|
||||
public abstract class SerializerBase<S extends SerializerBase<S>> implements Visitor<Void,Void> {
|
||||
|
||||
private final StringBuilder builder = new StringBuilder();
|
||||
|
||||
private static final String START = "\\b";
|
||||
|
||||
private static final String NUMBER = "([+\\-]?\\d+\\.?\\d*)";
|
||||
|
||||
private static final String WS = "\\s*";
|
||||
|
||||
private static final Pattern OPERATOR = Pattern.compile(WS + "[+\\-/*]" + WS);
|
||||
|
||||
private static final Pattern OPERATION = Pattern.compile(START + NUMBER + OPERATOR.pattern() + NUMBER);
|
||||
|
||||
private static final Pattern ADDITION = Pattern.compile(START + NUMBER + WS + "\\+" + WS + NUMBER);
|
||||
|
||||
private static final Pattern SUBTRACTION = Pattern.compile(START + NUMBER + WS + "\\-" + WS + NUMBER);
|
||||
|
||||
private static final Pattern DIVISION = Pattern.compile(START + NUMBER + WS + "/" + WS + NUMBER);
|
||||
|
||||
private static final Pattern MULTIPLICATION = Pattern.compile(START + NUMBER + WS + "\\*" + WS + NUMBER);
|
||||
|
||||
|
||||
private String constantPrefix = "a";
|
||||
|
||||
private String paramPrefix = "p";
|
||||
@ -84,59 +62,6 @@ public abstract class SerializerBase<S extends SerializerBase<S>> implements Vis
|
||||
|
||||
private boolean normalize = true;
|
||||
|
||||
public static final String normalize(String queryString) {
|
||||
StringBuilder rv = null;
|
||||
Matcher m = OPERATION.matcher(queryString);
|
||||
int end = 0;
|
||||
while (m.find()) {
|
||||
if (rv == null) {
|
||||
rv = new StringBuilder(queryString.length());
|
||||
}
|
||||
if (m.start() > end) {
|
||||
rv.append(queryString.subSequence(end, m.start()));
|
||||
}
|
||||
String str = queryString.substring(m.start(), m.end());
|
||||
boolean add = ADDITION.matcher(str).matches();
|
||||
boolean subtract = SUBTRACTION.matcher(str).matches();
|
||||
boolean divide = DIVISION.matcher(str).matches();
|
||||
boolean multiply = MULTIPLICATION.matcher(str).matches();
|
||||
Matcher matcher = OPERATION.matcher(str);
|
||||
matcher.matches();
|
||||
BigDecimal first = new BigDecimal(matcher.group(1));
|
||||
BigDecimal second = new BigDecimal(matcher.group(2));
|
||||
String result = null;
|
||||
if (multiply) {
|
||||
result = first.multiply(second).toString();
|
||||
} else if (divide) {
|
||||
result = first.divide(second, 10, RoundingMode.HALF_UP).toString();
|
||||
} else if (subtract) {
|
||||
result = first.subtract(second).toString();
|
||||
} else if (add) {
|
||||
result = first.add(second).toString();
|
||||
} else {
|
||||
throw new IllegalStateException("Unsupported expression " + str);
|
||||
}
|
||||
while (result.contains(".") && (result.endsWith("0") || result.endsWith("."))) {
|
||||
result = result.substring(0, result.length()-1);
|
||||
}
|
||||
rv.append(result);
|
||||
end = m.end();
|
||||
}
|
||||
if (end > 0) {
|
||||
if (end < queryString.length()) {
|
||||
rv.append(queryString.substring(end));
|
||||
}
|
||||
if (rv.toString().equals(queryString)) {
|
||||
return rv.toString();
|
||||
} else {
|
||||
return normalize(rv.toString());
|
||||
}
|
||||
} else {
|
||||
return queryString;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public SerializerBase(Templates templates) {
|
||||
this(templates, false);
|
||||
}
|
||||
@ -181,6 +106,11 @@ public abstract class SerializerBase<S extends SerializerBase<S>> implements Vis
|
||||
return templates.getTemplate(op);
|
||||
}
|
||||
|
||||
public final S handle(Expression<?> expr) {
|
||||
expr.accept(this, null);
|
||||
return self;
|
||||
}
|
||||
|
||||
public final S handle(Object arg) {
|
||||
if (arg instanceof Expression) {
|
||||
((Expression)arg).accept(this, null);
|
||||
@ -215,12 +145,7 @@ public abstract class SerializerBase<S extends SerializerBase<S>> implements Vis
|
||||
} else if (element.hasConverter()) {
|
||||
handle(element.convert(args.get(element.getIndex())));
|
||||
} else {
|
||||
Object arg = args.get(element.getIndex());
|
||||
if (arg instanceof Expression) {
|
||||
handle((Expression)arg);
|
||||
} else {
|
||||
visitConstant(arg);
|
||||
}
|
||||
handle(args.get(element.getIndex()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -266,7 +191,7 @@ public abstract class SerializerBase<S extends SerializerBase<S>> implements Vis
|
||||
@Override
|
||||
public String toString() {
|
||||
if (normalize) {
|
||||
return normalize(builder.toString());
|
||||
return Normalization.normalize(builder.toString());
|
||||
} else {
|
||||
return builder.toString();
|
||||
}
|
||||
@ -360,7 +285,7 @@ public abstract class SerializerBase<S extends SerializerBase<S>> implements Vis
|
||||
if (element.hasConverter()) {
|
||||
handle(element.convert(arg));
|
||||
} else {
|
||||
handle((Expression)arg);
|
||||
handle(arg);
|
||||
}
|
||||
if (wrap) {
|
||||
append(")");
|
||||
|
||||
@ -84,10 +84,10 @@ public class Templates {
|
||||
add(Ops.EQ_IGNORE_CASE, "eqIc({0},{1})", 18);
|
||||
add(Ops.INSTANCE_OF, "{0}.class = {1}");
|
||||
add(Ops.NE, "{0} != {1}", 25);
|
||||
add(Ops.IN, "{0} in {1}");
|
||||
add(Ops.IN, "{0} in {1}", 27);
|
||||
add(Ops.IS_NULL, "{0} is null", 26);
|
||||
add(Ops.IS_NOT_NULL, "{0} is not null", 26);
|
||||
add(Ops.ALIAS, "{0} as {1}");
|
||||
add(Ops.ALIAS, "{0} as {1}", 0);
|
||||
|
||||
add(Ops.EXISTS, "exists({0})");
|
||||
|
||||
@ -114,8 +114,8 @@ public class Templates {
|
||||
add(Ops.INDEX_OF, "indexOf({0},{1})");
|
||||
add(Ops.INDEX_OF_2ARGS, "indexOf({0},{1},{2})");
|
||||
add(Ops.STRING_IS_EMPTY, "empty({0})");
|
||||
add(Ops.LIKE, "{0} like {1}");
|
||||
add(Ops.LIKE_ESCAPE, "{0} like {1} escape '{2s}'");
|
||||
add(Ops.LIKE, "{0} like {1}", 26);
|
||||
add(Ops.LIKE_ESCAPE, "{0} like {1} escape '{2s}'", 26);
|
||||
|
||||
add(Ops.StringOps.LTRIM, "ltrim({0})");
|
||||
add(Ops.StringOps.RTRIM, "rtrim({0})");
|
||||
|
||||
@ -34,11 +34,11 @@ public class BooleanTemplate extends BooleanExpression implements TemplateExpres
|
||||
private static final long serialVersionUID = 5749369427497731719L;
|
||||
|
||||
public static BooleanExpression create(String template, Object... args) {
|
||||
return new BooleanTemplate(TemplateFactory.DEFAULT.create(template), Arrays.<Object>asList(args));
|
||||
return new BooleanTemplate(TemplateFactory.DEFAULT.create(template), Arrays.asList(args));
|
||||
}
|
||||
|
||||
public static BooleanExpression create(Template template, Object... args) {
|
||||
return new BooleanTemplate(template, Arrays.<Object>asList(args));
|
||||
return new BooleanTemplate(template, Arrays.asList(args));
|
||||
}
|
||||
|
||||
public static final BooleanExpression TRUE = create("true");
|
||||
|
||||
@ -16,7 +16,6 @@ package com.mysema.query.types.template;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.Template;
|
||||
import com.mysema.query.types.TemplateExpression;
|
||||
import com.mysema.query.types.TemplateExpressionImpl;
|
||||
@ -35,17 +34,17 @@ public class ComparableTemplate<T extends Comparable<?>> extends ComparableExpre
|
||||
|
||||
private static final long serialVersionUID = -6292853402028813007L;
|
||||
|
||||
public static <T extends Comparable<?>> ComparableExpression<T> create(Class<T> type, String template, Expression<?>... args) {
|
||||
return new ComparableTemplate<T>(type, TemplateFactory.DEFAULT.create(template), Arrays.<Expression<?>>asList(args));
|
||||
public static <T extends Comparable<?>> ComparableExpression<T> create(Class<T> type, String template, Object... args) {
|
||||
return new ComparableTemplate<T>(type, TemplateFactory.DEFAULT.create(template), Arrays.asList(args));
|
||||
}
|
||||
|
||||
public static <T extends Comparable<?>> ComparableExpression<T> create(Class<T> type, Template template, Expression<?>... args) {
|
||||
return new ComparableTemplate<T>(type, template, Arrays.<Expression<?>>asList(args));
|
||||
public static <T extends Comparable<?>> ComparableExpression<T> create(Class<T> type, Template template, Object... args) {
|
||||
return new ComparableTemplate<T>(type, template, Arrays.asList(args));
|
||||
}
|
||||
|
||||
private final TemplateExpression<T> templateMixin;
|
||||
|
||||
public ComparableTemplate(Class<T> type, Template template, List<Expression<?>> args) {
|
||||
public ComparableTemplate(Class<T> type, Template template, List<?> args) {
|
||||
super(new TemplateExpressionImpl<T>(type, template, args));
|
||||
templateMixin = (TemplateExpression<T>)mixin;
|
||||
}
|
||||
|
||||
@ -35,7 +35,7 @@ public class DateTemplate<T extends Comparable<?>> extends DateExpression<T> imp
|
||||
private static final long serialVersionUID = 4975559746071238026L;
|
||||
|
||||
public static <T extends Comparable<?>> DateExpression<T> create(Class<T> type, String template, Object... args) {
|
||||
return new DateTemplate<T>(type, TemplateFactory.DEFAULT.create(template), Arrays.<Object>asList(args));
|
||||
return new DateTemplate<T>(type, TemplateFactory.DEFAULT.create(template), Arrays.asList(args));
|
||||
}
|
||||
|
||||
public static <T extends Comparable<?>> DateExpression<T> create(Class<T> type, Template template, Object... args) {
|
||||
|
||||
@ -16,7 +16,6 @@ package com.mysema.query.types.template;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.Template;
|
||||
import com.mysema.query.types.TemplateExpression;
|
||||
import com.mysema.query.types.TemplateExpressionImpl;
|
||||
@ -35,17 +34,17 @@ public class DateTimeTemplate<T extends Comparable<?>> extends DateTimeExpressio
|
||||
|
||||
private static final long serialVersionUID = -2289699666347576749L;
|
||||
|
||||
public static <T extends Comparable<?>> DateTimeExpression<T> create(Class<T> type, String template, Expression<?>... args) {
|
||||
return new DateTimeTemplate<T>(type, TemplateFactory.DEFAULT.create(template), Arrays.<Expression<?>>asList(args));
|
||||
public static <T extends Comparable<?>> DateTimeExpression<T> create(Class<T> type, String template, Object... args) {
|
||||
return new DateTimeTemplate<T>(type, TemplateFactory.DEFAULT.create(template), Arrays.asList(args));
|
||||
}
|
||||
|
||||
public static <T extends Comparable<?>> DateTimeExpression<T> create(Class<T> type, Template template, Expression<?>... args) {
|
||||
return new DateTimeTemplate<T>(type, template, Arrays.<Expression<?>>asList(args));
|
||||
public static <T extends Comparable<?>> DateTimeExpression<T> create(Class<T> type, Template template, Object... args) {
|
||||
return new DateTimeTemplate<T>(type, template, Arrays.asList(args));
|
||||
}
|
||||
|
||||
private final TemplateExpression<T> templateMixin;
|
||||
|
||||
public DateTimeTemplate(Class<T> type, Template template, List<Expression<?>> args) {
|
||||
public DateTimeTemplate(Class<T> type, Template template, List<?> args) {
|
||||
super(new TemplateExpressionImpl<T>(type, template, args));
|
||||
templateMixin = (TemplateExpression<T>)mixin;
|
||||
}
|
||||
|
||||
@ -16,7 +16,6 @@ package com.mysema.query.types.template;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.Template;
|
||||
import com.mysema.query.types.TemplateExpression;
|
||||
import com.mysema.query.types.TemplateExpressionImpl;
|
||||
@ -35,17 +34,17 @@ public class DslTemplate<T> extends DslExpression<T> implements TemplateExpressi
|
||||
|
||||
private static final long serialVersionUID = -4697578522909045745L;
|
||||
|
||||
public static <T> DslExpression<T> create(Class<? extends T> type, String template, Expression<?>... args) {
|
||||
return new DslTemplate<T>(type, TemplateFactory.DEFAULT.create(template), Arrays.<Expression<?>>asList(args));
|
||||
public static <T> DslExpression<T> create(Class<? extends T> type, String template, Object... args) {
|
||||
return new DslTemplate<T>(type, TemplateFactory.DEFAULT.create(template), Arrays.asList(args));
|
||||
}
|
||||
|
||||
public static <T> DslExpression<T> create(Class<? extends T> type, Template template, Expression<?>... args) {
|
||||
return new DslTemplate<T>(type, template, Arrays.<Expression<?>>asList(args));
|
||||
public static <T> DslExpression<T> create(Class<? extends T> type, Template template, Object... args) {
|
||||
return new DslTemplate<T>(type, template, Arrays.asList(args));
|
||||
}
|
||||
|
||||
private final TemplateExpression<T> templateMixin;
|
||||
|
||||
public DslTemplate(Class<? extends T> type, Template template, List<Expression<?>> args) {
|
||||
public DslTemplate(Class<? extends T> type, Template template, List<?> args) {
|
||||
super(new TemplateExpressionImpl<T>(type, template, args));
|
||||
templateMixin = (TemplateExpression<T>)mixin;
|
||||
}
|
||||
|
||||
@ -16,7 +16,6 @@ package com.mysema.query.types.template;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.Template;
|
||||
import com.mysema.query.types.TemplateExpression;
|
||||
import com.mysema.query.types.TemplateExpressionImpl;
|
||||
@ -35,17 +34,17 @@ public class EnumTemplate<T extends Enum<T>> extends EnumExpression<T> implement
|
||||
|
||||
private static final long serialVersionUID = 351057421752203377L;
|
||||
|
||||
public static <T extends Enum<T>> EnumExpression<T> create(Class<T> type, String template, Expression<?>... args) {
|
||||
return new EnumTemplate<T>(type, TemplateFactory.DEFAULT.create(template), Arrays.<Expression<?>>asList(args));
|
||||
public static <T extends Enum<T>> EnumExpression<T> create(Class<T> type, String template, Object... args) {
|
||||
return new EnumTemplate<T>(type, TemplateFactory.DEFAULT.create(template), Arrays.asList(args));
|
||||
}
|
||||
|
||||
public static <T extends Enum<T>> EnumExpression<T> create(Class<T> type, Template template, Expression<?>... args) {
|
||||
return new EnumTemplate<T>(type, template, Arrays.<Expression<?>>asList(args));
|
||||
public static <T extends Enum<T>> EnumExpression<T> create(Class<T> type, Template template, Object... args) {
|
||||
return new EnumTemplate<T>(type, template, Arrays.asList(args));
|
||||
}
|
||||
|
||||
private final TemplateExpression<T> templateMixin;
|
||||
|
||||
public EnumTemplate(Class<T> type, Template template, List<Expression<?>> args) {
|
||||
public EnumTemplate(Class<T> type, Template template, List<?> args) {
|
||||
super(new TemplateExpressionImpl<T>(type, template, args));
|
||||
templateMixin = (TemplateExpression<T>)mixin;
|
||||
}
|
||||
|
||||
@ -16,7 +16,6 @@ package com.mysema.query.types.template;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.Template;
|
||||
import com.mysema.query.types.TemplateExpression;
|
||||
import com.mysema.query.types.TemplateExpressionImpl;
|
||||
@ -35,12 +34,12 @@ public class NumberTemplate<T extends Number & Comparable<?>> extends NumberExpr
|
||||
|
||||
private static final long serialVersionUID = 351057421752203377L;
|
||||
|
||||
public static <T extends Number & Comparable<?>> NumberExpression<T> create(Class<T> type, String template, Expression<?>... args) {
|
||||
return new NumberTemplate<T>(type, TemplateFactory.DEFAULT.create(template), Arrays.<Expression<?>>asList(args));
|
||||
public static <T extends Number & Comparable<?>> NumberExpression<T> create(Class<T> type, String template, Object... args) {
|
||||
return new NumberTemplate<T>(type, TemplateFactory.DEFAULT.create(template), Arrays.asList(args));
|
||||
}
|
||||
|
||||
public static <T extends Number & Comparable<?>> NumberExpression<T> create(Class<T> type, Template template, Expression<?>... args) {
|
||||
return new NumberTemplate<T>(type, template, Arrays.<Expression<?>>asList(args));
|
||||
public static <T extends Number & Comparable<?>> NumberExpression<T> create(Class<T> type, Template template, Object... args) {
|
||||
return new NumberTemplate<T>(type, template, Arrays.asList(args));
|
||||
}
|
||||
|
||||
public static final NumberExpression<Integer> ONE = create(Integer.class, "1");
|
||||
@ -53,7 +52,7 @@ public class NumberTemplate<T extends Number & Comparable<?>> extends NumberExpr
|
||||
|
||||
private final TemplateExpression<T> templateMixin;
|
||||
|
||||
public NumberTemplate(Class<T> type, Template template, List<Expression<?>> args) {
|
||||
public NumberTemplate(Class<T> type, Template template, List<?> args) {
|
||||
super(new TemplateExpressionImpl<T>(type, template, args));
|
||||
templateMixin = (TemplateExpression<T>)mixin;
|
||||
}
|
||||
|
||||
@ -16,7 +16,6 @@ package com.mysema.query.types.template;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.Template;
|
||||
import com.mysema.query.types.TemplateExpression;
|
||||
import com.mysema.query.types.TemplateExpressionImpl;
|
||||
@ -35,17 +34,17 @@ public class SimpleTemplate<T> extends SimpleExpression<T> implements TemplateEx
|
||||
|
||||
private static final long serialVersionUID = -4697578522909045745L;
|
||||
|
||||
public static <T> SimpleExpression<T> create(Class<? extends T> type, String template, Expression<?>... args) {
|
||||
return new SimpleTemplate<T>(type, TemplateFactory.DEFAULT.create(template), Arrays.<Expression<?>>asList(args));
|
||||
public static <T> SimpleExpression<T> create(Class<? extends T> type, String template, Object... args) {
|
||||
return new SimpleTemplate<T>(type, TemplateFactory.DEFAULT.create(template), Arrays.asList(args));
|
||||
}
|
||||
|
||||
public static <T> SimpleExpression<T> create(Class<? extends T> type, Template template, Expression<?>... args) {
|
||||
return new SimpleTemplate<T>(type, template, Arrays.<Expression<?>>asList(args));
|
||||
public static <T> SimpleExpression<T> create(Class<? extends T> type, Template template, Object... args) {
|
||||
return new SimpleTemplate<T>(type, template, Arrays.asList(args));
|
||||
}
|
||||
|
||||
private final TemplateExpression<T> templateMixin;
|
||||
|
||||
public SimpleTemplate(Class<? extends T> type, Template template, List<Expression<?>> args) {
|
||||
public SimpleTemplate(Class<? extends T> type, Template template, List<?> args) {
|
||||
super(new TemplateExpressionImpl<T>(type, template, args));
|
||||
templateMixin = (TemplateExpression<T>)mixin;
|
||||
}
|
||||
|
||||
@ -16,7 +16,6 @@ package com.mysema.query.types.template;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.Template;
|
||||
import com.mysema.query.types.TemplateExpression;
|
||||
import com.mysema.query.types.TemplateExpressionImpl;
|
||||
@ -34,17 +33,17 @@ public class StringTemplate extends StringExpression implements TemplateExpressi
|
||||
|
||||
private static final long serialVersionUID = 3181686132439356614L;
|
||||
|
||||
public static StringExpression create(String template, Expression<?>... args) {
|
||||
return new StringTemplate(TemplateFactory.DEFAULT.create(template), Arrays.<Expression<?>>asList(args));
|
||||
public static StringExpression create(String template, Object... args) {
|
||||
return new StringTemplate(TemplateFactory.DEFAULT.create(template), Arrays.asList(args));
|
||||
}
|
||||
|
||||
public static StringExpression create(Template template, Expression<?>... args) {
|
||||
return new StringTemplate(template, Arrays.<Expression<?>>asList(args));
|
||||
public static StringExpression create(Template template, Object... args) {
|
||||
return new StringTemplate(template, Arrays.asList(args));
|
||||
}
|
||||
|
||||
private final TemplateExpression<String> templateMixin;
|
||||
|
||||
public StringTemplate(Template template, List<Expression<?>> args) {
|
||||
public StringTemplate(Template template, List<?> args) {
|
||||
super(new TemplateExpressionImpl<String>(String.class, template, args));
|
||||
this.templateMixin = (TemplateExpression<String>)mixin;
|
||||
}
|
||||
|
||||
@ -16,7 +16,6 @@ package com.mysema.query.types.template;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.Template;
|
||||
import com.mysema.query.types.TemplateExpression;
|
||||
import com.mysema.query.types.TemplateExpressionImpl;
|
||||
@ -35,17 +34,17 @@ public class TimeTemplate<T extends Comparable<?>> extends TimeExpression<T> imp
|
||||
|
||||
private static final long serialVersionUID = -7684306954555037051L;
|
||||
|
||||
public static <T extends Comparable<?>> TimeTemplate<T> create(Class<T> type, String template, Expression<?>... args) {
|
||||
return new TimeTemplate<T>(type, TemplateFactory.DEFAULT.create(template), Arrays.<Expression<?>>asList(args));
|
||||
public static <T extends Comparable<?>> TimeTemplate<T> create(Class<T> type, String template, Object... args) {
|
||||
return new TimeTemplate<T>(type, TemplateFactory.DEFAULT.create(template), Arrays.asList(args));
|
||||
}
|
||||
|
||||
public static <T extends Comparable<?>> TimeTemplate<T> create(Class<T> type, Template template, Expression<?>... args) {
|
||||
return new TimeTemplate<T>(type, template, Arrays.<Expression<?>>asList(args));
|
||||
public static <T extends Comparable<?>> TimeTemplate<T> create(Class<T> type, Template template, Object... args) {
|
||||
return new TimeTemplate<T>(type, template, Arrays.asList(args));
|
||||
}
|
||||
|
||||
private final TemplateExpression<T> templateMixin;
|
||||
|
||||
public TimeTemplate(Class<T> type, Template template, List<Expression<?>> args) {
|
||||
public TimeTemplate(Class<T> type, Template template, List<?> args) {
|
||||
super(new TemplateExpressionImpl<T>(type, template, args));
|
||||
templateMixin = (TemplateExpression<T>)mixin;
|
||||
}
|
||||
|
||||
@ -0,0 +1,57 @@
|
||||
package com.mysema.query.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class NormalizationTest {
|
||||
|
||||
@Test
|
||||
public void Variables() {
|
||||
assertEquals("var1 + 3", Normalization.normalize("var1 + 3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Normalize_Addition() {
|
||||
assertEquals("3", Normalization.normalize("1+2"));
|
||||
assertEquals("where 3 = 3", Normalization.normalize("where 1+2 = 3"));
|
||||
assertEquals("where 3.3 = 3.3", Normalization.normalize("where 1.1+2.2 = 3.3"));
|
||||
assertEquals("where 3.3 = 3.3", Normalization.normalize("where 1.1 + 2.2 = 3.3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Normalize_Subtraction() {
|
||||
assertEquals("3", Normalization.normalize("5-2"));
|
||||
assertEquals("where 3 = 3", Normalization.normalize("where 5-2 = 3"));
|
||||
assertEquals("where 3.3 = 3.3", Normalization.normalize("where 5.5-2.2 = 3.3"));
|
||||
assertEquals("where 3.3 = 3.3", Normalization.normalize("where 5.5 - 2.2 = 3.3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Normalize_Multiplication() {
|
||||
assertEquals("10", Normalization.normalize("5*2"));
|
||||
assertEquals("where 10 = 10", Normalization.normalize("where 5*2 = 10"));
|
||||
assertEquals("where 11 = 11", Normalization.normalize("where 5.5*2 = 11"));
|
||||
assertEquals("where 10.8 = 10.8", Normalization.normalize("where 5.4 * 2 = 10.8"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Normalize_Division() {
|
||||
assertEquals("2.5", Normalization.normalize("5/2"));
|
||||
assertEquals("where 2.5 = 2.5", Normalization.normalize("where 5/2 = 2.5"));
|
||||
assertEquals("where 2.6 = 2.6", Normalization.normalize("where 5.2/2 = 2.6"));
|
||||
assertEquals("where 2.6 = 2.6", Normalization.normalize("where 5.2 / 2 = 2.6"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Mixed() {
|
||||
assertEquals("13", Normalization.normalize("2 * 5 + 3"));
|
||||
assertEquals("-2.5", Normalization.normalize("2.5 * -1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void PI() {
|
||||
assertEquals("0.1591549431", Normalization.normalize("0.5 / " + Math.PI));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user