svn commit: r1850378 - in /ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition: EntityExpr.java EntityFieldMap.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1850378 - in /ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition: EntityExpr.java EntityFieldMap.java

pgil
Author: pgil
Date: Fri Jan  4 14:57:31 2019
New Revision: 1850378

URL: http://svn.apache.org/viewvc?rev=1850378&view=rev
Log:
Improved: Refactoring ‘EntityCondition’ - Rewrite EntityFieldMap class
(OFBIZ-10691)

Useless explicit type parameters, and ‘this’ has been removed.  The·
javadoc has been expanded.

Thanks Mathieu for the contribution


Modified:
    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

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=1850378&r1=1850377&r2=1850378&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 Fri Jan  4 14:57:31 2019
@@ -20,7 +20,6 @@ package org.apache.ofbiz.entity.conditio
 
 import java.util.Collection;
 import java.util.List;
-import java.util.Locale;
 import java.util.Map;
 import java.util.Objects;
 

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=1850378&r1=1850377&r2=1850378&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 Fri Jan  4 14:57:31 2019
@@ -18,59 +18,115 @@
  *******************************************************************************/
 package org.apache.ofbiz.entity.condition;
 
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 import org.apache.ofbiz.base.util.UtilGenerics;
 import org.apache.ofbiz.entity.util.EntityUtil;
 
 /**
- * Encapsulates simple expressions used for specifying queries
- *
+ * A condition expression corresponding to an unordered collection of
+ * conditions containing two values compared with a comparison operator
+ * and that are joined by an operator.
+ * <p>
+ * The main objective it to express the conjunction or disjunction of a set of
+ * conditions which in the case of conjunction corresponds to SQL expression
+ * of the form {@code foo=bar AND bar=baz AND ...} and where the comparison
+ * operator is {@code =} and the join operator is {@code AND}.
  */
 @SuppressWarnings("serial")
 public final class EntityFieldMap extends EntityConditionListBase<EntityExpr> {
 
-    private final Map<String, ? extends Object> fieldMap;
-
-    public static <V> List<EntityExpr> makeConditionList(Map<String, V> fieldMap, EntityComparisonOperator<?,V> op) {
-        if (fieldMap == null) {
-            return Collections.emptyList();
-        }
-        List<EntityExpr> list = new ArrayList<>(fieldMap.size());
-        for (Map.Entry<String, ? extends Object> entry: fieldMap.entrySet()) {
-            list.add(EntityCondition.makeCondition(entry.getKey(), op, entry.getValue()));
-        }
-        return list;
-    }
+    /** The map whose entries correspond to the set of equality checks conditions. */
+    private final Map<String, ?> fieldMap;
 
+    /**
+     * Converts a map of condition fields into a list of condition expression.
+     *
+     * @param fieldMap the condition fields
+     * @param op the operator used to compared each entry in the condition field map.
+     * @return a list of condition expression
+     */
+    private static <V> List<EntityExpr> makeConditionList(Map<String, V> fieldMap, EntityComparisonOperator<?,V> op) {
+        return (fieldMap == null)
+            ? Collections.emptyList()
+            : fieldMap.entrySet().stream()
+                      .map(entry -> EntityCondition.makeCondition(entry.getKey(), op, entry.getValue()))
+                      .collect(Collectors.toList());
+    }
+
+    /**
+     * Constructs a map of fields.
+     *
+     * @param compOp the operator used to compare fields
+     * @param joinOp the operator used to join field comparisons
+     * @param keysValues a list of values that the field map will contain.
+     *        This list must be of even length and each successive pair will
+     *        be associated in the field map.
+     * @param <V> The type of values that are compared.
+     */
     @SafeVarargs
     public <V> EntityFieldMap(EntityComparisonOperator<?,?> compOp, EntityJoinOperator joinOp, V... keysValues) {
-        this(EntityUtil.makeFields(keysValues), UtilGenerics.<EntityComparisonOperator<String,V>>cast(compOp), joinOp);
-    }
-
-    public <V> EntityFieldMap(Map<String, V> fieldMap, EntityComparisonOperator<?,?> compOp, EntityJoinOperator joinOp) {
-        super(makeConditionList(fieldMap, UtilGenerics.<EntityComparisonOperator<String,V>>cast(compOp)), joinOp);
-        this.fieldMap = fieldMap == null ? Collections.<String, Object>emptyMap() : fieldMap;
+        this(EntityUtil.makeFields(keysValues), UtilGenerics.cast(compOp), joinOp);
     }
 
+    /**
+     * Constructs a map of fields.
+     *
+     * @param fieldMap the map containing the fields to compare
+     * @param compOp the operator to compare fields
+     * @param joinOp the operator to join entries in the field map
+     * @param <V> the type of values contained in {@code fieldMap}
+     */
+    public <V> EntityFieldMap(Map<String, V> fieldMap, EntityComparisonOperator<?,?> compOp,
+                              EntityJoinOperator joinOp) {
+        super(makeConditionList(fieldMap, UtilGenerics.cast(compOp)), joinOp);
+        this.fieldMap = (fieldMap == null) ? Collections.emptyMap() : fieldMap;
+    }
+
+    /**
+     * Gets the value associated with field {@code name}.
+     *
+     * @param name the name of the field
+     * @return the value associated with field {@code name}
+     * @throws NullPointerException if the specified name is {@code null}
+     *         and the field map does not permit null keys
+     */
     public Object getField(String name) {
-        return this.fieldMap.get(name);
+        return fieldMap.get(name);
     }
 
+    /**
+     * Checks if the field map contains the field {@code name}.
+     *
+     * @param name the name of the field to search
+     * @return {@code true} if field is defined in the field map
+     * @throws NullPointerException if the specified name is {@code null}
+     *         and the field map does not permit null keys
+     */
     public boolean containsField(String name) {
-        return this.fieldMap.containsKey(name);
+        return fieldMap.containsKey(name);
     }
 
+    /**
+     * Provides an iterator on the fields contained in the field map.
+     *
+     * @return an iterator of fields
+     */
     public Iterator<String> getFieldKeyIterator() {
-        return Collections.unmodifiableSet(this.fieldMap.keySet()).iterator();
+        return Collections.unmodifiableSet(fieldMap.keySet()).iterator();
     }
 
+    /**
+     * Provides an iterator on the entries contained in the field map.
+     *
+     * @return an iterator of field entries
+     */
     public Iterator<Map.Entry<String, ? extends Object>> getFieldEntryIterator() {
-        return Collections.<Map.Entry<String, ? extends Object>>unmodifiableSet(this.fieldMap.entrySet()).iterator();
+        return Collections.<Map.Entry<String, ? extends Object>>unmodifiableSet(fieldMap.entrySet()).iterator();
     }
 
     @Override