public final class BeanPredicateUtil extends Object
PredicateUtils,
BeanPredicate| Modifier and Type | Method and Description |
|---|---|
static <T,V extends Comparable<? super V>> |
comparatorPredicate(String propertyName,
V valueToCompare,
Comparator<V> comparator,
ComparatorPredicate.Criterion criterion)
判断指定的值
valueToCompare criterion 提取的bean对象属性propertyName的值 . |
static <T,V extends Comparable<? super V>> |
comparatorPredicate(String propertyName,
V valueToCompare,
ComparatorPredicate.Criterion criterion)
判断指定的值
valueToCompare criterion 提取的bean对象属性propertyName的值 . |
static <T,V> Predicate<T> |
containsPredicate(String propertyName,
Collection<V> propertyValueList)
|
static <T,V> Predicate<T> |
containsPredicate(String propertyName,
V... propertyValues)
调用
PropertyUtil.getProperty(Object, String) 获得 propertyName的值,使用
ArrayUtils.contains 判断是否在 values数组中. |
static <T> Predicate<T> |
equalIgnoreCasePredicate(String propertyName,
String propertyValue)
用来指定
T 对象的特定属性 propertyName equalsIgnoreCase 指定的 propertyValue. |
static <T> Predicate<T> |
equalPredicate(Map<String,?> propertyNameAndPropertyValueMap)
用来判断一个对象指定的属性以及属性值是否相等.
|
static <T,V> Predicate<T> |
equalPredicate(String propertyName,
V propertyValue)
用来指定
T 对象的特定属性 propertyName equals 指定的 propertyValue. |
static <T> Predicate<T> |
equalPredicate(T bean,
String... propertyNames)
构造属性与一个指定对象
bean 的一组属性的值 propertyNames 都相等的判断器. |
public static <T,V> Predicate<T> equalPredicate(String propertyName, V propertyValue)
T 对象的特定属性 propertyName equals 指定的 propertyValue.
- 常用于解析集合,如
select,find,selectRejected,group,groupCount,sum等方法.
场景: 在list中查找 名字是 关羽,并且 年龄是30 的user
User guanyu30 = new User("关羽", 30); List<User>list = toList(// new User("张飞", 23), new User("关羽", 24), new User("刘备", 25), guanyu30); Predicate<User>predicate = PredicateUtils .andPredicate(BeanPredicateUtil.equalPredicate("name", "关羽"), BeanPredicateUtil.equalPredicate("age", 30)); assertEquals(guanyu30, CollectionsUtil.find(list, predicate));
T - the generic typeV - the value typepropertyName - 泛型T对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见
propertyNamepropertyValue - the property valuepropertyName 是null,抛出 NullPointerExceptionpropertyName 是blank,抛出 IllegalArgumentExceptionPredicateUtils.equalPredicate(Object)public static <T> Predicate<T> equalIgnoreCasePredicate(String propertyName, String propertyValue)
T 对象的特定属性 propertyName equalsIgnoreCase 指定的 propertyValue.
- 常用于解析集合,如
select,find,selectRejected,group,groupCount,sum等方法.
场景: 在list中查找 name是 zhangfei(忽视大小写) 的user
User zhangfei1 = new User("zhangfei", 22); User zhangfei2 = new User("zhangFei", 22); User zhangfei3 = new User("Zhangfei", 22); User zhangfeinull = new User((String) null, 22); User guanyu = new User("guanyu", 25); User liubei = new User("liubei", 30); List<User>list = toList(zhangfei1, zhangfei2, zhangfei3, zhangfeinull, guanyu, liubei); 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)) // ));
T - the generic typepropertyName - 泛型T对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见
propertyNamepropertyValue - the property valuepropertyName 是null,抛出 NullPointerExceptionpropertyName 是blank,抛出 IllegalArgumentExceptionPredicateUtils.equalPredicate(Object),
IgnoreCaseEquator.INSTANCEpublic static <T> Predicate<T> equalPredicate(Map<String,?> propertyNameAndPropertyValueMap)
- 常用于解析集合,如
select,find,selectRejected,group,groupCount,sum等方法.
场景: 在list中查找 名字是 关羽,并且 年龄是30 的user
User guanyu30 = new User("关羽", 30); List<User>list = toList(// new User("张飞", 23), new User("关羽", 24), new User("刘备", 25), guanyu30); Predicate<User>predicate = PredicateUtils .andPredicate(BeanPredicateUtil.equalPredicate("name", "关羽"), BeanPredicateUtil.equalPredicate("age", 30)); assertEquals(guanyu30, CollectionsUtil.find(list, predicate));此时你可以优化成:
User guanyu30 = new User("关羽", 30); List<User>list = toList(// new User("张飞", 23), new User("关羽", 24), new User("刘备", 25), guanyu30); Map<String, Object>map = ConvertUtil.toMap("name", "关羽", "age", 30); assertEquals(guanyu30, find(list, BeanPredicateUtil.<User>equalPredicate(map)));
T - the generic typepropertyNameAndPropertyValueMap - 属性和指定属性值对应的map,其中key是泛型T对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见
propertyNamepropertyNameAndPropertyValueMap 是null,抛出 NullPointerExceptionpropertyNameAndPropertyValueMap 是empty,抛出IllegalArgumentExceptionpropertyNameAndPropertyValueMap 中有key是null,抛出NullPointerExceptionpropertyNameAndPropertyValueMap 中有key是blank,抛出IllegalArgumentExceptionequalPredicate(String, Object)public static <T> Predicate<T> equalPredicate(T bean, String... propertyNames)
bean 的一组属性的值 propertyNames 都相等的判断器.
- 常用于解析集合,如
select,find,selectRejected,group,groupCount,sum等方法.
场景: 在list中查找 名字是 关羽,并且 年龄是30 的user
User guanyu30 = new User("关羽", 30); List<User>list = toList(// new User("张飞", 23), new User("关羽", 24), new User("刘备", 25), guanyu30); Predicate<User>predicate = PredicateUtils .andPredicate(BeanPredicateUtil.equalPredicate("name", "关羽"), BeanPredicateUtil.equalPredicate("age", 30)); assertEquals(guanyu30, CollectionsUtil.find(list, predicate));此时你可以优化成:
User guanyu30 = new User("关羽", 30); List<User>list = toList(// new User("张飞", 23), new User("关羽", 24), new User("刘备", 25), guanyu30); assertEquals(guanyu30, find(list, BeanPredicateUtil.equalPredicate(guanyu30, "name", "age")));
T - the generic typebean - the beanpropertyNames - 如果 propertyNames 是null或者empty 那么取所有属性值.bean 是null,返回 PredicateUtils.nullPredicate()PropertyUtil.describe(Object, String...) 提取属性值,再调用 equalPredicate(Map)equalPredicate(String, Object),
PropertyUtil.describe(Object, String...)@SafeVarargs public static <T,V> Predicate<T> containsPredicate(String propertyName, V... propertyValues)
PropertyUtil.getProperty(Object, String) 获得 propertyName的值,使用
ArrayUtils.contains 判断是否在 values数组中.
- 常用于解析集合,如
select,find,selectRejected,group,groupCount,sum等方法.
T - the generic typeV - the value typepropertyName - 泛型T对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见
propertyNamepropertyValues - the property valuespropertyName 是null,抛出 NullPointerExceptionpropertyName 是blank,抛出 IllegalArgumentExceptionArrayUtils.contains(Object[], Object)public static <T,V> Predicate<T> containsPredicate(String propertyName, Collection<V> propertyValueList)
PropertyUtil.getProperty(Object, String) 获得 propertyName的值,使用Collection.contains 判断是否在values集合中.
- 常用于解析集合,如
select,find,selectRejected,group,groupCount,sum等方法.
T - the generic typeV - the value typepropertyName - 泛型T对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见
propertyNamepropertyValueList - the property value listpropertyName 是null,抛出 NullPointerExceptionpropertyName 是blank,抛出 IllegalArgumentExceptionCollection.contains(Object)public static <T,V extends Comparable<? super V>> Predicate<T> comparatorPredicate(String propertyName, V valueToCompare, ComparatorPredicate.Criterion criterion)
valueToCompare criterion 提取的bean对象属性propertyName的值 .
注意: 是参数 valueToCompare 和 属性值进行比较 criterion, BeanPredicateUtil.comparatorPredicate("age", 20,
Criterion.LESS) 表示提取所有比20 大的结果
- 使用
ComparatorUtils.naturalComparator()自然排序比较器.- 比较
comparator.compare(valueToCompare, propertyValue).- 常用于解析集合,如
select,find,selectRejected,group,groupCount,sum等方法.
ComparatorPredicate.Criterion:
字段 说明 ComparatorPredicate.Criterion.EQUALcomparator.compare(valueToCompare, propertyValue) == 0 ComparatorPredicate.Criterion.LESScomparator.compare(valueToCompare, propertyValue) <0ComparatorPredicate.Criterion.GREATERcomparator.compare(valueToCompare, propertyValue) >0ComparatorPredicate.Criterion.GREATER_OR_EQUALcomparator.compare(valueToCompare, propertyValue) >=0ComparatorPredicate.Criterion.LESS_OR_EQUALcomparator.compare(valueToCompare, propertyValue) <=0
场景: 大于20岁的人分个组
List你可以简化成:<User>list = toList(// new User("张飞", 10), new User("张飞", 28), new User("刘备", 32), new User("刘备", 30), new User("刘备", 10)); Map<String, List<User>>map = CollectionsUtil.group(list, "name", new Predicate<User>(){@Overridepublic boolean evaluate(User user){ return user.getAge()>20; } });List<User>list = toList(// new User("张飞", 10), new User("张飞", 28), new User("刘备", 32), new User("刘备", 30), new User("刘备", 10)); Predicate<User>comparatorPredicate = BeanPredicateUtil.comparatorPredicate("age", 20, Criterion.LESS);
T - the generic typeV - the value typepropertyName - 泛型T对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见
propertyNamevalueToCompare - the value to comparecriterion - the criterionpropertyName 是null,抛出 NullPointerExceptionpropertyName 是blank,抛出 IllegalArgumentExceptionComparatorUtils.naturalComparator(),
#comparatorPredicate(String, Comparable, Comparator, Criterion)public static <T,V extends Comparable<? super V>> Predicate<T> comparatorPredicate(String propertyName, V valueToCompare, Comparator<V> comparator, ComparatorPredicate.Criterion criterion)
valueToCompare criterion 提取的bean对象属性propertyName的值 .
- 使用
comparator比较器- 比较
comparator.compare(valueToCompare, propertyValue).- 常用于解析集合,如
select,find,selectRejected,group,groupCount,sum等方法.
ComparatorPredicate.Criterion:
字段 说明 ComparatorPredicate.Criterion.EQUALcomparator.compare(valueToCompare, propertyValue) == 0 ComparatorPredicate.Criterion.LESScomparator.compare(valueToCompare, propertyValue) <0ComparatorPredicate.Criterion.GREATERcomparator.compare(valueToCompare, propertyValue) >0ComparatorPredicate.Criterion.GREATER_OR_EQUALcomparator.compare(valueToCompare, propertyValue) >=0ComparatorPredicate.Criterion.LESS_OR_EQUALcomparator.compare(valueToCompare, propertyValue) <=0
T - the generic typeV - the value typepropertyName - 泛型T对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见
propertyNamevalueToCompare - the value to comparecomparator - the comparatorcriterion - the criterionpropertyName 是null,抛出 NullPointerExceptionpropertyName 是blank,抛出 IllegalArgumentExceptionComparatorPredicateCopyright © 2008-2019 by feilong