Skip to content

Commit

Permalink
Improved: Refactoring ‘EntityCondition’ - Rewrite EntityFieldMap class
Browse files Browse the repository at this point in the history
(OFBIZ-10691)

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

Thanks Mathieu for the contribution



git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1850378 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
gilPts committed Jan 4, 2019
1 parent 682f29b commit c932db3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 22 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@


import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,59 +18,115 @@
*******************************************************************************/ *******************************************************************************/
package org.apache.ofbiz.entity.condition; package org.apache.ofbiz.entity.condition;


import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;


import org.apache.ofbiz.base.util.UtilGenerics; import org.apache.ofbiz.base.util.UtilGenerics;
import org.apache.ofbiz.entity.util.EntityUtil; 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") @SuppressWarnings("serial")
public final class EntityFieldMap extends EntityConditionListBase<EntityExpr> { public final class EntityFieldMap extends EntityConditionListBase<EntityExpr> {


private final Map<String, ? extends Object> fieldMap; /** The map whose entries correspond to the set of equality checks conditions. */
private final Map<String, ?> fieldMap;


public static <V> List<EntityExpr> makeConditionList(Map<String, V> fieldMap, EntityComparisonOperator<?,V> op) { /**
if (fieldMap == null) { * Converts a map of condition fields into a list of condition expression.
return Collections.emptyList(); *
} * @param fieldMap the condition fields
List<EntityExpr> list = new ArrayList<>(fieldMap.size()); * @param op the operator used to compared each entry in the condition field map.
for (Map.Entry<String, ? extends Object> entry: fieldMap.entrySet()) { * @return a list of condition expression
list.add(EntityCondition.makeCondition(entry.getKey(), op, entry.getValue())); */
} private static <V> List<EntityExpr> makeConditionList(Map<String, V> fieldMap, EntityComparisonOperator<?,V> op) {
return list; 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 @SafeVarargs
public <V> EntityFieldMap(EntityComparisonOperator<?,?> compOp, EntityJoinOperator joinOp, V... keysValues) { public <V> EntityFieldMap(EntityComparisonOperator<?,?> compOp, EntityJoinOperator joinOp, V... keysValues) {
this(EntityUtil.makeFields(keysValues), UtilGenerics.<EntityComparisonOperator<String,V>>cast(compOp), joinOp); this(EntityUtil.makeFields(keysValues), UtilGenerics.cast(compOp), joinOp);
} }


public <V> EntityFieldMap(Map<String, V> fieldMap, EntityComparisonOperator<?,?> compOp, EntityJoinOperator joinOp) { /**
super(makeConditionList(fieldMap, UtilGenerics.<EntityComparisonOperator<String,V>>cast(compOp)), joinOp); * Constructs a map of fields.
this.fieldMap = fieldMap == null ? Collections.<String, Object>emptyMap() : fieldMap; *
* @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) { 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) { 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() { 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() { 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 @Override
Expand Down

0 comments on commit c932db3

Please sign in to comment.