diff options
Diffstat (limited to 'platform/util-rt/src/com/intellij/util/containers/ContainerUtilRt.java')
-rw-r--r-- | platform/util-rt/src/com/intellij/util/containers/ContainerUtilRt.java | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/platform/util-rt/src/com/intellij/util/containers/ContainerUtilRt.java b/platform/util-rt/src/com/intellij/util/containers/ContainerUtilRt.java index e4b3153917f7..a89de17fe6fe 100644 --- a/platform/util-rt/src/com/intellij/util/containers/ContainerUtilRt.java +++ b/platform/util-rt/src/com/intellij/util/containers/ContainerUtilRt.java @@ -18,6 +18,7 @@ package com.intellij.util.containers; import com.intellij.openapi.util.Pair; import com.intellij.util.ArrayUtilRt; import com.intellij.util.Function; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -40,16 +41,19 @@ public class ContainerUtilRt { private static final int ARRAY_COPY_THRESHOLD = 20; @NotNull + @Contract(pure=true) public static <K, V> HashMap<K, V> newHashMap() { return new com.intellij.util.containers.HashMap<K, V>(); } @NotNull + @Contract(pure=true) public static <K, V> HashMap<K, V> newHashMap(@NotNull Map<K, V> map) { return new com.intellij.util.containers.HashMap<K, V>(map); } @NotNull + @Contract(pure=true) public static <K, V> Map<K, V> newHashMap(@NotNull List<K> keys, @NotNull List<V> values) { if (keys.size() != values.size()) { throw new IllegalArgumentException(keys + " should have same length as " + values); @@ -63,6 +67,7 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <K, V> Map<K,V> newHashMap(@NotNull Pair<K, V> first, Pair<K, V>[] entries) { Map<K, V> map = newHashMap(); map.put(first.getFirst(), first.getSecond()); @@ -73,26 +78,43 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) + public static <K, V> Map<K, V> newHashMap(int initialCapacity) { + return new com.intellij.util.containers.HashMap<K, V>(initialCapacity); + } + + @NotNull + @Contract(pure=true) public static <K extends Comparable, V> TreeMap<K, V> newTreeMap() { return new TreeMap<K, V>(); } @NotNull + @Contract(pure=true) public static <K extends Comparable, V> TreeMap<K, V> newTreeMap(@NotNull Map<K, V> map) { return new TreeMap<K, V>(map); } @NotNull + @Contract(pure=true) public static <K, V> LinkedHashMap<K, V> newLinkedHashMap() { return new com.intellij.util.containers.LinkedHashMap<K, V>(); } @NotNull + @Contract(pure=true) + public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(int capacity) { + return new com.intellij.util.containers.LinkedHashMap<K, V>(capacity); + } + + @NotNull + @Contract(pure=true) public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(@NotNull Map<K, V> map) { return new com.intellij.util.containers.LinkedHashMap<K, V>(map); } @NotNull + @Contract(pure=true) public static <K, V> LinkedHashMap<K,V> newLinkedHashMap(@NotNull Pair<K, V> first, Pair<K, V>[] entries) { LinkedHashMap<K, V> map = newLinkedHashMap(); map.put(first.getFirst(), first.getSecond()); @@ -103,11 +125,13 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> LinkedList<T> newLinkedList() { return new LinkedList<T>(); } @NotNull + @Contract(pure=true) public static <T> LinkedList<T> newLinkedList(@NotNull T... elements) { final LinkedList<T> list = newLinkedList(); Collections.addAll(list, elements); @@ -115,16 +139,19 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> LinkedList<T> newLinkedList(@NotNull Iterable<? extends T> elements) { return copy(ContainerUtilRt.<T>newLinkedList(), elements); } @NotNull + @Contract(pure=true) public static <T> ArrayList<T> newArrayList() { return new ArrayList<T>(); } @NotNull + @Contract(pure=true) public static <T> ArrayList<T> newArrayList(@NotNull T... elements) { ArrayList<T> list = newArrayListWithCapacity(elements.length); Collections.addAll(list, elements); @@ -132,6 +159,7 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> ArrayList<T> newArrayList(@NotNull Iterable<? extends T> elements) { if (elements instanceof Collection) { @SuppressWarnings("unchecked") Collection<? extends T> collection = (Collection<? extends T>)elements; @@ -141,11 +169,13 @@ public class ContainerUtilRt { } /** @deprecated Use {@link #newArrayListWithCapacity(int)} (to remove in IDEA 15) */ + @Contract(pure=true) public static <T> ArrayList<T> newArrayListWithExpectedSize(int size) { return newArrayListWithCapacity(size); } @NotNull + @Contract(pure=true) public static <T> ArrayList<T> newArrayListWithCapacity(int size) { return new ArrayList<T>(size); } @@ -159,21 +189,25 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> HashSet<T> newHashSet() { return new com.intellij.util.containers.HashSet<T>(); } @NotNull + @Contract(pure=true) public static <T> HashSet<T> newHashSet(int initialCapacity) { return new com.intellij.util.containers.HashSet<T>(initialCapacity); } @NotNull + @Contract(pure=true) public static <T> HashSet<T> newHashSet(@NotNull T... elements) { return new com.intellij.util.containers.HashSet<T>(Arrays.asList(elements)); } @NotNull + @Contract(pure=true) public static <T> HashSet<T> newHashSet(@NotNull Iterable<? extends T> elements) { if (elements instanceof Collection) { @SuppressWarnings("unchecked") Collection<? extends T> collection = (Collection<? extends T>)elements; @@ -183,6 +217,7 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> HashSet<T> newHashSet(@NotNull Iterator<? extends T> iterator) { HashSet<T> set = newHashSet(); while (iterator.hasNext()) set.add(iterator.next()); @@ -190,16 +225,19 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> LinkedHashSet<T> newLinkedHashSet() { return new com.intellij.util.containers.LinkedHashSet<T>(); } @NotNull + @Contract(pure=true) public static <T> LinkedHashSet<T> newLinkedHashSet(@NotNull T... elements) { return newLinkedHashSet(Arrays.asList(elements)); } @NotNull + @Contract(pure=true) public static <T> LinkedHashSet<T> newLinkedHashSet(@NotNull Iterable<? extends T> elements) { if (elements instanceof Collection) { @SuppressWarnings("unchecked") Collection<? extends T> collection = (Collection<? extends T>)elements; @@ -209,11 +247,13 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> TreeSet<T> newTreeSet() { return new TreeSet<T>(); } @NotNull + @Contract(pure=true) public static <T> TreeSet<T> newTreeSet(@NotNull T... elements) { TreeSet<T> set = newTreeSet(); Collections.addAll(set, elements); @@ -221,26 +261,31 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> TreeSet<T> newTreeSet(@NotNull Iterable<? extends T> elements) { return copy(ContainerUtilRt.<T>newTreeSet(), elements); } @NotNull + @Contract(pure=true) public static <T> TreeSet<T> newTreeSet(@Nullable Comparator<? super T> comparator) { return new TreeSet<T>(comparator); } @NotNull + @Contract(pure=true) public static <T> Stack<T> newStack() { return new Stack<T>(); } @NotNull + @Contract(pure=true) public static <T> Stack<T> newStack(@NotNull Collection<T> elements) { return new Stack<T>(elements); } @NotNull + @Contract(pure=true) public static <T> Stack<T> newStack(@NotNull T... initial) { return new Stack<T>(Arrays.asList(initial)); } @@ -292,12 +337,14 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> List<T> emptyList() { //noinspection unchecked return (List<T>)EmptyList.INSTANCE; } @NotNull + @Contract(pure=true) public static <T> CopyOnWriteArrayList<T> createEmptyCOWList() { // does not create garbage new Object[0] return new CopyOnWriteArrayList<T>(ContainerUtilRt.<T>emptyList()); @@ -319,6 +366,7 @@ public class ContainerUtilRt { * @return read-only list consisting of the elements from array converted by mapper */ @NotNull + @Contract(pure=true) public static <T, V> List<V> map2List(@NotNull T[] array, @NotNull Function<T, V> mapper) { return map2List(Arrays.asList(array), mapper); } @@ -327,6 +375,7 @@ public class ContainerUtilRt { * @return read-only list consisting of the elements from collection converted by mapper */ @NotNull + @Contract(pure=true) public static <T, V> List<V> map2List(@NotNull Collection<? extends T> collection, @NotNull Function<T, V> mapper) { if (collection.isEmpty()) return emptyList(); List<V> list = new ArrayList<V>(collection.size()); @@ -340,6 +389,7 @@ public class ContainerUtilRt { * @return read-only set consisting of the elements from collection converted by mapper */ @NotNull + @Contract(pure=true) public static <T, V> Set<V> map2Set(@NotNull T[] collection, @NotNull Function<T, V> mapper) { return map2Set(Arrays.asList(collection), mapper); } @@ -348,6 +398,7 @@ public class ContainerUtilRt { * @return read-only set consisting of the elements from collection converted by mapper */ @NotNull + @Contract(pure=true) public static <T, V> Set<V> map2Set(@NotNull Collection<? extends T> collection, @NotNull Function<T, V> mapper) { if (collection.isEmpty()) return Collections.emptySet(); Set <V> set = new HashSet<V>(collection.size()); |