1 import java.lang.reflect.Field; 2 import java.lang.reflect.Modifier; 3 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Map.Entry; 9 import java.util.Set; 10 11 import org.apache.commons.beanutils.BeanPredicate; 12 import org.apache.commons.collections.CollectionUtils; 13 import org.apache.commons.collections.Predicate; 14 import org.apache.commons.collections.PredicateUtils; 15 import org.apache.commons.collections.functors.EqualPredicate; 16 17 import com.yanfuchang.work.common.Reflections; 18 19 /** 20 * @ClassName: ConditionFilterUtil 21 * @Description: 条件过滤工具类 22 */ 23 public class ConditionFilterUtil { 24 25 /** 26 * 用于处理属性与属性值的类 27 */ 28 public static class ConditionListMap { 29 private Map<String, List<Object>> map = new HashMap<String, List<Object>>(); 30 public List<Object> get(String key) { 31 return map.get(key); 32 }; 33 public Map<String, List<Object>> getMap() { 34 return map; 35 } 36 public void put(String key, Object value) { 37 List<Object> list = map.get(key); 38 if (list != null) { 39 list.add(value); 40 } else { 41 List<Object> arrayList = new ArrayList<Object>(); 42 arrayList.add(value); 43 map.put(key, arrayList); 44 } 45 } 46 } 47 48 /** 49 * 根据条件筛选出相应对象集合 50 */ 51 @SuppressWarnings("unchecked") 52 public static <T> List<T> findList(Collection<T> list, String propertyName, Object agers) { 53 if (agers == null) { // 如果是空则返回一个复制的集合 54 return new ArrayList<T>(list); 55 } 56 EqualPredicate parameter = new EqualPredicate(agers); 57 BeanPredicate tableCoulmn_paramerter = new BeanPredicate(propertyName, parameter); 58 List<T> filteredCollection = null; 59 synchronized (list) { 60 filteredCollection = (List<T>) CollectionUtils.select(list, tableCoulmn_paramerter); 61 } 62 return filteredCollection; 63 } 64 65 /** 66 * 根据条件筛选出相应对象集合,只对该对象本身的属性进行筛选,排除继承属性 67 */ 68 @SuppressWarnings("unchecked") 69 public static <T> List<T> findListByMultiCond(Collection<T> list, T obj) { 70 71 try { 72 if (obj == null) { // 如果是空则返回一个复制的集合 73 return new ArrayList<T>(list); 74 } 75 Predicate allPredicate = getAllPredicateByList(obj); 76 List<T> filteredCollection = null; 77 synchronized (list) { 78 filteredCollection = (List<T>) CollectionUtils.select(list, allPredicate); 79 } 80 return filteredCollection; 81 } catch (Exception e) { 82 e.printStackTrace(); 83 } 84 return new ArrayList<T>(); 85 } 86 87 /** 88 * 根据条件筛选出相应对象集合,只对该对象本身的属性进行筛选,排除继承属性 89 */ 90 @SuppressWarnings("unchecked") 91 public static <T> List<T> findListByMultiCond(List<T> list, T obj) { 92 93 try { 94 if (obj == null) { // 如果是空则返回一个复制的集合 95 return new ArrayList<T>(list); 96 } 97 Predicate allPredicate = getAllPredicateByList(obj); 98 List<T> filteredCollection = null; 99 synchronized (list) { 100 filteredCollection = (List<T>) CollectionUtils.select(list, allPredicate); 101 } 102 return filteredCollection; 103 } catch (Exception e) { 104 e.printStackTrace(); 105 } 106 return new ArrayList<T>(); 107 } 108 109 /** 110 * 获取满足所有条件的断言 111 */ 112 public static Predicate getAllPredicateByList(ConditionListMap conditionListMap) { 113 114 Map<String, List<Object>> propertyValMap = conditionListMap.getMap(); 115 List<Predicate> predicateList = new ArrayList<Predicate>(); 116 Set<Entry<String, List<Object>>> entrySet = propertyValMap.entrySet(); 117 for (Entry<String, List<Object>> entry : entrySet) { 118 String key = entry.getKey(); 119 List<Object> values = entry.getValue(); 120 for (Object value : values) { 121 EqualPredicate parameter = new EqualPredicate(value); 122 BeanPredicate tableCoulmn_paramerter = new BeanPredicate(key, parameter); 123 predicateList.add(tableCoulmn_paramerter); 124 125 } 126 } 127 Predicate predicate = PredicateUtils.allPredicate(predicateList); 128 return predicate; 129 } 130 131 /** 132 * 获取满足所有条件的断言 133 */ 134 public static Predicate getAllPredicateByList(Map<String, Object> propertyValMap) { 135 List<Predicate> predicateList = new ArrayList<Predicate>(); 136 Set<Entry<String, Object>> entrySet = propertyValMap.entrySet(); 137 for (Entry<String, Object> entry : entrySet) { 138 String key = entry.getKey(); 139 Object value = entry.getValue(); 140 141 EqualPredicate parameter = new EqualPredicate(value); 142 BeanPredicate tableCoulmn_paramerter = new BeanPredicate(key, parameter); 143 predicateList.add(tableCoulmn_paramerter); 144 } 145 Predicate predicate = PredicateUtils.allPredicate(predicateList); 146 return predicate; 147 } 148 149 /** 150 * 获取满足所有条件的断言 151 */ 152 public static <T> Predicate getAllPredicateByList(T obj) { 153 List<Predicate> predicateList = new ArrayList<Predicate>(); 154 Field[] declaredFields = obj.getClass().getDeclaredFields(); 155 for (Field field : declaredFields) { 156 if (Modifier.isFinal(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) 157 continue; // static或final遍历下一个 158 Object fieldValue = Reflections.getFieldValue(obj, field.getName()); 159 if (fieldValue == null) 160 continue; 161 EqualPredicate parameter = new EqualPredicate(fieldValue); 162 BeanPredicate tableCoulmn_paramerter = new BeanPredicate(field.getName(), parameter); 163 predicateList.add(tableCoulmn_paramerter); 164 } 165 166 Predicate finalPredicate = PredicateUtils.allPredicate(predicateList); 167 return finalPredicate; 168 } 169 170 /** 171 * 获取满足任意条件的断言 172 */ 173 public static Predicate getAnyPredicateByList(ConditionListMap conditionListMap) { 174 175 Map<String, List<Object>> propertyValMap = conditionListMap.getMap(); 176 List<Predicate> predicateList = new ArrayList<Predicate>(); 177 Set<Entry<String, List<Object>>> entrySet = propertyValMap.entrySet(); 178 for (Entry<String, List<Object>> entry : entrySet) { 179 String key = entry.getKey(); 180 List<Object> values = entry.getValue(); 181 for (Object value : values) { 182 EqualPredicate parameter = new EqualPredicate(value); 183 BeanPredicate tableCoulmn_paramerter = new BeanPredicate(key, parameter); 184 predicateList.add(tableCoulmn_paramerter); 185 186 } 187 } 188 Predicate predicate = PredicateUtils.anyPredicate(predicateList); 189 return predicate; 190 } 191 192 /** 193 * 获取满足任意条件的断言 194 */ 195 public static Predicate getAnyPredicateByList(Map<String, List<Object>> propertyValMap) { 196 197 List<Predicate> predicateList = new ArrayList<Predicate>(); 198 Set<Entry<String, List<Object>>> entrySet = propertyValMap.entrySet(); 199 for (Entry<String, List<Object>> entry : entrySet) { 200 String key = entry.getKey(); 201 List<Object> values = entry.getValue(); 202 for (Object value : values) { 203 EqualPredicate parameter = new EqualPredicate(value); 204 BeanPredicate tableCoulmn_paramerter = new BeanPredicate(key, parameter); 205 predicateList.add(tableCoulmn_paramerter); 206 207 } 208 } 209 Predicate predicate = PredicateUtils.anyPredicate(predicateList); 210 return predicate; 211 } 212 213 /** 214 * 获取满足所有条件的断言 215 */ 216 public static <T> Predicate getAnyPredicateByList(T obj) { 217 List<Predicate> predicateList = new ArrayList<Predicate>(); 218 Field[] declaredFields = obj.getClass().getDeclaredFields(); 219 for (Field field : declaredFields) { 220 if (Modifier.isFinal(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) 221 continue; // static或final遍历下一个 222 Object fieldValue = Reflections.getFieldValue(obj, field.getName()); 223 if (fieldValue == null) 224 continue; 225 EqualPredicate parameter = new EqualPredicate(fieldValue); 226 BeanPredicate tableCoulmn_paramerter = new BeanPredicate(field.getName(), parameter); 227 predicateList.add(tableCoulmn_paramerter); 228 } 229 230 Predicate finalPredicate = PredicateUtils.anyPredicate(predicateList); 231 return finalPredicate; 232 } 233 234 @SuppressWarnings("unchecked") 235 public static <T> T getFirst(Collection<T> list, String propertyName, Object agers) { 236 EqualPredicate parameter = new EqualPredicate(agers); 237 BeanPredicate tableCoulmn_paramerter = new BeanPredicate(propertyName, parameter); 238 T finalResult = null; 239 synchronized (list) { 240 finalResult = (T) CollectionUtils.find(list, tableCoulmn_paramerter); 241 } 242 return finalResult; 243 } 244 245 /** 246 * 根据条件筛选出相应对象集合 247 */ 248 @SuppressWarnings("unchecked") 249 public static <T> T getFirst(List<T> list, String propertyName, Object agers) { 250 Predicate finalPredicate = null; 251 if (agers == null) { 252 finalPredicate = PredicateUtils.truePredicate(); 253 } else { 254 EqualPredicate parameter = new EqualPredicate(agers); 255 BeanPredicate tableCoulmn_paramerter = new BeanPredicate(propertyName, parameter); 256 T finalResult = null; 257 synchronized (list) { 258 finalResult = (T) CollectionUtils.find(list, tableCoulmn_paramerter); 259 } 260 return finalResult; 261 } 262 return null; 263 } 264 265 @SuppressWarnings("unchecked") 266 public static <T> T getFirstByMultiCond(Collection<T> list, T obj) { 267 Predicate allPredicateByList = getAllPredicateByList(obj); 268 T finalResult = null; 269 synchronized (list) { 270 finalResult = (T) CollectionUtils.find(list, allPredicateByList); 271 } 272 return finalResult; 273 } 274 275 /** 276 * 根据条件筛选出相应对象集合 277 */ 278 @SuppressWarnings("unchecked") 279 public static <T> T getFirstByMultiCond(List<T> list, T obj) { 280 Predicate allPredicateByList = getAllPredicateByList(obj); 281 T finalResult = null; 282 synchronized (list) { 283 finalResult = (T) CollectionUtils.find(list, allPredicateByList); 284 } 285 return finalResult; 286 } 287 288 /** 289 * 获得ConditionListMap 290 */ 291 public static ConditionListMap newConditionListMap() { 292 return new ConditionListMap(); 293 } 294 }
依赖:
1 <dependency> 2 <groupId>commons-beanutils</groupId> 3 <artifactId>commons-beanutils</artifactId> 4 <version>1.9.1</version> 5 <exclusions> 6 <exclusion> 7 <groupId>commons-logging</groupId> 8 <artifactId>commons-logging</artifactId> 9 </exclusion> 10 </exclusions> 11 </dependency>