diff --git a/modules/utils/src/main/java/org/springside/modules/utils/Collections3.java b/modules/utils/src/main/java/org/springside/modules/utils/Collections3.java index dfcf267c..4d744b89 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/Collections3.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/Collections3.java @@ -18,20 +18,125 @@ import org.apache.commons.lang3.StringUtils; /** * Collections工具集. * - * 在JDK的Collections和Guava的Collections2后, 命名为Collections3. + * 主要由三部分组成: * - * 函数主要由两部分组成,一是自反射提取元素的功能,二是源自Apache Commons Collection, 争取不用在项目里引入它。 + * 1. 是否Empty的最常用函数 + * + * 2. 源自Apache Commons Collection, 争取不用在项目里引入它。 + * + * 3. 反射提取集合种元素及其属性的功能 + * + * 在JDK的Collections和Guava的Collections2后, 命名为Collections3. * * 另请直接使用: * - * JDK Collections的SingletonMap/SingletonList/EmptyMap/EmptyList/EmptyMap + * 1. JDK Collections的SingletonMap/SingletonList/EmptyMap/EmptyList/EmptyMap * - * Guava Lists/Maps的newArrayLists(Element...elements)等函数 + * 2. Guava Lists/Maps的newArrayLists(Element...elements)等函数 * * @author calvin */ +@SuppressWarnings("rawtypes") public class Collections3 { + /** + * 判断是否为空. + */ + public static boolean isEmpty(Collection collection) { + return (collection == null) || collection.isEmpty(); + } + + /** + * 判断是否为空. + */ + public static boolean isEmpty(Map map) { + return (map == null) || map.isEmpty(); + } + + /** + * 判断是否不为空. + */ + public static boolean isNotEmpty(Collection collection) { + return (collection != null) && !(collection.isEmpty()); + } + + /** + * 判断是否不为空. + */ + public static boolean isNotEmpty(Map map) { + return (map != null) && !(map.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; + } + + // 当类型为array[]做内部结构的类型时,直接取得最后一个元素. + if (collection instanceof ArrayList) { + 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; + } + /** * 提取集合中的对象的两个属性(通过Getter函数), 组合成Map. * @@ -106,94 +211,4 @@ public class Collections3 { return builder.toString(); } - /** - * 判断是否为空. - */ - public static boolean isEmpty(Collection collection) { - return (collection == null) || collection.isEmpty(); - } - - /** - * 判断是否为空. - */ - public static boolean isEmpty(Map map) { - return (map == null) || map.isEmpty(); - } - - /** - * 判断是否为空. - */ - public static boolean isNotEmpty(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; - } }