一、前言
关于fest-util源码包org.fest.util.Collections集合处理类,实现对数组转换List序列集合、集合duplicatesFrom克隆复制、集合判空isEmpty、并基于org.fest.util.CollectionFilter接口对集合过滤、基于org.fest.util.Strings对集合进行格式化。
二、源码说明
1.Collections集合类package org.fest.util;@b@@b@import java.util.ArrayList;@b@import java.util.Arrays;@b@import java.util.Collection;@b@import java.util.HashSet;@b@import java.util.Iterator;@b@import java.util.List;@b@import java.util.Set;@b@@b@public final class Collections@b@{@b@ public static List list(T[] elements)@b@ {@b@ if (elements == null) return null;@b@ return new ArrayList(Arrays.asList(elements));@b@ }@b@@b@ public static Collection duplicatesFrom(Collection c)@b@ {@b@ Set duplicates = new HashSet();@b@ if (isEmpty(c)) return duplicates;@b@ Set onlyOne = new HashSet();@b@ Iterator i$ = c.iterator();@b@ while (true) { Object e;@b@ while (true) { if (!(i$.hasNext())) break label84; e = i$.next();@b@ if (!(onlyOne.contains(e))) break;@b@ duplicates.add(e);@b@ }@b@@b@ onlyOne.add(e);@b@ }@b@ label84: return duplicates;@b@ }@b@@b@ public static boolean isEmpty(Collection> c)@b@ {@b@ return ((c == null) || (c.isEmpty()));@b@ }@b@@b@ public static List filter(Collection> target, CollectionFilter filter)@b@ {@b@ return filter.filter(target);@b@ }@b@@b@ public static String format(Collection> c)@b@ {@b@ if (c == null) return null;@b@ Iterator i = c.iterator();@b@ if (!(i.hasNext())) return "[]";@b@ StringBuilder b = new StringBuilder();@b@ b.append('[');@b@ while (true) {@b@ Object e = i.next();@b@ b.append((e == c) ? "(this Collection)" : Strings.quote(e));@b@ if (!(i.hasNext())) return ']';@b@ b.append(", ");@b@ }@b@ }@b@}
2.CollectionFilter过滤接口类、TypeFilter对应实现类package org.fest.util;@b@@b@import java.util.Collection;@b@import java.util.List;@b@@b@public abstract interface CollectionFilter@b@{@b@ public abstract List filter(Collection> paramCollection);@b@}package org.fest.util;@b@@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.Iterator;@b@import java.util.List;@b@@b@public class TypeFilter@b@ implements CollectionFilter@b@{@b@ private final Class type;@b@@b@ public static TypeFilter byType(Class type)@b@ {@b@ return new TypeFilter(type);@b@ }@b@@b@ TypeFilter(Class type)@b@ {@b@ this.type = type;@b@ }@b@@b@ public List filter(Collection> target)@b@ {@b@ if (target == null) throw new IllegalArgumentException("The collection to filter should not be null");@b@ List filtered = new ArrayList();@b@ Iterator i$ = target.iterator();@b@ while (true) { Object o;@b@ while (true) { if (!(i$.hasNext())) break label81; o = i$.next();@b@ if (o != null) break; @b@ }@b@ if (this.type.isAssignableFrom(o.getClass())) filtered.add(o);@b@ }@b@ label81: return filtered;@b@ }@b@}
3. Strings类package org.fest.util;@b@@b@public final class Strings@b@{@b@ public static boolean isEmpty(String s)@b@ {@b@ return ((s == null) || (s.length() == 0)); } @b@ public static String quote(String s) { // Byte code:@b@ // 0: aload_0@b@ // 1: ifnull +27 -> 28@b@ // 4: iconst_3@b@ // 5: anewarray 2java/lang/Object@b@ // 8: dup@b@ // 9: iconst_0@b@ // 10: ldc 3@b@ // 12: aastore@b@ // 13: dup@b@ // 14: iconst_1@b@ // 15: aload_0@b@ // 16: aastore@b@ // 17: dup@b@ // 18: iconst_2@b@ // 19: ldc 3@b@ // 21: aastore@b@ // 22: invokestatic 4org/fest/util/Strings:concat([Ljava/lang/Object;)Ljava/lang/String;@b@ // 25: goto +4 -> 29@b@ // 28: aconst_null@b@ // 29: areturn } @b@ public static Object quote(Object o) { return ((o instanceof String) ? quote(o.toString()) : o);@b@ }@b@@b@ public static String concat(Object[] objects)@b@ {@b@ if (Arrays.isEmpty(objects)) return null;@b@ StringBuilder b = new StringBuilder();@b@ Object[] arr$ = objects; int len$ = arr$.length; for (int i$ = 0; i$