Skip to content

Commit

Permalink
Replace Constraint(s) with a more generic Expression API.
Browse files Browse the repository at this point in the history
Expression API allows better evaluation of queries (eg. `field1 = field2`) previously impossible with Constraints. Also definition of new functions
doesn't require changing Visitor API.

This is work in progress.
  • Loading branch information
asereda-gs committed Mar 20, 2019
1 parent 05ecdd0 commit dbaf0cd
Show file tree
Hide file tree
Showing 20 changed files with 247 additions and 537 deletions.
4 changes: 3 additions & 1 deletion criteria/README.md
@@ -1,4 +1,6 @@
This module generates Criteria classes based on immutable model. This module generates Criteria classes based on immutable model.

The functionality is work in progress and API is not stable yet (use at your own risk).


Generated classes allow type-safe query building which can be evaluated on destination data-source. Generated classes allow type-safe query building which can be evaluated on destination data-source.


Expand Down
26 changes: 0 additions & 26 deletions criteria/src/org/immutables/criteria/Disjunction.java

This file was deleted.

Expand Up @@ -23,16 +23,16 @@
*/ */
public class BooleanCriteria<C extends DocumentCriteria<C, T>, T> extends ObjectCriteria<Boolean, C, T> { public class BooleanCriteria<C extends DocumentCriteria<C, T>, T> extends ObjectCriteria<Boolean, C, T> {


public BooleanCriteria(String name, Constraints.Constraint constraint, CriteriaCreator<C, T> creator) { public BooleanCriteria(Expression<T> expression, CriteriaCreator<C, T> creator) {
super(name, constraint, creator); super(expression, creator);
} }


public C isTrue() { public C isTrue() {
return creator.create(constraint.equal(name, false, Boolean.TRUE)); return creator.create(Expressions.<T>call(Operators.EQUAL, expression, Expressions.literal(Boolean.TRUE)));
} }


public C isFalse() { public C isFalse() {
return creator.create(constraint.equal(name, false, Boolean.FALSE)); return creator.create(Expressions.<T>call(Operators.EQUAL, expression, Expressions.literal(Boolean.FALSE)));
} }


} }
24 changes: 24 additions & 0 deletions criteria/src/org/immutables/criteria/constraints/Call.java
@@ -0,0 +1,24 @@
package org.immutables.criteria.constraints;

import java.util.List;

/**
* An expression formed by a call to an operator (eg. {@link Operators#EQUAL}) with zero or more arguments.
*/
public interface Call<T> extends Expression<T> {


/**
* Get the arguments of this operation
*
* @return arguments
*/
List<Expression<?>> getOperands();

/**
* Get the operator symbol for this operation
*
* @return operator
*/
Operator getOperator();
}
Expand Up @@ -16,7 +16,6 @@
package org.immutables.criteria.constraints; package org.immutables.criteria.constraints;




import com.google.common.collect.Range;
import org.immutables.criteria.DocumentCriteria; import org.immutables.criteria.DocumentCriteria;


/** /**
Expand All @@ -25,52 +24,38 @@
public class ComparableCriteria<V extends Comparable<V>, C extends DocumentCriteria<C, T>, T> public class ComparableCriteria<V extends Comparable<V>, C extends DocumentCriteria<C, T>, T>
extends ObjectCriteria<V, C, T> { extends ObjectCriteria<V, C, T> {


public ComparableCriteria(String name, Constraints.Constraint constraint, CriteriaCreator<C, T> creator) { public ComparableCriteria(Expression<T> expression, CriteriaCreator<C, T> creator) {
super(name, constraint, creator); super(expression, creator);
} }


/** /**]
* Checks that attribute is in {@code range}.
*/
public C isIn(Range<V> range) {
return creator.create(constraint.range(name, false, range));
}

/**
* Checks that attribute is <i>not</i> in {@code range}.
*/
public C isNotIn(Range<V> range) {
return creator.create(constraint.range(name, true, range));
}

/**
* Checks that attribute is less than (but not equal to) {@code upper}. * Checks that attribute is less than (but not equal to) {@code upper}.
* <p>Use {@link #isAtMost(Comparable)} for less <i>or equal</i> comparison</p> * <p>Use {@link #isAtMost(Comparable)} for less <i>or equal</i> comparison</p>
*/ */
public C isLessThan(V upper) { public C isLessThan(V upper) {
return isIn(Range.lessThan(upper)); return creator.create(Expressions.<T>call(Operators.LESS_THAN, expression, Expressions.literal(upper)));
} }


/** /**
* Checks that attribute is greater than (but not equal to) {@code lower}. * Checks that attribute is greater than (but not equal to) {@code lower}.
* <p>Use {@link #isAtLeast(Comparable)} for greater <i>or equal</i> comparison</p> * <p>Use {@link #isAtLeast(Comparable)} for greater <i>or equal</i> comparison</p>
*/ */
public C isGreaterThan(V lower) { public C isGreaterThan(V lower) {
return isIn(Range.greaterThan(lower)); return creator.create(Expressions.<T>call(Operators.GREATER_THAN, expression, Expressions.literal(lower)));
} }


/** /**
* Checks that attribute is less than or equal to {@code upperInclusive}. * Checks that attribute is less than or equal to {@code upperInclusive}.
*/ */
public C isAtMost(V upperInclusive) { public C isAtMost(V upperInclusive) {
return isIn(Range.atMost(upperInclusive)); return creator.create(Expressions.<T>call(Operators.LESS_THAN_OR_EQUAL, expression, Expressions.literal(upperInclusive)));
} }


/** /**
* Checks that attribute is greater or equal to {@code lowerInclusive}. * Checks that attribute is greater or equal to {@code lowerInclusive}.
*/ */
public C isAtLeast(V lowerInclusive) { public C isAtLeast(V lowerInclusive) {
return isIn(Range.atLeast(lowerInclusive)); return creator.create(Expressions.<T>call(Operators.GREATER_THAN_OR_EQUAL, expression, Expressions.literal(lowerInclusive)));
} }


} }
227 changes: 0 additions & 227 deletions criteria/src/org/immutables/criteria/constraints/Constraints.java

This file was deleted.

Expand Up @@ -18,10 +18,10 @@
import org.immutables.criteria.DocumentCriteria; import org.immutables.criteria.DocumentCriteria;


/** /**
* Creates document criteria from a constraint. * Creates document criteria from existing expression.
*/ */
public interface CriteriaCreator<C extends DocumentCriteria<C, T>, T> { public interface CriteriaCreator<C extends DocumentCriteria<C, T>, T> {


C create(Constraints.Constraint constraint); C create(Expression<T> expression);


} }

0 comments on commit dbaf0cd

Please sign in to comment.