#21 整理基础类的使用并更新文档, Collections类

This commit is contained in:
Calvin
2012-05-19 23:20:01 +08:00
parent 9270054cab
commit 3e76c076d9
8 changed files with 194 additions and 81 deletions
@@ -60,14 +60,14 @@ public class RsResponse {
}
/**
* 创建类型为"application/plain"的Response。
* 创建类型为"application/json"的Response。
*/
public static Response buildJsonResponse(Status status, Object content) {
return buildJsonResponse(status.getStatusCode(), content);
}
/**
* 创建类型为"application/plain"的Response。
* 创建类型为"application/json"的Response。
*/
public static Response buildJsonResponse(int status, Object content) {
return Response.status(status).entity(content).type(MediaType.APPLICATION_JSON).build();
@@ -8,6 +8,7 @@ package org.springside.modules.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -23,18 +24,7 @@ import org.apache.commons.lang3.StringUtils;
public class Collections3 {
/**
* 返回a-b的集合.
*/
public static <T> List<T> subtract(final Collection<T> a, final Collection<T> b) {
ArrayList<T> list = new ArrayList<T>(a);
for (Object element : b) {
list.remove(element);
}
return list;
}
/**
* 提取集合中的对象的属性(通过Getter函数), 组合成Map.
* 提取集合中的对象的两个属性(通过Getter函数), 组合成Map.
*
* @param collection 来源集合.
* @param keyPropertyName 要提取为Map中的Key值的属性名.
@@ -57,7 +47,7 @@ public class Collections3 {
}
/**
* 提取集合中的对象的属性(通过Getter函数), 组合成List.
* 提取集合中的对象的一个属性(通过Getter函数), 组合成List.
*
* @param collection 来源集合.
* @param propertyName 要提取的属性名.
@@ -77,7 +67,7 @@ public class Collections3 {
}
/**
* 提取集合中的对象的属性(通过Getter函数), 组合成由分割符分隔的字符串.
* 提取集合中的对象的一个属性(通过Getter函数), 组合成由分割符分隔的字符串.
*
* @param collection 来源集合.
* @param propertyName 要提取的属性名.
@@ -89,14 +79,14 @@ public class Collections3 {
}
/**
* 转换Collection为String, 中间以 separator分隔。
* 转换Collection所有元素(通过toString())为String, 中间以 separator分隔。
*/
public static String convertToString(final Collection collection, final String separator) {
return StringUtils.join(collection, separator);
}
/**
* 转换Collection为String, 每个元素的前面加入prefix,后面加入postfix,如<div>mymessage</div>。
* 转换Collection所有元素(通过toString())为String, 每个元素的前面加入prefix,后面加入postfix,如<div>mymessage</div>。
*/
public static String convertToString(final Collection collection, final String prefix, final String postfix) {
StringBuilder builder = new StringBuilder();
@@ -105,4 +95,79 @@ public class Collections3 {
}
return builder.toString();
}
/**
* 判断是否为空.
*/
public static boolean isEmpty(Collection collection) {
return (collection == null || collection.isEmpty());
}
/**
* 取得Collection的第一个元素,如果collection为空返回null.
*/
public static <T> T getFirst(Collection<T> collection) {
if (isEmpty(collection)) {
return null;
}
return collection.iterator().next();
}
/**
* 获取Collection的最后一个元素 ,如果collection为空返回null.
*/
public static <T> T getLast(Collection<T> collection) {
if (isEmpty(collection)) {
return null;
}
if (collection instanceof List) {
List<T> list = (List<T>) collection;
return list.get(list.size() - 1);
}
Iterator<T> iterator = collection.iterator();
while (true) {
T current = iterator.next();
if (!iterator.hasNext()) {
return current;
}
}
}
/**
* 返回a+b的新List.
*/
public static <T> List<T> union(final Collection<T> a, final Collection<T> b) {
List<T> result = new ArrayList<T>(a);
result.addAll(b);
return result;
}
/**
* 返回a-b的新List.
*/
public static <T> List<T> subtract(final Collection<T> a, final Collection<T> b) {
List<T> list = new ArrayList<T>(a);
for (T element : b) {
list.remove(element);
}
return list;
}
/**
* 返回a与b的交集的新List.
*/
public static <T> List<T> intersection(Collection<T> a, Collection<T> b) {
List<T> list = new ArrayList<T>();
for (T element : a) {
if (b.contains(element)) {
list.add(element);
}
}
return list;
}
}
@@ -25,9 +25,6 @@ import org.slf4j.LoggerFactory;
* @author calvin
*/
public class Reflections {
private static final String CGLIB_CLASS_SEPARATOR = "$$";
private static final String SETTER_PREFIX = "set";
private static final String GETTER_PREFIX = "get";
@@ -212,7 +209,7 @@ public class Reflections {
}
/**
* 通过反射, 获得Class定义中声明的父类的泛型参数的类型.
* 通过反射, 获得Class定义中声明的泛型参数的类型, 注意泛型必须定义在父类处
* 如无法找到, 返回Object.class.
* eg.
* public UserDao extends HibernateDao<User>
@@ -220,8 +217,8 @@ public class Reflections {
* @param clazz The class to introspect
* @return the first generic declaration, or Object.class if cannot be determined
*/
public static <T> Class<T> getSuperClassGenricType(final Class clazz) {
return getSuperClassGenricType(clazz, 0);
public static <T> Class<T> getClassGenricType(final Class clazz) {
return getClassGenricType(clazz, 0);
}
/**
@@ -234,7 +231,7 @@ public class Reflections {
* @param index the Index of the generic ddeclaration,start from 0.
* @return the index generic declaration, or Object.class if cannot be determined
*/
public static Class getSuperClassGenricType(final Class clazz, final int index) {
public static Class getClassGenricType(final Class clazz, final int index) {
Type genType = clazz.getGenericSuperclass();
@@ -258,19 +255,6 @@ public class Reflections {
return (Class) params[index];
}
/**
* 对于被cglib AOP过的对象, 取得真实的Class类型.
*/
public static Class<?> getUnproxyClass(Class<?> clazz) {
if (clazz != null && clazz.getName().contains(CGLIB_CLASS_SEPARATOR)) {
Class<?> superClass = clazz.getSuperclass();
if (superClass != null && !Object.class.equals(superClass)) {
return superClass;
}
}
return clazz;
}
/**
* 将反射时的checked exception转换为unchecked exception.
*/
@@ -92,14 +92,14 @@ public class ReflectionsTest {
@Test
public void getSuperClassGenricType() {
//获取第1,2个泛型类型
assertEquals(String.class, Reflections.getSuperClassGenricType(TestBean.class));
assertEquals(Long.class, Reflections.getSuperClassGenricType(TestBean.class, 1));
assertEquals(String.class, Reflections.getClassGenricType(TestBean.class));
assertEquals(Long.class, Reflections.getClassGenricType(TestBean.class, 1));
//定义父类时无泛型定义
assertEquals(Object.class, Reflections.getSuperClassGenricType(TestBean2.class));
assertEquals(Object.class, Reflections.getClassGenricType(TestBean2.class));
//无父类定义
assertEquals(Object.class, Reflections.getSuperClassGenricType(TestBean3.class));
assertEquals(Object.class, Reflections.getClassGenricType(TestBean3.class));
}
@Test