一、前言
关于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 <T> List<T> list(T[] elements)@b@ {@b@ if (elements == null) return null;@b@ return new ArrayList(Arrays.asList(elements));@b@ }@b@@b@ public static <T> Collection<T> duplicatesFrom(Collection<T> 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 <T> List<T> filter(Collection<?> target, CollectionFilter<T> 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<T>@b@{@b@ public abstract List<T> 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<T>@b@ implements CollectionFilter<T>@b@{@b@ private final Class<T> type;@b@@b@ public static <T> TypeFilter<T> byType(Class<T> type)@b@ {@b@ return new TypeFilter(type);@b@ }@b@@b@ TypeFilter(Class<T> type)@b@ {@b@ this.type = type;@b@ }@b@@b@ public List<T> 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 2 java/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 4 org/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$ < len$; ) { Object o = arr$[i$]; b.append(o); ++i$; }@b@ return b.toString();@b@ }@b@@b@ public static StringsToJoin join(String[] strings)@b@ {@b@ return new StringsToJoin(strings);@b@ }@b@@b@ public static StringToAppend append(String toAppend)@b@ {@b@ return new StringToAppend(toAppend);@b@ }@b@@b@ public static class StringToAppend@b@ {@b@ private final String toAppend;@b@@b@ StringToAppend(String toAppend)@b@ {@b@ this.toAppend = toAppend;@b@ }@b@@b@ public String to(String s)@b@ {@b@ if (!(s.endsWith(this.toAppend))) return Strings.concat(new Object[] { s, this.toAppend });@b@ return s;@b@ }@b@ }@b@@b@ public static class StringsToJoin@b@ {@b@ private final String[] strings;@b@@b@ StringsToJoin(String[] strings)@b@ {@b@ this.strings = strings;@b@ }@b@@b@ public String with(String delimeter)@b@ {@b@ if (delimeter == null) throw new IllegalArgumentException("Delimiter should not be null");@b@ if (Arrays.isEmpty(this.strings)) return "";@b@ StringBuilder b = new StringBuilder();@b@ int stringCount = this.strings.length;@b@ for (int i = 0; i < stringCount; ++i) {@b@ String s = this.strings[i];@b@ b.append((s != null) ? s : "");@b@ if (i < stringCount - 1) b.append(delimeter);@b@ }@b@ return b.toString();@b@ }@b@ }@b@}