Skip to content

Commit

Permalink
Merge branch 'release/1.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
paul.rutledge committed Mar 15, 2016
2 parents b7623af + b391bbc commit 57b1f03
Show file tree
Hide file tree
Showing 36 changed files with 550 additions and 109 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>

<artifactId>q-builders</artifactId>
<version>1.2</version>
<version>1.3</version>

<name>q-builders</name>
<url>http://github.com/rutledgepaulv/q-builders</url>
Expand All @@ -32,6 +32,12 @@

<dependencies>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.rutledgepaulv.qbuilders.builders;

import com.github.rutledgepaulv.qbuilders.conditions.Condition;
import com.github.rutledgepaulv.qbuilders.nodes.ComparisonNode;

public class GeneralQueryBuilder extends QBuilder<GeneralQueryBuilder> {

public Condition<GeneralQueryBuilder> passThrough(ComparisonNode node) {
return condition(node.getField(), node.getOperator(), node.getValues());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import com.github.rutledgepaulv.qbuilders.operators.ComparisonOperator;
import com.github.rutledgepaulv.qbuilders.properties.concrete.*;
import com.github.rutledgepaulv.qbuilders.properties.virtual.Property;
import com.github.rutledgepaulv.qbuilders.structures.FieldPath;
import com.github.rutledgepaulv.qbuilders.utilities.ObjectUtils;
import com.github.rutledgepaulv.qbuilders.visitors.NodeVisitor;
import com.github.rutledgepaulv.qbuilders.visitors.ContextualNodeVisitor;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -29,8 +30,8 @@
@SuppressWarnings("unchecked")
public class QBuilder<T extends QBuilder<T>> implements Partial<T> {

private LogicalNode root;
private LogicalNode current;
protected LogicalNode root;
protected LogicalNode current;

public QBuilder() {
root = current = new OrNode();
Expand Down Expand Up @@ -81,7 +82,7 @@ protected final <S extends PropertyDelegate<T>, Q extends Property<T>> Q prop(St
throw new IllegalArgumentException("Must provide a delegate that implements the interface to be returned.");
}

return (Q) ObjectUtils.init(delegate, field, self());
return (Q) ObjectUtils.init(delegate, new FieldPath(field), self());
}

@SafeVarargs
Expand Down Expand Up @@ -110,11 +111,11 @@ public final Condition<T> or(List<Condition<T>> conditions) {
private <S extends LogicalNode> Condition<T> combine(List<Condition<T>> conditions, Class<S> type) {

List<AbstractNode> children = conditions.stream()
.map(condition -> ((QBuilder<T>)((QBuilder<T>) condition).self()).current)
.map(condition -> ((QBuilder<T>) condition).self().current)
.collect(Collectors.toList());

S node = ObjectUtils.init(type, ((QBuilder<T>)self()).current, children);
((QBuilder<T>)self()).current.getChildren().add(node);
S node = ObjectUtils.init(type, self().current, children);
self().current.getChildren().add(node);

return new ConditionDelegate(self());
}
Expand All @@ -128,14 +129,14 @@ private <S extends LogicalNode> Condition<T> combine(List<Condition<T>> conditio
*
* @return A completed {@link Condition} that can be built into a query or logically composed into other conditions.
*/
protected final Condition<T> condition(String field, ComparisonOperator operator, Collection<?> values) {
ComparisonNode node = new ComparisonNode(((QBuilder<T>)self()).current);
protected final Condition<T> condition(FieldPath field, ComparisonOperator operator, Collection<?> values) {
ComparisonNode node = new ComparisonNode(self().current);

node.setField(field);
node.setOperator(operator);
node.setValues(values);

((QBuilder<T>)self()).current.getChildren().add(node);
self().current.getChildren().add(node);
return new ConditionDelegate(self());
}

Expand All @@ -158,7 +159,7 @@ protected T self() {
* condition can either be directly built into a query or it can be composed with other conditions in
* the form of 'AND' or 'OR'
*/
private final class ConditionDelegate extends Delegate<T> implements Condition<T> {
protected final class ConditionDelegate extends Delegate<T> implements Condition<T> {

private ConditionDelegate(T canonical) {
super(canonical);
Expand Down Expand Up @@ -204,11 +205,19 @@ public final T or() {
return (T) self;
}

public final <Q> Q query(NodeVisitor<Q> visitor) {
public final <Q> Q query(ContextualNodeVisitor<Q,Void> visitor) {
QBuilder<T> self = self();
return self.root.visit(visitor);
}

public final <Q,S> Q query(ContextualNodeVisitor<Q,S> visitor, S context) {
QBuilder<T> self = self();
return self.root.visit(visitor, context);
}

public final LogicalNode getRootNode() {
return self().root;
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.github.rutledgepaulv.qbuilders.nodes.AndNode;
import com.github.rutledgepaulv.qbuilders.nodes.ComparisonNode;
import com.github.rutledgepaulv.qbuilders.nodes.OrNode;
import com.github.rutledgepaulv.qbuilders.visitors.NodeVisitor;
import com.github.rutledgepaulv.qbuilders.visitors.ContextualNodeVisitor;

import java.util.List;

Expand All @@ -14,8 +14,6 @@
* that can be executed against a set of objects to determine those things
* which satisfy the criteria.
*
* Note that ANDs take precedence over ORs by default.
*
* @param <T> The final type of the builder, used for a fluid chaining interface.
*/
public interface Condition<T extends QBuilder<T>> {
Expand Down Expand Up @@ -51,10 +49,21 @@ public interface Condition<T extends QBuilder<T>> {
*
* @param visitor The visitor which specifies how to traverse the nodes in the visitor tree.
* Nodes can be {@link AndNode}s or {@link OrNode}s or {@link ComparisonNode}s.
* @param <Q> The type of the results returned from visiting any node in the tree.
* @return The result of the visitor's execution.
*/
<Q> Q query(ContextualNodeVisitor<Q, Void> visitor);

/**
* Given this logically complete condition, execute a node visitor against the
* underlying condition tree in order to build a query or predicate against which
* objects can be queried / tested.
*
* @param <Q> The type of the results returned from visiting any node in the tree.
*
* @param visitor The visitor which specifies how to traverse the nodes in the visitor tree.
* Nodes can be {@link AndNode}s or {@link OrNode}s or {@link ComparisonNode}s.
* @param <Q> The type of the results returned from visiting any node in the tree.
* @return The result of the visitor's execution.
*/
<Q> Q query(NodeVisitor<Q> visitor);
<Q, S> Q query(ContextualNodeVisitor<Q, S> visitor, S context);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import com.github.rutledgepaulv.qbuilders.delegates.virtual.ExistentialPropertyDelegate;
import com.github.rutledgepaulv.qbuilders.operators.ComparisonOperator;
import com.github.rutledgepaulv.qbuilders.properties.concrete.BooleanProperty;
import com.github.rutledgepaulv.qbuilders.structures.FieldPath;

import java.util.Collections;

public final class BooleanPropertyDelegate<T extends QBuilder<T>> extends ExistentialPropertyDelegate<T> implements BooleanProperty<T> {

public BooleanPropertyDelegate(String field, T canonical) {
public BooleanPropertyDelegate(FieldPath field, T canonical) {
super(field, canonical);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,57 @@
import com.github.rutledgepaulv.qbuilders.builders.QBuilder;
import com.github.rutledgepaulv.qbuilders.conditions.Condition;
import com.github.rutledgepaulv.qbuilders.delegates.virtual.PropertyDelegate;
import com.github.rutledgepaulv.qbuilders.nodes.*;
import com.github.rutledgepaulv.qbuilders.operators.ComparisonOperator;
import com.github.rutledgepaulv.qbuilders.properties.concrete.ConditionProperty;
import com.github.rutledgepaulv.qbuilders.structures.FieldPath;
import com.github.rutledgepaulv.qbuilders.visitors.AbstractVoidContextNodeVisitor;

import java.util.Collections;

public final class ConditionPropertyDelegate<T extends QBuilder<T>, S extends QBuilder<S>>
extends PropertyDelegate<T> implements ConditionProperty<T, S> {

public ConditionPropertyDelegate(String field, T canonical) {
public ConditionPropertyDelegate(FieldPath field, T canonical) {
super(field, canonical);
}

@Override
public Condition<T> any(Condition<S> condition) {

final LogicalNode root = ((ConditionDelegate) condition).getRootNode();

// prepend this field to all of the fields in the subtree
root.visit(new NamespacingVisitor(getField()));

return condition(getField(), ComparisonOperator.SUB_CONDITION_ANY, Collections.singleton(condition));
}

private class NamespacingVisitor extends AbstractVoidContextNodeVisitor<Void> {

private FieldPath parent;

public NamespacingVisitor(FieldPath parent) {
this.parent = parent;
}

@Override
protected Void visit(AndNode node) {
node.getChildren().stream().forEach(this::visitAny);
return null;
}

@Override
protected Void visit(OrNode node) {
node.getChildren().stream().forEach(this::visitAny);
return null;
}

@Override
protected Void visit(ComparisonNode node) {
node.setField(node.getField().prepend(parent));
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.github.rutledgepaulv.qbuilders.builders.QBuilder;
import com.github.rutledgepaulv.qbuilders.delegates.virtual.NumberPropertyDelegate;
import com.github.rutledgepaulv.qbuilders.properties.concrete.DoubleProperty;
import com.github.rutledgepaulv.qbuilders.structures.FieldPath;

public final class DoublePropertyDelegate<T extends QBuilder<T>> extends NumberPropertyDelegate<T, Double> implements DoubleProperty<T> {

public DoublePropertyDelegate(String field, T canonical) {
public DoublePropertyDelegate(FieldPath field, T canonical) {
super(field, canonical);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import com.github.rutledgepaulv.qbuilders.builders.QBuilder;
import com.github.rutledgepaulv.qbuilders.delegates.virtual.ListablePropertyDelegate;
import com.github.rutledgepaulv.qbuilders.properties.concrete.EnumProperty;
import com.github.rutledgepaulv.qbuilders.structures.FieldPath;

public final class EnumPropertyDelegate<T extends QBuilder<T>, S extends Enum<S>>
extends ListablePropertyDelegate<T,S> implements EnumProperty<T,S> {

public EnumPropertyDelegate(String field, T canonical) {
public EnumPropertyDelegate(FieldPath field, T canonical) {
super(field, canonical);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.github.rutledgepaulv.qbuilders.builders.QBuilder;
import com.github.rutledgepaulv.qbuilders.delegates.virtual.NumberPropertyDelegate;
import com.github.rutledgepaulv.qbuilders.properties.concrete.FloatProperty;
import com.github.rutledgepaulv.qbuilders.structures.FieldPath;

public final class FloatPropertyDelegate<T extends QBuilder<T>> extends NumberPropertyDelegate<T, Float> implements FloatProperty<T> {

public FloatPropertyDelegate(String field, T canonical) {
public FloatPropertyDelegate(FieldPath field, T canonical) {
super(field, canonical);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import com.github.rutledgepaulv.qbuilders.builders.QBuilder;
import com.github.rutledgepaulv.qbuilders.delegates.virtual.InstantLikePropertyDelegate;
import com.github.rutledgepaulv.qbuilders.properties.concrete.InstantProperty;
import com.github.rutledgepaulv.qbuilders.structures.FieldPath;

import java.time.Instant;

public final class InstantPropertyDelegate<T extends QBuilder<T>>
extends InstantLikePropertyDelegate<T, Instant> implements InstantProperty<T> {

public InstantPropertyDelegate(String field, T canonical) {
public InstantPropertyDelegate(FieldPath field, T canonical) {
super(field, canonical);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import com.github.rutledgepaulv.qbuilders.builders.QBuilder;
import com.github.rutledgepaulv.qbuilders.delegates.virtual.NumberPropertyDelegate;
import com.github.rutledgepaulv.qbuilders.properties.concrete.IntegerProperty;
import com.github.rutledgepaulv.qbuilders.structures.FieldPath;

public final class IntegerPropertyDelegate<T extends QBuilder<T>>
extends NumberPropertyDelegate<T, Integer> implements IntegerProperty<T> {

public IntegerPropertyDelegate(String field, T canonical) {
public IntegerPropertyDelegate(FieldPath field, T canonical) {
super(field, canonical);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.github.rutledgepaulv.qbuilders.builders.QBuilder;
import com.github.rutledgepaulv.qbuilders.delegates.virtual.NumberPropertyDelegate;
import com.github.rutledgepaulv.qbuilders.properties.concrete.LongProperty;
import com.github.rutledgepaulv.qbuilders.structures.FieldPath;

public final class LongPropertyDelegate<T extends QBuilder<T>> extends NumberPropertyDelegate<T, Long> implements LongProperty<T> {

public LongPropertyDelegate(String field, T canonical) {
public LongPropertyDelegate(FieldPath field, T canonical) {
super(field, canonical);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import com.github.rutledgepaulv.qbuilders.builders.QBuilder;
import com.github.rutledgepaulv.qbuilders.delegates.virtual.NumberPropertyDelegate;
import com.github.rutledgepaulv.qbuilders.properties.concrete.ShortProperty;
import com.github.rutledgepaulv.qbuilders.structures.FieldPath;

public final class ShortPropertyDelegate<T extends QBuilder<T>>
extends NumberPropertyDelegate<T, Short> implements ShortProperty<T> {

public ShortPropertyDelegate(String field, T canonical) {
public ShortPropertyDelegate(FieldPath field, T canonical) {
super(field, canonical);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import com.github.rutledgepaulv.qbuilders.delegates.virtual.ListablePropertyDelegate;
import com.github.rutledgepaulv.qbuilders.operators.ComparisonOperator;
import com.github.rutledgepaulv.qbuilders.properties.concrete.StringProperty;
import com.github.rutledgepaulv.qbuilders.structures.FieldPath;

import java.util.Collections;

public final class StringPropertyDelegate<T extends QBuilder<T>>
extends ListablePropertyDelegate<T, String> implements StringProperty<T> {

public StringPropertyDelegate(String field, T canonical) {
public StringPropertyDelegate(FieldPath field, T canonical) {
super(field, canonical);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import com.github.rutledgepaulv.qbuilders.conditions.Condition;
import com.github.rutledgepaulv.qbuilders.operators.ComparisonOperator;
import com.github.rutledgepaulv.qbuilders.properties.virtual.EquitableProperty;
import com.github.rutledgepaulv.qbuilders.structures.FieldPath;

import java.util.Collections;

public abstract class EquitablePropertyDelegate<T extends QBuilder<T>, S>
extends ExistentialPropertyDelegate<T> implements EquitableProperty<T,S> {

protected EquitablePropertyDelegate(String field, T canonical) {
protected EquitablePropertyDelegate(FieldPath field, T canonical) {
super(field, canonical);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import com.github.rutledgepaulv.qbuilders.conditions.Condition;
import com.github.rutledgepaulv.qbuilders.operators.ComparisonOperator;
import com.github.rutledgepaulv.qbuilders.properties.virtual.ExistentialProperty;
import com.github.rutledgepaulv.qbuilders.structures.FieldPath;

import java.util.Collections;

public abstract class ExistentialPropertyDelegate<T extends QBuilder<T>> extends PropertyDelegate<T>
implements ExistentialProperty<T> {

protected ExistentialPropertyDelegate(String field, T canonical) {
protected ExistentialPropertyDelegate(FieldPath field, T canonical) {
super(field, canonical);
}

Expand Down
Loading

0 comments on commit 57b1f03

Please sign in to comment.