mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-30 21:08:30 +08:00
added caching of Class constants
This commit is contained in:
parent
6db1042479
commit
97e9564ad6
@ -21,6 +21,8 @@ public interface ExprFactory {
|
||||
|
||||
Expr<Integer> createConstant(int i);
|
||||
|
||||
<A> Expr<Class<A>> createConstant(Class<A> obj);
|
||||
|
||||
<A> Expr<A> createConstant(A obj);
|
||||
|
||||
EBoolean createBoolean(Op<Boolean> operator, Expr<?>... args);
|
||||
|
||||
@ -5,6 +5,9 @@
|
||||
*/
|
||||
package com.mysema.query.grammar.types;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.mysema.query.grammar.Ops.Op;
|
||||
import com.mysema.query.grammar.types.Expr.EBoolean;
|
||||
import com.mysema.query.grammar.types.Expr.EComparable;
|
||||
@ -29,9 +32,7 @@ public class SimpleExprFactory implements ExprFactory{
|
||||
|
||||
private static final ExprFactory instance = new SimpleExprFactory();
|
||||
|
||||
public static ExprFactory getInstance(){
|
||||
return instance;
|
||||
}
|
||||
private final Map<Class<?>,Expr<?>> classToExpr = new HashMap<Class<?>,Expr<?>>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private final Expr<Integer>[] integers = new Expr[256];
|
||||
@ -42,12 +43,16 @@ public class SimpleExprFactory implements ExprFactory{
|
||||
}
|
||||
}
|
||||
|
||||
public Expr<Integer> createConstant(int i){
|
||||
if (i >= -128 && i <= 127) {
|
||||
return integers[i+128];
|
||||
}else{
|
||||
return new EConstant<Integer>(i);
|
||||
}
|
||||
public static ExprFactory getInstance(){
|
||||
return instance;
|
||||
}
|
||||
|
||||
public EBoolean createBoolean(Op<Boolean> operator, Expr<?>... args) {
|
||||
return new OBoolean(Assert.notNull(operator), Assert.notNull(args));
|
||||
}
|
||||
|
||||
public <OpType, RT extends Comparable<?>> EComparable<RT> createComparable(Class<RT> type, Op<OpType> operator, Expr<?>... args) {
|
||||
return new OComparable<OpType,RT>(type, Assert.notNull(operator), Assert.notNull(args));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@ -59,22 +64,33 @@ public class SimpleExprFactory implements ExprFactory{
|
||||
}
|
||||
}
|
||||
|
||||
public EBoolean createBoolean(Op<Boolean> operator, Expr<?>... args) {
|
||||
return new OBoolean(Assert.notNull(operator), Assert.notNull(args));
|
||||
@SuppressWarnings("unchecked")
|
||||
public <A> Expr<Class<A>> createConstant(Class<A> obj) {
|
||||
if (classToExpr.containsKey(obj)){
|
||||
return (Expr<Class<A>>) classToExpr.get(obj);
|
||||
}else{
|
||||
Expr<Class<A>> expr = new EConstant<Class<A>>(obj);
|
||||
classToExpr.put(obj, expr);
|
||||
return expr;
|
||||
}
|
||||
}
|
||||
|
||||
public <OpType, RT extends Comparable<?>> EComparable<RT> createComparable(Class<RT> type, Op<OpType> operator, Expr<?>... args) {
|
||||
return new OComparable<OpType,RT>(type, Assert.notNull(operator), Assert.notNull(args));
|
||||
public Expr<Integer> createConstant(int i){
|
||||
if (i >= -128 && i <= 127) {
|
||||
return integers[i+128];
|
||||
}else{
|
||||
return new EConstant<Integer>(i);
|
||||
}
|
||||
}
|
||||
|
||||
public <OpType extends Number,D extends Number & Comparable<?>> ENumber<D> createNumber(Class<? extends D> type, Op<OpType> operator, Expr<?>... args) {
|
||||
return new ONumber<OpType,D>(type, Assert.notNull(operator), Assert.notNull(args));
|
||||
}
|
||||
|
||||
|
||||
public EString createString(Op<String> operator, Expr<?>... args) {
|
||||
return new OString(Assert.notNull(operator), Assert.notNull(args));
|
||||
}
|
||||
|
||||
|
||||
public Expr<String[]> createStringArray(Op<String> operator, Expr<?>... args) {
|
||||
return new OStringArray(Assert.notNull(operator), Assert.notNull(args));
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user