Author: pgil
Date: Sat Oct 13 19:50:27 2018 New Revision: 1843780 URL: http://svn.apache.org/viewvc?rev=1843780&view=rev Log: Improved: Rewrite EntityConditionVisitor interface to respect visitor Pattern (OFBIZ-10593) Remove visit methods from current EntityConditionBase type hierarchy. Add or rewrite accept method in EntityCondition type hierachy. Add documentation in EntityConditionVisitor, introducing implementation examples. Add junit test validating documented examples. Thanks to Mathieu Lirzin for your contribution, and Jacques for the review. Added: ofbiz/ofbiz-framework/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/EntityConditionVisitorTests.java (with props) Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityComparisonOperator.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityCondition.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionBuilder.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionFunction.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionList.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionListBase.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionSubSelect.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionValue.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionVisitor.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityDateFilterCondition.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityExpr.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFieldMap.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFieldValue.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFunction.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityJoinOperator.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityOperator.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityWhereString.java Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityComparisonOperator.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityComparisonOperator.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityComparisonOperator.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityComparisonOperator.java Sat Oct 13 19:50:27 2018 @@ -78,12 +78,6 @@ public abstract class EntityComparisonOp } @Override - public void visit(EntityConditionVisitor visitor, L lhs, R rhs) { - visitor.accept(lhs); - visitor.accept(rhs); - } - - @Override public void addSqlValue(StringBuilder sql, ModelEntity entity, List<EntityConditionParam> entityConditionParams, boolean compat, L lhs, R rhs, Datasource datasourceInfo) { // if this is an IN operator and the rhs Object isEmpty, add "1=0" instead of the normal SQL. Note that "FALSE" does not work with all databases. Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityCondition.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityCondition.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityCondition.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityCondition.java Sat Oct 13 19:50:27 2018 @@ -109,9 +109,12 @@ public abstract class EntityCondition ex return makeWhereString(null, new ArrayList<EntityConditionParam>(), null); } - public void accept(EntityConditionVisitor visitor) { - throw new IllegalArgumentException(getClass().getName() + ".accept not implemented"); - } + /** + * Applies a visitor to this condition. + * + * @param visitor the visitor to be applied + */ + abstract public void accept(EntityConditionVisitor visitor); abstract public String makeWhereString(ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams, Datasource datasourceInfo); @@ -132,8 +135,4 @@ public abstract class EntityCondition ex abstract public boolean mapMatches(Delegator delegator, Map<String, ? extends Object> map); abstract public EntityCondition freeze(); - - public void visit(EntityConditionVisitor visitor) { - throw new IllegalArgumentException(getClass().getName() + ".visit not implemented"); - } } Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionBuilder.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionBuilder.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionBuilder.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionBuilder.java Sat Oct 13 19:50:27 2018 @@ -84,6 +84,10 @@ public class EntityConditionBuilder exte return condition.equals(obj); } + @Override + public void accept(EntityConditionVisitor visitor) { + throw new IllegalArgumentException(getClass().getName() + ".accept not implemented"); + } } @Override Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionFunction.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionFunction.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionFunction.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionFunction.java Sat Oct 13 19:50:27 2018 @@ -80,8 +80,8 @@ public abstract class EntityConditionFun } @Override - public void visit(EntityConditionVisitor visitor) { - visitor.acceptEntityConditionFunction(this, condition); + public void accept(EntityConditionVisitor visitor) { + visitor.visit(this); } @Override Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionList.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionList.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionList.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionList.java Sat Oct 13 19:50:27 2018 @@ -43,6 +43,6 @@ public class EntityConditionList<T exten @Override public void accept(EntityConditionVisitor visitor) { - visitor.acceptEntityConditionList(this); + visitor.visit(this); } } Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionListBase.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionListBase.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionListBase.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionListBase.java Sat Oct 13 19:50:27 2018 @@ -61,11 +61,6 @@ public abstract class EntityConditionLis } @Override - public void visit(EntityConditionVisitor visitor) { - visitor.acceptEntityJoinOperator(operator, conditionList); - } - - @Override public boolean isEmpty() { return operator.isEmpty(conditionList); } Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionSubSelect.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionSubSelect.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionSubSelect.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionSubSelect.java Sat Oct 13 19:50:27 2018 @@ -146,11 +146,4 @@ public class EntityConditionSubSelect ex public void validateSql(ModelEntity modelEntity) throws GenericModelException { // do nothing for now } - - @Override - public void visit(EntityConditionVisitor visitor) { - if (whereCond != null) { - whereCond.visit(visitor); - } - } } Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionValue.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionValue.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionValue.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionValue.java Sat Oct 13 19:50:27 2018 @@ -45,11 +45,6 @@ public abstract class EntityConditionVal } @Override - public void accept(EntityConditionVisitor visitor) { - visitor.acceptEntityConditionValue(this); - } - - @Override public void addSqlValue(StringBuilder sql, Map<String, String> tableAliases, ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams, boolean includeTableNamePrefix, Datasource datasourceinfo) { sql.append(value); } @@ -77,11 +72,6 @@ public abstract class EntityConditionVal @Override public void validateSql(org.apache.ofbiz.entity.model.ModelEntity modelEntity) { } - - @Override - public void visit(EntityConditionVisitor visitor) { - visitor.acceptObject(value); - } } public abstract ModelField getModelField(ModelEntity modelEntity); @@ -109,12 +99,6 @@ public abstract class EntityConditionVal public abstract EntityConditionValue freeze(); - public abstract void visit(EntityConditionVisitor visitor); - - public void accept(EntityConditionVisitor visitor) { - throw new IllegalArgumentException("accept not implemented"); - } - public void toString(StringBuilder sb) { addSqlValue(sb, null, new ArrayList<EntityConditionParam>(), false, null); } Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionVisitor.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionVisitor.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionVisitor.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionVisitor.java Sat Oct 13 19:50:27 2018 @@ -1,4 +1,4 @@ -/******************************************************************************* +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -15,40 +15,139 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. - *******************************************************************************/ - + */ package org.apache.ofbiz.entity.condition; -import java.util.List; - +// Keep the tests from EntityConditionVisitorTests in sync with the code examples. /** - * <p>Represents the conditions to be used to constrain a query.</p> - * <p>An EntityCondition can represent various type of constraints, including:</p> - * <ul> - * <li>EntityConditionList: a list of EntityConditions, combined with the operator specified - * <li>EntityExpr: for simple expressions or expressions that combine EntityConditions - * <li>EntityFieldMap: a map of fields where the field (key) equals the value, combined with the operator specified - * </ul> - * These can be used in various combinations using the EntityConditionList and EntityExpr objects. + * A visitor of entity conditions in the style of the visitor design pattern. + * <p> + * Classes implementing this interface can extend the dynamically dispatched + * behavior associated with {@link EntityCondition} without augmenting its + * interface. Those classes are meant be passed to the + * {@link EntityCondition#accept(EntityConditionVisitor) accept} method which + * calls the corresponding method in the visitor. + * <p> + * <b>Usage Examples:</b> + * Here is a dummy example that should print <i>EntityExpr\n</i> to + * the standard output. + * <pre>{@code + * EntityExpr expr; + * expr.accept(new EntityConditionVisitor() { + * public void visit(EntityConditionFunction func) { + * system.out.println("EntityConditionFunction"); + * } + * + * public <T extends EntityCondition> void visit(EntityConditionList<T> l) { + * system.out.println("EntityConditionList"); + * } + * + * public void visit(EntityFieldMap m) { + * system.out.println("EntityFieldMap"); + * } + * + * public void visit(EntityDateFilterCondition df) { + * system.out.println("EntityDateFilterCondition"); + * } + * + * public void visit(EntityExpr expr) { + * system.out.println("EntityExpr"); + * } + * + * public void visit(EntityWhereString ws) { + * system.out.println("EntityWhereString"); + * } + * }); + * }</pre> + * <p> + * Here is a more complex example asserting the presence of a raw string condition + * even when it is embedded inside another one. + * <pre>{@code + * class ContainsRawCondition implements EntityConditionVisitor { + * public boolean hasRawCondition = false; + * + * public void visit(EntityConditionFunction func) {} + * public void visit(EntityFieldMap m) {} + * public void visit(EntityDateFilterCondition df) {} * + * public <T extends EntityCondition> void visit(EntityConditionList<T> l) { + * Iterator<T> it = l.getConditionIterator(); + * while (it.hasNext()) { + * it.next().accept(this); + * } + * } + * + * public void visit(EntityExpr expr) { + * Object lhs = expr.getLhs(); + * Object rhs = expr.getRhs(); + * if (lhs instanceof EntityCondition) { + * ((EntityCondition) lhs).accept(this); + * } + * if (rhs instanceof EntityCondition) { + * ((EntityCondition) rhs).accept(this); + * } + * } + * + * public void visit(EntityWhereString ws) { + * hasRawCondition = true; + * } + * } + * + * EntityCondition ec = + * EntityCondition.makeCondition(EntityCondition.makeConditionWhere("foo=bar")); + * EntityConditionVisitor visitor = new ContainsRawCondition(); + * ec.accept(visitor); + * assert visitor.hasRawCondition; + * }</pre> + * + * @see EntityCondition */ public interface EntityConditionVisitor { - <T> void visit(T obj); - <T> void accept(T obj); - void acceptObject(Object obj); - void acceptEntityCondition(EntityCondition condition); - <T extends EntityCondition> void acceptEntityJoinOperator(EntityJoinOperator op, List<T> conditions); - <L,R,T> void acceptEntityOperator(EntityOperator<L, R, T> op, L lhs, R rhs); - <L,R> void acceptEntityComparisonOperator(EntityComparisonOperator<L, R> op, L lhs, R rhs); - void acceptEntityConditionValue(EntityConditionValue value); - void acceptEntityFieldValue(EntityFieldValue value); - - void acceptEntityExpr(EntityExpr expr); - <T extends EntityCondition> void acceptEntityConditionList(EntityConditionList<T> list); - void acceptEntityFieldMap(EntityFieldMap fieldMap); - void acceptEntityConditionFunction(EntityConditionFunction func, EntityCondition nested); - <T extends Comparable<?>> void acceptEntityFunction(EntityFunction<T> func); - void acceptEntityWhereString(EntityWhereString condition); + /** + * Visits an entity condition function. + * + * @param func the visited class + * @see EntityConditionFunction + */ + void visit(EntityConditionFunction func); + + /** + * Visits a list of entity conditions. + * + * @param l the visited class + * @see EntityConditionList + */ + <T extends EntityCondition> void visit(EntityConditionList<T> l); + + /** + * Visits a map of entity fields. + * + * @param m the visited class + * @see EntityFieldMap + */ + void visit(EntityFieldMap m); + + /** + * Visits a date filter condition. + * + * @param df the visited class + * @see EntityDateFilterCondition + */ + void visit(EntityDateFilterCondition df); + + /** + * Visits an entity expression. + * + * @param expr the visited class + * @see EntityExpr + */ + void visit(EntityExpr expr); - void acceptEntityDateFilterCondition(EntityDateFilterCondition condition); + /** + * Visits a raw string condition. + * + * @param ws the visited class + * @see EntityWhereString + */ + void visit(EntityWhereString ws); } Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityDateFilterCondition.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityDateFilterCondition.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityDateFilterCondition.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityDateFilterCondition.java Sat Oct 13 19:50:27 2018 @@ -82,13 +82,8 @@ public final class EntityDateFilterCondi } @Override - public void visit(EntityConditionVisitor visitor) { - visitor.acceptEntityDateFilterCondition(this); - } - - @Override public void accept(EntityConditionVisitor visitor) { - visitor.acceptEntityDateFilterCondition(this); + visitor.visit(this); } @Override Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityExpr.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityExpr.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityExpr.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityExpr.java Sat Oct 13 19:50:27 2018 @@ -149,13 +149,8 @@ public final class EntityExpr extends En } @Override - public void visit(EntityConditionVisitor visitor) { - visitor.acceptEntityOperator(operator, lhs, rhs); - } - - @Override public void accept(EntityConditionVisitor visitor) { - visitor.acceptEntityExpr(this); + visitor.visit(this); } public void checkRhsType(ModelEntity modelEntity, Delegator delegator) { Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFieldMap.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFieldMap.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFieldMap.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFieldMap.java Sat Oct 13 19:50:27 2018 @@ -74,6 +74,6 @@ public final class EntityFieldMap extend @Override public void accept(EntityConditionVisitor visitor) { - visitor.acceptEntityFieldMap(this); + visitor.visit(this); } } Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFieldValue.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFieldValue.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFieldValue.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFieldValue.java Sat Oct 13 19:50:27 2018 @@ -199,16 +199,6 @@ public class EntityFieldValue extends En } @Override - public void visit(EntityConditionVisitor visitor) { - visitor.acceptEntityFieldValue(this); - } - - @Override - public void accept(EntityConditionVisitor visitor) { - visitor.acceptEntityFieldValue(this); - } - - @Override public EntityConditionValue freeze() { return this; } Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFunction.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFunction.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFunction.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFunction.java Sat Oct 13 19:50:27 2018 @@ -201,20 +201,6 @@ public abstract class EntityFunction<T e } @Override - public void visit(EntityConditionVisitor visitor) { - if (nested != null) { - visitor.acceptEntityConditionValue(nested); - } else { - visitor.acceptObject(value); - } - } - - @Override - public void accept(EntityConditionVisitor visitor) { - visitor.acceptEntityFunction(this); - } - - @Override public ModelField getModelField(ModelEntity modelEntity) { if (nested != null) { return nested.getModelField(modelEntity); Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityJoinOperator.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityJoinOperator.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityJoinOperator.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityJoinOperator.java Sat Oct 13 19:50:27 2018 @@ -96,20 +96,6 @@ public class EntityJoinOperator extends return EntityCondition.makeCondition(newList, this); } - public void visit(EntityConditionVisitor visitor, List<? extends EntityCondition> conditionList) { - if (UtilValidate.isNotEmpty(conditionList)) { - for (EntityCondition condition: conditionList) { - visitor.visit(condition); - } - } - } - - @Override - public void visit(EntityConditionVisitor visitor, EntityCondition lhs, EntityCondition rhs) { - lhs.visit(visitor); - visitor.visit(rhs); - } - public Boolean eval(GenericEntity entity, EntityCondition lhs, EntityCondition rhs) { return entityMatches(entity, lhs, rhs) ? Boolean.TRUE : Boolean.FALSE; } Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityOperator.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityOperator.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityOperator.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityOperator.java Sat Oct 13 19:50:27 2018 @@ -296,7 +296,6 @@ public abstract class EntityOperator<L, public abstract void addSqlValue(StringBuilder sql, ModelEntity entity, List<EntityConditionParam> entityConditionParams, boolean compat, L lhs, R rhs, Datasource datasourceInfo); public abstract EntityCondition freeze(L lhs, R rhs); - public abstract void visit(EntityConditionVisitor visitor, L lhs, R rhs); public static final Comparable<?> WILDCARD = new Comparable<Object>() { public int compareTo(Object obj) { Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityWhereString.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityWhereString.java?rev=1843780&r1=1843779&r2=1843780&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityWhereString.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityWhereString.java Sat Oct 13 19:50:27 2018 @@ -82,8 +82,8 @@ public final class EntityWhereString ext } @Override - public void visit(EntityConditionVisitor visitor) { - visitor.acceptEntityWhereString(this); + public void accept(EntityConditionVisitor visitor) { + visitor.visit(this); } @Override Added: ofbiz/ofbiz-framework/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/EntityConditionVisitorTests.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/EntityConditionVisitorTests.java?rev=1843780&view=auto ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/EntityConditionVisitorTests.java (added) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/EntityConditionVisitorTests.java Sat Oct 13 19:50:27 2018 @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ofbiz.entity; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.util.Iterator; + +import org.apache.ofbiz.entity.condition.EntityComparisonOperator; +import org.apache.ofbiz.entity.condition.EntityCondition; +import org.apache.ofbiz.entity.condition.EntityConditionFunction; +import org.apache.ofbiz.entity.condition.EntityConditionList; +import org.apache.ofbiz.entity.condition.EntityConditionVisitor; +import org.apache.ofbiz.entity.condition.EntityDateFilterCondition; +import org.apache.ofbiz.entity.condition.EntityExpr; +import org.apache.ofbiz.entity.condition.EntityFieldMap; +import org.apache.ofbiz.entity.condition.EntityWhereString; +import org.junit.Test; + +/* Tests adapted from code examples described in the javadoc of the + * EntityConditionVisitor interface. They should be kept in sync with + * those code examples. */ +public class EntityConditionVisitorTests { + + // Checks the dummy visitor example which must print "EntityExpr\n". + @Test + public void basicTest() { + EntityExpr expr = new EntityExpr("foo", EntityComparisonOperator.EQUALS, "bar"); + OutputStream os = new ByteArrayOutputStream(); + PrintWriter pw = new PrintWriter(os); + expr.accept(new EntityConditionVisitor() { + @Override + public void visit(EntityConditionFunction func) { + pw.println("EntityConditionFunction"); + } + + @Override + public <T extends EntityCondition> void visit(EntityConditionList<T> l) { + pw.println("EntityConditionList"); + } + + @Override + public void visit(EntityFieldMap m) { + pw.println("EntityFieldMap"); + } + + @Override + public void visit(EntityDateFilterCondition df) { + pw.println("EntityDateFilterConfition"); + } + + @Override + public void visit(EntityExpr expr) { + pw.println("EntityExpr"); + } + + @Override + public void visit(EntityWhereString ws) { + pw.println("EntityWhereString"); + } + }); + pw.flush(); + assertEquals("EntityExpr\n", os.toString()); + } + + /* Checks the more complex example which asserts the presence of a raw string + * condition even when it is embedded inside another one. */ + @Test + public void complexTest() { + class ContainsRawCondition implements EntityConditionVisitor { + public boolean hasRawCondition = false; + + @Override public void visit(EntityConditionFunction func) {} + @Override public void visit(EntityFieldMap m) {} + @Override public void visit(EntityDateFilterCondition df) {} + + @Override + public <T extends EntityCondition> void visit(EntityConditionList<T> l) { + Iterator<T> it = l.getConditionIterator(); + while (it.hasNext()) { + it.next().accept(this); + } + } + + @Override + public void visit(EntityExpr expr) { + Object lhs = expr.getLhs(); + Object rhs = expr.getRhs(); + if (lhs instanceof EntityCondition) { + ((EntityCondition) lhs).accept(this); + } + if (rhs instanceof EntityCondition) { + ((EntityCondition) rhs).accept(this); + } + } + + @Override + public void visit(EntityWhereString ws) { + hasRawCondition = true; + } + } + + EntityCondition ec = EntityCondition.makeCondition(EntityCondition.makeConditionWhere("foo=bar")); + ContainsRawCondition visitor = new ContainsRawCondition(); + ec.accept(visitor); + assertTrue(visitor.hasRawCondition); + } +} Propchange: ofbiz/ofbiz-framework/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/EntityConditionVisitorTests.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/ofbiz-framework/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/EntityConditionVisitorTests.java ------------------------------------------------------------------------------ svn:keywords = Date Rev Author URL Id Propchange: ofbiz/ofbiz-framework/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/EntityConditionVisitorTests.java ------------------------------------------------------------------------------ svn:mime-type = text/plain |
Free forum by Nabble | Edit this page |