Skip to content

Commit

Permalink
新建 CollectionsUtil.selectRejected(Iterable<O>, Map<String, ?>) fix #369
Browse files Browse the repository at this point in the history
新建类似于 CollectionsUtil.find(Iterable<O>, Map<String, ?>) select 方法
fix #361
  • Loading branch information
venusdrogon committed Jul 3, 2022
1 parent 2e36ff5 commit db858b8
Show file tree
Hide file tree
Showing 7 changed files with 340 additions and 58 deletions.
2 changes: 1 addition & 1 deletion feilong-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ feilong core 让Java开发更简便的工具库
![JDK 1.8](https://img.shields.io/badge/JDK-1.8-green.svg "JDK 1.8")
[![jar size 110K](https://img.shields.io/badge/size-110K-green.svg "size 110K")](https://github.com/venusdrogon/feilong-platform/tree/repository/com/feilong/platform/feilong-core/1.14.0)
[![javadoc 83%](http://progressed.io/bar/83?title=javadoc "javadoc 83%")](http://venusdrogon.github.io/feilong-platform/javadocs/feilong-core/)
[![tests 2314](https://img.shields.io/badge/tests-2314%20%2F%202314-green.svg "tests 2314")](https://github.com/venusdrogon/feilong-core/tree/master/src/test/java/com/feilong/core)
[![tests 2351](https://img.shields.io/badge/tests-2351%20%2F%202351-green.svg "tests 2351")](https://github.com/venusdrogon/feilong-core/tree/master/src/test/java/com/feilong/core)
![Coverage 91%](http://progressed.io/bar/91?title=Coverage "Coverage 91%")

![sonar](http://venusdrogon.github.io/feilong-platform/mysource/sonar/feilong-core-summary2.jpg)
Expand Down
124 changes: 123 additions & 1 deletion feilong-core/src/main/java/com/feilong/core/util/CollectionsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1931,6 +1931,66 @@ public static <O, V> List<O> select(Iterable<O> beanIterable,String propertyName
: select(beanIterable, BeanPredicateUtil.<O, V> containsPredicate(propertyName, propertyValueList));
}

/**
* 找到 <code>iterable</code>中,属性和属性值都是propertyNameAndPropertyValueMap的对应元素.
*
* <h3>示例:</h3>
* <blockquote>
*
* <p>
* <b>场景:</b> 从list中查找name是 关羽,且年龄是24 的User对象
* </p>
*
* <pre class="code">
* List{@code <User>} list = new ArrayList{@code <>}();
* list.add(new User("张飞", 23));
*
* <span style="color:green">list.add(new User("关羽", 24));</span>
* list.add(new User("刘备", 25));
* <span style="color:green">list.add(new User("关羽", 24));</span>
* list.add(new User("关羽", 50));
*
* Map{@code <String, ?>} map = toMap("name", "关羽", "age", 24);
* LOGGER.info(JsonUtil.format(CollectionsUtil.select(list, map)));
* </pre>
*
* <b>返回:</b>
*
* <pre class="code">
* [{
* "age": 24,
* "name": "关羽"
* },
* {
* "age": 24,
* "name": "关羽"
* }]
* </pre>
*
* </blockquote>
*
* @param <O>
* the generic type
* @param beanIterable
* bean Iterable,诸如List{@code <User>},Set{@code <User>}等
* @param propertyNameAndPropertyValueMap
* 属性和指定属性值对应的map,其中key是泛型T对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见
* <a href="../../bean/BeanUtil.html#propertyName">propertyName</a>
* @return 如果 <code>beanIterable</code> 是null或者empty,返回 {@link Collections#emptyList()}<br>
* 如果 <code>propertyNameAndPropertyValueMap</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>propertyNameAndPropertyValueMap</code> 是empty,抛出{@link IllegalArgumentException}<br>
* 如果 <code>propertyNameAndPropertyValueMap</code> 中有key是null,抛出{@link NullPointerException}<br>
* 如果 <code>propertyNameAndPropertyValueMap</code> 中有key是blank,抛出{@link IllegalArgumentException}<br>
* 如果 <code>iterable</code>查找不到<code>propertyNameAndPropertyValueMap</code>,返回空集合
* @see #select(Iterable, Predicate)
* @see com.feilong.core.util.predicate.BeanPredicateUtil#equalPredicate(String, Object)
* @since 3.1.1
*/
public static <O> List<O> select(Iterable<O> beanIterable,Map<String, ?> propertyNameAndPropertyValueMap){
return isNullOrEmpty(beanIterable) ? Collections.<O> emptyList()
: select(beanIterable, BeanPredicateUtil.<O> equalPredicate(propertyNameAndPropertyValueMap));
}

/**
* 按照指定的 {@link Predicate},返回查询出来的集合.
*
Expand Down Expand Up @@ -2002,7 +2062,7 @@ public static <O, V> List<O> select(Iterable<O> beanIterable,String propertyName
* <li>....</li>
* </ul>
* @return 如果 <code>beanIterable</code> 是null或者empty,返回 {@link Collections#emptyList()}<br>
* 否则返回 {@link CollectionUtils#select(Iterable, Predicate)}
* 否则返回 {@link CollectionUtils#select(Iterable, Predicate)},如果查不到返回空集合
* @see com.feilong.lib.collection4.CollectionUtils#select(Iterable, Predicate)
*/
public static <O> List<O> select(Iterable<O> beanIterable,Predicate<O> predicate){
Expand Down Expand Up @@ -2136,6 +2196,68 @@ public static <O, V> List<O> selectRejected(Iterable<O> beanIterable,String prop
: selectRejected(beanIterable, BeanPredicateUtil.<O, V> containsPredicate(propertyName, propertyValueList));
}

/**
* 找到 <code>iterable</code>中,属性和属性值都<span style="color:green">不是</span>propertyNameAndPropertyValueMap的对应元素.
*
* <h3>示例:</h3>
* <blockquote>
*
* <p>
* <b>场景:</b> 从list中查找name是 不是关羽,且年龄不是24 的User对象
* </p>
*
* <pre class="code">
* List{@code <User>} list = new ArrayList{@code <>}();
* list.add(new User("张飞", 23));
*
* <span style="color:green">list.add(new User("关羽", 24));</span>
* list.add(new User("刘备", 25));
* <span style="color:green">list.add(new User("关羽", 24));</span>
* list.add(new User("关羽", 50));
*
* Map{@code <String, ?>} map = toMap("name", "关羽", "age", 24);
* LOGGER.info(JsonUtil.format(CollectionsUtil.selectRejected(list, map)));
* </pre>
*
* <b>返回:</b>
*
* <pre class="code">
* [{
* "age": 23,
* "name": "张飞"
* },
* {
* "age": 25,
* "name": "刘备"
* },{
* "age": 50,
* "name": "关羽"
* }]
* </pre>
*
* </blockquote>
*
* @param <O>
* the generic type
* @param beanIterable
* bean Iterable,诸如List{@code <User>},Set{@code <User>}等
* @param propertyNameAndPropertyValueMap
* 属性和指定属性值对应的map,其中key是泛型T对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见
* <a href="../../bean/BeanUtil.html#propertyName">propertyName</a>
* @return 如果 <code>beanIterable</code> 是null或者empty,返回 {@link Collections#emptyList()}<br>
* 如果 <code>propertyNameAndPropertyValueMap</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>propertyNameAndPropertyValueMap</code> 是empty,抛出{@link IllegalArgumentException}<br>
* 如果 <code>propertyNameAndPropertyValueMap</code> 中有key是null,抛出{@link NullPointerException}<br>
* 如果 <code>propertyNameAndPropertyValueMap</code> 中有key是blank,抛出{@link IllegalArgumentException}<br>
* @see #selectRejected(Iterable, Predicate)
* @see com.feilong.core.util.predicate.BeanPredicateUtil#equalPredicate(String, Object)
* @since 3.1.1
*/
public static <O> List<O> selectRejected(Iterable<O> beanIterable,Map<String, ?> propertyNameAndPropertyValueMap){
return isNullOrEmpty(beanIterable) ? Collections.<O> emptyList()
: selectRejected(beanIterable, BeanPredicateUtil.<O> equalPredicate(propertyNameAndPropertyValueMap));
}

/**
* 循环 <code>beanIterable</code>,获得元素 <code>bean</code>,判断是否不匹配<code>predicate</code>,<span style="color:red">如果不匹配</span>
* ,将该对象存入list中返回.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class TrimAndEqualsIgnoreCaseParameterizedTest extends Abstract2ParamsAnd
@Parameters(name = "index:{index}:StringUtil.trimAndEqualsIgnoreCase({0}, {1})={2}")
public static Iterable<Object[]> data(){
return toList(//
toArray(null, null, true),
toArray((String) null, null, true),
toArray(null, EMPTY, false),
toArray(EMPTY, null, false),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@
GroupWithTransformerAndPredicateTest.class,
GroupOneTest.class,

SelectWithMapTest.class,
SelectPredicateTest.class,
SelectArrayTest.class,
SelectCollectionTest.class,

SelectRejectedMapTest.class,
SelectRejectedArrayTest.class,
SelectRejectedCollectionTest.class,
SelectRejectedPredicateTest.class,
Expand All @@ -75,7 +77,7 @@
SelectNotNullOrEmptyStringPredicateTest.class,

SizeTest.class,
//
//
})
public class CollectionsUtilSuiteTests{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
*/
package com.feilong.core.util.collectionsutil;

import static com.feilong.core.bean.ConvertUtil.toList;
import static com.feilong.core.util.CollectionsUtil.select;
import static java.util.Collections.emptyList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -35,25 +37,14 @@
import com.feilong.core.util.predicate.BeanPredicateUtil;
import com.feilong.lib.collection4.ComparatorUtils;
import com.feilong.lib.collection4.functors.ComparatorPredicate;
import com.feilong.lib.collection4.functors.EqualPredicate;
import com.feilong.lib.collection4.functors.ComparatorPredicate.Criterion;
import com.feilong.lib.collection4.functors.EqualPredicate;
import com.feilong.store.member.User;

import static com.feilong.core.bean.ConvertUtil.toList;
import static com.feilong.core.util.CollectionsUtil.select;

/**
* The Class CollectionsUtilSelectPredicateTest.
*
* @author <a href="https://github.com/ifeilong/feilong">feilong</a>
*/
public class SelectPredicateTest{

/**
* Test collections util select predicate test.
*/
@Test
public void testCollectionsUtilSelectPredicateTest(){
public void testSelectPredicateTest(){
User zhangfei1 = new User("zhangfei", 22);
User zhangfei2 = new User("zhangFei", 22);
User zhangfei3 = new User("Zhangfei", 22);
Expand All @@ -63,27 +54,28 @@ public void testCollectionsUtilSelectPredicateTest(){

List<User> list = toList(zhangfei1, zhangfei2, zhangfei3, zhangfeinull, guanyu, liubei);

List<User> select = select(list, new BeanPredicate<User>(
"name", //
EqualPredicate.equalPredicate("zhangfei", IgnoreCaseEquator.INSTANCE)));

assertThat(select, allOf(//
hasItem(zhangfei1),
hasItem(zhangfei2),
hasItem(zhangfei3),

not(hasItem(zhangfeinull)),
not(hasItem(guanyu)),
not(hasItem(liubei))
//
));
List<User> select = select(
list,
new BeanPredicate<User>(
"name", //
EqualPredicate.equalPredicate("zhangfei", IgnoreCaseEquator.INSTANCE)));

assertThat(
select,
allOf(//
hasItem(zhangfei1),
hasItem(zhangfei2),
hasItem(zhangfei3),

not(hasItem(zhangfeinull)),
not(hasItem(guanyu)),
not(hasItem(liubei))
//
));
}

/**
* Test collections util select predicate test 1.
*/
@Test
public void testCollectionsUtilSelectPredicateTest1(){
public void testSelectPredicateTest1(){
User zhangfei1 = new User("zhangfei", 22);
User zhangfei2 = new User("zhangFei", 22);
User zhangfei3 = new User("Zhangfei", 22);
Expand All @@ -95,21 +87,20 @@ public void testCollectionsUtilSelectPredicateTest1(){

List<User> select = select(list, BeanPredicateUtil.<User> equalIgnoreCasePredicate("name", "zhangfei"));

assertThat(select, allOf(//
hasItem(zhangfei1),
hasItem(zhangfei2),
hasItem(zhangfei3),

not(hasItem(zhangfeinull)),
not(hasItem(guanyu)),
not(hasItem(liubei))
//
));
assertThat(
select,
allOf(//
hasItem(zhangfei1),
hasItem(zhangfei2),
hasItem(zhangfei3),

not(hasItem(zhangfeinull)),
not(hasItem(guanyu)),
not(hasItem(liubei))
//
));
}

/**
* Test select predicate.
*/
@Test
public void testSelectPredicate(){
//查询 >10 的元素
Expand All @@ -119,28 +110,24 @@ public void testSelectPredicate(){
assertThat(result, contains(30, 55, 88, 12));
}

/**
* Test select predicate 1.
*/
@Test
public void testSelectPredicateEqualPredicate(){
List<Long> list = toList(1L, 1L, 2L, 3L);
assertThat(CollectionsUtil.select(list, new EqualPredicate<Long>(1L)), contains(1L, 1L));
}

/**
* Test select predicate null collection.
*/
@Test
public void testSelectPredicateNullCollection(){
assertEquals(emptyList(), CollectionsUtil.select(null, new EqualPredicate<Long>(1L)));
}

/**
* Test select predicate empty collection.
*/
@Test
public void testSelectPredicateEmptyCollection(){
assertEquals(emptyList(), CollectionsUtil.select(new ArrayList<Long>(), new EqualPredicate<Long>(1L)));
}

@Test
public void testSelectNotFindPredicate(){
assertEquals(emptyList(), CollectionsUtil.select(toList(5L, 8L), new EqualPredicate<Long>(1L)));
}
}
Loading

0 comments on commit db858b8

Please sign in to comment.