diff --git a/testable-core/src/main/java/com/alibaba/testable/core/model/Pair.java b/testable-core/src/main/java/com/alibaba/testable/core/model/Pair.java new file mode 100644 index 0000000..2305888 --- /dev/null +++ b/testable-core/src/main/java/com/alibaba/testable/core/model/Pair.java @@ -0,0 +1,33 @@ +package com.alibaba.testable.core.model; + +import java.io.Serializable; + +/** + * @author flin + */ +public class Pair implements Serializable { + + private static final long serialVersionUID = -5197546316467446976L; + + /** Left object */ + private L left; + /** Right object */ + private R right; + + public Pair(L left, R right) { + this.left = left; + this.right = right; + } + + public L getLeft() { + return left; + } + + public R getRight() { + return right; + } + + public static Pair of(L l, R r) { + return new Pair(l, r); + } +} diff --git a/testable-core/src/main/java/com/alibaba/testable/core/util/CollectionUtil.java b/testable-core/src/main/java/com/alibaba/testable/core/util/CollectionUtil.java index 0c2bb61..51fb660 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/util/CollectionUtil.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/util/CollectionUtil.java @@ -1,10 +1,14 @@ package com.alibaba.testable.core.util; -import java.util.Collection; -import java.util.Iterator; +import com.alibaba.testable.core.model.Pair; + +import java.util.*; public class CollectionUtil { + /** + * Get slice of args[pos, args.length] + */ public static Object[] slice(Object[] args, int pos) { int size = args.length - pos; if (size <= 0) { @@ -15,6 +19,9 @@ public class CollectionUtil { return slicedArgs; } + /** + * Join a collection to string + */ public static String join(Collection collection, String joinSymbol) { StringBuilder sb = new StringBuilder(); for(Iterator i = collection.iterator(); i.hasNext(); sb.append((String)i.next())) { @@ -25,6 +32,9 @@ public class CollectionUtil { return sb.toString(); } + /** + * Check whether target exist in collection + */ public static boolean contains(T[] collection, T target) { for (T item : collection) { if (target.equals(item)) { @@ -33,4 +43,39 @@ public class CollectionUtil { } return false; } + + /** + * Create an array + */ + public static T[] arrayOf(T... items) { + return items; + } + + /** + * Create a list + */ + public static List listOf(T... items) { + return Arrays.asList(items); + } + + /** + * Create a map + */ + public static Map mapOf(Pair... pair) { + return mapOf(new HashMap(pair.length), pair); + } + + /** + * Create an ordered map + */ + public static Map orderMapOf(Pair... pair) { + return mapOf(new LinkedHashMap(pair.length), pair); + } + + private static Map mapOf(Map kvs, Pair[] pair) { + for (Pair p : pair) { + kvs.put(p.getLeft(), p.getRight()); + } + return kvs; + } }