diff --git a/framework/entity/src/main/java/org/apache/ofbiz/entity/test/EntityUtilTestSuite.java b/framework/entity/src/main/java/org/apache/ofbiz/entity/test/EntityUtilTestSuite.java new file mode 100644 index 0000000000..9fcdb1f8ff --- /dev/null +++ b/framework/entity/src/main/java/org/apache/ofbiz/entity/test/EntityUtilTestSuite.java @@ -0,0 +1,101 @@ +/******************************************************************************* + * 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.test; + +import org.apache.ofbiz.base.util.StringUtil; +import org.apache.ofbiz.base.util.UtilMisc; +import org.apache.ofbiz.entity.GenericValue; +import org.apache.ofbiz.entity.condition.EntityCondition; +import org.apache.ofbiz.entity.condition.EntityExpr; +import org.apache.ofbiz.entity.condition.EntityOperator; +import org.apache.ofbiz.entity.testtools.EntityTestCase; +import org.apache.ofbiz.entity.util.EntityUtil; + +import java.util.ArrayList; +import java.util.List; + +public class EntityUtilTestSuite extends EntityTestCase { + + public static final String module = EntityUtilTestSuite.class.getName(); + + public static final long TEST_COUNT = 1000; + + public EntityUtilTestSuite(String name) { + super(name); + } + + private List prepareGenericValueList() { + List newValues = new ArrayList<>(); + for (int i = 0; i < TEST_COUNT; i++) { + newValues.add(delegator.makeValue("Testing", "testingId", StringUtil.padNumberString(String.valueOf(i), 5), "description", "Description " + i % 10)); + } + return newValues; + } + + public void testGetFieldListFromEntityList() { + List newValues = prepareGenericValueList(); + List descriptionList = EntityUtil.getFieldListFromEntityList(newValues, "description", false); + assertEquals("Get not distinct field list from " + TEST_COUNT + " entity", TEST_COUNT, descriptionList.size()); + assertEquals("Get first description value", "Description 0", descriptionList.get(0)); + assertEquals("Get tens description value", "Description 0", descriptionList.get(10)); + + descriptionList = EntityUtil.getFieldListFromEntityList(newValues, "description", true); + assertEquals("Get distinct field list from " + TEST_COUNT + " entity, modulo 10 values", 10, descriptionList.size()); + assertEquals("Get first description value", "Description 0", descriptionList.get(0)); + } + + public void testFilterByCondition() { + List newValues = prepareGenericValueList(); + EntityExpr condition = EntityCondition.makeCondition("description", "Description 0"); + List filteredValues = EntityUtil.filterByCondition(newValues, condition); + assertEquals("Filter on 10% description condition " + TEST_COUNT + " entity", TEST_COUNT / 10, filteredValues.size()); + assertEquals("Get first description value", "Description 0", filteredValues.get(0).get("description")); + + filteredValues = EntityUtil.filterOutByCondition(newValues, condition); + assertEquals("Filter out on 10% description condition " + TEST_COUNT + " entity", TEST_COUNT - TEST_COUNT / 10, filteredValues.size()); + assertEquals("Get first description value", "Description 1", filteredValues.get(0).get("description")); + } + + public void testFilterByAnd() { + List newValues = prepareGenericValueList(); + List condition = UtilMisc.toList(EntityCondition.makeCondition("description", "Description 0"), + EntityCondition.makeCondition("testingId", "00010")); + List filteredWithMap = EntityUtil.filterByAnd(newValues, UtilMisc.toMap("description", "Description 0", "testingId", "00010")); + List filteredWithCondition = EntityUtil.filterByAnd(newValues, condition); + assertEquals("Filter with same condition using Map and List ", filteredWithCondition, filteredWithMap); + + condition = UtilMisc.toList(EntityCondition.makeCondition("description", "Description 0"), + EntityCondition.makeCondition("testingId", EntityOperator.LIKE, "000%")); + filteredWithCondition = EntityUtil.filterByAnd(newValues, condition); + assertEquals("Filter condition using List must have 10 results", 10, filteredWithCondition.size()); + filteredWithCondition.forEach(genericValue -> + assertEquals("Filter condition using List must get simple description", + "Description 0", genericValue.get("description"))); + } + + public void testFilterByOr() { + List newValues = prepareGenericValueList(); + List condition = UtilMisc.toList(EntityCondition.makeCondition("description", "Description 0"), + EntityCondition.makeCondition("testingId", "00001")); + List filteredWithCondition = EntityUtil.filterByOr(newValues, condition); + + assertEquals("Filter condition using List must have " + (TEST_COUNT / 10 + 1) + " results", + TEST_COUNT / 10 + 1, filteredWithCondition.size()); + } +} diff --git a/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntityUtil.java b/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntityUtil.java index 06067ea594..6602772c39 100644 --- a/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntityUtil.java +++ b/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntityUtil.java @@ -32,6 +32,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.stream.Stream; import org.apache.ofbiz.base.util.Debug; import org.apache.ofbiz.base.util.UtilDateTime; @@ -49,6 +50,8 @@ import org.apache.ofbiz.entity.condition.EntityDateFilterCondition; import org.apache.ofbiz.entity.condition.OrderByList; import org.apache.ofbiz.entity.model.ModelField; +import static java.util.stream.Collectors.toList; + /** * Helper methods when dealing with Entities, especially ones that follow certain conventions */ @@ -265,21 +268,10 @@ public final class EntityUtil { *@return List of GenericValue's that match the values in fields */ public static List filterByAnd(List values, Map fields) { - if (values == null) return null; - - List result = null; - if (UtilValidate.isEmpty(fields)) { - result = new LinkedList(); - result.addAll(values); - } else { - result = new LinkedList(); - for (T value: values) { - if (value.matchesFields(fields)) { - result.add(value); - }// else did not match - } + if (values == null || UtilValidate.isEmpty(fields)) { + return values; } - return result; + return values.stream().filter(value -> value.matchesFields(fields)).collect(toList()); } /** @@ -290,25 +282,13 @@ public final class EntityUtil { *@return List of GenericValue's that match the values in fields */ public static List filterByAnd(List values, List exprs) { - if (values == null) return null; - if (UtilValidate.isEmpty(exprs)) { - // no constraints... oh well + if (values == null || UtilValidate.isEmpty(exprs)) { return values; } - List result = new LinkedList(); - for (T value: values) { - boolean include = true; - - for (EntityCondition condition: exprs) { - include = condition.entityMatches(value); - if (!include) break; - } - if (include) { - result.add(value); - } - } - return result; + return values.stream() + .filter(value -> exprs.stream().allMatch(condition -> condition.entityMatches(value))) + .collect(toList()); } /** @@ -319,24 +299,13 @@ public final class EntityUtil { *@return List of GenericValue's that match the values in fields */ public static List filterByOr(List values, List exprs) { - if (values == null) return null; - if (UtilValidate.isEmpty(exprs)) { + if (values == null || UtilValidate.isEmpty(exprs)) { return values; } - List result = new LinkedList(); - for (T value: values) { - boolean include = false; - - for (EntityCondition condition: exprs) { - include = condition.entityMatches(value); - if (include) break; - } - if (include) { - result.add(value); - } - } - return result; + return values.stream() + .filter(value -> exprs.stream().anyMatch(condition -> condition.entityMatches(value))) + .collect(toList()); } /** @@ -415,27 +384,17 @@ public final class EntityUtil { } public static List filterByCondition(List values, EntityCondition condition) { - if (values == null) return null; - - List result = new LinkedList(); - for (T value: values) { - if (condition.entityMatches(value)) { - result.add(value); - } + if (values == null || UtilValidate.isEmpty(condition)) { + return values; } - return result; + return values.stream().filter(value -> condition.entityMatches(value)).collect(toList()); } public static List filterOutByCondition(List values, EntityCondition condition) { - if (values == null) return null; - - List result = new LinkedList(); - for (T value: values) { - if (!condition.entityMatches(value)) { - result.add(value); - } + if (values == null || UtilValidate.isEmpty(condition)) { + return values; } - return result; + return values.stream().filter(value -> !condition.entityMatches(value)).collect(toList()); } public static List findDatedInclusionEntity(Delegator delegator, String entityName, Map search) throws GenericEntityException { @@ -505,27 +464,13 @@ public final class EntityUtil { if (genericValueList == null || fieldName == null) { return null; } - List fieldList = new LinkedList(); - Set distinctSet = null; + + Stream tStream = genericValueList.stream().map(genericValue -> UtilGenerics.cast(genericValue.get(fieldName))); if (distinct) { - distinctSet = new HashSet(); + return tStream.distinct().collect(toList()); + } else { + return tStream.collect(toList()); } - - for (GenericValue value: genericValueList) { - T fieldValue = UtilGenerics.cast(value.get(fieldName)); - if (fieldValue != null) { - if (distinct) { - if (!distinctSet.contains(fieldValue)) { - fieldList.add(fieldValue); - distinctSet.add(fieldValue); - } - } else { - fieldList.add(fieldValue); - } - } - } - - return fieldList; } public static List getFieldListFromEntityListIterator(EntityListIterator genericValueEli, String fieldName, boolean distinct) { diff --git a/framework/entity/testdef/entitytests.xml b/framework/entity/testdef/entitytests.xml index dce7577862..1eb341c9c6 100644 --- a/framework/entity/testdef/entitytests.xml +++ b/framework/entity/testdef/entitytests.xml @@ -22,6 +22,7 @@ under the License. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/test-suite.xsd"> +