Skip to content

Commit

Permalink
Criteria. Rename Criteria to Matcher (all classes except generated ones)
Browse files Browse the repository at this point in the history
  • Loading branch information
asereda-gs committed May 25, 2019
1 parent 3bc0e14 commit 0f64e8a
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 82 deletions.
Expand Up @@ -2,13 +2,10 @@


import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import org.immutables.criteria.constraints.CriteriaContext;


import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects;


/** /**
* A set of predefined utilities and factories for expressions like {@link Constant} or {@link Call} * A set of predefined utilities and factories for expressions like {@link Constant} or {@link Call}
Expand Down Expand Up @@ -54,30 +51,6 @@ private static Expression reduce(Operator operator, Iterable<? extends Expressi
return call(operator, expressions); return call(operator, expressions);
} }


/**
* Hacky (and temporary) reflection until we define proper sub-classes for criterias
* (to hide Expressional implementation).
*/
public static Expression extract(Object object) {
Objects.requireNonNull(object, "object");
try {
Class<?> current = object.getClass();
while(current.getSuperclass() != null){
if (Arrays.stream(current.getDeclaredFields()).anyMatch(f -> f.getName().equals("context"))) {
Field field = current.getDeclaredField("context");
field.setAccessible(true);
CriteriaContext<?> context = (CriteriaContext<?>) field.get(object);
return context.expression();
}
current = current.getSuperclass();
}
} catch (NoSuchFieldException|IllegalAccessException e) {
throw new RuntimeException("No field in " + object.getClass().getName(), e);
}

throw new UnsupportedOperationException("No field context found in " + object.getClass().getName());
}

/** /**
* Converts a {@link ExpressionVisitor} into a {@link ExpressionBiVisitor} (with ignored payload). * Converts a {@link ExpressionVisitor} into a {@link ExpressionBiVisitor} (with ignored payload).
*/ */
Expand Down
Expand Up @@ -13,18 +13,18 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package org.immutables.criteria.constraints; package org.immutables.criteria.matcher;




import org.immutables.criteria.expression.Expressions; import org.immutables.criteria.expression.Expressions;
import org.immutables.criteria.expression.Operators; import org.immutables.criteria.expression.Operators;


/** /**
* Very simple criteria for booleans just has {@code true} / {@code false} checks. * Very simple matcher for booleans just has {@code true} / {@code false} checks.
*/ */
public class BooleanCriteria<R> extends ObjectCriteria<R, Boolean> { public class BooleanMatcher<R> extends ObjectMatcher<R, Boolean> {


public BooleanCriteria(CriteriaContext<R> context) { public BooleanMatcher(CriteriaContext<R> context) {
super(context); super(context);
} }


Expand All @@ -36,8 +36,8 @@ public R isFalse() {
return create(e -> Expressions.call(Operators.EQUAL, e, Expressions.constant(Boolean.FALSE))); return create(e -> Expressions.call(Operators.EQUAL, e, Expressions.constant(Boolean.FALSE)));
} }


public static class Self extends BooleanCriteria<Self> { public static class Self extends BooleanMatcher<Self> {
public Self(CriteriaContext<BooleanCriteria.Self> context) { public Self(CriteriaContext<BooleanMatcher.Self> context) {
super(context); super(context);
} }
} }
Expand Down
@@ -1,4 +1,4 @@
package org.immutables.criteria.constraints; package org.immutables.criteria.matcher;


import org.immutables.criteria.DocumentCriteria; import org.immutables.criteria.DocumentCriteria;
import org.immutables.criteria.expression.Expression; import org.immutables.criteria.expression.Expression;
Expand All @@ -8,13 +8,13 @@
import java.util.Objects; import java.util.Objects;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;


public class CollectionCriteria<R, S, C> implements DocumentCriteria<R> { public class CollectionMatcher<R, S, C> implements DocumentCriteria<R> {


private final CriteriaContext<R> context; private final CriteriaContext<R> context;
private final CriteriaCreator<S> inner; private final CriteriaCreator<S> inner;
private final CriteriaCreator<C> outer; private final CriteriaCreator<C> outer;


public CollectionCriteria(CriteriaContext<R> context, CriteriaCreator<S> inner, CriteriaCreator<C> outer) { public CollectionMatcher(CriteriaContext<R> context, CriteriaCreator<S> inner, CriteriaCreator<C> outer) {
this.context = Objects.requireNonNull(context, "context"); this.context = Objects.requireNonNull(context, "context");
this.inner = Objects.requireNonNull(inner, "inner"); this.inner = Objects.requireNonNull(inner, "inner");
this.outer = Objects.requireNonNull(outer, "outer"); this.outer = Objects.requireNonNull(outer, "outer");
Expand Down Expand Up @@ -70,11 +70,11 @@ private UnaryOperator<Expression> toExpressionOperator(UnaryOperator<C> operator
return expression -> { return expression -> {
final C initial = context.withCreator(outer).create(); final C initial = context.withCreator(outer).create();
final C changed = operator.apply(initial); final C changed = operator.apply(initial);
return Expressions.extract(changed); return Matchers.extract(changed);
}; };
} }


public static class Self extends CollectionCriteria<Self, Self, Self> { public static class Self extends CollectionMatcher<Self, Self, Self> {
public Self(CriteriaContext<Self> context) { public Self(CriteriaContext<Self> context) {
super(context, Self::new, Self::new); super(context, Self::new, Self::new);
} }
Expand Down
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package org.immutables.criteria.constraints; package org.immutables.criteria.matcher;




import org.immutables.criteria.expression.Expressions; import org.immutables.criteria.expression.Expressions;
Expand All @@ -22,10 +22,10 @@
/** /**
* Criteria for comparables (like {@code >, <=, >} and ranges). * Criteria for comparables (like {@code >, <=, >} and ranges).
*/ */
public class ComparableCriteria<R, V extends Comparable<V>> public class ComparableMatcher<R, V extends Comparable<V>>
extends ObjectCriteria<R, V> { extends ObjectMatcher<R, V> {


public ComparableCriteria(CriteriaContext<R> context) { public ComparableMatcher(CriteriaContext<R> context) {
super(context); super(context);
} }


Expand Down Expand Up @@ -59,8 +59,8 @@ public R isAtLeast(V lowerInclusive) {
return create(e -> Expressions.call(Operators.GREATER_THAN_OR_EQUAL, e, Expressions.constant(lowerInclusive))); return create(e -> Expressions.call(Operators.GREATER_THAN_OR_EQUAL, e, Expressions.constant(lowerInclusive)));
} }


public static class Self<V extends Comparable<V>> extends ComparableCriteria<Self<V>, V> { public static class Self<V extends Comparable<V>> extends ComparableMatcher<Self<V>, V> {
public Self(CriteriaContext<ComparableCriteria.Self<V>> context) { public Self(CriteriaContext<ComparableMatcher.Self<V>> context) {
super(context); super(context);
} }
} }
Expand Down
@@ -1,4 +1,4 @@
package org.immutables.criteria.constraints; package org.immutables.criteria.matcher;


import org.immutables.criteria.expression.DnfExpression; import org.immutables.criteria.expression.DnfExpression;
import org.immutables.criteria.expression.Expression; import org.immutables.criteria.expression.Expression;
Expand Down
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package org.immutables.criteria.constraints; package org.immutables.criteria.matcher;


/** /**
* Creates document criteria from existing expression. * Creates document criteria from existing expression.
Expand Down
@@ -1,4 +1,4 @@
package org.immutables.criteria.constraints; package org.immutables.criteria.matcher;


public interface Disjunction<R> { public interface Disjunction<R> {


Expand Down
35 changes: 35 additions & 0 deletions criteria/common/src/org/immutables/criteria/matcher/Matchers.java
@@ -0,0 +1,35 @@
package org.immutables.criteria.matcher;

import org.immutables.criteria.expression.Expression;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Objects;

public class Matchers {


/**
* Hacky (and temporary) reflection until we define proper sub-classes for criterias
* (to hide Expressional implementation).
*/
static Expression extract(Object object) {
Objects.requireNonNull(object, "object");
try {
Class<?> current = object.getClass();
while(current.getSuperclass() != null){
if (Arrays.stream(current.getDeclaredFields()).anyMatch(f -> f.getName().equals("context"))) {
Field field = current.getDeclaredField("context");
field.setAccessible(true);
CriteriaContext<?> context = (CriteriaContext<?>) field.get(object);
return context.expression();
}
current = current.getSuperclass();
}
} catch (NoSuchFieldException|IllegalAccessException e) {
throw new RuntimeException("No field in " + object.getClass().getName(), e);
}

throw new UnsupportedOperationException("No field context found in " + object.getClass().getName());
}
}
Expand Up @@ -14,7 +14,7 @@
limitations under the License. limitations under the License.
*/ */


package org.immutables.criteria.constraints; package org.immutables.criteria.matcher;


import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
Expand All @@ -34,11 +34,11 @@
* @param <V> attribute type for which criteria is applied * @param <V> attribute type for which criteria is applied
* @param <R> Criteria self-type, allowing {@code this}-returning methods to avoid needing subclassing * @param <R> Criteria self-type, allowing {@code this}-returning methods to avoid needing subclassing
*/ */
public class ObjectCriteria<R, V> implements DocumentCriteria<R> { public class ObjectMatcher<R, V> implements DocumentCriteria<R> {


protected final CriteriaContext<R> context; protected final CriteriaContext<R> context;


public ObjectCriteria(CriteriaContext<R> context) { public ObjectMatcher(CriteriaContext<R> context) {
this.context = Preconditions.checkNotNull(context, "context"); this.context = Preconditions.checkNotNull(context, "context");
} }


Expand Down Expand Up @@ -85,7 +85,7 @@ public R isNotIn(Iterable<? super V> values) {
return create(e -> Expressions.call(Operators.NOT_IN, e, Expressions.constant(ImmutableList.copyOf(values)))); return create(e -> Expressions.call(Operators.NOT_IN, e, Expressions.constant(ImmutableList.copyOf(values))));
} }


public static class Self<V> extends ObjectCriteria<Self<V>, V> { public static class Self<V> extends ObjectMatcher<Self<V>, V> {
public Self(CriteriaContext<Self<V>> context) { public Self(CriteriaContext<Self<V>> context) {
super(context); super(context);
} }
Expand Down
Expand Up @@ -14,7 +14,7 @@
limitations under the License. limitations under the License.
*/ */


package org.immutables.criteria.constraints; package org.immutables.criteria.matcher;


import org.immutables.criteria.expression.Expression; import org.immutables.criteria.expression.Expression;
import org.immutables.criteria.expression.Expressions; import org.immutables.criteria.expression.Expressions;
Expand All @@ -24,17 +24,17 @@
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;


/** /**
* Criteria for optional attributes. * Matcher for optional attributes
*/ */
public class OptionalCriteria<R, S, C> { public class OptionalMatcher<R, S, C> {


final CriteriaContext<R> context; final CriteriaContext<R> context;


private final CriteriaCreator<S> inner; private final CriteriaCreator<S> inner;


private final CriteriaCreator<C> outer; private final CriteriaCreator<C> outer;


public OptionalCriteria(CriteriaContext<R> context, CriteriaCreator<S> inner, CriteriaCreator<C> outer) { public OptionalMatcher(CriteriaContext<R> context, CriteriaCreator<S> inner, CriteriaCreator<C> outer) {
this.context = Objects.requireNonNull(context, "context"); this.context = Objects.requireNonNull(context, "context");
this.inner = Objects.requireNonNull(inner, "inner"); this.inner = Objects.requireNonNull(inner, "inner");
this.outer = Objects.requireNonNull(outer, "outer"); this.outer = Objects.requireNonNull(outer, "outer");
Expand All @@ -57,7 +57,7 @@ public R value(UnaryOperator<C> consumer) {
final UnaryOperator<Expression> fn = expression -> { final UnaryOperator<Expression> fn = expression -> {
final C initial = context.withCreator(outer).create(); final C initial = context.withCreator(outer).create();
final C changed = consumer.apply(initial); final C changed = consumer.apply(initial);
return Expressions.extract(changed); return Matchers.extract(changed);
}; };


return context.create(fn); return context.create(fn);
Expand Down
Expand Up @@ -14,17 +14,17 @@
limitations under the License. limitations under the License.
*/ */


package org.immutables.criteria.constraints; package org.immutables.criteria.matcher;


import org.immutables.criteria.expression.Expressions; import org.immutables.criteria.expression.Expressions;
import org.immutables.criteria.expression.Operators; import org.immutables.criteria.expression.Operators;


/** /**
* String specific criterias like {@code isAbsent}, {@code contains} etc. * String specific criterias like {@code isAbsent}, {@code contains} etc.
*/ */
public class StringCriteria<R> extends ComparableCriteria<R, String> { public class StringMatcher<R> extends ComparableMatcher<R, String> {


public StringCriteria(CriteriaContext<R> context) { public StringMatcher(CriteriaContext<R> context) {
super(context); super(context);
} }


Expand Down Expand Up @@ -52,13 +52,13 @@ public R hasSize(int size) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }


public static class Self extends StringCriteria<Self> implements Disjunction<StringCriteria<Self>> { public static class Self extends StringMatcher<Self> implements Disjunction<StringMatcher<Self>> {
public Self(CriteriaContext<StringCriteria.Self> context) { public Self(CriteriaContext<StringMatcher.Self> context) {
super(context); super(context);
} }


@Override @Override
public StringCriteria<StringCriteria.Self> or() { public StringMatcher<StringMatcher.Self> or() {
return context.or().create(); return context.or().create();
} }
} }
Expand Down
Expand Up @@ -35,15 +35,15 @@ package [type.package];
[/if] [/if]


import org.immutables.criteria.DocumentCriteria; import org.immutables.criteria.DocumentCriteria;
import org.immutables.criteria.constraints.CriteriaContext; import org.immutables.criteria.matcher.CriteriaContext;
import org.immutables.criteria.constraints.CriteriaCreator; import org.immutables.criteria.matcher.CriteriaCreator;
import org.immutables.criteria.constraints.CollectionCriteria; import org.immutables.criteria.matcher.CollectionMatcher;
import org.immutables.criteria.constraints.Disjunction; import org.immutables.criteria.matcher.Disjunction;
import org.immutables.criteria.constraints.OptionalCriteria; import org.immutables.criteria.matcher.OptionalMatcher;
import org.immutables.criteria.constraints.ObjectCriteria; import org.immutables.criteria.matcher.ObjectMatcher;
import org.immutables.criteria.constraints.StringCriteria; import org.immutables.criteria.matcher.StringMatcher;
import org.immutables.criteria.constraints.BooleanCriteria; import org.immutables.criteria.matcher.BooleanMatcher;
import org.immutables.criteria.constraints.ComparableCriteria; import org.immutables.criteria.matcher.ComparableMatcher;
import org.immutables.criteria.expression.Expression; import org.immutables.criteria.expression.Expression;
import org.immutables.criteria.expression.Expressional; import org.immutables.criteria.expression.Expressional;
import org.immutables.criteria.expression.ExpressionVisitor; import org.immutables.criteria.expression.ExpressionVisitor;
Expand Down Expand Up @@ -128,29 +128,29 @@ import [starImport];


[template criteriaType Attribute a Type type][output.trim] [template criteriaType Attribute a Type type][output.trim]
[if (a.boolean or (a.type eq 'java.lang.Boolean'))] [if (a.boolean or (a.type eq 'java.lang.Boolean'))]
BooleanCriteria<R> BooleanMatcher<R>
[else if a.stringType] [else if a.stringType]
StringCriteria<R> StringMatcher<R>
[else if a.optionalType] [else if a.optionalType]
[if a.hasSimpleScalarElementType] [if a.hasSimpleScalarElementType]
OptionalCriteria<R, [scalarElementCriteria a 'R'], [scalarElementCriteria a 'Self']> OptionalMatcher<R, [scalarElementCriteria a 'R'], [scalarElementCriteria a 'Self']>
[else if a.hasCriteria] [else if a.hasCriteria]
OptionalCriteria<R, [a.unwrappedElementType]Criteria<R>, [a.unwrappedElementType]Criteria.Self> OptionalMatcher<R, [a.unwrappedElementType]Criteria<R>, [a.unwrappedElementType]Criteria.Self>
[/if] [/if]
[else if a.comparable] [else if a.comparable]
ComparableCriteria<R, [a.wrappedElementType]> ComparableMatcher<R, [a.wrappedElementType]>
[else if a.collectionType] [else if a.collectionType]
[if a.hasSimpleScalarElementType] [if a.hasSimpleScalarElementType]
CollectionCriteria<R, [scalarElementCriteria a 'R'], [scalarElementCriteria a 'Self']> CollectionMatcher<R, [scalarElementCriteria a 'R'], [scalarElementCriteria a 'Self']>
[else if a.hasCriteria] [else if a.hasCriteria]
CollectionCriteria<R, [a.unwrappedElementType]Criteria<R>, [a.unwrappedElementType]Criteria.Self> CollectionMatcher<R, [a.unwrappedElementType]Criteria<R>, [a.unwrappedElementType]Criteria.Self>
[else] [else]
[output.error]Can't create criteria for collection [type.name].[a.name] [a.unwrappedElementType][/output.error] [output.error]Can't create criteria for collection [type.name].[a.name] [a.unwrappedElementType][/output.error]
[/if] [/if]
[else if a.hasCriteria] [else if a.hasCriteria]
[a.unwrappedElementType]Criteria<R> [a.unwrappedElementType]Criteria<R>
[else] [else]
ObjectCriteria<R, [a.wrappedElementType]> ObjectMatcher<R, [a.wrappedElementType]>
[/if] [/if]
[/output.trim][/template] [/output.trim][/template]


Expand All @@ -169,11 +169,11 @@ context.add(Expressions.path("[a.name]"))
[-- Used to create criteria for T (eg. List<T>) when T is a scalar / comparable--] [-- Used to create criteria for T (eg. List<T>) when T is a scalar / comparable--]
[template scalarElementCriteria Attribute a String elementName][output.trim] [template scalarElementCriteria Attribute a String elementName][output.trim]
[if a.unwrappedElementType eq 'boolean'] [if a.unwrappedElementType eq 'boolean']
[if elementName eq 'Self']BooleanCriteria.Self[else]BooleanCriteria<[elementName]>[/if] [if elementName eq 'Self']BooleanMatcher.Self[else]BooleanMatcher<[elementName]>[/if]
[else if a.unwrappedElementType eq 'java.lang.String'] [else if a.unwrappedElementType eq 'java.lang.String']
[if elementName eq 'Self']StringCriteria.Self[else]StringCriteria<[elementName]>[/if] [if elementName eq 'Self']StringMatcher.Self[else]StringMatcher<[elementName]>[/if]
[else if a.isMaybeComparableKey] [else if a.isMaybeComparableKey]
[if elementName eq 'Self']ComparableCriteria.Self<[a.wrappedElementType]>[else]ComparableCriteria<[elementName], [a.wrappedElementType]>[/if] [if elementName eq 'Self']ComparableMatcher.Self<[a.wrappedElementType]>[else]ComparableMatcher<[elementName], [a.wrappedElementType]>[/if]
[else] [else]
[output.error]unexpected type [a.name] [a.unwrappedElementType]<[elementName]>, not a simple one[/output.error] [output.error]unexpected type [a.name] [a.unwrappedElementType]<[elementName]>, not a simple one[/output.error]
[/if] [/if]
Expand Down

0 comments on commit 0f64e8a

Please sign in to comment.