mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-27 21:01:15 +08:00
renamed CascadingBoolean to BooleanBuilder
This commit is contained in:
parent
1fda1b83c7
commit
66fba4a0b2
109
querydsl-core/src/main/java/com/mysema/query/BooleanBuilder.java
Normal file
109
querydsl-core/src/main/java/com/mysema/query/BooleanBuilder.java
Normal file
@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.mysema.query.types.Visitor;
|
||||
import com.mysema.query.types.expr.EBoolean;
|
||||
|
||||
/**
|
||||
* CascadingBoolean is a cascading builder for Boolean expressions.
|
||||
*
|
||||
* @author tiwe
|
||||
* @version $Id$
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class BooleanBuilder extends EBoolean{
|
||||
|
||||
@Nullable
|
||||
private EBoolean expr;
|
||||
|
||||
@Override
|
||||
public BooleanBuilder and(EBoolean right) {
|
||||
if (expr == null){
|
||||
expr = right;
|
||||
}else{
|
||||
expr = expr.and(right);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the intersection of this and the union of the given args
|
||||
* <p>(this && (arg1 || arg2 ... || argN))</p>
|
||||
*
|
||||
* @param args
|
||||
* @return
|
||||
*/
|
||||
public BooleanBuilder andAnyOf(EBoolean... args) {
|
||||
if (args.length > 0){
|
||||
EBoolean any = args[0];
|
||||
for (int i = 1; i < args.length; i++){
|
||||
any = any.or(args[i]);
|
||||
}
|
||||
and(any);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public BooleanBuilder not(EBoolean right) {
|
||||
return and(right.not());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BooleanBuilder or(EBoolean right) {
|
||||
if (expr == null){
|
||||
expr = right;
|
||||
}else{
|
||||
expr = expr.or(right);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the union of this and the intersection of the given args
|
||||
* <p>(this || (arg1 && arg2 ... && argN))</p>
|
||||
*
|
||||
* @param args
|
||||
* @return
|
||||
*/
|
||||
public BooleanBuilder orAllOf(EBoolean... args) {
|
||||
if (args.length > 0){
|
||||
EBoolean all = args[0];
|
||||
for (int i = 1; i < args.length; i++){
|
||||
all = all.and(args[i]);
|
||||
}
|
||||
or(all);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BooleanBuilder not(){
|
||||
expr = expr.not();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the value is set, and false, if not
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean hasValue(){
|
||||
return expr != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(Visitor v) {
|
||||
if (expr != null){
|
||||
expr.accept(v);
|
||||
}else{
|
||||
throw new RuntimeException("CascadingBoolean has no value");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,109 +1,11 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.mysema.query.types.Visitor;
|
||||
import com.mysema.query.types.expr.EBoolean;
|
||||
|
||||
/**
|
||||
* CascadingBoolean is a cascading builder for Boolean expressions.
|
||||
*
|
||||
* @author tiwe
|
||||
* @version $Id$
|
||||
* Use BooleanBuilder instead
|
||||
*
|
||||
*/
|
||||
@Deprecated
|
||||
@SuppressWarnings("serial")
|
||||
public class CascadingBoolean extends EBoolean{
|
||||
|
||||
@Nullable
|
||||
private EBoolean expr;
|
||||
|
||||
@Override
|
||||
public CascadingBoolean and(EBoolean right) {
|
||||
if (expr == null){
|
||||
expr = right;
|
||||
}else{
|
||||
expr = expr.and(right);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public class CascadingBoolean extends BooleanBuilder{
|
||||
|
||||
/**
|
||||
* Create the intersection of this and the union of the given args
|
||||
* <p>(this && (arg1 || arg2 ... || argN))</p>
|
||||
*
|
||||
* @param args
|
||||
* @return
|
||||
*/
|
||||
public CascadingBoolean andAnyOf(EBoolean... args) {
|
||||
if (args.length > 0){
|
||||
EBoolean any = args[0];
|
||||
for (int i = 1; i < args.length; i++){
|
||||
any = any.or(args[i]);
|
||||
}
|
||||
and(any);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public CascadingBoolean not(EBoolean right) {
|
||||
return and(right.not());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CascadingBoolean or(EBoolean right) {
|
||||
if (expr == null){
|
||||
expr = right;
|
||||
}else{
|
||||
expr = expr.or(right);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the union of this and the intersection of the given args
|
||||
* <p>(this || (arg1 && arg2 ... && argN))</p>
|
||||
*
|
||||
* @param args
|
||||
* @return
|
||||
*/
|
||||
public CascadingBoolean orAllOf(EBoolean... args) {
|
||||
if (args.length > 0){
|
||||
EBoolean all = args[0];
|
||||
for (int i = 1; i < args.length; i++){
|
||||
all = all.and(args[i]);
|
||||
}
|
||||
or(all);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CascadingBoolean not(){
|
||||
expr = expr.not();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the value is set, and false, if not
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean hasValue(){
|
||||
return expr != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(Visitor v) {
|
||||
if (expr != null){
|
||||
expr.accept(v);
|
||||
}else{
|
||||
throw new RuntimeException("CascadingBoolean has no value");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -33,7 +33,7 @@ public class DefaultQueryMetadata implements QueryMetadata {
|
||||
|
||||
private final List<Expr<?>> groupBy = new ArrayList<Expr<?>>();
|
||||
|
||||
private final CascadingBoolean having = new CascadingBoolean();
|
||||
private final BooleanBuilder having = new BooleanBuilder();
|
||||
|
||||
private final List<JoinExpression> joins = new ArrayList<JoinExpression>();
|
||||
|
||||
@ -46,29 +46,28 @@ public class DefaultQueryMetadata implements QueryMetadata {
|
||||
|
||||
private boolean unique;
|
||||
|
||||
private final CascadingBoolean where = new CascadingBoolean();
|
||||
private final BooleanBuilder where = new BooleanBuilder();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void addFrom(Expr<?>... args) {
|
||||
for (Expr<?> arg : args) {
|
||||
addJoinElement(arg);
|
||||
if (arg instanceof Path){
|
||||
ensureRoot((Path<?>) arg);
|
||||
}
|
||||
if (!exprInJoins.contains(arg)) {
|
||||
joins.add(new JoinExpression(JoinType.DEFAULT, arg));
|
||||
exprInJoins.add(arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void addJoinElement(Expr<?> expr){
|
||||
if (expr instanceof Path){
|
||||
Path<?> path = (Path<?>)expr;
|
||||
if (path.getMetadata().getParent() != null){
|
||||
throw new IllegalArgumentException("Only root paths are allowed for from : " + path);
|
||||
}
|
||||
private void ensureRoot(Path<?> path){
|
||||
if (path.getMetadata().getParent() != null){
|
||||
throw new IllegalArgumentException("Only root paths are allowed for joins : " + path);
|
||||
}
|
||||
if (!exprInJoins.contains(expr)) {
|
||||
joins.add(new JoinExpression(JoinType.DEFAULT, expr));
|
||||
exprInJoins.add(expr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void addGroupBy(Expr<?>... o) {
|
||||
groupBy.addAll(Arrays.<Expr<?>> asList(o));
|
||||
@ -81,17 +80,14 @@ public class DefaultQueryMetadata implements QueryMetadata {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addJoin(JoinExpression joinExpression) {
|
||||
if (!exprInJoins.contains(joinExpression.getTarget())) {
|
||||
joins.add(joinExpression);
|
||||
exprInJoins.add(joinExpression.getTarget());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void addJoin(JoinType joinType, Expr<?> expr) {
|
||||
if (!exprInJoins.contains(expr)) {
|
||||
if (expr instanceof Path){
|
||||
ensureRoot((Path<?>) expr);
|
||||
}
|
||||
joins.add(new JoinExpression(joinType, expr));
|
||||
exprInJoins.add(expr);
|
||||
}
|
||||
|
||||
@ -12,7 +12,6 @@ import javax.annotation.Nullable;
|
||||
import com.mysema.query.types.OrderSpecifier;
|
||||
import com.mysema.query.types.expr.EBoolean;
|
||||
import com.mysema.query.types.expr.Expr;
|
||||
import com.mysema.query.types.path.PEntity;
|
||||
|
||||
/**
|
||||
* QueryMetadata defines query metadata such as query sources, filtering
|
||||
@ -29,7 +28,7 @@ public interface QueryMetadata {
|
||||
|
||||
void addHaving(EBoolean... o);
|
||||
|
||||
void addJoin(JoinExpression joinExpression);
|
||||
// void addJoin(JoinExpression joinExpression);
|
||||
|
||||
void addJoin(JoinType joinType, Expr<?> expr);
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@ import com.mysema.query.types.expr.EBoolean;
|
||||
* @author tiwe
|
||||
* @version $Id$
|
||||
*/
|
||||
public class CascadingBooleanTest {
|
||||
public class BooleanBuilderTest {
|
||||
|
||||
private EBoolean first = EBoolean.TRUE;
|
||||
|
||||
@ -24,12 +24,12 @@ public class CascadingBooleanTest {
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
new CascadingBoolean().and(first).or(second);
|
||||
new BooleanBuilder().and(first).or(second);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void advanced(){
|
||||
CascadingBoolean builder = new CascadingBoolean();
|
||||
BooleanBuilder builder = new BooleanBuilder();
|
||||
builder.andAnyOf(first, second, first);
|
||||
builder.orAllOf(first, second, first);
|
||||
System.out.println(builder);
|
||||
@ -10,7 +10,7 @@ import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.mysema.query.CascadingBoolean;
|
||||
import com.mysema.query.BooleanBuilder;
|
||||
import com.mysema.query.JoinExpression;
|
||||
import com.mysema.query.JoinType;
|
||||
import com.mysema.query.QueryMetadata;
|
||||
@ -72,7 +72,7 @@ public abstract class HQLQueryBase<SubType extends HQLQueryBase<SubType>> extend
|
||||
|
||||
protected EBoolean createQBECondition(PEntity<?> entity,
|
||||
Map<String, Object> map) {
|
||||
CascadingBoolean expr = new CascadingBoolean();
|
||||
BooleanBuilder expr = new BooleanBuilder();
|
||||
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
||||
PathMetadata<String> md = PathMetadata.forProperty(entity, entry
|
||||
.getKey());
|
||||
|
||||
Loading…
Reference in New Issue
Block a user