org.calrissian.mango.criteria.builder.QueryBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mango-criteria Show documentation
Show all versions of mango-criteria Show documentation
Utilties for working with boolean expression operations
The newest version!
/*
* Copyright (C) 2019 The Calrissian Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.calrissian.mango.criteria.builder;
import org.calrissian.mango.criteria.domain.*;
import java.util.Collection;
import static java.util.Arrays.asList;
public class QueryBuilder {
protected ParentNode current;
protected QueryBuilder parentBuilder;
protected boolean finished = false;
public static QueryBuilder create() {
return new QueryBuilder();
}
protected QueryBuilder() {}
protected QueryBuilder(ParentNode current, QueryBuilder parentBuilder) {
this.current = current;
this.parentBuilder = parentBuilder;
}
public QueryBuilder and() {
checkFinished();
AndNode andNode = new AndNode(current);
if (current != null) {
current.addChild(andNode);
return new QueryBuilder(andNode, this);
}
current = andNode;
return new QueryBuilder(andNode, null);
}
public QueryBuilder or() {
checkFinished();
OrNode orNode = new OrNode(current);
if (current != null) {
current.addChild(orNode);
return new QueryBuilder(orNode, this);
}
current = orNode;
return new QueryBuilder(orNode, null);
}
public Node build() {
if (!finished) throw new IllegalArgumentException("Query Node not built, end first");
return current;
}
public QueryBuilder eq(String type, Object value) {
checkFinished();
if (this.current == null) {
this.current = new AndNode();
finished = true;
}
EqualsLeaf equalsLeaf = new EqualsLeaf<>(type, value, current);
this.current.addChild(equalsLeaf);
return this;
}
protected void checkFinished() {
if (finished)
throw new IllegalArgumentException("Builder finished. Call build() to get constructed Query Node");
}
public QueryBuilder has(String key) {
checkFinished();
if (this.current == null) {
this.current = new AndNode();
finished = true;
}
HasLeaf hasKeyLeaf = new HasLeaf(key, current);
this.current.addChild(hasKeyLeaf);
return this;
}
public QueryBuilder hasNot(String key) {
checkFinished();
if (this.current == null) {
this.current = new AndNode();
finished = true;
}
HasNotLeaf hasNotLeaf = new HasNotLeaf(key, current);
this.current.addChild(hasNotLeaf);
return this;
}
public QueryBuilder in(String key, Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy