com.minlia.cloud.utils.Collections3 Maven / Gradle / Ivy
/**
* Copyright (c) 2005-2012 springside.org.cn Licensed under the Apache License, Version 2.0 (the "License");
*/
package com.minlia.cloud.utils;
import com.google.common.collect.Lists;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.*;
/**
* 反射工具类. 提供调用getter/setter方法, 访问私有变量, 调用私有方法, 获取泛型类型Class, 被AOP过的真实类等工具函数.
*
* @author calvin
* @version 2013-01-15
*/
@SuppressWarnings("rawtypes")
public class Collections3 {
/**
* 提取集合中的对象的两个属性(通过Getter函数), 组合成Map.
*
* @param collection 来源集合.
* @param keyPropertyName 要提取为Map中的Key值的属性名.
* @param valuePropertyName 要提取为Map中的Value值的属性名.
*/
@SuppressWarnings("unchecked")
public static Map extractToMap(final Collection collection, final String keyPropertyName, final String valuePropertyName) {
Map map = new HashMap(collection.size());
try {
for (Object obj : collection) {
map.put(PropertyUtils.getProperty(obj, keyPropertyName), PropertyUtils.getProperty(obj, valuePropertyName));
}
} catch (Exception e) {
throw Reflections.convertReflectionExceptionToUnchecked(e);
}
return map;
}
/**
* 提取集合中的对象的一个属性(通过Getter函数), 组合成List.
*
* @param collection 来源集合.
* @param propertyName 要提取的属性名.
*/
@SuppressWarnings("unchecked")
public static List extractToList(final Collection collection, final String propertyName) {
List list = Lists.newArrayList();
try {
if (collection != null) {
Object item = null;
for (Object obj : collection) {
if (obj instanceof Map) {
item = ((Map) obj).get(propertyName);
} else {
item = PropertyUtils.getProperty(obj, propertyName);
}
if (item != null)
list.add(item);
}
}
} catch (Exception e) {
throw Reflections.convertReflectionExceptionToUnchecked(e);
}
return list;
}
/**
* 提取集合中的对象的一个属性(通过Getter函数), 组合成由分割符分隔的字符串.
*
* @param collection 来源集合.
* @param propertyName 要提取的属性名.
* @param separator 分隔符.
*/
public static String extractToString(final Collection collection, final String propertyName, final String separator) {
String rs = "";
if (collection != null) {
List list = extractToList(collection, propertyName);
rs = StringUtils.join(list, separator);
}
return rs;
}
/**
* 转换Collection所有元素(通过toString())为String, 中间以 separator分隔。
*/
public static String convertToString(final Collection collection, final String separator) {
return StringUtils.join(collection, separator);
}
/**
* 转换Collection所有元素(通过toString())为String, 每个元素的前面加入prefix,后面加入postfix,如mymessage。
*/
public static String convertToString(final Collection collection, final String prefix, final String postfix) {
StringBuilder builder = new StringBuilder();
for (Object o : collection) {
builder.append(prefix).append(o).append(postfix);
}
return builder.toString();
}
/**
* 判断是否为空.
*/
public static boolean isEmpty(Collection collection) {
return (collection == null || collection.isEmpty());
}
/**
* 取得Collection的第一个元素,如果collection为空返回null.
*/
public static T getFirst(Collection collection) {
if (isEmpty(collection)) {
return null;
}
return collection.iterator().next();
}
/**
* 获取Collection的最后一个元素 ,如果collection为空返回null.
*/
public static T getLast(Collection collection) {
if (isEmpty(collection)) {
return null;
}
// 当类型为List时,直接取得最后一个元素 。
if (collection instanceof List) {
List list = (List) collection;
return list.get(list.size() - 1);
}
// 其他类型通过iterator滚动到最后一个元素.
Iterator iterator = collection.iterator();
while (true) {
T current = iterator.next();
if (!iterator.hasNext()) {
return current;
}
}
}
/**
* 返回a+b的新List.
*/
public static List union(final Collection a, final Collection b) {
List result = new ArrayList(a);
result.addAll(b);
return result;
}
/**
* 返回a-b的新List.
*/
public static List subtract(final Collection a, final Collection b) {
List list = new ArrayList(a);
for (T element : b) {
list.remove(element);
}
return list;
}
/**
* 返回a与b的交集的新List.
*/
public static List intersection(Collection a, Collection b) {
List list = new ArrayList();
for (T element : a) {
if (b.contains(element)) {
list.add(element);
}
}
return list;
}
/**
* 判断集合中对象的某一属性是否与给定的值相同,true 返回该对象 false 则返回null
*
* @return
*/
public static T checkCollectionProperty(List targetList, String propertyName, Object val) {
T t = null;
if (PreconditionsHelper.isNotEmpty(targetList))
for (T item : targetList) {
if (Reflections.getFieldValue(item, propertyName).equals(val)) {
t = item;
break;
}
}
return t;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy