diff --git a/platform/util/src/com/intellij/util/containers/CollectionFactory.java b/platform/util/src/com/intellij/util/containers/CollectionFactory.java index 11f0326cc6fd..354d581052be 100644 --- a/platform/util/src/com/intellij/util/containers/CollectionFactory.java +++ b/platform/util/src/com/intellij/util/containers/CollectionFactory.java @@ -31,31 +31,42 @@ public class CollectionFactory { } @NotNull - public static Set hashSet() { + public static HashSet hashSet() { return new HashSet(); } @NotNull - public static Set hashSet(@NotNull Collection elements) { + public static HashSet hashSet(@NotNull Collection elements) { return new HashSet(elements); } @NotNull - public static Set hashSet(@NotNull T... elements) { + public static HashSet hashSet(@NotNull T... elements) { return hashSet(Arrays.asList(elements)); } + @NotNull public static LinkedHashSet linkedHashSet() { return new LinkedHashSet(); } @NotNull - public static Set troveSet(@NotNull T... elements) { + public static LinkedHashSet linkedHashSet(@NotNull Collection elements) { + return new LinkedHashSet(elements); + } + + @NotNull + public static LinkedHashSet linkedHashSet(@NotNull T... elements) { + return linkedHashSet(Arrays.asList(elements)); + } + + @NotNull + public static THashSet troveSet(@NotNull T... elements) { return troveSet(Arrays.asList(elements)); } @NotNull - public static Set troveSet(@NotNull Collection elements) { + public static THashSet troveSet(@NotNull Collection elements) { return new THashSet(elements); } @@ -90,31 +101,47 @@ public class CollectionFactory { } @NotNull - public static HashMap hashMap() { - return new HashMap(); + public static HashMap hashMap() { + return new HashMap(); } @NotNull - public static TreeMap treeMap() { - return new TreeMap(); + public static HashMap hashMap(@NotNull Map map) { + return new HashMap(map); } @NotNull - public static THashMap troveMap() { - return new THashMap(); - } - - public static LinkedHashMap linkedHashMap() { - return new LinkedHashMap(); + public static TreeMap treeMap() { + return new TreeMap(); } @NotNull - public static Map hashMap(@NotNull final List keys, @NotNull final List values) { + public static THashMap troveMap() { + return new THashMap(); + } + + @NotNull + public static LinkedHashMap linkedHashMap() { + return new LinkedHashMap(); + } + + @NotNull + public static , V> EnumMap enumMap(@NotNull Class klass) { + return new EnumMap(klass); + } + + @NotNull + public static , V> EnumMap enumMap(@NotNull Map map) { + return new EnumMap(map); + } + + @NotNull + public static Map hashMap(@NotNull final List keys, @NotNull final List values) { if (keys.size() != values.size()) { throw new IllegalArgumentException(keys + " should have same length as " + values); } - final HashMap map = new HashMap(); + final HashMap map = new HashMap(); for (int i = 0; i < keys.size(); ++i) { map.put(keys.get(i), values.get(i)); } @@ -132,7 +159,7 @@ public class CollectionFactory { } @NotNull - public static ArrayList arrayList(Collection elements) { + public static ArrayList arrayList(@NotNull Collection elements) { return new ArrayList(elements); } @@ -141,6 +168,15 @@ public class CollectionFactory { return arrayList(Arrays.asList(elements)); } + @NotNull + public static ArrayList arrayList(@NotNull Iterable elements) { + if (elements instanceof Collection) { + return arrayList((Collection)elements); + } + + return copy(elements, CollectionFactory.arrayList()); + } + @NotNull public static List arrayList(@NotNull final T[] elements, final int start, final int end) { if (start < 0 || start > end || end > elements.length) throw new IllegalArgumentException("start:" + start + " end:" + end + " length:" + elements.length); @@ -161,6 +197,38 @@ public class CollectionFactory { }; } + @NotNull + public static LinkedList linkedList() { + return new LinkedList(); + } + + @NotNull + public static LinkedList linkedList(@NotNull Collection elements) { + return new LinkedList(elements); + } + + @NotNull + public static LinkedList linkedList(T... elements) { + return linkedList(Arrays.asList(elements)); + } + + @NotNull + public static LinkedList linkedList(@NotNull Iterable elements) { + if (elements instanceof Collection) { + return linkedList((Collection)elements); + } + + return copy(elements, CollectionFactory.linkedList()); + } + + @NotNull + private static > C copy(@NotNull Iterable

source, @NotNull C target) { + for (P e : source) { + target.add(e); + } + return target; + } + @NotNull public static Stack stack() { return new Stack();